diff --git a/CHANGELOG.md b/CHANGELOG.md index 31705b4..42a2585 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## 8.13.1 - Updated Russian and Turkish translation +- Separated ChestSort plugin and API ## 8.13.0 - Updated Chinese (Traditional) and Spanish translation diff --git a/HOW_TO_USE_API.md b/HOW_TO_USE_API.md index 1df4a67..935f0bb 100644 --- a/HOW_TO_USE_API.md +++ b/HOW_TO_USE_API.md @@ -5,7 +5,7 @@ If you want to use ChestSort's advanced sorting features for your own plugin, or ## Maven repository You can use maven to add ChestSort as a dependency to your Spigot-/Bukkit-Plugin: -``` +```xml jeff-media-repo @@ -15,15 +15,37 @@ You can use maven to add ChestSort as a dependency to your Spigot-/Bukkit-Plugin de.jeff_media - ChestSort - 8.12.0 - provided + ChestSortAPI + 8.13.1 + compile ``` +If you use the `Sortable`class or the `ISortable` interface, you must also shade the ChestSortAPI into your plugin: + +```xml + + + + org.apache.maven.plugins + maven-shade-plugin + 3.1.0 + + + package + + shade + + + + + + +``` + ## Accessing the API -Then you can access it via the plugin manager: +Then you can access the API via the plugin manager: ``` ChestSortPlugin chestSort = (ChestSortPlugin) getServer().getPluginManager().getPlugin("ChestSort"); @@ -39,19 +61,19 @@ ChestSortAPI chestSortAPI = chestSort.getAPI(); Now, you can sort any Inventory! Just like this: -``` +```java chestSortAPI.sortInventory(Inventory inventory); ``` To sort only specific slots, you can pass slot numbers where to start and end sorting. ChestSort will not modify the inventory outside the given slot range. -``` +```java chestSortAPI.sortInventory(Inventory inventory, int startSlot, int endSlot); ``` You can also check if a player has automatic sorting enabled or disabled: -``` +```java boolean sortingEnabled = chestSortAPI.sortingEnabled(Player player); ``` @@ -59,7 +81,7 @@ boolean sortingEnabled = chestSortAPI.sortingEnabled(Player player); If you want to prevent ChestSort from sorting a certain inventory, you can listen to the ChestSortEvent event. -``` +```java @EventHandler public void onChestSortEvent(ChestSortEvent event) { if(event.getInventory() == whatever) { @@ -68,6 +90,15 @@ public void onChestSortEvent(ChestSortEvent event) { } ``` +### Making custom inventories sortable + +If you create a new Inventory inside your plugin, you can use the `Sortable` class to tell ChestSort that your custom inventory should be sortable. + +```java +Sortable holder = new Sortable(); +Inventory inv = Bukkit.createInventory(holder, 9, "Example"); +``` + ## Example Plugin Here is a complete example plugin that shows to add and use the ChestSort API: [LINK](https://github.com/JEFF-Media-GbR/ChestSortAPIExample) diff --git a/pom.xml b/pom.xml index b8013be..0c8b9dc 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ JeffChestSort https://www.chestsort.de Automatically sorts your chests! - 8.13.0 + 8.13.1 jar @@ -52,6 +52,12 @@ de.jeff_media.ChestSort + + + org.jetbrains:* + org.intellij.lang:* + + @@ -104,6 +110,12 @@ 1.16.1-R0.1-SNAPSHOT provided + + de.jeff_media + ChestSortAPI + 8.13.1 + compile + org.bstats bstats-bukkit diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortAPI.java b/src/main/java/de/jeff_media/ChestSort/ChestSortAPIHandler.java similarity index 83% rename from src/main/java/de/jeff_media/ChestSort/ChestSortAPI.java rename to src/main/java/de/jeff_media/ChestSort/ChestSortAPIHandler.java index 9f1b287..9dae8ea 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortAPI.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortAPIHandler.java @@ -3,25 +3,28 @@ package de.jeff_media.ChestSort; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; -public class ChestSortAPI { +public class ChestSortAPIHandler implements ChestSortAPI { final ChestSortPlugin plugin; - ChestSortAPI(ChestSortPlugin plugin) { + ChestSortAPIHandler(ChestSortPlugin plugin) { this.plugin = plugin; } // Public API method to sort any given inventory + @Override public void sortInventory(Inventory inv) { plugin.organizer.sortInventory(inv); } // Public API method to sort any given inventory inbetween startSlot and endSlot + @Override public void sortInventory(Inventory inv, int startSlot, int endSlot) { plugin.organizer.sortInventory(inv, startSlot, endSlot); } // Public API method to check if player has automatic chest sorting enabled + @Override public boolean sortingEnabled(Player p) { return plugin.isSortingEnabled(p); } diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortDebugger.java b/src/main/java/de/jeff_media/ChestSort/ChestSortDebugger.java index bfb6881..3613a60 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortDebugger.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortDebugger.java @@ -24,6 +24,9 @@ public class ChestSortDebugger implements @NotNull Listener { System.out.println(" "); System.out.println("InventoryClickEvent:"); System.out.println("- Holder: " + e.getInventory().getHolder()); + if(e.getInventory().getHolder()!=null) { + System.out.println("- Holder class: "+e.getInventory().getHolder().getClass()); + } System.out.println("- Slot: "+e.getRawSlot()); System.out.println("- Left-Click: "+e.isLeftClick()); System.out.println("- Right-Click: "+e.isRightClick()); diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortListener.java b/src/main/java/de/jeff_media/ChestSort/ChestSortListener.java index 97bff8c..8d0f415 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortListener.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortListener.java @@ -116,7 +116,7 @@ public class ChestSortListener implements Listener { Player p = (Player) event.getPlayer(); Inventory inventory = event.getInventory(); - if (!belongsToChestLikeBlock(inventory) && !LlamaUtils.belongsToLlama(inventory)) { + if (!isAPICall(inventory) && !belongsToChestLikeBlock(inventory) && !LlamaUtils.belongsToLlama(inventory)) { return; } @@ -159,7 +159,7 @@ public class ChestSortListener implements Listener { Player p = (Player) event.getPlayer(); Inventory inventory = event.getInventory(); - if (!belongsToChestLikeBlock(inventory) && !LlamaUtils.belongsToLlama(inventory)) { + if (!isAPICall(inventory) && !belongsToChestLikeBlock(inventory) && !LlamaUtils.belongsToLlama(inventory)) { return; } @@ -315,14 +315,6 @@ public class ChestSortListener implements Listener { return; } - // DEBUG START -// p.sendMessage("====================="); -// p.sendMessage("Click type: " + event.getClick().name()); -// p.sendMessage("Right click: " + event.isRightClick()); -// p.sendMessage("Shift click: " + event.isShiftClick()); -// p.sendMessage("====================="); - // DEBUG END - if (!p.hasPermission("chestsort.use") && !p.hasPermission("chestsort.use.inventory")) { return; } @@ -331,10 +323,17 @@ public class ChestSortListener implements Listener { if (event.getClickedInventory() == null) { return; } + + boolean isAPICall = isAPICall(event.getClickedInventory()); + // Possible fix for #57 - if (event.getClickedInventory().getHolder() != null + if (!isAPICall && (event.getClickedInventory().getHolder() != null && event.getClickedInventory().getHolder() == p - && event.getClickedInventory() != p.getInventory()) return; + && event.getClickedInventory() != p.getInventory())) { + if(plugin.debug) System.out.println("10"); + return; + } + // End Possible fix for #57 InventoryHolder holder = event.getClickedInventory().getHolder(); @@ -347,11 +346,13 @@ public class ChestSortListener implements Listener { if (event.getClickedInventory() == setting.guiInventory) { return; } + // Prevent player from putting items into GUI inventory if (event.getInventory() == setting.guiInventory) { event.setCancelled(true); return; } + switch (event.getClick()) { case MIDDLE: cause = ChestSortLogger.SortCause.H_MIDDLE; @@ -402,7 +403,7 @@ public class ChestSortListener implements Listener { return; } - if (belongsToChestLikeBlock(event.getClickedInventory()) || LlamaUtils.belongsToLlama(event.getClickedInventory()) || minepacksHook.isMinepacksBackpack(event.getClickedInventory())) { + if (isAPICall || belongsToChestLikeBlock(event.getClickedInventory()) || LlamaUtils.belongsToLlama(event.getClickedInventory()) || minepacksHook.isMinepacksBackpack(event.getClickedInventory())) { if (!p.hasPermission("chestsort.use")) { return; @@ -435,6 +436,10 @@ public class ChestSortListener implements Listener { } } + private boolean isAPICall(Inventory inv) { + return inv.getHolder() instanceof ISortable; + } + @EventHandler public void onAdditionalHotkeys(InventoryClickEvent e) { diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortOrganizer.java b/src/main/java/de/jeff_media/ChestSort/ChestSortOrganizer.java index 6e44f75..ec10c63 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortOrganizer.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortOrganizer.java @@ -127,7 +127,7 @@ public class ChestSortOrganizer { } static boolean doesInventoryContain(Inventory inv, Material mat) { - for (ItemStack item : inv.getContents()) { + for (ItemStack item : inv.getStorageContents()) { if (item == null) continue; if (item.getType() == mat) { return true; diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortPlugin.java b/src/main/java/de/jeff_media/ChestSort/ChestSortPlugin.java index 0720c06..d805e58 100644 --- a/src/main/java/de/jeff_media/ChestSort/ChestSortPlugin.java +++ b/src/main/java/de/jeff_media/ChestSort/ChestSortPlugin.java @@ -55,7 +55,7 @@ import org.bukkit.plugin.java.JavaPlugin; import at.pcgamingfreaks.Minepacks.Bukkit.API.MinepacksPlugin; import de.jeff_media.ChestSort.utils.Utils; -public class ChestSortPlugin extends JavaPlugin { +public class ChestSortPlugin extends JavaPlugin implements ChestSort { ChestSortLogger lgr; Map perPlayerSettings = new HashMap<>(); @@ -67,7 +67,7 @@ public class ChestSortPlugin extends JavaPlugin { ChestSortPermissionsHandler permissionsHandler; String sortingMethod; ArrayList disabledWorlds; - ChestSortAPI api; + ChestSortAPIHandler api; final int currentConfigVersion = 35; boolean usingMatchingConfig = true; protected boolean debug = false; @@ -84,13 +84,15 @@ public class ChestSortPlugin extends JavaPlugin { // 1.14.4 = 1_14_R1 // 1.8.0 = 1_8_R1 int mcMinorVersion; // 14 for 1.14, 13 for 1.13, ... - - public ChestSortAPI getAPI() { + + @Override + public ChestSortAPIHandler getAPI() { return this.api; } // Public API method to sort any given inventory @Deprecated + @Override public void sortInventory(Inventory inv) { StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); getLogger().warning(String.format("%s has performed a call to a deprecated ChestSort API method. This is NOT a ChestSort error.",stackTraceElements[2])); @@ -99,6 +101,7 @@ public class ChestSortPlugin extends JavaPlugin { // Public API method to sort any given inventory inbetween startSlot and endSlot @Deprecated + @Override public void sortInventory(Inventory inv, int startSlot, int endSlot) { StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); getLogger().warning(String.format("%s has performed a call to a deprecated ChestSort API method. This is NOT a ChestSort error.",stackTraceElements[2])); @@ -107,6 +110,7 @@ public class ChestSortPlugin extends JavaPlugin { // Public API method to check if player has automatic chest sorting enabled @Deprecated + @Override public boolean sortingEnabled(Player p) { StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); getLogger().warning(String.format("%s has performed a call to a deprecated ChestSort API method. This is NOT a ChestSort error.",stackTraceElements[2])); @@ -452,7 +456,7 @@ public class ChestSortPlugin extends JavaPlugin { settingsGUI = new ChestSortSettingsGUI(this); updateChecker = new PluginUpdateChecker(this, "https://api.jeff-media.de/chestsort/chestsort-latest-version.txt", "https://chestsort.de", "https://chestsort.de/changelog", "https://chestsort.de/donate"); listener = new ChestSortListener(this); - api = new ChestSortAPI(this); + api = new ChestSortAPIHandler(this); permissionsHandler = new ChestSortPermissionsHandler(this); updateCheckInterval = (int) (getConfig().getDouble("check-interval")*60*60); sortingMethod = getConfig().getString("sorting-method"); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 89210c0..0e19768 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ main: de.jeff_media.ChestSort.ChestSortPlugin name: ChestSort -version: 8.13.0 +version: 8.13.1 api-version: "1.13" description: Allows automatic chest sorting author: mfnalex