cn.nukkit.event.player.PlayerItemHeldEvent Java Examples

The following examples show how to use cn.nukkit.event.player.PlayerItemHeldEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PlayerInventory.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Called when a client equips a hotbar inventorySlot. This method should not be used by plugins.
 * This method will call PlayerItemHeldEvent.
 *
 * @param slot hotbar slot Number of the hotbar slot to equip.
 * @return boolean if the equipment change was successful, false if not.
 */
public boolean equipItem(int slot) {
    if (!isHotbarSlot(slot)) {
        this.sendContents((Player) this.getHolder());
        return false;
    }

    if (this.getHolder() instanceof Player) {
        PlayerItemHeldEvent ev = new PlayerItemHeldEvent((Player) this.getHolder(), this.getItem(slot), slot);
        this.getHolder().getLevel().getServer().getPluginManager().callEvent(ev);

        if (ev.isCancelled()) {
            this.sendContents(this.getViewers());
            return false;
        }
    }

    this.setHeldItemIndex(slot, false);
    return true;
}
 
Example #2
Source File: PlayerInventory.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Called when a client equips a hotbar inventorySlot. This method should not be used by plugins.
 * This method will call PlayerItemHeldEvent.
 *
 * @param slot hotbar slot Number of the hotbar slot to equip.
 * @return boolean if the equipment change was successful, false if not.
 */
public boolean equipItem(int slot) {
    if (!isHotbarSlot(slot)) {
        this.sendContents((Player) this.getHolder());
        return false;
    }

    if (this.getHolder() instanceof Player) {
        PlayerItemHeldEvent ev = new PlayerItemHeldEvent((Player) this.getHolder(), this.getItem(slot), slot);
        this.getHolder().getLevel().getServer().getPluginManager().callEvent(ev);

        if (ev.isCancelled()) {
            this.sendContents(this.getViewers());
            return false;
        }
    }

    this.setHeldItemIndex(slot, false);
    return true;
}
 
Example #3
Source File: PlayerInventory.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Called when a client equips a hotbar inventorySlot. This method should not be used by plugins.
 * This method will call PlayerItemHeldEvent.
 *
 * @param slot hotbar slot Number of the hotbar slot to equip.
 * @return boolean if the equipment change was successful, false if not.
 */
public boolean equipItem(int slot) {
    if (!isHotbarSlot(slot)) {
        this.sendContents((Player) this.getHolder());
        return false;
    }

    if (this.getHolder() instanceof Player) {
        Player player = (Player) this.getHolder();
        PlayerItemHeldEvent ev = new PlayerItemHeldEvent(player, this.getItem(slot), slot);
        this.getHolder().getLevel().getServer().getPluginManager().callEvent(ev);

        if (ev.isCancelled()) {
            this.sendContents(this.getViewers());
            return false;
        }

        if (player.fishing != null) {
            if (!(this.getItem(slot).equals(player.fishing.rod))) {
                player.stopFishing(false);
            }
        }
    }

    this.setHeldItemIndex(slot, false);
    return true;
}
 
Example #4
Source File: BrushListener.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerItemHoldEvent(final PlayerItemHeldEvent event) {
    Player nukkitPlayer = event.getPlayer();
    if (nukkitPlayer.isSneaking()) {
        return;
    }
    FawePlayer<Object> fp = FawePlayer.wrap(nukkitPlayer);
    com.sk89q.worldedit.entity.Player player = fp.getPlayer();
    LocalSession session = fp.getSession();
    Tool tool = session.getTool(player);
    if (tool instanceof ScrollTool) {
        final int slot = event.getInventorySlot();
        final int oldSlot = event.getSlot();
        final int ri;
        if ((((slot - oldSlot) <= 4) && ((slot - oldSlot) > 0)) || (((slot - oldSlot) < -4))) {
            ri = 1;
        } else {
            ri = -1;
        }
        ScrollTool scrollable = (ScrollTool) tool;
        if (scrollable.increment(player, ri)) {
            final PlayerInventory inv = nukkitPlayer.getInventory();
            final Item item = inv.getItem(slot);
            final Item newItem = inv.getItem(oldSlot);
            inv.setItem(slot, newItem);
            inv.setItem(oldSlot, item);
            inv.sendContents(nukkitPlayer);
        }
    }
}