diff --git a/pom.xml b/pom.xml index d4eea19..fcb912a 100644 --- a/pom.xml +++ b/pom.xml @@ -34,7 +34,7 @@ org.spigotmc spigot-api - 1.8-R0.1-SNAPSHOT + 1.14.4-R0.1-SNAPSHOT provided diff --git a/src/main/java/org/ipvp/ingot/ActionHandler.java b/src/main/java/org/ipvp/ingot/ActionHandler.java index 4a846a4..17f721a 100644 --- a/src/main/java/org/ipvp/ingot/ActionHandler.java +++ b/src/main/java/org/ipvp/ingot/ActionHandler.java @@ -26,6 +26,10 @@ enum ActionType { * Holding the item in a players hand */ HOVER, + /** + * Previous hovering item in a players hand + */ + UNHOVER, /** * Clicking on the item inside an inventory */ diff --git a/src/main/java/org/ipvp/ingot/HotbarAction.java b/src/main/java/org/ipvp/ingot/HotbarAction.java index e83037a..8f8dc12 100644 --- a/src/main/java/org/ipvp/ingot/HotbarAction.java +++ b/src/main/java/org/ipvp/ingot/HotbarAction.java @@ -1,11 +1,11 @@ package org.ipvp.ingot; -import java.util.Optional; - import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.event.Cancellable; +import java.util.Optional; + /** * Represents an action performed with a Hotbar item. */ @@ -15,6 +15,8 @@ public class HotbarAction implements Cancellable { private final Cancellable cancellable; private Entity clicked; private Block block; + + private boolean interactable = false; public HotbarAction(ActionHandler.ActionType type, Cancellable cancellable) { this.type = type; @@ -64,6 +66,15 @@ public Optional getClickedBlock() { return Optional.ofNullable(block); } + /** + * Returns whether an item is interactable + * + * @return true if interactable + */ + public boolean isInteractable() { + return interactable; + } + @Override public boolean isCancelled() { return cancellable.isCancelled(); @@ -71,6 +82,7 @@ public boolean isCancelled() { @Override public void setCancelled(boolean cancel) { + interactable = !cancel; cancellable.setCancelled(cancel); } } diff --git a/src/main/java/org/ipvp/ingot/HotbarApi.java b/src/main/java/org/ipvp/ingot/HotbarApi.java index 98e17ef..ff40440 100644 --- a/src/main/java/org/ipvp/ingot/HotbarApi.java +++ b/src/main/java/org/ipvp/ingot/HotbarApi.java @@ -55,6 +55,7 @@ private static void giveHotbarItems(Player player, Hotbar hotbar) { for (int slot = 0 ; slot < slots.size() ; slot++) { player.getInventory().setItem(slot, slots.get(slot).getItem()); } + player.updateInventory(); } private HotbarApi() { diff --git a/src/main/java/org/ipvp/ingot/HotbarFunctionListener.java b/src/main/java/org/ipvp/ingot/HotbarFunctionListener.java index eea3795..19c52d3 100644 --- a/src/main/java/org/ipvp/ingot/HotbarFunctionListener.java +++ b/src/main/java/org/ipvp/ingot/HotbarFunctionListener.java @@ -1,26 +1,20 @@ package org.ipvp.ingot; -import java.util.Optional; - import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; -import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; +import org.bukkit.event.*; import org.bukkit.event.block.Action; import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityPickupItemEvent; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryType; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEntityEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerItemHeldEvent; +import org.bukkit.event.player.*; import org.bukkit.inventory.Inventory; +import java.util.Optional; + public class HotbarFunctionListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH) @@ -90,37 +84,39 @@ private void passAction(Hotbar hotbar, int index, Player who, ActionHandler.Acti } // Passes the action to a player - private void passAction(Hotbar hotbar, int index, Player who, ActionHandler.ActionType action, Cancellable cancellable, Block block) { - passAction(hotbar, index, who, new HotbarAction(action, cancellable, block)); + private HotbarAction passAction(Hotbar hotbar, int index, Player who, ActionHandler.ActionType action, Cancellable cancellable, Block block) { + HotbarAction hotbarAction = new HotbarAction(action, cancellable, block); + passAction(hotbar, index, who, hotbarAction); + return hotbarAction; } private void passAction(Hotbar hotbar, int index, Player who, HotbarAction action) { Slot slot = hotbar.getSlot(index); Optional actionHandlerOptional = slot.getActionHandler(); - if (actionHandlerOptional.isPresent()) { - actionHandlerOptional.get().performAction(who, action); - } + actionHandlerOptional.ifPresent(actionHandler -> actionHandler.performAction(who, action)); } @EventHandler public void handleHotbarUse(PlayerInteractEvent event) { Player player = event.getPlayer(); Hotbar hotbar = HotbarApi.getCurrentHotbar(player); - + // Don't process the event if the player has no hotbar if (hotbar == null || event.getAction() == Action.PHYSICAL) { return; } - + Action action = event.getAction(); int slot = player.getInventory().getHeldItemSlot(); - ActionHandler.ActionType type = (action == Action.LEFT_CLICK_BLOCK || action == Action.LEFT_CLICK_AIR) + ActionHandler.ActionType type = (action == Action.LEFT_CLICK_BLOCK || action == Action.LEFT_CLICK_AIR) ? ActionHandler.ActionType.LEFT_CLICK : ActionHandler.ActionType.RIGHT_CLICK; - + // Pass the action to the slot - passAction(hotbar, slot, player, type, event, event.getClickedBlock()); - event.setUseItemInHand(Event.Result.DENY); - event.setCancelled(true); + HotbarAction hotbarAction = passAction(hotbar, slot, player, type, event, event.getClickedBlock()); + if (!hotbarAction.isInteractable()) { // Allow actions to determine whether it should be cancelled + event.setCancelled(true); + event.setUseInteractedBlock(Event.Result.DENY); + } } @EventHandler @@ -159,7 +155,7 @@ public void handleHotbarUse(EntityDamageByEntityEvent event) { } @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH) - public void hadleHotbarHover(PlayerItemHeldEvent event) { + public void handleHotbarHover(PlayerItemHeldEvent event) { Player player = event.getPlayer(); Hotbar hotbar = HotbarApi.getCurrentHotbar(player); @@ -169,6 +165,8 @@ public void hadleHotbarHover(PlayerItemHeldEvent event) { } int slot = event.getNewSlot(); + int previousSlot = event.getPreviousSlot(); + passAction(hotbar, previousSlot, player, ActionHandler.ActionType.UNHOVER, event); passAction(hotbar, slot, player, ActionHandler.ActionType.HOVER, event); } @@ -178,11 +176,39 @@ public void handleHotbarDrop(PlayerDropItemEvent event) { Player player = event.getPlayer(); Hotbar hotbar = HotbarApi.getCurrentHotbar(player); - // Don't process if the player has no Hotbar + // Don't process if the player has no hotbar if (hotbar == null) { return; } event.setCancelled(true); } + + @EventHandler + public void handleHotbarPickup(EntityPickupItemEvent event) { + if (event.getEntity() instanceof Player) { + Player player = (Player) event.getEntity(); + Hotbar hotbar = HotbarApi.getCurrentHotbar(player); + + // Don't process if the player has no hotbar + if (hotbar == null) { + return; + } + + event.setCancelled(true); + } + } + + @EventHandler + public void handleHotbarSwap(PlayerSwapHandItemsEvent event) { + Player player = event.getPlayer(); + Hotbar hotbar = HotbarApi.getCurrentHotbar(player); + + // Don't process if the player has no hotbar + if (hotbar == null) { + return; + } + + event.setCancelled(true); + } } \ No newline at end of file diff --git a/src/main/java/org/ipvp/ingot/type/VanillaHotbar.java b/src/main/java/org/ipvp/ingot/type/VanillaHotbar.java index bebd4a9..0521743 100644 --- a/src/main/java/org/ipvp/ingot/type/VanillaHotbar.java +++ b/src/main/java/org/ipvp/ingot/type/VanillaHotbar.java @@ -1,12 +1,12 @@ package org.ipvp.ingot.type; +import org.ipvp.ingot.Hotbar; +import org.ipvp.ingot.Slot; + import java.util.Arrays; import java.util.List; import java.util.Optional; -import org.ipvp.ingot.Hotbar; -import org.ipvp.ingot.Slot; - /** * A simple Hotbar implementation that can be given to players. */