Java Code Examples for org.bukkit.event.player.PlayerItemHeldEvent#getPlayer()

The following examples show how to use org.bukkit.event.player.PlayerItemHeldEvent#getPlayer() . 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: CreateListener.java    From Shopkeepers with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
void onItemHeld(PlayerItemHeldEvent event) {
	Player player = event.getPlayer();
	if (player.getGameMode() == GameMode.CREATIVE) return;
	ItemStack newItemInHand = player.getInventory().getItem(event.getNewSlot());
	if (!Settings.isShopCreationItem(newItemInHand)) {
		return;
	}

	if (!ShopkeepersPlugin.getInstance().hasCreatePermission(player)) {
		// player cannot create any shopkeeper at all
		return;
	}

	// print info message about usage:
	Utils.sendMessage(player, Settings.msgCreationItemSelected);
}
 
Example 2
Source File: ActionListener.java    From ActionHealth with MIT License 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onSwap(PlayerItemHeldEvent event) {
    if (!main.configStore.actionStore.enabled)
        return;

    ActionStore.ActionType actionType = ActionStore.ActionType.SWAP;
    Player player = event.getPlayer();

    ItemStack itemStack = player.getInventory().getItem(event.getNewSlot());

    actionHelper.executeTriggers(actionType, player, itemStack);
}
 
Example 3
Source File: DurabilityBar.java    From AdditionsAPI with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onItemSwitch(PlayerItemHeldEvent event) {
	if (event.isCancelled())
		return;
	Player player = event.getPlayer();
	Bukkit.getScheduler().scheduleSyncDelayedTask(AdditionsAPI.getInstance(), () -> {
		sendDurabilityBossBar(player, player.getInventory().getItemInMainHand(), EquipmentSlot.HAND);
		sendDurabilityBossBar(player, player.getInventory().getItemInOffHand(), EquipmentSlot.OFF_HAND);
	});
}
 
Example 4
Source File: PlayerListener.java    From WorldEditSelectionVisualizer with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerHeldItem(PlayerItemHeldEvent e) {
    Player player = e.getPlayer();
    ItemStack oldItem = player.getInventory().getItem(e.getPreviousSlot());
    ItemStack newItem = player.getInventory().getItem(e.getNewSlot());

    if ((oldItem == null && newItem == null) || (oldItem != null && newItem != null && oldItem.getType() == newItem.getType())) {
        return;
    }

    Bukkit.getScheduler().runTask(plugin, () ->
            plugin.updateHoldingSelectionItem(plugin.getPlayerInfos(player)));
}
 
Example 5
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) {
    final Player bukkitPlayer = event.getPlayer();
    if (bukkitPlayer.isSneaking()) {
        return;
    }
    FawePlayer<Object> fp = FawePlayer.wrap(bukkitPlayer);
    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.getNewSlot();
        final int oldSlot = event.getPreviousSlot();
        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)) {
            if (Settings.IMP.EXPERIMENTAL.PERSISTENT_BRUSHES) {
                bukkitPlayer.getInventory().setHeldItemSlot(oldSlot);
            } else {
                final PlayerInventory inv = bukkitPlayer.getInventory();
                final ItemStack item = inv.getItem(slot);
                final ItemStack newItem = inv.getItem(oldSlot);
                inv.setItem(slot, newItem);
                inv.setItem(oldSlot, item);
                bukkitPlayer.updateInventory();
            }
        }
    }
}
 
Example 6
Source File: MenuConvIO.java    From BetonQuest with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void playerItemHeldEvent(PlayerItemHeldEvent event) {
    if (event.getPlayer() != player) {
        return;
    }

    if (!controls.containsKey(CONTROL.SCROLL)) {
        return;
    }

    event.setCancelled(true);

    if (debounce) {
        return;
    }

    // Cheat and assume the closest distance between previous and new slots is the direction scrolled
    int slotDistance = event.getPreviousSlot() - event.getNewSlot();

    if (slotDistance > 5 || (slotDistance < 0 && slotDistance >= -5)) {
        // Scrolled down
        if (selectedOption < options.size() - 1) {
            oldSelectedOption = selectedOption;
            selectedOption++;
            updateDisplay();
            debounce = true;
        }
    } else if (slotDistance != 0) {
        // Scrolled up
        if (selectedOption > 0) {
            oldSelectedOption = selectedOption;
            selectedOption--;
            updateDisplay();
            debounce = true;
        }
    }

}