Java Code Examples for org.bukkit.inventory.EquipmentSlot#OFF_HAND

The following examples show how to use org.bukkit.inventory.EquipmentSlot#OFF_HAND . 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: CraftEventFactory.java    From Kettle with 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 File: PlayerInteractEntityListener.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
@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 3
Source File: EquipmentSlotUtils.java    From AdditionsAPI with 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 4
Source File: ListenerMenuBindItem.java    From TrMenu with MIT License 6 votes vote down vote up
@EventHandler
public void onInteract(PlayerInteractEvent e) {
    ItemStack item = e.getItem();

    try {
        if (item == null || Materials.getVersion() != Materials.MinecraftVersion.V1_8) {
            if (e.getHand() == EquipmentSlot.OFF_HAND) {
                return;
            }
        }
    } catch (Throwable ignored) {
    }

    TrMenuAPI.getMenus().stream().filter(menu -> menu.getBindItemLore() != null && menu.getBindItemLore().stream().anyMatch(lore -> Items.hasLore(item, lore))).findFirst().ifPresent(menu -> {
        menu.open(e.getPlayer());
        e.setCancelled(true);
    });
}
 
Example 5
Source File: PlayerInfoListener.java    From NovaGuilds with GNU General Public License v3.0 5 votes vote down vote up
@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 6
Source File: Parser.java    From CardinalPGM with MIT License 5 votes vote down vote up
public static EquipmentSlot getEquipmentSlot(String slotName) {
    if (!slotName.startsWith("slot.")) slotName = "slot." + slotName;
    EquipmentSlot equipmentSlot = null;
    String[] path = slotName.split("\\.");
    if (path.length != 3) return null;
    if (path[1].equalsIgnoreCase("armor")) {
        equipmentSlot = EquipmentSlot.valueOf(Strings.getTechnicalName(path[2]));
    } else if (path[1].equalsIgnoreCase("weapon")) {
        if (path[2].equalsIgnoreCase("mainhand")) equipmentSlot = EquipmentSlot.HAND;
        if (path[2].equalsIgnoreCase("offhand")) equipmentSlot = EquipmentSlot.OFF_HAND;
    }
    return equipmentSlot;
}
 
Example 7
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 8
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 9
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 10
Source File: IronGolemListener.java    From Slimefun4 with 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 11
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 12
Source File: EquipmentSlotUtils.java    From AdditionsAPI with 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 13
Source File: CraftEventFactory.java    From Kettle with 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 14
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 15
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 16
Source File: InteractEvent.java    From StackMob-3 with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler
public void onInteract(PlayerInteractEntityEvent event) {
    Entity entity = event.getRightClicked();
    if(!(StackTools.hasValidData(entity))){
        return;
    }
    if(event.getHand() == EquipmentSlot.OFF_HAND){
        return;
    }
    if(event.isCancelled()){
        return;
    }

    int stackSize = StackTools.getSize(entity);
    if(entity instanceof Animals){
        Animals animals = (Animals) entity;
        if(sm.getCompat().checkFood(entity, event.getPlayer().getInventory().getItemInMainHand()) && animals.canBreed()){
            if(StackTools.hasSizeMoreThanOne(entity)) {
                if (sm.getCustomConfig().getBoolean("multiply.breed")) {
                    int breedSize = stackSize;
                    int handSize = event.getPlayer().getInventory().getItemInMainHand().getAmount();
                    if (handSize < breedSize) {
                        breedSize = event.getPlayer().getInventory().getItemInMainHand().getAmount();
                        event.getPlayer().getInventory().setItemInMainHand(null);
                    }

                    int childAmount = breedSize / 2;
                    Animals child = (Animals) sm.getTools().duplicate(entity);
                    StackTools.setSize(child, childAmount);
                    child.setBaby();

                    event.getPlayer().getInventory().getItemInMainHand().setAmount(handSize - breedSize);
                    animals.setBreed(false);
                } else if (sm.getCustomConfig().getBoolean("divide-on.breed")) {
                    Entity newEntity = sm.getTools().duplicate(entity);
                    StackTools.setSize(newEntity,stackSize - 1);
                    StackTools.makeSingle(entity);
                }
                return;
            }
        }
    }
    if(sm.getCustomConfig().getBoolean("divide-on.name")) {
        ItemStack handItem = event.getPlayer().getInventory().getItemInMainHand();
        if (handItem.getType() == Material.NAME_TAG && handItem.getItemMeta().hasDisplayName()) {
            if (stackSize > 1) {
                Entity dupe = sm.getTools().duplicate(entity);
                StackTools.setSize(dupe,stackSize - 1);
            }
            StackTools.removeSize(entity);
        }
    }
}
 
Example 17
Source File: Slot.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
protected OffHand() {
    super("weapon.offhand", 40, EquipmentSlot.OFF_HAND);
}
 
Example 18
Source File: BlockListener.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onClick(PlayerInteractEvent event) {
	Player player = event.getPlayer();

	ItemStack norm = null;
	if (event.getHand() == EquipmentSlot.HAND) {
		norm = player.getInventory().getItemInMainHand();
	} else if (event.getHand() == EquipmentSlot.OFF_HAND) {
		norm = player.getInventory().getItemInOffHand();
	}

	if (norm == null) return;

	if (event.getAction() == Action.RIGHT_CLICK_AIR) {
		if (modManager.isModifierItem(norm)) {
			event.setCancelled(true);
		}
	} else if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
		Block block = event.getClickedBlock();

		if (block == null) {
			return;
		}
		if (!player.isSneaking()) {
			Material type = block.getType();

			if (type == Material.ANVIL || type == Material.CRAFTING_TABLE
					|| type == Material.CHEST || type == Material.ENDER_CHEST
					|| type == Material.DROPPER || type == Material.HOPPER
					|| type == Material.DISPENSER || type == Material.TRAPPED_CHEST
					|| type == Material.FURNACE || type == Material.ENCHANTING_TABLE) {

				return;
			}
		}

		if (modManager.isModifierItem(norm)) {
			event.setCancelled(true);
			return;
		}

		if (block.getType() == Material.getMaterial(MineTinker.getPlugin().getConfig().getString("BlockToEnchantModifiers", Material.BOOKSHELF.name()))) {
			ItemStack item = player.getInventory().getItemInMainHand();

			for (Modifier m : modManager.getAllMods()) {
				if (m.getModItem().getType().equals(item.getType())) {
					if (!m.isEnchantable()) continue;
					m.enchantItem(player);
					event.setCancelled(true);
					break;
				}
			}
		}
	}
}