From 64d79e9743c910e46252346ab08f5e9bf19ac440 Mon Sep 17 00:00:00 2001 From: Zrips Date: Mon, 6 Aug 2018 16:05:21 +0300 Subject: [PATCH] This is correct way to do villager trade --- .../gamingmesh/jobs/container/ActionType.java | 2 +- .../jobs/listeners/JobsPaymentListener.java | 25 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/container/ActionType.java b/src/main/java/com/gamingmesh/jobs/container/ActionType.java index 7837bf0e..850d88d4 100644 --- a/src/main/java/com/gamingmesh/jobs/container/ActionType.java +++ b/src/main/java/com/gamingmesh/jobs/container/ActionType.java @@ -26,7 +26,7 @@ public enum ActionType { MMKILL("MMKill"), FISH("Fish"), CRAFT("Craft"), - VTRADE("Villager Trade"), + VTRADE("VTrade"), SMELT("Smelt"), BREW("Brew"), ENCHANT("Enchant"), diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index 435dc699..a81300be 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -44,7 +44,6 @@ import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; import org.bukkit.entity.Sheep; import org.bukkit.entity.Tameable; -import org.bukkit.entity.Villager; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -66,9 +65,7 @@ import org.bukkit.event.inventory.BrewEvent; import org.bukkit.event.inventory.CraftItemEvent; import org.bukkit.event.inventory.FurnaceSmeltEvent; import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.InventoryType.SlotType; import org.bukkit.event.player.PlayerFishEvent; @@ -118,12 +115,15 @@ public class JobsPaymentListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void villagerTradeInventoryClick(InventoryClickEvent event) { + //disabling plugin in world if (event.getWhoClicked() != null && !Jobs.getGCManager().canPerformActionInWorld(event.getWhoClicked().getWorld())) return; + // make sure plugin is enabled if (!this.plugin.isEnabled()) return; + if (event.isCancelled()) return; @@ -147,7 +147,7 @@ public class JobsPaymentListener implements Listener { if (!event.getSlotType().equals(SlotType.RESULT)) return; - ItemStack resultStack = event.getCurrentItem(); + ItemStack resultStack = event.getClickedInventory().getItem(2); if (resultStack == null) return; @@ -183,9 +183,9 @@ public class JobsPaymentListener implements Listener { ItemStack toStore = event.getCursor(); // Make sure we are actually traded anything if (hasItems(toCraft)) - if (event.isShiftClick()) - schedulePostDetection(player, toCraft.clone(), jPlayer, resultStack.clone()); - else { + if (event.isShiftClick()) { + schedulePostDetection(player, toCraft.clone(), jPlayer, resultStack.clone(), ActionType.VTRADE); + } else { // The items are stored in the cursor. Make sure there's enough space. if (isStackSumLegal(toCraft, toStore)) { int newItemsCount = toCraft.getAmount(); @@ -633,7 +633,7 @@ public class JobsPaymentListener implements Listener { // Make sure we are actually crafting anything if (hasItems(toCraft)) if (event.isShiftClick()) - schedulePostDetection(player, toCraft.clone(), jPlayer, resultStack.clone()); + schedulePostDetection(player, toCraft.clone(), jPlayer, resultStack.clone(), ActionType.CRAFT); else { // The items are stored in the cursor. Make sure there's enough space. if (isStackSumLegal(toCraft, toStore)) { @@ -650,9 +650,14 @@ public class JobsPaymentListener implements Listener { } + @Deprecated + private Integer schedulePostDetection(final HumanEntity player, final ItemStack compareItem, final JobsPlayer jPlayer, final ItemStack resultStack) { + return schedulePostDetection(player, compareItem, jPlayer, resultStack, ActionType.CRAFT); + } + // HACK! The API doesn't allow us to easily determine the resulting number of // crafted items, so we're forced to compare the inventory before and after. - private Integer schedulePostDetection(final HumanEntity player, final ItemStack compareItem, final JobsPlayer jPlayer, final ItemStack resultStack) { + private Integer schedulePostDetection(final HumanEntity player, final ItemStack compareItem, final JobsPlayer jPlayer, final ItemStack resultStack, final ActionType type) { final ItemStack[] preInv = player.getInventory().getContents(); // Clone the array. The content may (was for me) be mutable. for (int i = 0; i < preInv.length; i++) { @@ -676,7 +681,7 @@ public class JobsPaymentListener implements Listener { if (newItemsCount > 0) { while (newItemsCount >= 1) { newItemsCount--; - Jobs.action(jPlayer, new ItemActionInfo(resultStack, ActionType.CRAFT)); + Jobs.action(jPlayer, new ItemActionInfo(resultStack, type)); } } return;