From 366bcaff566f3a88fd6f425574b7c94936d890ec Mon Sep 17 00:00:00 2001 From: mfnalex <1122571+mfnalex@users.noreply.github.com> Date: Sat, 13 Jul 2019 14:03:44 +0200 Subject: [PATCH] added middle click support --- .../JeffChestSort/JeffChestSortListener.java | 40 +++++++++++++++++-- .../JeffChestSort/JeffChestSortPlugin.java | 4 +- src/main/resources/config.yml | 7 +++- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortListener.java b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortListener.java index 60a4d53..36aceef 100644 --- a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortListener.java +++ b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortListener.java @@ -9,6 +9,7 @@ import org.bukkit.block.DoubleChest; import org.bukkit.block.EnderChest; import org.bukkit.block.ShulkerBox; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -18,6 +19,7 @@ import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryEvent; import org.bukkit.event.inventory.InventoryOpenEvent; +import org.bukkit.event.inventory.InventoryType.SlotType; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.Inventory; @@ -270,6 +272,11 @@ public class JeffChestSortListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onInventoryClickEvent(InventoryClickEvent event) { + + if(!plugin.getConfig().getBoolean("allow-shortcut")) { + return; + } + if(!(event.getWhoClicked() instanceof Player)) { return; } @@ -282,14 +289,41 @@ public class JeffChestSortListener implements Listener { InventoryHolder holder = event.getInventory().getHolder(); - if(!(holder instanceof Player) && !belongsToChestLikeBlock(event.getInventory())) { + if(event.getClick() != ClickType.MIDDLE) { return; } - if(event.getClick() == ClickType.MIDDLE) { + if(belongsToChestLikeBlock(event.getInventory())) { + //p.sendMessage("Sorting chest"); plugin.organizer.sortInventory(event.getInventory()); + updateInventoryView(event); + return; + } else if(holder instanceof Player) { + //p.sendMessage("Sorting player inv"); + if(event.getSlotType() == SlotType.QUICKBAR) { + //p.sendMessage("Sorting Hotbar"); + plugin.organizer.sortInventory(p.getInventory(),0,8); + updateInventoryView(event); + return; + } + else if(event.getSlotType() == SlotType.CONTAINER) { + //p.sendMessage("Sorting container"); + plugin.organizer.sortInventory(p.getInventory(),9,35); + updateInventoryView(event); + return; + } + return; + } + + } + + void updateInventoryView(InventoryClickEvent event) { + for(HumanEntity viewer : event.getViewers()) { + if(viewer instanceof Player) { + Player playerViewer = (Player) viewer; + playerViewer.updateInventory(); + } } - } } diff --git a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java index 6120d02..dd68e56 100644 --- a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java +++ b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java @@ -61,7 +61,7 @@ public class JeffChestSortPlugin extends JavaPlugin { JeffChestSortListener listener; String sortingMethod; ArrayList disabledWorlds; - int currentConfigVersion = 11; + int currentConfigVersion = 12; boolean usingMatchingConfig = true; boolean debug = false; boolean verbose = true; @@ -134,6 +134,7 @@ public class JeffChestSortPlugin extends JavaPlugin { getConfig().addDefault("check-for-updates", "true"); getConfig().addDefault("auto-generate-category-files", true); getConfig().addDefault("sort-time", "close"); + getConfig().addDefault("allow-shortcut", true); getConfig().addDefault("verbose", true); // Prints some information in onEnable() } @@ -251,6 +252,7 @@ public class JeffChestSortPlugin extends JavaPlugin { getLogger().info("Sorting enabled by default: " + getConfig().getBoolean("sorting-enabled-by-default")); getLogger().info("Auto generate category files: " + getConfig().getBoolean("auto-generate-category-files")); getLogger().info("Sort time: " + getConfig().getString("sort-time")); + getLogger().info("Allow shortcut: " + getConfig().getString("allow-shortcut")); getLogger().info("Check for updates: " + getConfig().getString("check-for-updates")); getLogger().info("Categories: " + getCategoryList()); } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index ad7a6c0..9d8cbea 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -63,6 +63,11 @@ auto-generate-category-files: true # Available options: open, close, both sort-time: close +# Instead of automatic sorting, you can also use middle-click +# when using an inventory to have it sorted immediately. +# You can disable this by setting this to false. +allow-shortcut: true + # should we check for updates? # when enabled, a message is printed in the console if a new # version has been found, and OPs will be notified when they join the server @@ -274,4 +279,4 @@ message-error-invalid-options: "&cError: Unknown option %s. Valid options are %s ######################### # please do not change the following line manually! -config-version: 11 +config-version: 12