diff --git a/pom.xml b/pom.xml index a2ed41e..d9e000b 100644 --- a/pom.xml +++ b/pom.xml @@ -6,11 +6,11 @@ com.trophonix TradePlus - 3.76 + 3.77 ${project.basedir}/res/ - 3.76.1 + 3.77 diff --git a/src/main/java/com/trophonix/tradeplus/TradePlus.java b/src/main/java/com/trophonix/tradeplus/TradePlus.java index fd6e698..d9d99a1 100644 --- a/src/main/java/com/trophonix/tradeplus/TradePlus.java +++ b/src/main/java/com/trophonix/tradeplus/TradePlus.java @@ -68,13 +68,12 @@ public void onEnable() { .newChain() .async(tradeConfig::load) .async(tradeConfig::update) + .async(tradeConfig::save) .sync( () -> { excessChests = new ArrayList<>(); setupCommands(); reload(); - tradeConfig.save(); - InvUtils.reloadItems(this); if (Sounds.version > 17) { getServer().getPluginManager().registerEvents(new InteractListener(this), this); } diff --git a/src/main/java/com/trophonix/tradeplus/config/TradePlusConfig.java b/src/main/java/com/trophonix/tradeplus/config/TradePlusConfig.java index fb8fd7a..341631d 100644 --- a/src/main/java/com/trophonix/tradeplus/config/TradePlusConfig.java +++ b/src/main/java/com/trophonix/tradeplus/config/TradePlusConfig.java @@ -14,6 +14,8 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; @Getter public class TradePlusConfig { @@ -58,7 +60,10 @@ public class TradePlusConfig { private String guiTitle; private String spectatorTitle; + private List mySlots, theirSlots; + private ItemFactory force, accept, cancel, theirAccept, theirCancel, separator; + private int forceSlot, acceptSlot, theirAcceptSlot; private boolean forceEnabled, acceptEnabled, headEnabled; private String headDisplayName; @@ -171,6 +176,9 @@ public void reload() { ChatColor.translateAlternateColorCodes( '&', gui.getString("spectator-title", "Player 1 <| |> Player 2")); + mySlots = gui.getStringList("my-slots").stream().map(s -> Integer.valueOf(s)).collect(Collectors.toList()); + theirSlots = gui.getStringList("their-slots").stream().map(s -> Integer.valueOf(s)).collect(Collectors.toList()); + extrasTypePrefix = ChatColor.translateAlternateColorCodes( '&', config.getString("extras.type.prefix", "&6&l!!&6> ")); @@ -329,18 +337,21 @@ public void reload() { headEnabled = gui.getBoolean("head.enabled", true); headDisplayName = gui.getString("head.display-name", "&7You are trading with: &3&l%PLAYER%"); - acceptEnabled = gui.getBoolean("accept-enabled", true); + acceptEnabled = gui.getBoolean("accept.enabled", true); + acceptSlot = gui.getInt("accept.my-slot", 0); + theirAcceptSlot = gui.getInt("accept.their-slot", 8); - accept = new ItemFactory(gui, "accept"); - cancel = new ItemFactory(gui, "cancel"); + accept = new ItemFactory(gui, "accept.my-icon"); + cancel = new ItemFactory(gui, "accept.my-cancel"); - theirAccept = new ItemFactory(gui, "their-accept"); - theirCancel = new ItemFactory(gui, "their-cancel"); + theirAccept = new ItemFactory(gui, "accept.their-icon"); + theirCancel = new ItemFactory(gui, "accept.their-cancel"); separator = new ItemFactory(gui, "separator"); - forceEnabled = gui.getBoolean("force-enabled", config.getBoolean("gui.force.enabled", true)); - force = new ItemFactory(gui, "force"); + forceEnabled = gui.getBoolean("force.enabled", config.getBoolean("gui.force.enabled", true)); + forceSlot = gui.getInt("force.slot", 49); + force = new ItemFactory(gui, "force.icon"); } public void load() { @@ -418,8 +429,8 @@ public void loadConfig() { config.set("extras.economy.enabled", true); config.set("extras.economy.name", "money"); config.set("extras.economy.material", "gold_ingot"); - config.set("extras.economy.display", "&eYour current money offer is &6%AMOUNT%"); - config.set("extras.economy.theirdisplay", "&eTheir current money offer is &6%AMOUNT%"); + config.set("extras.economy.display", "&eYour money offer is &6%AMOUNT%"); + config.set("extras.economy.theirdisplay", "&eTheir money offer is &6%AMOUNT%"); config.set("extras.economy.lore", Collections.singletonList("&fClick to edit your offer!")); config.set("extras.economy.customModelData", 0); config.set("extras.economy.increment", 10.0); @@ -431,10 +442,10 @@ public void loadConfig() { config.set( "extras.experience.material", Sounds.version < 113 ? "exp_bottle" : "experience_bottle"); config.set( - "extras.experience.display", "&aYour current XP offer is &2%AMOUNT% &c(%LEVELS% levels)"); + "extras.experience.display", "&aYour XP offer is &2%AMOUNT% &c(%LEVELS% levels)"); config.set( "extras.experience.theirdisplay", - "&aTheir current XP offer is &2%AMOUNT% &a(+%LEVELS% levels)"); + "&aTheir XP offer is &2%AMOUNT% &a(+%LEVELS% levels)"); config.set( "extras.experience.lore", Arrays.asList("&fClick to edit your offer!", "&fYou have %BALANCE% XP.")); @@ -447,9 +458,9 @@ public void loadConfig() { config.set("extras.playerpoints.enabled", true); config.set("extras.playerpoints.name", "player points"); config.set("extras.playerpoints.material", "diamond"); - config.set("extras.playerpoints.display", "&bYour current PlayerPoints offer is &3%AMOUNT%"); + config.set("extras.playerpoints.display", "&bYour PlayerPoints offer is &3%AMOUNT%"); config.set( - "extras.playerpoints.theirdisplay", "&bTheir current PlayerPoints offer is &3%AMOUNT%"); + "extras.playerpoints.theirdisplay", "&bTheir PlayerPoints offer is &3%AMOUNT%"); config.set( "extras.playerpoints.lore", Collections.singletonList("&fClick to edit your offer!")); config.set("extras.playerpoints.customModelData", 0); @@ -462,10 +473,10 @@ public void loadConfig() { config.set( "extras.griefprevention.material", Sounds.version > 112 ? "golden_shovel" : "gold_spade"); config.set( - "extras.griefprevention.display", "&eYour current GriefPrevention offer is &6%AMOUNT%"); + "extras.griefprevention.display", "&eYour GriefPrevention offer is &6%AMOUNT%"); config.set( "extras.griefprevention.theirdisplay", - "&eTheir current GriefPrevention offer is &6%AMOUNT%"); + "&eTheir GriefPrevention offer is &6%AMOUNT%"); config.set( "extras.griefprevention.lore", Collections.singletonList("&fClick to edit your offer!")); config.set("extras.griefprevention.customModelData", 0); @@ -476,9 +487,9 @@ public void loadConfig() { config.set("extras.enjinpoints.enabled", false); config.set("extras.enjinpoints.name", "enjin points"); config.set("extras.enjinpoints.material", "emerald"); - config.set("extras.enjinpoints.display", "&eYour current EnjinPoints offer is &6%AMOUNT%"); + config.set("extras.enjinpoints.display", "&eYour EnjinPoints offer is &6%AMOUNT%"); config.set( - "extras.enjinpoints.theirdisplay", "&eTheir current EnjinPoints offer is &6%AMOUNT%"); + "extras.enjinpoints.theirdisplay", "&eTheir EnjinPoints offer is &6%AMOUNT%"); config.set( "extras.enjinpoints.lore", Collections.singletonList("&fClick to edit your offer!")); config.set("extras.enjinpoints.customModelData", 0); @@ -490,10 +501,10 @@ public void loadConfig() { config.set("extras.tokenenchant.name", "token enchant points"); config.set("extras.tokenenchant.material", "enchanted_book"); config.set( - "extras.tokenenchant.display", "&eYour current TokenEnchant tokens offer is &6%AMOUNT%"); + "extras.tokenenchant.display", "&eYour TokenEnchant tokens offer is &6%AMOUNT%"); config.set( "extras.tokenenchant.theirdisplay", - "&eTheir current TokenEnchants tokens offer is &6%AMOUNT%"); + "&eTheir TokenEnchants tokens offer is &6%AMOUNT%"); config.set( "extras.tokenenchant.lore", Collections.singletonList("&fClick to edit your offer!")); config.set("extras.tokenenchant.customModelData", 0); @@ -504,10 +515,10 @@ public void loadConfig() { config.set("extras.tokenmanager.enabled", true); config.set("extras.tokenmanager.name", "tokens"); config.set("extras.tokenmanager.material", "emerald"); - config.set("extras.tokenmanager.display", "&eYour current tokens offer is &6%AMOUNT%"); + config.set("extras.tokenmanager.display", "&eYour tokens offer is &6%AMOUNT%"); config.set( "extras.tokenmanager.theirdisplay", - "&eTheir current TokenManager tokens offer is &6%AMOUNT%"); + "&eTheir TokenManager tokens offer is &6%AMOUNT%"); config.set( "extras.tokenmanager.lore", Collections.singletonList("&fClick to edit your offer!")); config.set("extras.tokenmanager.customModelData", 0); @@ -518,9 +529,9 @@ public void loadConfig() { config.set("extras.votingplugin.name", "vote points"); config.set("extras.votingplugin.enabled", false); config.set("extras.votingplugin.material", "sunflower"); - config.set("extras.votingplugin.display", "&7Your current vote points offer is &b%AMOUNT%"); + config.set("extras.votingplugin.display", "&7Your vote points offer is &b%AMOUNT%"); config.set( - "extras.votingplugin.theirdisplay", "&7Their current vote points offer is &b%AMOUNT%"); + "extras.votingplugin.theirdisplay", "&7Their vote points offer is &b%AMOUNT%"); config.set("extras.votingplugin.lore", Arrays.asList("&fClick to edit your offer!")); config.set("extras.votingplugin.taxpercent", 0); @@ -631,20 +642,33 @@ public void loadGui() { gui.set("title", "Your Items <| |> Their Items"); gui.set("spectator-title", "Player 1 <| |> Player 2"); + gui.set( + "my-slots", + Stream.of( + 1, 2, 3, 9, 10, 11, 12, 18, 19, 20, 21, 27, 28, 29, 30, 36, 37, 38, 39, 45, 46, 47, + 48).map(i -> Integer.toString(i)).collect(Collectors.toList())); + gui.set("their-slots", Stream.of( + 5, 6, 7, 14, 15, 16, 17, 23, 24, 25, 26, 32, 33, 34, 35, 41, 42, 43, 44, 50, 51, 52, + 53).map(i -> Integer.toString(i)).collect(Collectors.toList())); + gui.set("head.enabled", true); gui.set("head.display-name", "&7You are trading with: &3&l%PLAYER%"); + gui.set("force.enabled", true); + gui.set("force.slot", 49); new ItemFactory(Material.getMaterial(Sounds.version > 112 ? "CLOCK" : "WATCH")) .display(("&4&lForce Trade")) .lore( Arrays.asList( "&7Click to force the trade", "&7to countdown and accept as", "&7it stands now.")) .flag("HIDE_ATTRIBUTES") - .save(gui, "force"); + .save(gui, "force.icon"); + gui.set("accept.enabled", true); + gui.set("accept.my-slot", 0); + gui.set("accept.their-slot", 8); new ItemFactory( - Material.getMaterial( - Sounds.version > 112 ? "RED_STAINED_GLASS_PANE" : "BARRIER")) + Material.getMaterial(Sounds.version > 112 ? "RED_STAINED_GLASS_PANE" : "BARRIER")) .display("&aClick to Accept") .lore( Arrays.asList( @@ -653,19 +677,16 @@ public void loadGui() { "&7you will have to accept", "&7the trade again.")) .flag("HIDE_ATTRIBUTES") - .save(gui, "accept"); - gui.set("accept-enabled", true); + .save(gui, "accept.my-icon"); new ItemFactory( - Material.getMaterial( - Sounds.version > 112 ? "GREEN_STAINED_GLASS_PANE" : "EMERALD")) + Material.getMaterial(Sounds.version > 112 ? "GREEN_STAINED_GLASS_PANE" : "EMERALD")) .display("&cClick to Cancel") .flag("HIDE_ATTRIBUTES") - .save(gui, "cancel"); + .save(gui, "accept.my-cancel"); new ItemFactory( - Material.getMaterial( - Sounds.version > 112 ? "GREEN_STAINED_GLASS_PANE" : "EMERALD")) + Material.getMaterial(Sounds.version > 112 ? "GREEN_STAINED_GLASS_PANE" : "EMERALD")) .display("&aThey've accepted your offer.") .lore( Arrays.asList( @@ -673,11 +694,10 @@ public void loadGui() { "&7trade as shown right now,", "&7click your accept button!")) .flag("HIDE_ATTRIBUTES") - .save(gui, "their-accept"); + .save(gui, "accept.their-icon"); new ItemFactory( - Material.getMaterial( - Sounds.version > 112 ? "RED_STAINED_GLASS_PANE" : "BARRIER")) + Material.getMaterial(Sounds.version > 112 ? "RED_STAINED_GLASS_PANE" : "BARRIER")) .display("&aYour partner is still considering.") .lore( Arrays.asList( @@ -686,7 +706,7 @@ public void loadGui() { "&7as it is now, or wait", "&7for them to offer more!")) .flag("HIDE_ATTRIBUTES") - .save(gui, "their-cancel"); + .save(gui, "accept.their-cancel"); new ItemFactory( Material.getMaterial( @@ -694,16 +714,12 @@ public void loadGui() { .display(" ") .flag("HIDE_ATTRIBUTES") .save(gui, "separator"); - - forceEnabled = gui.getBoolean("force-enabled", config.getBoolean("gui.force.enabled", true)); } } public void update() { - double configVersion = - config.contains("configversion") && config.isDouble("configversion") - ? config.getDouble("configversion") - : 0; + if (!config.isDouble("configversion")) return; + double configVersion = config.getDouble("configversion"); if (configVersion < 1.11) { config.set( @@ -728,7 +744,7 @@ public void update() { if (configVersion < 1.2) { config.set("economy.enabled", true); config.set("economy.clear", "&4&lClick to clear your money offer"); - config.set("economy.offer", "&7Your current money offer is &3&l%MONEYAMOUNT% %CURRENCYNAME%"); + config.set("economy.offer", "&7Your money offer is &3&l%MONEYAMOUNT% %CURRENCYNAME%"); config.set( "economy.theiroffer", "&7Their current money offer is &3&l%MONEYAMOUNT% %CURRENCYNAME%"); config.set( @@ -787,7 +803,7 @@ public void update() { config.set("hooks.economy.material", "gold_ingot"); config.set( "hooks.economy.youroffer", - config.getString("economy.offer", "&7Your current money offer is &e%MONEYAMOUNT%")); + config.getString("economy.offer", "&7Your money offer is &e%MONEYAMOUNT%")); config.set( "hooks.economy.theiroffer", config.getString("economy.theiroffer", "&7Their current money offer is &e%MONEYAMOUNT%")); @@ -1349,7 +1365,47 @@ public void update() { if (configVersion < 3.76) { gui.set("title", config.getString("gui.title", "Your Items <| |> Their Items")); - gui.set("spectator-title", config.getString("gui.spectator-title", "Player 1 <| |> Player 2")); + gui.set( + "spectator-title", + config.getString("gui.spectator-title", "Player 1 <| |> Player 2")); + } + + if (configVersion < 3.77) { + gui.set( + "my-slots", + Stream.of( + 1, 2, 3, 9, 10, 11, 12, 18, 19, 20, 21, 27, 28, 29, 30, 36, 37, 38, 39, 45, 46, 47, + 48).map(i -> Integer.toString(i)).collect(Collectors.toList())); + gui.set("their-slots", Stream.of( + 5, 6, 7, 14, 15, 16, 17, 23, 24, 25, 26, 32, 33, 34, 35, 41, 42, 43, 44, 50, 51, 52, + 53).map(i -> Integer.toString(i)).collect(Collectors.toList())); + + ItemFactory acceptIcon = new ItemFactory(gui, "accept"); + ItemFactory cancelIcon = new ItemFactory(gui, "cancel"); + ItemFactory theirAcceptIcon = new ItemFactory(gui, "their-accept"); + ItemFactory theirCancelIcon = new ItemFactory(gui, "their-cancel"); + + gui.set("accept", null); + gui.set("cancel", null); + gui.set("their-accept", null); + gui.set("their-cancel", null); + + gui.set("accept.enabled", gui.getBoolean("accept-enabled")); + gui.set("accept-enabled", null); + + gui.set("accept.my-slot", 0); + gui.set("accept.their-slot", 8); + + acceptIcon.save(gui, "accept.my-icon"); + theirAcceptIcon.save(gui, "accept.their-icon"); + cancelIcon.save(gui, "accept.my-cancel"); + theirCancelIcon.save(gui, "accept.their-cancel"); + + ItemFactory forceIcon = new ItemFactory(gui, "force"); + gui.set("force", null); + gui.set("force.enabled", gui.getBoolean("force-enabled", true)); + gui.set("force.slot", 49); + forceIcon.save(gui, "force.icon"); } config.set("configversion", Double.parseDouble(plugin.getDescription().getVersion())); diff --git a/src/main/java/com/trophonix/tradeplus/extras/Extra.java b/src/main/java/com/trophonix/tradeplus/extras/Extra.java index e1923af..670b475 100644 --- a/src/main/java/com/trophonix/tradeplus/extras/Extra.java +++ b/src/main/java/com/trophonix/tradeplus/extras/Extra.java @@ -9,10 +9,7 @@ import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.conversations.ConversationContext; -import org.bukkit.conversations.ConversationFactory; -import org.bukkit.conversations.NumericPrompt; -import org.bukkit.conversations.Prompt; +import org.bukkit.conversations.*; import org.bukkit.entity.Player; import org.bukkit.event.Listener; import org.bukkit.event.inventory.ClickType; @@ -26,6 +23,7 @@ public abstract class Extra implements Listener { public final String name; final ItemStack icon; final Player player1; + private Conversation convo1, convo2; final Player player2; final double increment; final ItemStack theirIcon; @@ -42,6 +40,7 @@ public abstract class Extra implements Listener { private Trade trade; Extra(String name, Player player1, Player player2, TradePlus pl, Trade trade) { + pl.getServer().getPluginManager().registerEvents(this, pl); this.pl = pl; this.name = name; ConfigurationSection section = @@ -84,7 +83,7 @@ public void onClick(Player player, ClickType click) { if (mode.equals("chat")) { trade.setCancelOnClose(player, false); player.closeInventory(); - new ConversationFactory(pl) + Conversation convo = new ConversationFactory(pl) .withPrefix( conversationContext -> ChatColor.translateAlternateColorCodes( @@ -149,8 +148,13 @@ public String getPromptText(ConversationContext conversationContext) { trade.updateExtras(); trade.setCancelOnClose(player, true); }) - .buildConversation(player) - .begin(); + .buildConversation(player); + if (player.equals(player1)) { + convo1 = convo; + } else { + convo2 = convo; + } + convo.begin(); } else { if (click.isLeftClick()) { if (click.isShiftClick()) { @@ -214,6 +218,11 @@ public void updateMax(boolean delay) { public abstract void onTradeEnd(); + public void onCancel() { + if (convo1 != null && player1.isConversing()) player1.abandonConversation(convo1); + if (convo2 != null && player2.isConversing()) player2.abandonConversation(convo2); + } + public ItemStack getIcon(Player player) { return ItemFactory.replaceInMeta( _getIcon(player), "%BALANCE%", Double.toString(getMax(player)), "%EXTRA%", displayName); diff --git a/src/main/java/com/trophonix/tradeplus/extras/LandsExtra.java b/src/main/java/com/trophonix/tradeplus/extras/LandsExtra.java new file mode 100644 index 0000000..c825ee7 --- /dev/null +++ b/src/main/java/com/trophonix/tradeplus/extras/LandsExtra.java @@ -0,0 +1,60 @@ +package com.trophonix.tradeplus.extras; + +import com.trophonix.tradeplus.TradePlus; +import com.trophonix.tradeplus.trade.Trade; +import me.angeschossen.lands.api.integration.LandsIntegration; +import me.angeschossen.lands.api.land.Land; +import me.angeschossen.lands.api.player.LandPlayer; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +public class LandsExtra extends Extra { + + private LandsIntegration landsApi; + private String selectedLand1, selectedLand2; + + public LandsExtra(Player player1, Player player2, TradePlus pl, Trade trade) { + super("lands", player1, player2, pl, trade); + this.landsApi = new LandsIntegration(pl, false); + } + + private Land getSelectedLand(Player player) { + LandPlayer landPlayer = landsApi.getLandPlayer(player.getUniqueId()); + if (landPlayer == null) return null; + + Land land; + if (player1.equals(player)) { + land = landPlayer.getLand(selectedLand1); + if (land == null) selectedLand1 = null; + } else if (player2.equals(player)) { + land = landPlayer.getLand(selectedLand2); + if (land == null) selectedLand2 = null; + } else return null; + return land; + } + + @Override + protected double getMax(Player player) { + Land land = getSelectedLand(player); + if (land == null) { + return 0; + } + return land.getMaxChunks() - land.getSize(); + } + + @Override + public void onTradeEnd() { + Land land1 = getSelectedLand(player1); + Land land2 = getSelectedLand(player2); + } + + @Override + protected ItemStack _getIcon(Player player) { + return null; + } + + @Override + protected ItemStack _getTheirIcon(Player player) { + return null; + } +} diff --git a/src/main/java/com/trophonix/tradeplus/trade/Trade.java b/src/main/java/com/trophonix/tradeplus/trade/Trade.java index 56d742c..7682f78 100644 --- a/src/main/java/com/trophonix/tradeplus/trade/Trade.java +++ b/src/main/java/com/trophonix/tradeplus/trade/Trade.java @@ -135,6 +135,8 @@ public Trade(Player p1, Player p2) { extra.init(); } updateExtras(); + + updateAcceptance(); }) .sync( () -> { @@ -177,7 +179,7 @@ public void onDrag(InventoryDragEvent event) { } for (int slot : event.getInventorySlots()) { - if (!InvUtils.leftSlots.contains(slot)) { + if (!pl.getTradeConfig().getMySlots().contains(slot)) { event.setCancelled(true); return; } @@ -232,7 +234,7 @@ public void onClick(InventoryClickEvent event) { // if it's in the left side, // the event will affect the // player's trade - if (InvUtils.leftSlots.contains(slot) && getExtra(slot) == null) { + if (slot != pl.getTradeConfig().getAcceptSlot() && pl.getTradeConfig().getMySlots().contains(slot) && getExtra(slot) == null) { if (accept1 && accept2) { event.setCancelled(true); return; @@ -257,8 +259,7 @@ public void onClick(InventoryClickEvent event) { ItemStack item = inv.getItem(slot); if (item != null) { // toggle button - if (item.isSimilar(pl.getTradeConfig().getAccept().build()) - || item.isSimilar(pl.getTradeConfig().getCancel().build())) { + if (slot == pl.getTradeConfig().getAcceptSlot()) { if (!forced) { if (player.equals(player1)) { accept1 = !accept1; @@ -303,6 +304,10 @@ public void onClick(InventoryClickEvent event) { } // if they click in the bottom } else if (player.getInventory().equals(inv)) { + if (cancelled) { + event.setCancelled(true); + return; + } Inventory open = player.getOpenInventory().getTopInventory(); if (inv1.getViewers().contains(player) || inv2.getViewers().contains(player)) { // Using my own double click @@ -317,8 +322,8 @@ public void onClick(InventoryClickEvent event) { ItemStack cursor = player.getItemOnCursor(); if ((item == null || item.getType().equals(Material.AIR)) && !cursor.getType().equals(Material.AIR)) { - for (int j : InvUtils.leftSlots) { - if (getExtra(j) != null) continue; + for (int j : pl.getTradeConfig().getMySlots()) { + if (j == pl.getTradeConfig().getAcceptSlot() || getExtra(j) != null) continue; ItemStack i = open.getItem(j); if (i != null && cursor.isSimilar(i)) { int amount = cursor.getAmount() + i.getAmount(); @@ -555,9 +560,9 @@ private void giveItemsOnLeft(Inventory inv, Player player) { private List getItemsOnLeft(Inventory inv) { List items = new ArrayList<>(); - InvUtils.leftSlots.forEach( + pl.getTradeConfig().getMySlots().forEach( slot -> { - if (getExtra(slot) == null) { + if (slot != pl.getTradeConfig().getAcceptSlot() && getExtra(slot) == null) { ItemStack item = inv.getItem(slot); if (item != null) { items.add(item); @@ -567,10 +572,14 @@ private List getItemsOnLeft(Inventory inv) { return items; } + private int getRight(int left) { + return pl.getTradeConfig().getTheirSlots().get(pl.getTradeConfig().getMySlots().indexOf(left)); + } + private void updateInventories() { - InvUtils.leftSlots.forEach( + pl.getTradeConfig().getMySlots().forEach( slot -> { - if (getExtra(slot) == null) { + if (getExtra(slot) == null && slot != pl.getTradeConfig().getAcceptSlot()) { ItemStack item1 = inv1.getItem(slot); if (isBlocked(item1)) { Sounds.villagerHit(player1, 1); @@ -582,6 +591,7 @@ private void updateInventories() { inv2.setItem(getRight(slot), item1); spectatorInv.setItem(slot, item1); } + ItemStack item2 = inv2.getItem(slot); if (isBlocked(item2)) { Sounds.villagerHit(player2, 1); @@ -636,30 +646,30 @@ public void updateExtras() { private void updateAcceptance() { if (pl.getTradeConfig().isAcceptEnabled()) { inv1.setItem( - 0, + pl.getTradeConfig().getAcceptSlot(), accept1 ? pl.getTradeConfig().getCancel().build() : pl.getTradeConfig().getAccept().build()); inv1.setItem( - 8, + pl.getTradeConfig().getTheirAcceptSlot(), accept2 ? pl.getTradeConfig().getTheirAccept().build() : pl.getTradeConfig().getTheirCancel().build()); inv2.setItem( - 0, + pl.getTradeConfig().getAcceptSlot(), accept2 ? pl.getTradeConfig().getCancel().build() : pl.getTradeConfig().getAccept().build()); inv2.setItem( - 8, + pl.getTradeConfig().getTheirAcceptSlot(), accept1 ? pl.getTradeConfig().getTheirAccept().build() : pl.getTradeConfig().getTheirCancel().build()); - inv1.getItem(0).setAmount(pl.getTradeConfig().getAntiscamCountdown()); - inv1.getItem(8).setAmount(pl.getTradeConfig().getAntiscamCountdown()); - inv2.getItem(0).setAmount(pl.getTradeConfig().getAntiscamCountdown()); - inv2.getItem(8).setAmount(pl.getTradeConfig().getAntiscamCountdown()); + inv1.getItem(pl.getTradeConfig().getAcceptSlot()).setAmount(pl.getTradeConfig().getAntiscamCountdown()); + inv1.getItem(pl.getTradeConfig().getTheirAcceptSlot()).setAmount(pl.getTradeConfig().getAntiscamCountdown()); + inv2.getItem(pl.getTradeConfig().getAcceptSlot()).setAmount(pl.getTradeConfig().getAntiscamCountdown()); + inv2.getItem(pl.getTradeConfig().getTheirAcceptSlot()).setAmount(pl.getTradeConfig().getAntiscamCountdown()); spectatorInv.setItem( 4, @@ -696,12 +706,12 @@ private void checkAcceptance() { .runTaskTimer( pl, () -> { - int current = inv1.getItem(0).getAmount(); + int current = inv1.getItem(pl.getTradeConfig().getAcceptSlot()).getAmount(); if (current > 1) { - inv1.getItem(0).setAmount(current - 1); - inv1.getItem(8).setAmount(current - 1); - inv2.getItem(0).setAmount(current - 1); - inv2.getItem(8).setAmount(current - 1); + inv1.getItem(pl.getTradeConfig().getAcceptSlot()).setAmount(current - 1); + inv1.getItem(pl.getTradeConfig().getTheirAcceptSlot()).setAmount(current - 1); + inv2.getItem(pl.getTradeConfig().getAcceptSlot()).setAmount(current - 1); + inv2.getItem(pl.getTradeConfig().getTheirAcceptSlot()).setAmount(current - 1); spectatorInv.getItem(4).setAmount(current - 1); if (pl.getTradeConfig().isSoundEffectsEnabled() && pl.getTradeConfig().isSoundOnCountdown()) { @@ -759,7 +769,7 @@ private void checkAcceptance() { } } - for (int leftSlot : InvUtils.leftSlots) { + for (int leftSlot : pl.getTradeConfig().getMySlots()) { int rightSlot = getRight(leftSlot); inv1.setItem(leftSlot, inv1.getItem(rightSlot)); inv2.setItem(leftSlot, inv2.getItem(rightSlot)); @@ -842,32 +852,10 @@ private Extra getExtra(int slot) { return placedExtras.get(slot); } - private int getRight(int slot) { - return roundUpToNine(slot) - 1 - Integer.remainderUnsigned(slot, 9); - } - - private int roundUpToNine(int slot) { - if (slot < 9) { - return 9; - } - if (slot < 18) { - return 18; - } - if (slot < 27) { - return 27; - } - if (slot < 36) { - return 36; - } - if (slot < 45) { - return 45; - } - return 54; - } - private ItemStack putOnLeft(Inventory inventory, ItemStack toMove, int amountToMove) { int moved = 0; - for (int slot : InvUtils.leftSlots) { + for (int slot : pl.getTradeConfig().getMySlots()) { + if (getExtra(slot) != null || slot == pl.getTradeConfig().getAcceptSlot()) continue; ItemStack inInventory = inventory.getItem(slot); if (inInventory != null && inInventory.isSimilar(toMove) @@ -883,7 +871,8 @@ private ItemStack putOnLeft(Inventory inventory, ItemStack toMove, int amountToM } } } - for (int slot : InvUtils.leftSlots) { + for (int slot : pl.getTradeConfig().getMySlots()) { + if (getExtra(slot) != null || slot == pl.getTradeConfig().getAcceptSlot()) continue; ItemStack i = inventory.getItem(slot); if (!(i == null || i.getType().equals(Material.AIR))) { continue; @@ -979,10 +968,6 @@ private boolean isBlocked(ItemStack item) { return false; } - private boolean similar(ItemStack item1, ItemStack item2) { - return item1 == item2 || (item1 != null && item1.isSimilar(item2)); - } - public void open(Player player) { if (cancelled) { player.closeInventory(); @@ -1004,10 +989,16 @@ public void setCancelOnClose(Player player, boolean cancelOnClose) { } private void cancel() { - for (int leftSlot : InvUtils.leftSlots) { - int rightSlot = getRight(leftSlot); - inv1.setItem(rightSlot, pl.getTradeConfig().getSeparator().build()); - inv2.setItem(rightSlot, pl.getTradeConfig().getSeparator().build()); + if (inv1.getViewers().isEmpty()) + player1.openInventory(inv1); + if (inv2.getViewers().isEmpty()) + player2.openInventory(inv2); + for (int slot : pl.getTradeConfig().getTheirSlots()) { + inv1.setItem(slot, pl.getTradeConfig().getSeparator().build()); + inv2.setItem(slot, pl.getTradeConfig().getSeparator().build()); + } + for (Extra extra : extras) { + extra.onCancel(); } cancelled = true; } diff --git a/src/main/java/com/trophonix/tradeplus/util/InvUtils.java b/src/main/java/com/trophonix/tradeplus/util/InvUtils.java index 7d15f30..79073a2 100644 --- a/src/main/java/com/trophonix/tradeplus/util/InvUtils.java +++ b/src/main/java/com/trophonix/tradeplus/util/InvUtils.java @@ -12,12 +12,12 @@ import java.util.List; public class InvUtils { +// public static final List leftSlots = +// new LinkedList<>( +// Arrays.asList(0, +// 1, 2, 3, 9, 10, 11, 12, 18, 19, 20, 21, 27, 28, 29, 30, 36, 37, 38, 39, 45, 46, 47, +// 48)); - public static final List leftSlots = - new LinkedList<>( - Arrays.asList( - 1, 2, 3, 9, 10, 11, 12, 18, 19, 20, 21, 27, 28, 29, 30, 36, 37, 38, 39, 45, 46, 47, - 48)); private static TradePlus pl; @@ -38,8 +38,8 @@ public static Inventory getTradeInventory(Player player1, Player player2) { && player1.hasPermission("tradeplus.admin")) inv.setItem(49, pl.getTradeConfig().getForce().build()); } else { - inv.setItem(0, separator); - inv.setItem(8, separator); + inv.setItem(pl.getTradeConfig().getAcceptSlot(), separator); + inv.setItem(pl.getTradeConfig().getTheirAcceptSlot(), separator); } if (pl.getTradeConfig().isHeadEnabled()) inv.setItem( @@ -59,9 +59,10 @@ public static Inventory getSpectatorInventory(Player player1, Player player2) { ItemStack separator = pl.getTradeConfig().getSeparator().build(); for (int i = 4; i <= 49; i += 9) inv.setItem(i, separator); for (int i = 45; i <= 53; i++) inv.setItem(i, separator); - inv.setItem(0, ItemFactory.getPlayerSkull(player1, "&f" + player1.getName())); - inv.setItem(8, ItemFactory.getPlayerSkull(player2, "&f" + player2.getName())); + inv.setItem(pl.getTradeConfig().getAcceptSlot(), ItemFactory.getPlayerSkull(player1, "&f" + player1.getName())); + inv.setItem(pl.getTradeConfig().getTheirAcceptSlot(), ItemFactory.getPlayerSkull(player2, "&f" + player2.getName())); inv.setItem(4, pl.getTradeConfig().getTheirCancel().build()); + return inv; } }