From 5462a9738976217fcb751d01449ec48f010e281d Mon Sep 17 00:00:00 2001 From: RaphaelFakhri Date: Wed, 5 Mar 2025 21:05:14 +0200 Subject: [PATCH 1/4] Prevent number key usage in GPS-related GUIs 1. Added isNumberKey() method to ClickAction class to detect number key usage 2. Modified MenuListener to pass InventoryClickEvent to ClickAction constructor 3. Added special handlers to GPS Control Panel, Waypoint Panel, and GEO Scanner GUIs 4. These changes prevent users from using number keys in GPS GUIs which was causing items to be lost --- .../slimefun4/api/geo/ResourceManager.java | 13 ++++++++++ .../slimefun4/api/gps/GPSNetwork.java | 26 +++++++++++++++++++ .../general/Inventory/ClickAction.java | 15 ++++++++++- .../general/Inventory/MenuListener.java | 6 ++--- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java index ce4fea9b02..067545bc92 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java @@ -222,6 +222,19 @@ public void scan(@Nonnull Player p, @Nonnull Block block, int page) { String title = "&4" + Slimefun.getLocalization().getResourceString(p, "tooltips.results"); ChestMenu menu = new ChestMenu(title); + + // Prevent number key usage + menu.setPlayerInventoryClickable(true); + menu.setEmptySlotsClickable(false); + + // Add a special click handler to prevent number keys + menu.addPlayerInventoryClickHandler((player, slot, item, action) -> { + // Check if the click is a number key + if (action.isNumberKey()) { + return false; + } + return true; + }); for (int slot : backgroundSlots) { menu.addItem(slot, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler()); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java index 47574fb2ca..4c9069e694 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java @@ -144,6 +144,19 @@ public int countTransmitters(@Nonnull UUID uuid) { */ public void openTransmitterControlPanel(@Nonnull Player p) { ChestMenu menu = new ChestMenu(ChatColor.BLUE + Slimefun.getLocalization().getMessage(p, "machines.GPS_CONTROL_PANEL.title")); + + // Prevent number key usage + menu.setPlayerInventoryClickable(true); + menu.setEmptySlotsClickable(false); + + // Add a special click handler to prevent number keys + menu.addPlayerInventoryClickHandler((player, slot, item, action) -> { + // Check if the click is a number key + if (action.isNumberKey()) { + return false; + } + return true; + }); for (int slot : border) { menu.addItem(slot, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler()); @@ -227,6 +240,19 @@ public void openTransmitterControlPanel(@Nonnull Player p) { public void openWaypointControlPanel(@Nonnull Player p) { PlayerProfile.get(p, profile -> { ChestMenu menu = new ChestMenu(ChatColor.BLUE + Slimefun.getLocalization().getMessage(p, "machines.GPS_CONTROL_PANEL.title")); + + // Prevent number key usage + menu.setPlayerInventoryClickable(true); + menu.setEmptySlotsClickable(false); + + // Add a special click handler to prevent number keys + menu.addPlayerInventoryClickHandler((player, slot, item, action) -> { + // Check if the click is a number key + if (action.isNumberKey()) { + return false; + } + return true; + }); for (int slot : border) { menu.addItem(slot, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler()); diff --git a/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/ClickAction.java b/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/ClickAction.java index 4ad692a6e1..de4ab4d7ca 100644 --- a/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/ClickAction.java +++ b/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/ClickAction.java @@ -1,5 +1,7 @@ package me.mrCookieSlime.CSCoreLibPlugin.general.Inventory; +import org.bukkit.event.inventory.InventoryClickEvent; + /** * An old remnant of CS-CoreLib. * This will be removed once we updated everything. @@ -9,10 +11,18 @@ public class ClickAction { private boolean right; private boolean shift; + private boolean numberKey; public ClickAction(boolean rightClicked, boolean shiftClicked) { this.right = rightClicked; this.shift = shiftClicked; + this.numberKey = false; + } + + public ClickAction(InventoryClickEvent e) { + this.right = e.isRightClick(); + this.shift = e.isShiftClick(); + this.numberKey = e.getClick().name().equals("NUMBER_KEY"); } public boolean isRightClicked() { @@ -22,5 +32,8 @@ public boolean isRightClicked() { public boolean isShiftClicked() { return shift; } - + + public boolean isNumberKey() { + return numberKey; + } } diff --git a/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/MenuListener.java b/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/MenuListener.java index 1647e5aeee..615d5c7770 100644 --- a/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/MenuListener.java +++ b/src/main/java/me/mrCookieSlime/CSCoreLibPlugin/general/Inventory/MenuListener.java @@ -48,12 +48,12 @@ public void onClick(InventoryClickEvent e) { if (handler == null) { e.setCancelled(!menu.isEmptySlotsClickable() && (e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR)); } else if (handler instanceof AdvancedMenuClickHandler) { - e.setCancelled(!((AdvancedMenuClickHandler) handler).onClick(e, (Player) e.getWhoClicked(), e.getSlot(), e.getCursor(), new ClickAction(e.isRightClick(), e.isShiftClick()))); + e.setCancelled(!((AdvancedMenuClickHandler) handler).onClick(e, (Player) e.getWhoClicked(), e.getSlot(), e.getCursor(), new ClickAction(e))); } else { - e.setCancelled(!handler.onClick((Player) e.getWhoClicked(), e.getSlot(), e.getCurrentItem(), new ClickAction(e.isRightClick(), e.isShiftClick()))); + e.setCancelled(!handler.onClick((Player) e.getWhoClicked(), e.getSlot(), e.getCurrentItem(), new ClickAction(e))); } } else { - e.setCancelled(!menu.getPlayerInventoryClickHandler().onClick((Player) e.getWhoClicked(), e.getSlot(), e.getCurrentItem(), new ClickAction(e.isRightClick(), e.isShiftClick()))); + e.setCancelled(!menu.getPlayerInventoryClickHandler().onClick((Player) e.getWhoClicked(), e.getSlot(), e.getCurrentItem(), new ClickAction(e))); } } } From 03231dfabd2027b8a1ae1cc79e347482db5404f2 Mon Sep 17 00:00:00 2001 From: RaphaelFakhri Date: Wed, 5 Mar 2025 21:39:51 +0200 Subject: [PATCH 2/4] Resolves #4260 --- pom.xml | 2 +- .../slimefun4/api/geo/ResourceManager.java | 17 ++--- .../slimefun4/api/gps/GPSNetwork.java | 70 +++++++++---------- 3 files changed, 40 insertions(+), 49 deletions(-) diff --git a/pom.xml b/pom.xml index 7cdf099b04..57d6c680ec 100644 --- a/pom.xml +++ b/pom.xml @@ -387,7 +387,7 @@ - com.github.MockBukkit + com.github.mockbukkit MockBukkit c7cc678834 test diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java index 067545bc92..b6caeb8686 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java @@ -222,20 +222,17 @@ public void scan(@Nonnull Player p, @Nonnull Block block, int page) { String title = "&4" + Slimefun.getLocalization().getResourceString(p, "tooltips.results"); ChestMenu menu = new ChestMenu(title); - - // Prevent number key usage - menu.setPlayerInventoryClickable(true); + + // Prevent both shift-clicking and number key usage + menu.setPlayerInventoryClickable(false); menu.setEmptySlotsClickable(false); - - // Add a special click handler to prevent number keys + + // Add click handler to explicitly deny shift and number key actions menu.addPlayerInventoryClickHandler((player, slot, item, action) -> { - // Check if the click is a number key - if (action.isNumberKey()) { - return false; - } - return true; + return false; // Deny all player inventory interactions }); + // Rest of the method remains unchanged for (int slot : backgroundSlots) { menu.addItem(slot, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler()); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java index 4c9069e694..fd72786706 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java @@ -44,9 +44,9 @@ * The {@link GPSNetwork} is a manager class for all {@link GPSTransmitter Transmitters} and waypoints. * There can only be one instance of this class per {@link Server}. * It is also responsible for teleportation and resource management. - * + * * @author TheBusyBiscuit - * + * * @see TeleportationManager * @see ResourceManager * @@ -64,7 +64,7 @@ public class GPSNetwork { /** * This constructs a new {@link GPSNetwork}. * Note that this network is per {@link Server} and not per {@link Player}. - * + * * @param plugin * Our {@link Slimefun} instance */ @@ -74,7 +74,7 @@ public GPSNetwork(@Nonnull Slimefun plugin) { /** * This method updates the status of a {@link GPSTransmitter}. - * + * * @param l * The {@link Location} of the {@link GPSTransmitter} * @param uuid @@ -96,10 +96,10 @@ public void updateTransmitter(@Nonnull Location l, @Nonnull UUID uuid, boolean o * This method calculates the GPS complexity for the given {@link UUID}. * The complexity is determined by the Y level of each {@link GPSTransmitter} * multiplied by the multiplier of that transmitter. - * + * * @param uuid * The {@link UUID} who to calculate it for - * + * * @return The network complexity for that {@link UUID} */ public int getNetworkComplexity(@Nonnull UUID uuid) { @@ -124,10 +124,10 @@ public int getNetworkComplexity(@Nonnull UUID uuid) { /** * This method returns the amount of {@link GPSTransmitter Transmitters} for the * given {@link UUID}. - * + * * @param uuid * The {@link UUID} who these transmitters belong to - * + * * @return The amount of transmitters */ public int countTransmitters(@Nonnull UUID uuid) { @@ -138,26 +138,23 @@ public int countTransmitters(@Nonnull UUID uuid) { /** * This method opens the {@link GPSTransmitter} control panel to the given * {@link Player}. - * + * * @param p * The {@link Player} */ public void openTransmitterControlPanel(@Nonnull Player p) { ChestMenu menu = new ChestMenu(ChatColor.BLUE + Slimefun.getLocalization().getMessage(p, "machines.GPS_CONTROL_PANEL.title")); - - // Prevent number key usage - menu.setPlayerInventoryClickable(true); + + // Prevent both shift-clicking and number key usage + menu.setPlayerInventoryClickable(false); menu.setEmptySlotsClickable(false); - - // Add a special click handler to prevent number keys + + // Add click handler to explicitly deny shift and number key actions menu.addPlayerInventoryClickHandler((player, slot, item, action) -> { - // Check if the click is a number key - if (action.isNumberKey()) { - return false; - } - return true; + return false; // Deny all player inventory interactions }); + // Rest of the method remains unchanged for (int slot : border) { menu.addItem(slot, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler()); } @@ -205,14 +202,14 @@ public void openTransmitterControlPanel(@Nonnull Player p) { * The icon is dependent on the {@link Environment} of the waypoint's {@link World}. * However if the name of this waypoint indicates that this is actually a deathmarker * then a different texture will be used. - * + * * Otherwise it will return a globe, a nether or end sphere according to the {@link Environment}. - * + * * @param name * The name of a waypoint * @param environment * The {@link Environment} of the waypoint's {@link World} - * + * * @return An icon for this waypoint */ @ParametersAreNonnullByDefault @@ -240,20 +237,17 @@ public void openTransmitterControlPanel(@Nonnull Player p) { public void openWaypointControlPanel(@Nonnull Player p) { PlayerProfile.get(p, profile -> { ChestMenu menu = new ChestMenu(ChatColor.BLUE + Slimefun.getLocalization().getMessage(p, "machines.GPS_CONTROL_PANEL.title")); - - // Prevent number key usage - menu.setPlayerInventoryClickable(true); + + // Prevent both shift-clicking and number key usage + menu.setPlayerInventoryClickable(false); menu.setEmptySlotsClickable(false); - - // Add a special click handler to prevent number keys + + // Add click handler to explicitly deny shift and number key actions menu.addPlayerInventoryClickHandler((player, slot, item, action) -> { - // Check if the click is a number key - if (action.isNumberKey()) { - return false; - } - return true; + return false; // Deny all player inventory interactions }); + // Rest of the method remains unchanged for (int slot : border) { menu.addItem(slot, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler()); } @@ -299,7 +293,7 @@ public void openWaypointControlPanel(@Nonnull Player p) { /** * This method will prompt the given {@link Player} to enter a name for a waypoint. * After entering the name, it will be added to his waypoint list. - * + * * @param p * The {@link Player} who should get a new waypoint * @param l @@ -324,7 +318,7 @@ public void createWaypoint(@Nonnull Player p, @Nonnull Location l) { /** * This method adds a new waypoint with the given name and {@link Location} for that {@link Player}. - * + * * @param p * The {@link Player} to get the new waypoint * @param name @@ -369,10 +363,10 @@ public void addWaypoint(@Nonnull Player p, @Nonnull String name, @Nonnull Locati /** * This method returns a {@link Set} of {@link Location Locations} for all {@link GPSTransmitter Transmitters} * owned by the given {@link UUID}. - * + * * @param uuid * The {@link UUID} owning those transmitters - * + * * @return A {@link Set} with all {@link Location Locations} of transmitters for this {@link UUID} */ @Nonnull @@ -383,7 +377,7 @@ public Set getTransmitters(@Nonnull UUID uuid) { /** * This returns the {@link TeleportationManager} for this {@link GPSNetwork}. * It is responsible for all actions that relate to the {@link Teleporter}. - * + * * @return The {@link TeleportationManager} for this {@link GPSNetwork} */ @Nonnull @@ -394,7 +388,7 @@ public TeleportationManager getTeleportationManager() { /** * This returns the {@link ResourceManager} for this {@link GPSNetwork}. * Use this to access {@link GEOResource GEOResources}. - * + * * @return The {@link ResourceManager} for this {@link GPSNetwork} */ @Nonnull From d20e69616a4a9ad046c2be9ea55c3181b594482f Mon Sep 17 00:00:00 2001 From: RaphaelFakhri Date: Wed, 5 Mar 2025 21:42:39 +0200 Subject: [PATCH 3/4] Resolves #4260 --- .../github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java | 1 - .../io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java index b6caeb8686..c6d74824a4 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/geo/ResourceManager.java @@ -232,7 +232,6 @@ public void scan(@Nonnull Player p, @Nonnull Block block, int page) { return false; // Deny all player inventory interactions }); - // Rest of the method remains unchanged for (int slot : backgroundSlots) { menu.addItem(slot, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler()); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java index fd72786706..6590fd355d 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java @@ -247,7 +247,6 @@ public void openWaypointControlPanel(@Nonnull Player p) { return false; // Deny all player inventory interactions }); - // Rest of the method remains unchanged for (int slot : border) { menu.addItem(slot, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler()); } From d3ef39a5219f8e8563b4d4aed386564d22ab51f3 Mon Sep 17 00:00:00 2001 From: RaphaelFakhri <153192858+RaphaelFakhri@users.noreply.github.com> Date: Thu, 6 Mar 2025 10:18:22 +0200 Subject: [PATCH 4/4] removed comment that was on the same line as code (formatting) --- .../io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java index 6590fd355d..1aa57958c9 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java @@ -151,10 +151,9 @@ public void openTransmitterControlPanel(@Nonnull Player p) { // Add click handler to explicitly deny shift and number key actions menu.addPlayerInventoryClickHandler((player, slot, item, action) -> { - return false; // Deny all player inventory interactions + return false; }); - // Rest of the method remains unchanged for (int slot : border) { menu.addItem(slot, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler()); }