From 40c31a13c76bd57e95086474e951394c3164c3bf Mon Sep 17 00:00:00 2001 From: PretzelJohn <58197328+PretzelJohn@users.noreply.github.com> Date: Fri, 11 Mar 2022 08:56:18 -0500 Subject: [PATCH] Version 1.5.3-pre1: * Prevent moving items to/from invsee gui * Barrier item in invsee gui now closes gui when clicked --- pom.xml | 4 +-- .../VillagerTradeLimiter.java | 4 +++ .../commands/CommandManager.java | 36 +++++++++---------- .../listeners/InventoryListener.java | 17 +++++++++ src/main/resources/plugin.yml | 2 +- 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index c9d6be9..d5cffd5 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.pretzel.dev VillagerTradeLimiter - 1.5.2 + 1.5.3 1.8 @@ -46,7 +46,7 @@ mysql mysql-connector-java - 8.0.27 + 8.0.28 provided diff --git a/src/com/pretzel/dev/villagertradelimiter/VillagerTradeLimiter.java b/src/com/pretzel/dev/villagertradelimiter/VillagerTradeLimiter.java index d25919f..f614f8a 100644 --- a/src/com/pretzel/dev/villagertradelimiter/VillagerTradeLimiter.java +++ b/src/com/pretzel/dev/villagertradelimiter/VillagerTradeLimiter.java @@ -15,6 +15,7 @@ import com.pretzel.dev.villagertradelimiter.settings.Settings; import org.bukkit.ChatColor; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; @@ -117,4 +118,7 @@ public class VillagerTradeLimiter extends JavaPlugin { /** Returns a player's data container */ public HashMap getPlayerData() { return this.playerData; } + + /** Returns the invsee inventory's barrier block */ + public ItemStack getBarrier() { return this.commandManager.getBarrier(); } } diff --git a/src/com/pretzel/dev/villagertradelimiter/commands/CommandManager.java b/src/com/pretzel/dev/villagertradelimiter/commands/CommandManager.java index 0ba67da..82c529a 100644 --- a/src/com/pretzel/dev/villagertradelimiter/commands/CommandManager.java +++ b/src/com/pretzel/dev/villagertradelimiter/commands/CommandManager.java @@ -19,10 +19,18 @@ import java.util.Arrays; public class CommandManager { private final VillagerTradeLimiter instance; + private final ItemStack barrier; /** @param instance The instance of VillagerTradeLimiter.java */ public CommandManager(final VillagerTradeLimiter instance) { this.instance = instance; + this.barrier = new ItemStack(Material.BARRIER, 1); + ItemMeta meta = barrier.getItemMeta(); + if(meta != null) { + meta.setDisplayName(ChatColor.RED+"Close"); + meta.setLore(Arrays.asList(ChatColor.GRAY+"Click to close", ChatColor.GRAY+"this inventory.")); + } + barrier.setItemMeta(meta); } /** @return The root command node, to be registered by the plugin */ @@ -53,10 +61,7 @@ public class CommandManager { //Get the closest villager. If a nearby villager wasn't found, send the player an error message Entity closestEntity = getClosestEntity(p); - if(closestEntity == null) { - Util.sendMsg(instance.getLang("see.novillager"), p); - return; - } + if(closestEntity == null) return; //Gets the other player by name, using the first argument of the command OfflinePlayer otherPlayer = Bukkit.getOfflinePlayer(args[0]); @@ -80,10 +85,7 @@ public class CommandManager { //Get the closest villager. If a nearby villager wasn't found, send the player an error message Entity closestEntity = getClosestEntity(p); - if(closestEntity == null) { - Util.sendMsg(instance.getLang("see.novillager"), p); - return; - } + if(closestEntity == null) return; //Open the villager's inventory view for the calling player final Villager closestVillager = (Villager)closestEntity; @@ -92,7 +94,7 @@ public class CommandManager { if(item == null) continue; inventory.addItem(item.clone()); } - inventory.setItem(8, getBarrier()); + inventory.setItem(8, barrier); p.openInventory(inventory); })); return cmd; @@ -117,6 +119,9 @@ public class CommandManager { } } } + if(closestEntity == null) { + Util.sendMsg(instance.getLang("see.novillager"), player); + } return closestEntity; } @@ -140,15 +145,8 @@ public class CommandManager { } } - /** @return A custom barrier block to show players villagers only have 8 inventory slots */ - private ItemStack getBarrier() { - ItemStack barrier = new ItemStack(Material.BARRIER, 1); - ItemMeta meta = barrier.getItemMeta(); - if(meta != null) { - meta.setDisplayName(ChatColor.RED+"N/A"); - meta.setLore(Arrays.asList(ChatColor.GRAY+"Villagers only have", ChatColor.GRAY+"8 inventory slots!")); - } - barrier.setItemMeta(meta); - return barrier; + /** @return The barrier item */ + public ItemStack getBarrier() { + return this.barrier; } } diff --git a/src/com/pretzel/dev/villagertradelimiter/listeners/InventoryListener.java b/src/com/pretzel/dev/villagertradelimiter/listeners/InventoryListener.java index d90fb5d..162b77d 100644 --- a/src/com/pretzel/dev/villagertradelimiter/listeners/InventoryListener.java +++ b/src/com/pretzel/dev/villagertradelimiter/listeners/InventoryListener.java @@ -35,6 +35,23 @@ public class InventoryListener implements Listener { this.settings = settings; } + /** Handles when a player clicks in an inventory window */ + @EventHandler + public void onPlayerClickInventory(final InventoryClickEvent event) { + if(event.getInventory().getType() != InventoryType.CHEST) return; + if(event.getInventory().getSize() != 9) return; + if(!(event.getWhoClicked() instanceof Player)) return; + if(settings.shouldSkipNPC(event.getWhoClicked())) return; //Skips NPCs + ItemStack barrier = event.getInventory().getItem(8); + if(barrier == null || !barrier.isSimilar(instance.getBarrier())) return; + + //If the inventory matches the invsee inventory, cancel click events + event.setCancelled(true); + if(event.getCurrentItem() != null && event.getCurrentItem().isSimilar(instance.getBarrier())) { + Bukkit.getScheduler().runTaskLater(instance, () -> event.getWhoClicked().closeInventory(), 0L); + } + } + /** Handles when a player stops trading with a villager */ @EventHandler public void onPlayerStopTrading(final InventoryCloseEvent event) { diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 7366b33..8568551 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ name: VillagerTradeLimiter author: PretzelJohn main: com.pretzel.dev.villagertradelimiter.VillagerTradeLimiter -version: 1.5.2 +version: 1.5.3 api-version: 1.14 commands: