Java Code Examples for org.bukkit.inventory.ItemStack#setType()

The following examples show how to use org.bukkit.inventory.ItemStack#setType() . 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: ColorChanger.java    From BedWars with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public ItemStack applyColor(org.screamingsandals.bedwars.api.TeamColor apiColor, ItemStack stack) {
    try {
        TeamColor color = TeamColor.fromApiColor(apiColor);
        Material material = stack.getType();
        if (Main.isLegacy()) {
            stack = changeLegacyStackColor(stack, color);
        } else {
            stack.setType(changeMaterialColor(material, color));
        }
        stack = changeLeatherArmorColor(stack, color);
        return stack;
    } catch (NullPointerException e) {
        Debug.warn("DEFINED ITEM DOES NOT EXISTS. CHECK YOUR CONFIG.");
        e.printStackTrace();
        return new ItemStack(Material.BLACK_WOOL);
    }
}
 
Example 2
Source File: ItemListener.java    From HubBasics with GNU Lesser General Public License v3.0 6 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 = ReflectionUtils.invokeMethod(event.getPlayer(), this.getItemInHandMethod);
    if (itemStack != null && 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;
        item.onCommand(event.getPlayer());
    }
}
 
Example 3
Source File: ItemListener.java    From HubBasics with GNU Lesser General Public License v3.0 6 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 = ReflectionUtils.invokeMethod(event.getPlayer(), this.getItemInHandMethod);
    if (itemStack != null && 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;
        item.onCommand(event.getPlayer());
    }
}
 
Example 4
Source File: ItemListener.java    From HubBasics with GNU Lesser General Public License v3.0 6 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onItemMove(InventoryClickEvent event) {
    ItemStack currentItem = event.getCurrentItem();
    if (currentItem == null || currentItem.getType() == Material.AIR) return;
    NBTItem nbtItem = new NBTItem(currentItem);
    if (!nbtItem.hasKey("HubBasics")) return;

    CustomItem item = HubBasics.getInstance().getItemManager().get(nbtItem.getString("HubBasics"));
    if (item == null) {
        currentItem.setType(Material.AIR); // Destroy old item
        return;
    }
    if (item.getRunOnInventory()) {
        item.onCommand((Player) event.getWhoClicked());
    }
    if (!item.getAllowMove())
        event.setCancelled(true); // Call setCancelled only when needed to not conflict with other plugins
}
 
Example 5
Source File: ItemListener.java    From HubBasics with GNU Lesser General Public License v3.0 6 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onItemMove(InventoryClickEvent event) {
    ItemStack currentItem = event.getCurrentItem();
    if (currentItem == null || currentItem.getType() == Material.AIR) return;
    NBTItem nbtItem = new NBTItem(currentItem);
    if (!nbtItem.hasKey("HubBasics")) return;

    CustomItem item = HubBasics.getInstance().getItemManager().get(nbtItem.getString("HubBasics"));
    if (item == null) {
        currentItem.setType(Material.AIR); // Destroy old item
        return;
    }
    if (item.getRunOnInventory()) {
        item.onCommand((Player) event.getWhoClicked());
    }
    if (!item.getAllowMove())
        event.setCancelled(true); // Call setCancelled only when needed to not conflict with other plugins
}
 
Example 6
Source File: MenuUtil.java    From Civs with GNU General Public License v3.0 6 votes vote down vote up
public static void sanitizeItem(ItemStack item) {
    Material mat = item.getType();
    if (mat == Material.RED_BED || mat == Material.BLACK_BED || mat == Material.BLUE_BED
            || mat == Material.BROWN_BED || mat == Material.CYAN_BED
            || mat == Material.GRAY_BED || mat == Material.GREEN_BED || mat == Material.LIGHT_BLUE_BED
            || mat == Material.LIGHT_GRAY_BED || mat == Material.LIME_BED || mat == Material.MAGENTA_BED
            || mat == Material.ORANGE_BED || mat == Material.PINK_BED || mat == Material.PURPLE_BED
            || mat == Material.WHITE_BED || mat == Material.YELLOW_BED) {
        divideByTwo(item);
    } else if (mat == Material.OAK_DOOR || mat == Material.IRON_DOOR || mat == Material.DARK_OAK_DOOR
            || mat == Material.BIRCH_DOOR || mat == Material.ACACIA_DOOR || mat == Material.SPRUCE_DOOR
            || mat == Material.JUNGLE_DOOR) {
        divideByTwo(item);
    } else if (mat == Material.REDSTONE_WIRE) {
        item.setType(Material.REDSTONE);
    } else if (mat == Material.WATER) {
        item.setType(Material.WATER_BUCKET);
    } else if (mat == Material.LAVA) {
        item.setType(Material.LAVA_BUCKET);
    } else if (mat == Material.POTATOES) {
        item.setType(Material.POTATO);
    } else if (mat == Material.CARROTS) {
        item.setType(Material.CARROT);
    }
}
 
Example 7
Source File: GuiUtils.java    From Guilds with MIT License 6 votes vote down vote up
public static ItemStack createItem(String material, String name, List<String> lore) {
    Optional<XMaterial> tempMaterial = XMaterial.matchXMaterial(material);
    XMaterial tempCheck = tempMaterial.orElse(XMaterial.GLASS_PANE);
    ItemStack item = tempCheck.parseItem();
    if (item == null) {
        item = XMaterial.GLASS_PANE.parseItem();
    }
    // Extra check
    if (problemItems.contains(item.getType())) {
        LoggingUtils.warn("Problematic Material Type Found! Switching to Barrier.");
        item.setType(XMaterial.BARRIER.parseMaterial());
    }
    ItemBuilder builder = new ItemBuilder(item);
    builder.setName(StringUtils.color(name));
    if (!lore.isEmpty()) {
        builder.setLore(lore.stream().map(StringUtils ::color).collect(Collectors.toList()));
    }
    builder.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
    return builder.build();
}
 
Example 8
Source File: QualityArmory.java    From QualityArmory with GNU General Public License v3.0 6 votes vote down vote up
public static boolean removeAmmoFromInventory(Player player, Ammo a, int amount) {
	int remaining = amount;
	if(player.getGameMode()==GameMode.CREATIVE)
		return true;
	for (int i = 0; i < player.getInventory().getSize(); i++) {
		ItemStack is = player.getInventory().getItem(i);
		if (is != null && QualityArmory.isAmmo(is)&&QualityArmory.getAmmo(is).equals(a)) {
			int temp = is.getAmount();
			if (remaining < is.getAmount()) {
				is.setAmount(is.getAmount() - remaining);
			} else {
				is.setType(Material.AIR);
			}
			remaining -= temp;
			player.getInventory().setItem(i, is);
			if (remaining <= 0)
				break;

		}
	}
	return remaining <= 0;
}
 
Example 9
Source File: ColorChanger.java    From BedWars with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public ItemStack applyColor(org.screamingsandals.bedwars.api.TeamColor apiColor, ItemStack stack) {
    try {
        TeamColor color = TeamColor.fromApiColor(apiColor);
        Material material = stack.getType();
        if (Main.isLegacy()) {
            stack = changeLegacyStackColor(stack, color);
        } else {
            stack.setType(changeMaterialColor(material, color));
        }
        stack = changeLeatherArmorColor(stack, color);
        return stack;
    } catch (NullPointerException e) {
        Debug.warn("DEFINED ITEM DOES NOT EXISTS. CHECK YOUR CONFIG.");
        e.printStackTrace();
        return new ItemStack(Material.BLACK_WOOL);
    }
}
 
Example 10
Source File: InventoryForVillagerOffers.java    From NBTEditor with GNU General Public License v3.0 5 votes vote down vote up
public void updateBottleButton() {
	ItemStack item = getItem(43);
	ItemMeta meta = item.getItemMeta();
	if (_rewardExp[_selected]) {
		item.setType(Material.EXPERIENCE_BOTTLE);
		meta.setDisplayName("XP reward is ON.");
	} else {
		item.setType(Material.GLASS_BOTTLE);
		meta.setDisplayName("XP reward is OFF.");
	}
	item.setItemMeta(meta);
}
 
Example 11
Source File: SentinelItemHelper.java    From Sentinel with MIT License 5 votes vote down vote up
/**
 * Processes weapon redirection for an item, returning the redirected item (or an unchanged one).
 */
public ItemStack autoRedirect(ItemStack stack) {
    if (stack == null) {
        return null;
    }
    String redirect = sentinel.weaponRedirects.get(stack.getType().name().toLowerCase());
    if (redirect == null) {
        return stack;
    }
    Material mat = Material.valueOf(redirect.toUpperCase());
    ItemStack newStack = stack.clone();
    newStack.setType(mat);
    return newStack;
}
 
Example 12
Source File: PotionVariable.java    From NBTEditor with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setItem(ItemStack item) {
	if (item != null) {
		Material type = item.getType();
		if (type != Material.SPLASH_POTION && type != Material.LINGERING_POTION) {
			item.setType(Material.SPLASH_POTION);
		}
	}
	super.setItem(item);
}
 
Example 13
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 14
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 15
Source File: SmeltersPickaxe.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
private void smelt(Block b, ItemStack drop, int fortune) {
    Optional<ItemStack> furnaceOutput = SlimefunPlugin.getMinecraftRecipeService().getFurnaceOutput(drop);

    if (furnaceOutput.isPresent()) {
        b.getWorld().playEffect(b.getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
        drop.setType(furnaceOutput.get().getType());
        drop.setAmount(fortune);
    }
}
 
Example 16
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 onItemDrop(PlayerDropItemEvent event) {
    ItemStack currentItem = event.getItemDrop().getItemStack();
    if (currentItem.getType() == Material.AIR) return;
    NBTItem nbtItem = new NBTItem(currentItem);
    if (!nbtItem.hasKey("HubBasics")) return;

    CustomItem item = HubBasics.getInstance().getItemManager().get(nbtItem.getString("HubBasics"));
    if (item == null) {
        currentItem.setType(Material.AIR); // Destroy old item
        return;
    }
    if (!item.getAllowDrop())
        event.setCancelled(true); // Call setCancelled only when needed to not conflict with other plugins
}
 
Example 17
Source File: ItemShortcut.java    From Minepacks with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Removes the backpack item form the drops on death
 */
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onDeath(PlayerDeathEvent event)
{
	//region prevent drop
	Iterator<ItemStack> itemStackIterator = event.getDrops().iterator();
	while(itemStackIterator.hasNext())
	{
		if(isItemShortcut(itemStackIterator.next()))
		{
			itemStackIterator.remove();
			break;
		}
	}
	//endregion
	if(improveDeathChestCompatibility)
	{ // improveDeathChestCompatibility
		for(ItemStack itemStack : event.getEntity().getInventory())
		{
			if(isItemShortcut(itemStack))
			{
				itemStack.setAmount(0);
				itemStack.setType(Material.AIR);
			}
		}
	}
}
 
Example 18
Source File: CustomItem.java    From NBTEditor with GNU General Public License v3.0 5 votes vote down vote up
protected CustomItem(String slug, String name, Material material) {
	_slug = slug;
	_name = name;
	_material = material;
	_item = new ItemStack(material, 1);
	_item = customizeItem(_item);
	_item.setType(material);
	_item.setAmount(1);
	setDefaultConfig("enabled", true);
	setDefaultConfig("name", _name);
	setDefaultConfig("lore", new ArrayList<String>());
	setDefaultConfig("allowed-worlds", new ArrayList<String>());
	setDefaultConfig("blocked-worlds", new ArrayList<String>());
}
 
Example 19
Source File: ColorChanger.java    From BedWars with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static ItemStack changeLegacyStackColor(ItemStack itemStack, TeamColor teamColor) {
    Material material = itemStack.getType();
    String materialName = material.name();

    if (Main.autoColoredMaterials.contains(materialName)) {
        itemStack.setDurability((short) teamColor.woolData);
    } else if (material.toString().contains("GLASS")) {
        itemStack.setType(Material.getMaterial("STAINED_GLASS"));
        itemStack.setDurability((short) teamColor.woolData);
    } else if (material.toString().contains("GLASS_PANE")) {
        itemStack.setType(Material.getMaterial("STAINED_GLASS_PANE"));
        itemStack.setDurability((short) teamColor.woolData);
    }
    return itemStack;
}
 
Example 20
Source File: PlayerInteractListener.java    From IridiumSkyblock with GNU General Public License v2.0 4 votes vote down vote up
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
    try {
        final Player player = event.getPlayer();
        final Location playerLocation = player.getLocation();
        final IslandManager islandManager = IridiumSkyblock.getIslandManager();
        if (!islandManager.isIslandWorld(playerLocation)) return;

        final User user = User.getUser(player);
        final Block block = event.getClickedBlock();

        if (event.getAction().toString().startsWith("RIGHT_CLICK")) {
            if (player.getItemInHand() != null) {
                int crystals = Utils.getCrystals(player.getItemInHand()) * player.getItemInHand().getAmount();
                if (crystals != 0) {
                    player.setItemInHand(null);
                    user.getIsland().setCrystals(user.getIsland().getCrystals() + crystals);
                    player.sendMessage(Utils.color(IridiumSkyblock.getMessages().depositedCrystals.replace("%amount%", crystals + "").replace("%prefix%", IridiumSkyblock.getConfiguration().prefix)));
                }
            }
        }

        if (block != null) {
            final Location location = block.getLocation();
            final Island island = islandManager.getIslandViaLocation(location);
            if (island != null) {
                if (!island.getPermissions(user).interact) {
                    event.setCancelled(true);
                    return;
                }
                final ItemStack itemInHand = player.getItemInHand();
                if (itemInHand.getType().equals(Material.BUCKET) && island.failedGenerators.remove(location)) {
                    if (itemInHand.getAmount() == 1)
                        itemInHand.setType(Material.LAVA_BUCKET);
                    else {
                        player.getInventory().addItem(new ItemStack(Material.LAVA_BUCKET));
                        player.getItemInHand().setAmount(itemInHand.getAmount() - 1);
                    }
                    block.setType(Material.AIR);
                }
            } else if (!user.bypassing) {
                event.setCancelled(true);
                return;
            }
        }

        final ItemStack item = event.getItem();
        if (IridiumSkyblock.getConfiguration().allowWaterInNether
                && event.getAction().equals(Action.RIGHT_CLICK_BLOCK)
                && item != null
                && block != null) {
            final World world = block.getWorld();
            if (!world.getEnvironment().equals(World.Environment.NETHER)) return;
            if (!item.getType().equals(Material.WATER_BUCKET)) return;

            event.setCancelled(true);

            final BlockFace face = event.getBlockFace();
            block.getRelative(face).setType(Material.WATER);

            final Block relative = block.getRelative(face);
            final BlockPlaceEvent blockPlaceEvent = new BlockPlaceEvent(relative, relative.getState(), block, item, player, false);
            if (blockPlaceEvent.isCancelled()) {
                block.getRelative(face).setType(Material.AIR);
            } else if (player.getGameMode().equals(GameMode.SURVIVAL)) {
                if (item.getAmount() == 1) {
                    item.setType(Material.BUCKET);
                } else {
                    item.setAmount(item.getAmount() - 1);
                    player.getInventory().addItem(new ItemStack(Material.BUCKET));
                }
            }
        }
    } catch (Exception e) {
        IridiumSkyblock.getInstance().sendErrorMessage(e);
    }
}