Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package be.isach.hubmanager.util;
- import net.minecraft.server.v1_8_R3.NBTTagCompound;
- import net.minecraft.server.v1_8_R3.NBTTagList;
- import org.bukkit.Material;
- import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
- import org.bukkit.enchantments.Enchantment;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.material.MaterialData;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- /**
- * Allows easy creation of ItemStacks.
- * <p>
- * Created by Sacha on 22/11/15.
- */
- public class CustomItemStack {
- /**
- * The material of the item.
- */
- private Material material;
- /**
- * The data of the item.
- */
- private byte data;
- /**
- * The displayName, if null there won't be any.
- */
- private String displayName;
- /**
- * The lore of the item, it's the description.
- */
- private List<String> lore;
- /**
- * If true, a NBT Tag will be added to the item so it's glowing.
- */
- private boolean glowing;
- /**
- * The enchantments of the item.
- */
- private Map<Enchantment, Integer> enchantments;
- /**
- * The amount of the item, default to 1.
- */
- private int amount = 1;
- /**
- * The durabilty of the item, default to -1.
- */
- private short durability = -1;
- /**
- * Creates a CustomItemStack with a Material and a data.
- *
- * @param material The material of the item.
- * @param data The data of the item.
- */
- public CustomItemStack(Material material, byte data) {
- this.material = material;
- this.data = data;
- this.lore = new ArrayList<>();
- this.glowing = false;
- this.enchantments = new HashMap<>();
- }
- /**
- * Creates a CustomItemStack with a Material and a data of 0.
- *
- * @param material The material of the item.
- */
- public CustomItemStack(Material material) {
- this(material, (byte) 0);
- }
- /**
- * Creates a CustomItemStack with a Material-ID and a data of 0.
- *
- * @param id The id of the item.
- */
- public CustomItemStack(int id) {
- this(Material.getMaterial(id), (byte) 0);
- }
- /**
- * Creates a CustomItemStack with a Material-ID and a data.
- *
- * @param id The id of the item.
- * @param data The data of the item.
- */
- public CustomItemStack(int id, byte data) {
- this(Material.getMaterial(id), data);
- }
- /**
- * Sets the display name of the Item.
- *
- * @param displayName The new display name.
- */
- public CustomItemStack withDisplayName(String displayName) {
- this.displayName = displayName.replace('&', '§');
- return this;
- }
- /**
- * Sets the amount of items.
- *
- * @param amount The new amount.
- */
- public CustomItemStack withAmount(int amount) {
- this.amount = amount;
- return this;
- }
- /**
- * Sets the durability.
- *
- * @param durability The new durability.
- */
- public CustomItemStack withDurability(short durability) {
- this.durability = durability;
- return this;
- }
- /**
- * Sets the lore to a new list. & are changed into §.
- *
- * @param newLore The new lore list.
- */
- public CustomItemStack withLore(List<String> newLore) {
- List<String> newLoreClone = newLore.stream()
- .map(s -> s.replace('&', '§')).collect(Collectors.toList());
- return this;
- }
- /**
- * Adds new lines to lore. & are changed into §.
- *
- * @param desc The lines.
- */
- public CustomItemStack addToLore(String... desc) {
- if (lore != null)
- for (String s : desc)
- lore.add(s.replace('&', '§'));
- return this;
- }
- /**
- * Sets the enchantment to a new map.
- *
- * @param enchantments The map of enchantments.
- */
- public CustomItemStack withEnchantments(Map<Enchantment, Integer> enchantments) {
- this.enchantments = enchantments;
- return this;
- }
- /**
- * Adds an enchantment to the enchantments map of the items.
- *
- * @param ench The enchantment.
- * @param level The level.
- */
- public CustomItemStack addToEnchants(Enchantment ench, int level) {
- if (enchantments != null)
- enchantments.put(ench, level);
- return this;
- }
- /**
- * Sets if the item should glow, or not.
- *
- * @param glowing if {@code true}, the item will glow, otherwise it won't.
- */
- public CustomItemStack setGlowing(boolean glowing) {
- this.glowing = glowing;
- return this;
- }
- /**
- * Private method modifying Item NBT Tags to add a glowing effect.
- *
- * @param item The ItemStack which will receive the effect.
- * @return The itemStack with the effect.
- */
- private ItemStack addGlowEffect(ItemStack item) {
- net.minecraft.server.v1_8_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
- NBTTagCompound tag = null;
- if (!nmsStack.hasTag()) {
- tag = new NBTTagCompound();
- nmsStack.setTag(tag);
- }
- if (tag == null) tag = nmsStack.getTag();
- NBTTagList ench = new NBTTagList();
- tag.set("ench", ench);
- nmsStack.setTag(tag);
- return CraftItemStack.asCraftMirror(nmsStack);
- }
- /**
- * Builds the ItemStack with the current things set.
- *
- * @return The itemStack with all the things set.
- */
- public ItemStack build() {
- ItemStack itemStack = new MaterialData(material, data).toItemStack(amount);
- final ItemStack FINAL_STACK = glowing ? addGlowEffect(itemStack) : itemStack;
- if (durability != -1)
- FINAL_STACK.setDurability((short) Math.min(durability, material.getMaxDurability()));
- enchantments.keySet().stream().forEach(enchantment ->
- FINAL_STACK.addUnsafeEnchantment(enchantment, enchantments.get(enchantment)));
- ItemMeta itemMeta = FINAL_STACK.getItemMeta();
- if (displayName != null && !displayName.equals(""))
- itemMeta.setDisplayName(displayName);
- if (lore != null && !lore.isEmpty())
- itemMeta.setLore(lore);
- FINAL_STACK.setItemMeta(itemMeta);
- return glowing ? addGlowEffect(FINAL_STACK) : FINAL_STACK;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement