Java Code Examples for org.bukkit.inventory.EquipmentSlot

The following examples show how to use org.bukkit.inventory.EquipmentSlot. These examples are extracted from open source projects. 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 Project: Kettle   Source File: CraftEventFactory.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static BlockPlaceEvent callBlockPlaceEvent(World world, EntityPlayer who, EnumHand hand, BlockState replacedBlockState, int clickedX, int clickedY, int clickedZ) {
    CraftWorld craftWorld = world.getWorld();
    CraftServer craftServer = world.getServer();

    Player player = (Player) who.getBukkitEntity();

    Block blockClicked = craftWorld.getBlockAt(clickedX, clickedY, clickedZ);
    Block placedBlock = replacedBlockState.getBlock();

    boolean canBuild = canBuild(craftWorld, player, placedBlock.getX(), placedBlock.getZ());

    org.bukkit.inventory.ItemStack item;
    EquipmentSlot equipmentSlot;
    if (hand == EnumHand.MAIN_HAND) {
        item = player.getInventory().getItemInMainHand();
        equipmentSlot = EquipmentSlot.HAND;
    } else {
        item = player.getInventory().getItemInOffHand();
        equipmentSlot = EquipmentSlot.OFF_HAND;
    }

    BlockPlaceEvent event = new BlockPlaceEvent(placedBlock, replacedBlockState, blockClicked, item, player, canBuild, equipmentSlot);
    craftServer.getPluginManager().callEvent(event);

    return event;
}
 
Example 2
Source Project: Minepacks   Source File: ItemShortcut.java    License: GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onItemFrameInteract(PlayerInteractEntityEvent event)
{
	Player player = event.getPlayer();
	ItemStack item;
	if(MCVersion.isDualWieldingMC())
	{
		item = (event.getHand() == EquipmentSlot.HAND) ? player.getInventory().getItemInMainHand() : player.getInventory().getItemInOffHand();
	}
	else
	{
		item = player.getItemInHand();
	}
	if(isItemShortcut(item))
	{
		event.getPlayer().performCommand(openCommand);
		event.setCancelled(true);
	}
}
 
Example 3
Source Project: NameTagChanger   Source File: ChannelPacketHandler.java    License: MIT License 6 votes vote down vote up
private Object getEnumItemSlot(EquipmentSlot slot) {
    switch (slot) {
        case HAND:
            return ReflectUtil.invokeMethod(null, ENUM_ITEM_SLOT_BY_NAME, "mainhand").getOrThrow();
        case OFF_HAND:
            return ReflectUtil.invokeMethod(null, ENUM_ITEM_SLOT_BY_NAME, "offhand").getOrThrow();
        case FEET:
            return ReflectUtil.invokeMethod(null, ENUM_ITEM_SLOT_BY_NAME, "feet").getOrThrow();
        case LEGS:
            return ReflectUtil.invokeMethod(null, ENUM_ITEM_SLOT_BY_NAME, "legs").getOrThrow();
        case CHEST:
            return ReflectUtil.invokeMethod(null, ENUM_ITEM_SLOT_BY_NAME, "chest").getOrThrow();
        case HEAD:
            return ReflectUtil.invokeMethod(null, ENUM_ITEM_SLOT_BY_NAME, "head").getOrThrow();
        default:
            logMessage(Level.SEVERE, "Unknown EquipmentSlot: " + slot, null);
            return null;
    }
}
 
Example 4
Source Project: UHC   Source File: HeadDropsModule.java    License: MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.LOW)
public void on(PlayerDeathEvent event) {
    if (!isEnabled() || RANDOM.nextDouble() < (1D - dropRate)) {
        // set to an empty map to avoid stale metadata problems
        event.getEntity().setMetadata(StandItemsMetadata.KEY, new StandItemsMetadata(plugin));
        return;
    }

    final Player player = event.getEntity();

    // create a head
    final ItemStack head = playerHeadProvider.getPlayerHeadItem(player);

    // add it to the drops
    event.getDrops().add(head);

    // add metadata for the armour stand module to put the helmet on the player and remove from drops
    final EnumMap<EquipmentSlot, ItemStack> standItems = Maps.newEnumMap(EquipmentSlot.class);
    standItems.put(EquipmentSlot.HEAD, head);

    player.setMetadata(StandItemsMetadata.KEY, new StandItemsMetadata(plugin, standItems));
}
 
Example 5
Source Project: Civs   Source File: RegionsTests.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Test
public void membersShouldBeAbleToOpenChests() {
    loadRegionTypePower(false);
    Region region = PortMenuTest.loadRegion("power");
    region.getPeople().put(TestUtil.player.getUniqueId(), "member");
    PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent(
            TestUtil.player,
            Action.RIGHT_CLICK_BLOCK,
            new ItemStack(Material.AIR),
            TestUtil.blockUnique,
            BlockFace.NORTH,
            EquipmentSlot.HAND);
    ProtectionHandler protectionHandler = new ProtectionHandler();
    protectionHandler.onBlockInteract(playerInteractEvent);
    assertSame(Event.Result.ALLOW, playerInteractEvent.useInteractedBlock());
}
 
Example 6
Source Project: AdditionsAPI   Source File: EquipmentSlotUtils.java    License: MIT License 6 votes vote down vote up
public static EquipmentSlot valueFromAttribute(String string) {
	switch(string) {
	case "mainhand":
		return EquipmentSlot.HAND;
	case "offhand":
		return EquipmentSlot.OFF_HAND;
	case "head":
		return EquipmentSlot.HEAD;
	case "chest":
		return EquipmentSlot.CHEST;
	case "feet":
		return EquipmentSlot.FEET;
	case "legs":
		return EquipmentSlot.LEGS;
	default:
		return EquipmentSlot.HAND;
	}
}
 
Example 7
Source Project: AdditionsAPI   Source File: EquipmentSlotUtils.java    License: MIT License 6 votes vote down vote up
public static String valueFromLangFile(EquipmentSlot slot) {
	switch(slot) {
	case CHEST:
		return LangFileUtils.get("when_in_body");
	case FEET:
		return LangFileUtils.get("when_in_feet");
	case HAND:
		return LangFileUtils.get("when_in_main_hand");
	case HEAD:
		return LangFileUtils.get("when_in_head");
	case LEGS:
		return LangFileUtils.get("when_in_legs");
	case OFF_HAND:
		return LangFileUtils.get("when_in_off_hand");
	default:
		return null;
	}
}
 
Example 8
Source Project: BetonQuest   Source File: QuestItemHandler.java    License: GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onItemFrameClick(final PlayerInteractEntityEvent event) {
    if (event.getPlayer().getGameMode() == GameMode.CREATIVE) {
        return;
    }
    // this prevents the journal from being placed inside of item frame
    if (event.getRightClicked() instanceof ItemFrame) {
        ItemStack item = null;
        item = (event.getHand() == EquipmentSlot.HAND) ? event.getPlayer().getInventory().getItemInMainHand()
                : event.getPlayer().getInventory().getItemInOffHand();

        final String playerID = PlayerConverter.getID(event.getPlayer());
        if (Journal.isJournal(playerID, item) || Utils.isQuestItem(item)) {
            event.setCancelled(true);
        }
    }
}
 
Example 9
Source Project: AdditionsAPI   Source File: DurabilityBar.java    License: MIT License 6 votes vote down vote up
public static void hideDurabilityBossBar(Player player, EquipmentSlot slot) {
	UUID uuid = player.getUniqueId();
	BossBar bar;
	HashMap<UUID, BossBar> playersBars;
	if (slot.equals(EquipmentSlot.HAND)) {
		playersBars = playersBarsMain;
	} else if (slot.equals(EquipmentSlot.OFF_HAND)) {
		playersBars = playersBarsOff;
	} else {
		return;
	}
	if (!playersBars.containsKey(uuid)) {
		return;
	} else {
		bar = playersBars.get(uuid);
	}

	bar.setVisible(false);
	bar.setProgress(1.0D);
	bar.setColor(BarColor.GREEN);
}
 
Example 10
Source Project: AdditionsAPI   Source File: CustomItem.java    License: MIT License 6 votes vote down vote up
/**
 * Adds an Attribute to the CustomItem. If you don't know what they do or how
 * they work, check out the Minecraft wiki. The UUID will be the one that is
 * found by default in Minecraft for the specified Slot. The name will be, as a
 * placeholder, TBD (which means To Be Determined).
 * 
 * @param type
 *            The attribute that will be added.
 * @param amount
 *            The amount of the attribute. Can also be a negative value.
 * @param slot
 *            The {@link EquipmentSlot} at which the Attribute will be applied.
 *            For example, the EquipmentSlot HAND will apply the specified
 *            Attribute to the Main Hand only.
 * @param operation
 *            The math operation that will be used for the amount specified.
 */
public CustomItem addAttribute(AttributeType type, double amount, EquipmentSlot slot, Operation operation) {
	switch (slot) {
	case HEAD:
		addAttribute(type, amount, slot, operation, HEAD_UUID);
		return this;
	case CHEST:
		addAttribute(type, amount, slot, operation, CHEST_UUID);
		return this;
	case LEGS:
		addAttribute(type, amount, slot, operation, LEGS_UUID);
		return this;
	case FEET:
		addAttribute(type, amount, slot, operation, FEET_UUID);
		return this;
	case HAND:
		addAttribute(type, amount, slot, operation, MAIN_HAND_UUID);
		return this;
	case OFF_HAND:
		addAttribute(type, amount, slot, operation, OFF_HAND_UUID);
		return this;
	}
	return this;
}
 
Example 11
@EventHandler
public void onInteractEntity(PlayerInteractAtEntityEvent e) {
    if (!e.getRightClicked().isValid()) {
        return;
    }

    ItemStack itemStack;

    if (e.getHand() == EquipmentSlot.OFF_HAND) {
        itemStack = e.getPlayer().getInventory().getItemInOffHand();
    }
    else {
        itemStack = e.getPlayer().getInventory().getItemInMainHand();
    }

    SlimefunItem sfItem = SlimefunItem.getByItem(itemStack);

    if (sfItem != null && Slimefun.hasUnlocked(e.getPlayer(), sfItem, true)) {
        sfItem.callItemHandler(EntityInteractHandler.class, handler -> handler.onInteract(e.getPlayer(), e.getRightClicked(), itemStack, e.getHand() == EquipmentSlot.OFF_HAND));
    }
}
 
Example 12
Source Project: Slimefun4   Source File: DebugFishListener.java    License: 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 13
Source Project: MineTinker   Source File: Speedy.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
	ItemMeta meta = tool.getItemMeta();

	if (meta == null) {
		return false;
	}

	//To check if armor modifiers are on the armor
	Collection<AttributeModifier> attributeModifiers = meta.getAttributeModifiers(Attribute.GENERIC_ARMOR);

	if (attributeModifiers == null || attributeModifiers.isEmpty()) {
		modManager.addArmorAttributes(tool);
		meta = tool.getItemMeta();
	}

	Collection<AttributeModifier> speedModifiers = meta.getAttributeModifiers(Attribute.GENERIC_MOVEMENT_SPEED);
	double speedOnItem = 0.0D;

	if (!(speedModifiers == null || speedModifiers.isEmpty())) {
		HashSet<String> names = new HashSet<>();

		for (AttributeModifier am : speedModifiers) {
			if (names.add(am.getName())) speedOnItem += am.getAmount();
		}
	}

	meta.removeAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED);
	meta.addAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED,
			new AttributeModifier(UUID.randomUUID(), (MineTinker.is16compatible) ? "generic.movement_speed" : "generic.movementSpeed", speedOnItem + this.speedPerLevel, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.LEGS));
	meta.addAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED,
			new AttributeModifier(UUID.randomUUID(), (MineTinker.is16compatible) ? "generic.movement_speed" : "generic.movementSpeed", speedOnItem + this.speedPerLevel, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.FEET));

	tool.setItemMeta(meta);
	return true;
}
 
Example 14
Source Project: MineTinker   Source File: Tanky.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
	ItemMeta meta = tool.getItemMeta();

	if (meta == null) {
		return false;
	}

	//To check if armor modifiers are on the armor
	Collection<AttributeModifier> attributeModifiers = meta.getAttributeModifiers(Attribute.GENERIC_ARMOR);

	if (attributeModifiers == null || attributeModifiers.isEmpty()) {
		modManager.addArmorAttributes(tool);
		meta = tool.getItemMeta();
	}

	Collection<AttributeModifier> healthModifiers = meta.getAttributeModifiers(Attribute.GENERIC_MAX_HEALTH);

	double healthOnItem = 0.0D;
	if (!(healthModifiers == null || healthModifiers.isEmpty())) {
		HashSet<String> names = new HashSet<>();
		for (AttributeModifier am : healthModifiers) {
			if (names.add(am.getName())) healthOnItem += am.getAmount();
		}
	}
	meta.removeAttributeModifier(Attribute.GENERIC_MAX_HEALTH);
	modManager.addArmorAttributes(tool);
	if (ToolType.CHESTPLATE.contains(tool.getType())) {
		meta.addAttributeModifier(Attribute.GENERIC_MAX_HEALTH, new AttributeModifier(UUID.randomUUID(), (MineTinker.is16compatible) ? "generic.max_health" : "generic.maxHealth", healthOnItem + this.healthPerLevel, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.CHEST));
	} else {
		meta.addAttributeModifier(Attribute.GENERIC_MAX_HEALTH, new AttributeModifier(UUID.randomUUID(), (MineTinker.is16compatible) ? "generic.max_health" : "generic.maxHealth", healthOnItem + this.healthPerLevel, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.LEGS));
	}

	tool.setItemMeta(meta);
	return true;
}
 
Example 15
@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 16
Source Project: Slimefun4   Source File: IronGolemListener.java    License: GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onIronGolemHeal(PlayerInteractEntityEvent e) {
    if (e.getRightClicked().getType() == EntityType.IRON_GOLEM) {
        PlayerInventory inv = e.getPlayer().getInventory();
        ItemStack item = null;

        if (e.getHand() == EquipmentSlot.HAND) {
            item = inv.getItemInMainHand();
        }
        else if (e.getHand() == EquipmentSlot.OFF_HAND) {
            item = inv.getItemInOffHand();
        }

        if (item != null && item.getType() == Material.IRON_INGOT) {
            SlimefunItem sfItem = SlimefunItem.getByItem(item);

            if (sfItem != null && !(sfItem instanceof VanillaItem)) {
                e.setCancelled(true);
                SlimefunPlugin.getLocalization().sendMessage(e.getPlayer(), "messages.no-iron-golem-heal");

                // This is just there to update the Inventory...
                // Somehow cancelling it isn't enough.
                if (e.getHand() == EquipmentSlot.HAND) {
                    inv.setItemInMainHand(item);
                }
                else if (e.getHand() == EquipmentSlot.OFF_HAND) {
                    inv.setItemInOffHand(item);
                }
            }
        }
    }
}
 
Example 17
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerClickPlayer(PlayerInteractEntityEvent event) {
	if(ConfigManager.getServerVersion().isNewerThan(ConfigManager.ServerVersion.MINECRAFT_1_8_R3) && event.getHand() == EquipmentSlot.OFF_HAND) {
		return;
	}

	Player player = event.getPlayer();
	if((event.getRightClicked() instanceof Player) && player.isSneaking()) {
		if(Permission.NOVAGUILDS_PLAYERINFO.has(player)) {
			NovaPlayer nCPlayer = PlayerManager.getPlayer(event.getRightClicked());
			plugin.getPlayerManager().sendPlayerInfo(player, nCPlayer);
		}
	}
}
 
Example 18
Source Project: Kettle   Source File: CraftEventFactory.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static PlayerInteractEvent callPlayerInteractEvent(EntityPlayer who, Action action, BlockPos position, EnumFacing direction, ItemStack itemstack, boolean cancelledBlock, EnumHand hand) {
    Player player = (who == null) ? null : (Player) who.getBukkitEntity();
    CraftItemStack itemInHand = CraftItemStack.asCraftMirror(itemstack);

    CraftWorld craftWorld = (CraftWorld) player.getWorld();
    CraftServer craftServer = (CraftServer) player.getServer();

    Block blockClicked = null;
    if (position != null) {
        blockClicked = craftWorld.getBlockAt(position.getX(), position.getY(), position.getZ());
    } else {
        switch (action) {
            case LEFT_CLICK_BLOCK:
                action = Action.LEFT_CLICK_AIR;
                break;
            case RIGHT_CLICK_BLOCK:
                action = Action.RIGHT_CLICK_AIR;
                break;
        }
    }
    BlockFace blockFace = CraftBlock.notchToBlockFace(direction);

    if (itemInHand.getType() == Material.AIR || itemInHand.getAmount() == 0) {
        itemInHand = null;
    }

    PlayerInteractEvent event = new PlayerInteractEvent(player, action, itemInHand, blockClicked, blockFace, (hand == null) ? null : ((hand == EnumHand.OFF_HAND) ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND));
    if (cancelledBlock) {
        event.setUseInteractedBlock(Event.Result.DENY);
    }
    craftServer.getPluginManager().callEvent(event);

    return event;
}
 
Example 19
Source Project: ProjectAres   Source File: XMLUtils.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public static EquipmentSlot parseEquipmentSlot(Node node) throws InvalidXMLException {
    final EquipmentSlot slot = parsePlayerSlot(node).toEquipmentSlot();
    if(slot == null) {
        throw new InvalidXMLException("Not an equipment slot", node);
    }
    return slot;
}
 
Example 20
@Test
public void testNonCargoNode() {
    Player player = server.addPlayer();
    Location l = new Location(player.getWorld(), 190, 50, 400);
    Block b = l.getBlock();
    Block against = b.getRelative(BlockFace.DOWN);

    ItemStack item = new ItemStack(Material.PLAYER_HEAD);

    BlockPlaceEvent event = new BlockPlaceEvent(b, new BlockStateMock(), against, item, player, true, EquipmentSlot.HAND);
    listener.onCargoNodePlace(event);
    Assertions.assertFalse(event.isCancelled());
}
 
Example 21
Source Project: ProjectAres   Source File: NMSHacks.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public static void useEntity(Player user, Entity target, boolean attack, EquipmentSlot hand) {
    ((CraftPlayer) user).getHandle().playerConnection.a(new PacketPlayInUseEntity(
        target.getEntityId(),
        attack ? PacketPlayInUseEntity.EnumEntityUseAction.ATTACK : PacketPlayInUseEntity.EnumEntityUseAction.INTERACT,
        null,
        hand == EquipmentSlot.OFF_HAND ? EnumHand.OFF_HAND : EnumHand.MAIN_HAND
    ));
}
 
Example 22
Source Project: RandomTeleport   Source File: SignListener.java    License: 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 23
@ParameterizedTest
@SlimefunItemsSource(items = { "CARGO_INPUT_NODE", "CARGO_OUTPUT_NODE", "CARGO_OUTPUT_NODE_2" })
public void testSidePlacement(ItemStack item) {
    Player player = server.addPlayer();
    Location l = new Location(player.getWorld(), 190, 50, 400);
    Block b = l.getBlock();
    Block against = b.getRelative(BlockFace.NORTH);

    BlockPlaceEvent event = new BlockPlaceEvent(b, new BlockStateMock(), against, item, player, true, EquipmentSlot.HAND);
    listener.onCargoNodePlace(event);
    Assertions.assertFalse(event.isCancelled());
}
 
Example 24
@EventHandler
public void onItemClick(final PlayerInteractEvent event) {
    final Player player = event.getPlayer();

    if (event.getMaterial() == Material.AIR) {
        return;
    }
    if (event.getAction().equals(Action.LEFT_CLICK_BLOCK) || event.getAction().equals(Action.LEFT_CLICK_AIR)) {
        return;
    }
    final ItemStack item = event.getItem();
    if (!ItemManager.getGive(item).isEmpty()) {
        String[] give = ItemManager.getGive(item).split(":");
        if (give.length == 2) {
            int id = Integer.parseInt(give[0]);
            int amount = Integer.parseInt(give[1]);
            if (id < 0) {
                Main.getApi().addAdvice(player.getName(), amount);
                Util.sendMessage(SettingsManager.lang.getString("materialize.adviceSucess")
                        .replace("%level%", Integer.toString(amount)), player);
            } else {
                Main.getApi().addItem(player.getName(), id, amount);
                Util.sendMessage(SettingsManager.lang.getString("materialize.success")
                        .replace("%amount%", Integer.toString(amount))
                        .replace("%item%", SettingsManager.lang.getString("item." + id)), player);
            }
            // Consume the item.
            try {
                if (event.getHand().equals(EquipmentSlot.HAND)) {
                    consumeItem(Util.getMainHand(player), player);
                } else if (event.getHand().equals(EquipmentSlot.OFF_HAND)) {
                    consumeItem(player.getInventory().getItemInOffHand(), player);
                }
            } catch (NoSuchMethodError ex) {
                consumeItem(Util.getMainHand(player), player);
            }
            event.setCancelled(true);
        }
    }
}
 
Example 25
Source Project: CardinalPGM   Source File: ObserverModule.java    License: MIT License 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
    if (testObserver(event.getPlayer())) {
        if (event.getRightClicked() instanceof Player && !event.getPlayer().isSneaking()){
            if (event.getHand().equals(EquipmentSlot.HAND)) openInventory(event.getPlayer(), (Player) event.getRightClicked(), false);
        } else if (event.getRightClicked() instanceof ItemFrame) {
            event.setCancelled(true);
        }
    }
}
 
Example 26
@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 27
Source Project: AdditionsAPI   Source File: EquipmentSlotUtils.java    License: MIT License 5 votes vote down vote up
public static String toAttributeString(EquipmentSlot slot) {
	if (slot == EquipmentSlot.HAND)
		return "mainhand";
	if (slot == EquipmentSlot.OFF_HAND)
		return "offhand";
	return slot.toString().toLowerCase();
}
 
Example 28
Source Project: AdditionsAPI   Source File: DurabilityBar.java    License: MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onHandSwitch(PlayerSwapHandItemsEvent event) {
	if (event.isCancelled())
		return;
	sendDurabilityBossBar(event.getPlayer(), event.getMainHandItem(), EquipmentSlot.HAND);
	sendDurabilityBossBar(event.getPlayer(), event.getOffHandItem(), EquipmentSlot.OFF_HAND);
}
 
Example 29
Source Project: AdditionsAPI   Source File: DurabilityBar.java    License: MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onItemDamage(PlayerItemDamageEvent 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 30
Source Project: AdditionsAPI   Source File: DurabilityBar.java    License: MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onItemBreak(PlayerItemBreakEvent event) {
	ItemStack broken = event.getBrokenItem();
	Player player = event.getPlayer();
	PlayerInventory inv = player.getInventory();
	if (broken.equals(inv.getItemInMainHand()))
		hideDurabilityBossBar(player, EquipmentSlot.HAND);
	else if (broken.equals(inv.getItemInOffHand()))
		hideDurabilityBossBar(player, EquipmentSlot.OFF_HAND);
}