Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.8-R0.1-SNAPSHOT</version>
<version>1.14.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/ipvp/ingot/ActionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/org/ipvp/ingot/HotbarAction.java
Original file line number Diff line number Diff line change
@@ -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.
*/
Expand All @@ -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;
Expand Down Expand Up @@ -64,13 +66,23 @@ public Optional<Block> 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();
}

@Override
public void setCancelled(boolean cancel) {
interactable = !cancel;
cancellable.setCancelled(cancel);
}
}
1 change: 1 addition & 0 deletions src/main/java/org/ipvp/ingot/HotbarApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
76 changes: 51 additions & 25 deletions src/main/java/org/ipvp/ingot/HotbarFunctionListener.java
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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<ActionHandler> 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
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}

Expand All @@ -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);
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/ipvp/ingot/type/VanillaHotbar.java
Original file line number Diff line number Diff line change
@@ -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.
*/
Expand Down