Java Code Examples for org.bukkit.event.player.PlayerInteractEvent#getHand()

The following examples show how to use org.bukkit.event.player.PlayerInteractEvent#getHand() . 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: ClickListener.java    From AnimatedFrames with MIT License 6 votes vote down vote up
@EventHandler
public void on(PlayerInteractEvent event) {
	if (Minecraft.VERSION.newerThan(Minecraft.Version.v1_9_R1)) {
		if (event.getHand() != EquipmentSlot.HAND) { return; }
	}

	int actionId = 0;
	switch (event.getAction()) {
		case RIGHT_CLICK_AIR:
		case RIGHT_CLICK_BLOCK:
			actionId = 0;
			break;
		case LEFT_CLICK_AIR:
		case LEFT_CLICK_BLOCK:
			actionId = 1;
			break;
	}
	handleInteract(event.getPlayer(), event, actionId);
}
 
Example 2
Source File: HuntEffect.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
    if (event.getClickedBlock() == null || event.getHand() == null) {
        return;
    }

    if (!event.getAction().equals(Action.RIGHT_CLICK_BLOCK) ||
            event.getHand().equals(EquipmentSlot.HAND)) {
        return;
    }

    Region r = RegionManager.getInstance().getRegionAt(event.getClickedBlock().getLocation());

    if (r == null || !r.getEffects().containsKey(KEY) ||
            !Util.equivalentLocations(event.getClickedBlock().getLocation(), r.getLocation())) {
        return;
    }

    Player player = event.getPlayer();
    RegionType regionType = (RegionType) ItemManager.getInstance().getItemType(r.getType());
    Player targetPlayer = hasValidSign(r.getLocation(), regionType, player.getUniqueId());
    if (targetPlayer == null) {
        return;
    }

    if (!regionType.getUpkeeps().isEmpty() && !r.runUpkeep(false)) {
        player.sendMessage(Civs.getPrefix() + LocaleManager.getInstance().getTranslationWithPlaceholders(player,
                "region-missing-upkeep-items"));
        return;
    }

    Location location = targetPlayer.getLocation();
    int radius = 200;

    Location teleportTarget = findNearbyLocationForTeleport(location, radius, player);
    if (teleportTarget != null) {
        player.teleport(teleportTarget);
        messageNearbyPlayers(player, "hunting-players", null);
    }
}
 
Example 3
Source File: PlayerInteract.java    From AdditionsAPI with MIT License 5 votes vote down vote up
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
	Player player = event.getPlayer();
	PlayerInventory inv = player.getInventory();
	ItemStack item = inv.getItemInMainHand();
	if (event.getHand() != null)
		switch (event.getHand()) {
		case OFF_HAND:
			item = inv.getItemInOffHand();
			break;
		default:
			break;
		}

	if (AdditionsAPI.isCustomItem(item)) {
		CustomItemPlayerInteractEvent customEvent = new CustomItemPlayerInteractEvent(event,
				new CustomItemStack(item));
		Bukkit.getServer().getPluginManager().callEvent(customEvent);
	}
	if (event.getAction() != null && event.getAction() == Action.RIGHT_CLICK_BLOCK
			&& ToolType.getToolType(item.getType()) != null
			&& ToolType.getToolType(item.getType()).equals(ToolType.HOE)) {
		Block block = event.getClickedBlock();
		Material material = block.getType();
		Location blockLoc = block.getLocation();
		blockLoc.setY(blockLoc.getBlockY() + 1);
		Material materialUp = blockLoc.getBlock().getType();
		@SuppressWarnings("deprecation")
		byte data = block.getData();

		BlockFace face = event.getBlockFace();
		if (materialUp == Material.AIR && (face == BlockFace.UP || face == BlockFace.EAST || face == BlockFace.NORTH
				|| face == BlockFace.SOUTH || face == BlockFace.WEST))
			if (shouldPlaySound(material, item, data, player))
				player.playSound(block.getLocation(), "additionsapi.hoe.till", 1.0F, 1.0F);
	}
}
 
Example 4
Source File: ItemListener.java    From HubBasics with GNU Lesser General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onRightClick(PlayerInteractEvent event) {
    if (!(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
        return;
    }
    ItemStack itemStack;
    if (event.getHand() == EquipmentSlot.HAND) {
        itemStack = event.getPlayer().getInventory().getItemInMainHand();
    } else if (event.getHand() == EquipmentSlot.OFF_HAND) {
        itemStack = event.getPlayer().getInventory().getItemInOffHand();
    } else {
        return;
    }
    if (itemStack.getType() != Material.AIR) {
        NBTItem nbtItem = new NBTItem(itemStack);
        if (!nbtItem.hasKey("HubBasics")) return;
        event.setCancelled(true);
        CustomItem item = HubBasics.getInstance().getItemManager().get(nbtItem.getString("HubBasics"));
        if (item == null) {
            itemStack.setType(Material.AIR); // Destroy old item
            return;
        }
        if (!item.getRunOnRightClick()) return;
        if (event.getHand() == EquipmentSlot.OFF_HAND) {
            if (item.getRunOnOffhand()) {
                item.onCommand(event.getPlayer());
            }
        } else {
            item.onCommand(event.getPlayer());
        }
    }
}
 
Example 5
Source File: ItemListener.java    From HubBasics with GNU Lesser General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onLeftClick(PlayerInteractEvent event) {
    if (!(event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK)) {
        return;
    }
    ItemStack itemStack;
    if (event.getHand() == EquipmentSlot.HAND) {
        itemStack = event.getPlayer().getInventory().getItemInMainHand();
    } else if (event.getHand() == EquipmentSlot.OFF_HAND) {
        itemStack = event.getPlayer().getInventory().getItemInOffHand();
    } else {
        return;
    }
    if (itemStack.getType() != Material.AIR) {
        NBTItem nbtItem = new NBTItem(itemStack);
        if (!nbtItem.hasKey("HubBasics")) return;
        event.setCancelled(true);
        CustomItem item = HubBasics.getInstance().getItemManager().get(nbtItem.getString("HubBasics"));
        if (item == null) {
            itemStack.setType(Material.AIR); // Destroy old item
            return;
        }
        if (!item.getRunOnLeftClick()) return;
        if (event.getHand() == EquipmentSlot.OFF_HAND) {
            if (item.getRunOnOffhand()) {
                item.onCommand(event.getPlayer());
            }
        } else {
            item.onCommand(event.getPlayer());
        }
    }
}
 
Example 6
Source File: SignListener.java    From RandomTeleport with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onSignClick(PlayerInteractEvent event) {
    if (event.getHand() == EquipmentSlot.HAND && event.getAction() == Action.RIGHT_CLICK_BLOCK
            && event.getClickedBlock() != null && event.getClickedBlock().getType().name().contains("SIGN")) {
        Sign sign = (Sign) event.getClickedBlock().getState();
        if (plugin.matchesSignVariable(sign.getLine(1))) {
            String preset = sign.getLine(2).toLowerCase();
            if (event.getPlayer().hasPermission("randomteleport.sign.preset." + preset)) {
                if (plugin.getConfig().getString("presets." + preset) == null) {
                    plugin.sendMessage(event.getPlayer(), "error.preset-doesnt-exist", "preset", preset);
                } else {
                    for (RandomSearcher searcher : plugin.getRunningSearchers().values()) {
                        if (searcher.getTargets().contains(event.getPlayer())) {
                            plugin.sendMessage(event.getPlayer(), "error.already-searching", "preset", preset);
                            return;
                        }
                    }

                    try {
                        plugin.runPreset(plugin.getServer().getConsoleSender(), preset, event.getPlayer(), event.getClickedBlock().getLocation());
                    } catch (IllegalArgumentException e) {
                        plugin.sendMessage(event.getPlayer(), "error.preset-invalid", "preset", preset);
                        plugin.getLogger().log(Level.SEVERE, "Error while parsing preset " + preset, e);
                    }
                }
            } else {
                plugin.sendMessage(event.getPlayer(), "sign.no-permission.use",
                        "preset", preset,
                        "perm", "randomteleport.sign.use"
                );
            }
        }
    }
}
 
Example 7
Source File: DWorldListener.java    From DungeonsXL with GNU General Public License v3.0 5 votes vote down vote up
private ItemStack getItemInHand(PlayerInteractEvent event) {
    PlayerInventory inventory = event.getPlayer().getInventory();
    if (Version.isAtLeast(Version.MC1_9)) {
        return event.getHand() == EquipmentSlot.HAND ? inventory.getItemInMainHand() : inventory.getItemInOffHand();
    } else {
        return inventory.getItemInHand();
    }
}
 
Example 8
Source File: StepObjective.java    From BetonQuest with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onStep(PlayerInteractEvent event) {
    // Only fire the event for the main hand to avoid that the event is triggered two times.
    if (event.getHand() == EquipmentSlot.OFF_HAND && event.getHand() != null) {
        return; // off hand packet, ignore.
    }
    if (event.getAction() != Action.PHYSICAL) {
        return;
    }
    if (event.getClickedBlock() == null) {
        return;
    }
    try {
        String playerID = PlayerConverter.getID(event.getPlayer());
        Block block = loc.getLocation(playerID).getBlock();
        if (!event.getClickedBlock().equals(block)) {
            return;
        }

        if (!new BlockSelector("*_PRESSURE_PLATE").match(block)) {
            return;
        }
        if (!containsPlayer(playerID)) {
            return;
        }
        // player stepped on the pressure plate
        if (checkConditions(playerID))
            completeObjective(playerID);
    } catch (QuestRuntimeException e) {
        LogUtils.getLogger().log(Level.WARNING, "Error while handling '" + instruction.getID() + "' objective: " + e.getMessage());
        LogUtils.logThrowable(e);
    }
}
 
Example 9
Source File: DebugFishListener.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onDebug(PlayerInteractEvent e) {
    if (e.getAction() == Action.PHYSICAL || e.getHand() != EquipmentSlot.HAND) {
        return;
    }

    Player p = e.getPlayer();

    if (p.isOp() && SlimefunUtils.isItemSimilar(e.getItem(), SlimefunItems.DEBUG_FISH, true)) {
        e.setCancelled(true);

        if (e.getAction() == Action.LEFT_CLICK_BLOCK) {
            if (p.isSneaking()) {
                if (BlockStorage.hasBlockInfo(e.getClickedBlock())) {
                    BlockStorage.clearBlockInfo(e.getClickedBlock());
                }
            }
            else {
                e.setCancelled(false);
            }
        }
        else if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if (p.isSneaking()) {
                Block b = e.getClickedBlock().getRelative(e.getBlockFace());
                b.setType(Material.PLAYER_HEAD);
                SkullBlock.setFromHash(b, HeadTexture.MISSING_TEXTURE.getTexture());
            }
            else if (BlockStorage.hasBlockInfo(e.getClickedBlock())) {
                sendInfo(p, e.getClickedBlock());
            }
        }
    }
}
 
Example 10
Source File: PlayerRightClickEvent.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
public PlayerRightClickEvent(PlayerInteractEvent e) {
    event = e;
    player = e.getPlayer();
    clickedBlock = Optional.ofNullable(e.getClickedBlock());
    face = e.getBlockFace();
    hand = e.getHand();

    if (e.getItem() == null || e.getItem().getType() == Material.AIR || e.getItem().getAmount() == 0) {
        itemStack = Optional.empty();
    }
    else {
        itemStack = Optional.of(e.getItem());
    }
}
 
Example 11
Source File: SlimefunItemListener.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onRightClick(PlayerInteractEvent e) {
    if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
        if (SlimefunUtils.isItemSimilar(e.getItem(), SlimefunItems.DEBUG_FISH, true)) {
            return;
        }

        PlayerRightClickEvent event = new PlayerRightClickEvent(e);
        Bukkit.getPluginManager().callEvent(event);

        boolean itemUsed = e.getHand() == EquipmentSlot.OFF_HAND;

        if (event.useItem() != Result.DENY) {
            rightClickItem(e, event, itemUsed);
        }

        if (!itemUsed && event.useBlock() != Result.DENY && !rightClickBlock(e, event)) {
            return;
        }

        if (e.useInteractedBlock() != Result.DENY) {
            e.setUseInteractedBlock(event.useBlock());
        }

        if (e.useItemInHand() != Result.DENY) {
            e.setUseItemInHand(event.useItem());
        }
    }
}
 
Example 12
Source File: NMSHandler.java    From Shopkeepers with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean isMainHandInteraction(PlayerInteractEvent event) {
	return event.getHand() == EquipmentSlot.HAND;
}
 
Example 13
Source File: NMSHandler.java    From Shopkeepers with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean isMainHandInteraction(PlayerInteractEvent event) {
	return event.getHand() == EquipmentSlot.HAND;
}
 
Example 14
Source File: NMSHandler.java    From Shopkeepers with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean isMainHandInteraction(PlayerInteractEvent event) {
	return event.getHand() == EquipmentSlot.HAND;
}
 
Example 15
Source File: ActionObjective.java    From BetonQuest with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onInteract(PlayerInteractEvent event) {
    // Only fire the event for the main hand to avoid that the event is triggered two times.
    if (event.getHand() == EquipmentSlot.OFF_HAND && event.getHand() != null) {
        return; // off hand packet, ignore.
    }
    String playerID = PlayerConverter.getID(event.getPlayer());
    if (!containsPlayer(playerID)) {
        return;
    }
    if (selector == null) {
        switch (action) {
            case RIGHT:
                if ((event.getAction().equals(Action.RIGHT_CLICK_AIR)
                        || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) && checkConditions(playerID)) {
                    if (cancel)
                        event.setCancelled(true);
                    completeObjective(playerID);
                }
                break;
            case LEFT:
                if ((event.getAction().equals(Action.LEFT_CLICK_AIR)
                        || event.getAction().equals(Action.LEFT_CLICK_BLOCK)) && checkConditions(playerID)) {
                    if (cancel)
                        event.setCancelled(true);
                    completeObjective(playerID);
                }
                break;
            case ANY:
            default:
                if ((event.getAction().equals(Action.LEFT_CLICK_AIR)
                        || event.getAction().equals(Action.LEFT_CLICK_BLOCK)
                        || event.getAction().equals(Action.RIGHT_CLICK_AIR)
                        || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) && checkConditions(playerID)) {
                    if (cancel)
                        event.setCancelled(true);
                    completeObjective(playerID);
                }
                break;
        }
    } else {
        Action actionEnum;
        switch (action) {
            case RIGHT:
                actionEnum = Action.RIGHT_CLICK_BLOCK;
                break;
            case LEFT:
                actionEnum = Action.LEFT_CLICK_BLOCK;
                break;
            case ANY:
            default:
                actionEnum = null;
                break;
        }
        try {
            if (((actionEnum == null && (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)
                    || event.getAction().equals(Action.LEFT_CLICK_BLOCK))) || event.getAction().equals(actionEnum))
                    && (event.getClickedBlock() != null && (((selector.match(Material.FIRE) || selector.match(Material.LAVA) || selector.match(Material.WATER))
                    && selector.match(event.getClickedBlock().getRelative(event.getBlockFace())))
                    || selector.match(event.getClickedBlock())))) {
                if (loc != null) {
                    Location location = loc.getLocation(playerID);
                    double r = range.getDouble(playerID);
                    if (!event.getClickedBlock().getWorld().equals(location.getWorld())
                            || event.getClickedBlock().getLocation().distance(location) > r) {
                        return;
                    }
                }
                if (checkConditions(playerID)) {
                    if (cancel) {
                        event.setCancelled(true);
                    }
                    completeObjective(playerID);
                }
            }
        } catch (QuestRuntimeException e) {
            LogUtils.getLogger().log(Level.WARNING, "Error while handling '" + instruction.getID() + "' objective: " + e.getMessage());
            LogUtils.logThrowable(e);
        }
    }
}
 
Example 16
Source File: BukkitImageListener.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerInteract(PlayerInteractEvent event) {
    if (event.useItemInHand() == Event.Result.DENY) return;

    Player player = event.getPlayer();
    FawePlayer<Object> fp = FawePlayer.wrap(player);
    if (fp.getMeta("CFISettings") == null) return;
    try {
        if (event.getHand() == EquipmentSlot.OFF_HAND) return;
    } catch (NoSuchFieldError | NoSuchMethodError ignored) {}

    List<Block> target = player.getLastTwoTargetBlocks((Set<Material>) null, 100);
    if (target.isEmpty()) return;

    Block targetBlock = target.get(0);
    World world = player.getWorld();
    mutable.setWorld(world);
    mutable.setX(targetBlock.getX() + 0.5);
    mutable.setY(targetBlock.getY() + 0.5);
    mutable.setZ(targetBlock.getZ() + 0.5);
    Collection<Entity> entities = world.getNearbyEntities(mutable, 0.46875, 0, 0.46875);

    if (!entities.isEmpty()) {
        Action action = event.getAction();
        boolean primary = action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK;

        double minDist = Integer.MAX_VALUE;
        ItemFrame minItemFrame = null;

        for (Entity entity : entities) {
            if (entity instanceof ItemFrame) {
                ItemFrame itemFrame = (ItemFrame) entity;
                Location loc = itemFrame.getLocation();
                double dx = loc.getX() - mutable.getX();
                double dy = loc.getY() - mutable.getY();
                double dz = loc.getZ() - mutable.getZ();
                double dist = dx * dx + dy * dy + dz * dz;
                if (dist < minDist) {
                    minItemFrame = itemFrame;
                    minDist = dist;
                }
            }
        }
        if (minItemFrame != null) {
            handleInteract(event, minItemFrame, primary);
            if (event.isCancelled()) return;
        }
    }
}
 
Example 17
Source File: InteractListener.java    From SaneEconomy with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler
public void onPlayerInteract(PlayerInteractEvent evt) {
    if (!evt.getPlayer().hasPermission("saneeconomy.signshop.use")) {
        return;
    }

    if (evt.getHand() != EquipmentSlot.HAND) {
        return;
    }

    if ((evt.getPlayer().getInventory().getItemInMainHand() != null) && (evt.getPlayer().getInventory().getItemInMainHand().getType() == Material.DIAMOND_AXE)) {
        return;
    }

    if ((evt.getAction() != Action.RIGHT_CLICK_BLOCK) && (evt.getAction() != Action.LEFT_CLICK_BLOCK)) {
        return;
    }

    Optional<SignShop> shopOptional = this.plugin.getSignShopManager().getSignShop(evt.getClickedBlock().getLocation());

    if (!shopOptional.isPresent()) {
        return;
    }

    SignShop shop = shopOptional.get();

    // Buy
    if (evt.getAction() == Action.RIGHT_CLICK_BLOCK) {
        evt.setCancelled(true);
        if (!shop.canBuy()) {
            this.plugin.getMessenger().sendMessage(evt.getPlayer(), "This shop does not permit buying.");
            return;
        }

        this.doBuy(shop, evt.getPlayer());
    }

    // Sell
    if (evt.getAction() == Action.LEFT_CLICK_BLOCK) {
        evt.setCancelled(true);
        if (!shop.canSell()) {
            this.plugin.getMessenger().sendMessage(evt.getPlayer(), "This shop does not permit selling.");
            return;
        }

        this.doSell(shop, evt.getPlayer());
    }
}
 
Example 18
Source File: NMSHandler.java    From Shopkeepers with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean isMainHandInteraction(PlayerInteractEvent event) {
	return event.getHand() == EquipmentSlot.HAND;
}
 
Example 19
Source File: RepairEffect.java    From Civs with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
    if (event.getClickedBlock() == null || event.getHand() == null) {
        return;
    }

    if ((!event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && !event.getAction().equals(Action.LEFT_CLICK_BLOCK)) ||
            (!event.getClickedBlock().getType().equals(Material.IRON_BLOCK)) ||
            event.getHand().equals(EquipmentSlot.HAND)) {
        return;
    }

    Region r = RegionManager.getInstance().getRegionAt(event.getClickedBlock().getLocation());

    if (r == null || !r.getEffects().containsKey(KEY)) {
        return;
    }

    Player player = event.getPlayer();

    ItemStack item = player.getInventory().getItemInMainHand();
    ItemMeta itemMeta = item.getItemMeta();
    if (!(itemMeta instanceof Damageable)) {
        return;
    }
    Damageable damageable = (Damageable) itemMeta;

    if (getRequiredReagent(item.getType()).isEmpty()) {
        return;
    }
    if (item.getType() == Material.AIR) {
        player.sendMessage(Civs.getPrefix() +
                LocaleManager.getInstance().getTranslationWithPlaceholders(player, "hold-repair-item"));
        return;
    }

    if (!damageable.hasDamage()) {
        return;
    }
    repairItem(event, player, item);
}
 
Example 20
Source File: NMSHandler.java    From Shopkeepers with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean isMainHandInteraction(PlayerInteractEvent event) {
	return event.getHand() == EquipmentSlot.HAND;
}