Search in sources :

Example 1 with PlayerItemHeldEvent

use of org.bukkit.event.player.PlayerItemHeldEvent in project Glowstone by GlowstoneMC.

the class HeldItemHandler method handle.

@Override
public void handle(GlowSession session, HeldItemMessage message) {
    int slot = message.getSlot();
    if (slot < 0 || slot > 8) {
        // sanity check
        return;
    }
    GlowPlayer player = session.getPlayer();
    int oldSlot = player.getInventory().getHeldItemSlot();
    if (slot == oldSlot) {
        // ignore
        return;
    }
    PlayerItemHeldEvent event = new PlayerItemHeldEvent(player, oldSlot, slot);
    EventFactory.callEvent(event);
    if (!event.isCancelled()) {
        player.getInventory().setRawHeldItemSlot(slot);
    } else {
        // sends a packet to switch back to the previous held slot
        player.getInventory().setHeldItemSlot(oldSlot);
    }
}
Also used : GlowPlayer(net.glowstone.entity.GlowPlayer) PlayerItemHeldEvent(org.bukkit.event.player.PlayerItemHeldEvent)

Aggregations

GlowPlayer (net.glowstone.entity.GlowPlayer)1 PlayerItemHeldEvent (org.bukkit.event.player.PlayerItemHeldEvent)1