From 2ede04148892e7a1d77fe1ad351fb4eaa258badf Mon Sep 17 00:00:00 2001
From: mfnalex <1122571+mfnalex@users.noreply.github.com>
Date: Thu, 16 Jul 2020 22:18:20 +0200
Subject: [PATCH] 8.17.0-SNAPSHOT (fake event)
---
CHANGELOG.md | 3 ++
pom.xml | 4 +--
.../ChestSort/ChestSortListener.java | 29 +++++++++----------
.../ChestSort/hooks/GenericGUIHook.java | 13 +++++++++
src/main/resources/plugin.yml | 2 +-
5 files changed, 32 insertions(+), 19 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e4b1478..58ec04e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,8 @@
# Changelog
+## 8.17.0-SNAPSHOT
+- Rewrote custom GUI detection completely, now using a fake InventoryClickEvent
+
## 8.16.1
- Allow middle-click hotkey in creative mode when clicked slot is empty
diff --git a/pom.xml b/pom.xml
index 3961d25..f427580 100644
--- a/pom.xml
+++ b/pom.xml
@@ -9,7 +9,7 @@
ChestSort
https://www.chestsort.de
Automatically sorts your chests!
- 8.16.0
+ 8.17.0-SNAPSHOT
jar
@@ -113,7 +113,7 @@
de.jeff_media
ChestSortAPI
- 1.0.0
+ 2.0.0-SNAPSHOT
compile
diff --git a/src/main/java/de/jeff_media/ChestSort/ChestSortListener.java b/src/main/java/de/jeff_media/ChestSort/ChestSortListener.java
index 9729fb9..5fad252 100644
--- a/src/main/java/de/jeff_media/ChestSort/ChestSortListener.java
+++ b/src/main/java/de/jeff_media/ChestSort/ChestSortListener.java
@@ -4,6 +4,7 @@ import de.jeff_media.ChestSort.hooks.CrateReloadedHook;
import de.jeff_media.ChestSort.hooks.HeadDatabaseHook;
import de.jeff_media.ChestSort.hooks.MinepacksHook;
import de.jeff_media.ChestSort.utils.LlamaUtils;
+import de.jeff_media.ChestSortAPI.ChestSortCheckForCustomGUIEvent;
import de.jeff_media.ChestSortAPI.ChestSortEvent;
import de.jeff_media.ChestSortAPI.ISortable;
import org.bukkit.Bukkit;
@@ -310,6 +311,7 @@ public class ChestSortListener implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onHotkey(InventoryClickEvent event) {
+ if(event instanceof ChestSortCheckForCustomGUIEvent) return;
if (!(event.getWhoClicked() instanceof Player)) {
return;
@@ -341,11 +343,8 @@ public class ChestSortListener implements Listener {
return;
}
- // Possible fix for #57
- if (!isAPICall && (event.getClickedInventory().getHolder() != null
- && event.getClickedInventory().getHolder() == p
- && event.getClickedInventory() != p.getInventory())) {
- if(plugin.debug) System.out.println("10");
+ if(plugin.genericHook.isPluginGUIByEvent(event.getView(),event.getSlotType(),event.getSlot(),event.getClick(),event.getAction())) {
+ plugin.debug("Event cancelled because genericHook#isPluginGUIByEvent");
return;
}
@@ -458,37 +457,35 @@ public class ChestSortListener implements Listener {
@EventHandler
public void onAdditionalHotkeys(InventoryClickEvent e) {
+ if(e instanceof ChestSortCheckForCustomGUIEvent) return;
if (LlamaUtils.belongsToLlama(e.getInventory()) || LlamaUtils.belongsToLlama(e.getClickedInventory())) {
+ plugin.debug("Cancel AdditionalHotkeys: Belongs to Llama");
return;
+
}
if (!plugin.getConfig().getBoolean("allow-additional-hotkeys")) {
+ plugin.debug("Cancel AdditionalHotkeys: allow-additional-hotkeys false");
return;
}
if (!(e.getWhoClicked() instanceof Player)) {
+ plugin.debug("Cancel AdditionalHotkeys: getWhoClicked !instanceof Player");
return;
}
Player p = (Player) e.getWhoClicked();
// Only continue if clicked outside of the chest
if (e.getClickedInventory() != null) {
+ plugin.debug("Cancel AdditionalHotkeys: getCLickedInventory != null");
return;
}
// Only continue if hand is empty
if (e.getCursor() != null && e.getCursor().getType() != null && e.getCursor().getType() != Material.AIR) {
+ plugin.debug("Cancel AdditionalHotkeys: getCursor != null && getCursor.getType!=null || != AIR");
return;
}
- // Possible fix for #57
- if (e.getInventory().getHolder() == null) return;
- if (e.getInventory().getHolder() == p && e.getInventory() != p.getInventory()) return;
- // End Possible fix for #57
- if (e.getInventory().getType() != InventoryType.CHEST
- && e.getInventory().getType() != InventoryType.DISPENSER
- && e.getInventory().getType() != InventoryType.DROPPER
- && e.getInventory().getType() != InventoryType.ENDER_CHEST
- && !e.getInventory().getType().name().equalsIgnoreCase("SHULKER_BOX")
- && (e.getInventory().getHolder() == null || !e.getInventory().getHolder().getClass().toString().endsWith(".CraftBarrel"))
- && !(e.getInventory().getHolder() instanceof ISortable)) {
+ if(plugin.genericHook.isPluginGUIByEvent(e.getView(),e.getSlotType(),e.getSlot(),e.getClick(),e.getAction())) {
+ plugin.debug("Event cancelled because genericHook#isPluginGUIByEvent");
return;
}
diff --git a/src/main/java/de/jeff_media/ChestSort/hooks/GenericGUIHook.java b/src/main/java/de/jeff_media/ChestSort/hooks/GenericGUIHook.java
index 04e4c44..52d50db 100644
--- a/src/main/java/de/jeff_media/ChestSort/hooks/GenericGUIHook.java
+++ b/src/main/java/de/jeff_media/ChestSort/hooks/GenericGUIHook.java
@@ -1,7 +1,13 @@
package de.jeff_media.ChestSort.hooks;
import de.jeff_media.ChestSort.ChestSortPlugin;
+import de.jeff_media.ChestSortAPI.ChestSortCheckForCustomGUIEvent;
+import org.bukkit.event.inventory.ClickType;
+import org.bukkit.event.inventory.InventoryAction;
+import org.bukkit.event.inventory.InventoryClickEvent;
+import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
+import org.bukkit.inventory.InventoryView;
public class GenericGUIHook {
@@ -13,6 +19,13 @@ public class GenericGUIHook {
this.enabled=enabled;
}
+ public boolean isPluginGUIByEvent(InventoryView view, InventoryType.SlotType type, int slot, ClickType click, InventoryAction action) {
+ if(slot==-999) slot= 9;
+ ChestSortCheckForCustomGUIEvent event = new ChestSortCheckForCustomGUIEvent(view, type, slot, click, action);
+ main.getServer().getPluginManager().callEvent(event);
+ return event.isCancelled();
+ }
+
public boolean isPluginGUI(Inventory inv) {
if(!enabled) return false;
if(inv.getHolder()!=null && inv.getHolder().getClass().getName().contains("GUI")) {
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index 9569773..116cb62 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.16.0
+version: 8.17.0-SNAPSHOT
api-version: "1.13"
description: Allows automatic chest sorting
author: mfnalex