From 4792124224fcd7c8dc30b44a32883d139fe0114e Mon Sep 17 00:00:00 2001 From: Senne Date: Sat, 1 Jan 2022 22:46:11 +0100 Subject: [PATCH] Refactor MiddleClickListener --- .../listeners/MiddleClickListener.java | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/MiddleClickListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/MiddleClickListener.java index bc5d62bc6..3c212392d 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/MiddleClickListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/MiddleClickListener.java @@ -15,6 +15,7 @@ import org.bukkit.event.inventory.InventoryType.SlotType; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; +import me.mrCookieSlime.Slimefun.api.BlockStorage; /** * The {@link MiddleClickListener} is responsible for listening to the {@link InventoryCreativeEvent}. @@ -37,44 +38,26 @@ public class MiddleClickListener implements Listener { /* * When clicking outside of an inventory with middle click, * ClickType is not MIDDLE but CREATIVE (because this ClickType covers - * multiple cases, we have to filter out more) + * multiple cases, we have to filter out more later on) */ if (e.getClick() == ClickType.CREATIVE && e.getSlotType() == SlotType.QUICKBAR) { HumanEntity player = e.getWhoClicked(); // get the block the player is looking at for later Block b = player.getTargetBlockExact(5); - /* - * In the case on a middle click outside the user inventory, cursor will be set - * to the actual block that is middle clicked, while currentItem will be AIR. - * - * This check is really weird due to the weird nature of this event's behaviour. - * It checks if the block the player is looking at is of the same type as the cursor; - * after this we can make sure that it is a middle click outside of the inventory - * currentItem should also be air, otherwise it is not outside of the inventory - */ - boolean isOutsideInventoryClick = e.getCursor().getType() == b.getType() && e.getCurrentItem().getType() == Material.AIR; + if (isActualMiddleClick(e, b)) { + // find the actual slimefun item the user is looking at + String id = BlockStorage.checkID(b); + SlimefunItem sfItem = SlimefunItem.getById(id); - /* - * This is an edge case where the player is looking at a WALL_HEAD (eg. cargo) - * and then the boolean above wont match because WALL_HEAD != PLAYER_HEAD. - * This check makes up for that lack. - */ - boolean isPlayerWallhead = b.getType() == Material.PLAYER_WALL_HEAD && e.getCursor().getType() == Material.PLAYER_HEAD; - - if (isOutsideInventoryClick || isPlayerWallhead) { - Optional blockId = Slimefun.getBlockDataService().getBlockData(b); - - if (!blockId.isPresent()) { + // vanilla block -> ignore + if (sfItem == null) { return; } - - // find the actual slimefun item the user is looking at - SlimefunItem sfItem = SlimefunItem.getById(blockId.get()); /* * Before giving the item to the user, check if you can swap - * to the item instead (user already has item in inventory). + * to the item instead (user already has item in hotbar). * This is sometimes bypassed by the client itself (not fixable though). */ for (int i = 0; i < 9; i++) { @@ -92,4 +75,26 @@ public class MiddleClickListener implements Listener { } } } + + private boolean isActualMiddleClick(InventoryCreativeEvent e, Block b) { + /* + * On a middle click outside the user inventory, cursor will be set + * to the actual block that is middle clicked, while currentItem will be AIR. + * + * This check is really weird due to the weird nature of this event's behaviour. + * It checks if the block the player is looking at is of the same type as the cursor; + * after this we can make sure that it is a middle click outside of the inventory + * currentItem should also be air, otherwise it is not outside of the inventory + */ + boolean isOutsideInventoryClick = e.getCursor().getType() == b.getType() && e.getCurrentItem().getType() == Material.AIR; + + /* + * This is an edge case where the player is looking at a WALL_HEAD (eg. cargo) + * and then the boolean above wont match because WALL_HEAD != PLAYER_HEAD. + * This check makes up for that lack. + */ + boolean isPlayerWallhead = b.getType() == Material.PLAYER_WALL_HEAD && e.getCursor().getType() == Material.PLAYER_HEAD; + + return isOutsideInventoryClick || isPlayerWallhead; + } }