package com.yaohun.trade.data; import com.yaohun.trade.TradeMain; import com.yaohun.trade.util.StackUtil; import com.yaohun.trade.config.Config; import com.yaohun.trade.util.MessageUtil; import com.yaohun.trade.util.TradeState; import me.Demon.DemonPlugin.DemonAPI; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class TradeData { private final String tradeID = "DATA-"+System.currentTimeMillis(); private Player player; private String playerName; private Player target; private String targetName; private boolean isTradeStop; private boolean isTradeBreakTheLaw; private HashMap stateMap = new HashMap<>(); private Inventory inventory; public TradeData(Player player, Player target,Inventory inventory) { this.player = player; this.playerName = player.getName(); stateMap.put(player, TradeState.OPEN); this.target = target; this.targetName = target.getName(); stateMap.put(target, TradeState.OPEN); this.inventory = inventory; this.isTradeStop = false; this.isTradeBreakTheLaw = false; } public String getTradeID() { return tradeID; } public void setTradeBreakTheLaw(boolean tradeBreakTheLaw) { isTradeBreakTheLaw = tradeBreakTheLaw; } public boolean isTradeStop() { return isTradeStop; } public Player getPlayer() { return player; } public String getPlayerName() { return playerName; } public Player getTarget() { return target; } public String getTargetName() { return targetName; } public TradeState getState(Player player){ return stateMap.get(player); } public void setStateData(Player player, TradeState state){ stateMap.put(player, state); } public boolean isMutualComfirm(){ return stateMap.get(player) == TradeState.CONFIRM && stateMap.get(target) == TradeState.CONFIRM; } private boolean isInventoryFull(boolean targetD){ if(targetD){ int slotAmount = 0; for (int slot : Config.playerButts.clone()) { ItemStack stack = inventory.getItem(slot); if (!DemonAPI.itemIsNull(stack)) { slotAmount++; } } if (!DemonAPI.hasEmptyInventorySlots(target, slotAmount)) { return false; } } else { int slotAmount = 0; for (int slot : Config.targetSlots.clone()) { ItemStack stack = inventory.getItem(slot); if (!DemonAPI.itemIsNull(stack)) { slotAmount++; } } if (!DemonAPI.hasEmptyInventorySlots(player, slotAmount)) { return false; } } return true; } public void successTrade(){ if(!isInventoryFull(false)){ isTradeBreakTheLaw = true; player.closeInventory(); MessageUtil.sendMessage(player,Config.getString("insufficientSpace"), Sound.ENTITY_BLAZE_DEATH); MessageUtil.sendMessage(target,Config.getString("insufficientSpace"), Sound.ENTITY_BLAZE_DEATH); return; } if(!isInventoryFull(true)){ isTradeBreakTheLaw = true; target.closeInventory(); MessageUtil.sendMessage(player,Config.getString("insufficientSpace2"), Sound.ENTITY_BLAZE_DEATH); MessageUtil.sendMessage(target,Config.getString("insufficientSpace2"), Sound.ENTITY_BLAZE_DEATH); return; } this.isTradeStop = true; int playerCount = 0; List playerList = new ArrayList<>(); for (int slot : Config.targetSlots.clone()){ ItemStack stack = inventory.getItem(slot); if(!DemonAPI.itemIsNull(stack)){ if(stack.getType().equals(Material.STAINED_GLASS_PANE)){ continue; } ItemStack item = StackUtil.removeTagStack(stack); int amount = stack.getAmount(); String itemName = DemonAPI.getItemName(item); playerList.add(ChatColor.stripColor(itemName)+"x"+amount); DemonAPI.giveItem(player, item); playerCount += amount; } } player.closeInventory(); int tragetCount = 0; List targetList = new ArrayList<>(); for (int slot : Config.playerSlots.clone()){ ItemStack stack = inventory.getItem(slot); if(!DemonAPI.itemIsNull(stack)){ if(stack.getType().equals(Material.STAINED_GLASS_PANE)){ continue; } ItemStack item = StackUtil.removeTagStack(stack); int amount = stack.getAmount(); String itemName = DemonAPI.getItemName(item); targetList.add(ChatColor.stripColor(itemName)+"x"+amount); DemonAPI.giveItem(target, item); tragetCount += stack.getAmount(); } } target.closeInventory(); String CompleteTrade = Config.getString("CompleteTrade"); String CompleteTrade2 = Config.getString("CompleteTrade2"); if(playerCount >= 1) { MessageUtil.sendMessage(player, CompleteTrade.replace("{count}", String.valueOf(playerCount)), Sound.ENTITY_EXPERIENCE_ORB_PICKUP); } else { MessageUtil.sendMessage(player, CompleteTrade2, Sound.ENTITY_EXPERIENCE_ORB_PICKUP); } if(tragetCount >= 1) { MessageUtil.sendMessage(target, CompleteTrade.replace("{count}", String.valueOf(tragetCount)), Sound.ENTITY_EXPERIENCE_ORB_PICKUP); } else { MessageUtil.sendMessage(target, CompleteTrade2, Sound.ENTITY_EXPERIENCE_ORB_PICKUP); } TradeLogData logData = new TradeLogData(); logData.setTradeID(getTradeID()); logData.setPlayerName(player.getName()); logData.setTargetName(target.getName()); logData.setTradeTime(System.currentTimeMillis()); logData.setPlayerList(playerList); logData.setTargetList(targetList); TradeMain.getLogsManager().addLogData(logData); } public void closeTradeGui(boolean closePlayer){ this.isTradeStop = true; for (int slot : Config.playerSlots.clone()){ ItemStack stack = inventory.getItem(slot); if(!DemonAPI.itemIsNull(stack)){ if(stack.getType().equals(Material.STAINED_GLASS_PANE)){ continue; } ItemStack item = StackUtil.removeTagStack(stack); DemonAPI.giveItem(player, item); } } player.closeInventory(); for (int slot : Config.targetSlots.clone()){ ItemStack stack = inventory.getItem(slot); if(!DemonAPI.itemIsNull(stack)){ if(stack.getType().equals(Material.STAINED_GLASS_PANE)){ continue; } ItemStack item = StackUtil.removeTagStack(stack); DemonAPI.giveItem(target, item); } } target.closeInventory(); if(!isTradeBreakTheLaw) { String message = Config.getString("closeTheInterface"); String message2 = Config.getString("closeTheInterface2"); if (closePlayer) { MessageUtil.sendMessage(player, message.replace("{name}",targetName), Sound.ENTITY_VILLAGER_NO); MessageUtil.sendMessage(target, message2.replace("{name}",playerName), Sound.ENTITY_VILLAGER_NO); } else { MessageUtil.sendMessage(target, message.replace("{name}",playerName), Sound.ENTITY_VILLAGER_NO); MessageUtil.sendMessage(player, message2.replace("{name}",targetName), Sound.ENTITY_VILLAGER_NO); } } } }