1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 11:45:51 +00:00

Refactor MiddleClickListener

This commit is contained in:
Senne 2022-01-01 22:46:11 +01:00
parent 4e92102a38
commit 4792124224
No known key found for this signature in database
GPG Key ID: EA68733326BBA376

View File

@ -15,6 +15,7 @@ import org.bukkit.event.inventory.InventoryType.SlotType;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
/** /**
* The {@link MiddleClickListener} is responsible for listening to the {@link InventoryCreativeEvent}. * 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, * When clicking outside of an inventory with middle click,
* ClickType is not MIDDLE but CREATIVE (because this ClickType covers * 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) { if (e.getClick() == ClickType.CREATIVE && e.getSlotType() == SlotType.QUICKBAR) {
HumanEntity player = e.getWhoClicked(); HumanEntity player = e.getWhoClicked();
// get the block the player is looking at for later // get the block the player is looking at for later
Block b = player.getTargetBlockExact(5); Block b = player.getTargetBlockExact(5);
/* if (isActualMiddleClick(e, b)) {
* In the case on a middle click outside the user inventory, cursor will be set // find the actual slimefun item the user is looking at
* to the actual block that is middle clicked, while currentItem will be AIR. String id = BlockStorage.checkID(b);
* SlimefunItem sfItem = SlimefunItem.getById(id);
* 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;
/* // vanilla block -> ignore
* This is an edge case where the player is looking at a WALL_HEAD (eg. cargo) if (sfItem == null) {
* 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<String> blockId = Slimefun.getBlockDataService().getBlockData(b);
if (!blockId.isPresent()) {
return; 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 * 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). * This is sometimes bypassed by the client itself (not fixable though).
*/ */
for (int i = 0; i < 9; i++) { 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;
}
} }