Advertisement
iSach

ItemStack util.

Nov 22nd, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.53 KB | None | 0 0
  1. package be.isach.hubmanager.util;
  2.  
  3. import net.minecraft.server.v1_8_R3.NBTTagCompound;
  4. import net.minecraft.server.v1_8_R3.NBTTagList;
  5. import org.bukkit.Material;
  6. import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
  7. import org.bukkit.enchantments.Enchantment;
  8. import org.bukkit.inventory.ItemStack;
  9. import org.bukkit.inventory.meta.ItemMeta;
  10. import org.bukkit.material.MaterialData;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.stream.Collectors;
  17.  
  18. /**
  19.  * Allows easy creation of ItemStacks.
  20.  * <p>
  21.  * Created by Sacha on 22/11/15.
  22.  */
  23. public class CustomItemStack {
  24.  
  25.     /**
  26.      * The material of the item.
  27.      */
  28.     private Material material;
  29.  
  30.     /**
  31.      * The data of the item.
  32.      */
  33.     private byte data;
  34.  
  35.     /**
  36.      * The displayName, if null there won't be any.
  37.      */
  38.     private String displayName;
  39.  
  40.     /**
  41.      * The lore of the item, it's the description.
  42.      */
  43.     private List<String> lore;
  44.  
  45.     /**
  46.      * If true, a NBT Tag will be added to the item so it's glowing.
  47.      */
  48.     private boolean glowing;
  49.  
  50.     /**
  51.      * The enchantments of the item.
  52.      */
  53.     private Map<Enchantment, Integer> enchantments;
  54.  
  55.     /**
  56.      * The amount of the item, default to 1.
  57.      */
  58.     private int amount = 1;
  59.  
  60.     /**
  61.      * The durabilty of the item, default to -1.
  62.      */
  63.     private short durability = -1;
  64.  
  65.     /**
  66.      * Creates a CustomItemStack with a Material and a data.
  67.      *
  68.      * @param material The material of the item.
  69.      * @param data     The data of the item.
  70.      */
  71.     public CustomItemStack(Material material, byte data) {
  72.         this.material = material;
  73.         this.data = data;
  74.         this.lore = new ArrayList<>();
  75.         this.glowing = false;
  76.         this.enchantments = new HashMap<>();
  77.     }
  78.  
  79.     /**
  80.      * Creates a CustomItemStack with a Material and a data of 0.
  81.      *
  82.      * @param material The material of the item.
  83.      */
  84.     public CustomItemStack(Material material) {
  85.         this(material, (byte) 0);
  86.     }
  87.  
  88.     /**
  89.      * Creates a CustomItemStack with a Material-ID and a data of 0.
  90.      *
  91.      * @param id The id of the item.
  92.      */
  93.     public CustomItemStack(int id) {
  94.         this(Material.getMaterial(id), (byte) 0);
  95.     }
  96.  
  97.     /**
  98.      * Creates a CustomItemStack with a Material-ID and a data.
  99.      *
  100.      * @param id   The id of the item.
  101.      * @param data The data of the item.
  102.      */
  103.     public CustomItemStack(int id, byte data) {
  104.         this(Material.getMaterial(id), data);
  105.     }
  106.  
  107.     /**
  108.      * Sets the display name of the Item.
  109.      *
  110.      * @param displayName The new display name.
  111.      */
  112.     public CustomItemStack withDisplayName(String displayName) {
  113.         this.displayName = displayName.replace('&', '§');
  114.         return this;
  115.     }
  116.  
  117.     /**
  118.      * Sets the amount of items.
  119.      *
  120.      * @param amount The new amount.
  121.      */
  122.     public CustomItemStack withAmount(int amount) {
  123.         this.amount = amount;
  124.         return this;
  125.     }
  126.  
  127.     /**
  128.      * Sets the durability.
  129.      *
  130.      * @param durability The new durability.
  131.      */
  132.     public CustomItemStack withDurability(short durability) {
  133.         this.durability = durability;
  134.         return this;
  135.     }
  136.  
  137.     /**
  138.      * Sets the lore to a new list. & are changed into §.
  139.      *
  140.      * @param newLore The new lore list.
  141.      */
  142.     public CustomItemStack withLore(List<String> newLore) {
  143.         List<String> newLoreClone = newLore.stream()
  144.                 .map(s -> s.replace('&', '§')).collect(Collectors.toList());
  145.         return this;
  146.     }
  147.  
  148.     /**
  149.      * Adds new lines to lore. & are changed into §.
  150.      *
  151.      * @param desc The lines.
  152.      */
  153.     public CustomItemStack addToLore(String... desc) {
  154.         if (lore != null)
  155.             for (String s : desc)
  156.                 lore.add(s.replace('&', '§'));
  157.         return this;
  158.     }
  159.  
  160.     /**
  161.      * Sets the enchantment to a new map.
  162.      *
  163.      * @param enchantments The map of enchantments.
  164.      */
  165.     public CustomItemStack withEnchantments(Map<Enchantment, Integer> enchantments) {
  166.         this.enchantments = enchantments;
  167.         return this;
  168.     }
  169.  
  170.     /**
  171.      * Adds an enchantment to the enchantments map of the items.
  172.      *
  173.      * @param ench  The enchantment.
  174.      * @param level The level.
  175.      */
  176.     public CustomItemStack addToEnchants(Enchantment ench, int level) {
  177.         if (enchantments != null)
  178.             enchantments.put(ench, level);
  179.         return this;
  180.     }
  181.  
  182.     /**
  183.      * Sets if the item should glow, or not.
  184.      *
  185.      * @param glowing if {@code true}, the item will glow, otherwise it won't.
  186.      */
  187.     public CustomItemStack setGlowing(boolean glowing) {
  188.         this.glowing = glowing;
  189.         return this;
  190.     }
  191.  
  192.     /**
  193.      * Private method modifying Item NBT Tags to add a glowing effect.
  194.      *
  195.      * @param item The ItemStack which will receive the effect.
  196.      * @return The itemStack with the effect.
  197.      */
  198.     private ItemStack addGlowEffect(ItemStack item) {
  199.         net.minecraft.server.v1_8_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(item);
  200.         NBTTagCompound tag = null;
  201.         if (!nmsStack.hasTag()) {
  202.             tag = new NBTTagCompound();
  203.             nmsStack.setTag(tag);
  204.         }
  205.         if (tag == null) tag = nmsStack.getTag();
  206.         NBTTagList ench = new NBTTagList();
  207.         tag.set("ench", ench);
  208.         nmsStack.setTag(tag);
  209.         return CraftItemStack.asCraftMirror(nmsStack);
  210.     }
  211.  
  212.     /**
  213.      * Builds the ItemStack with the current things set.
  214.      *
  215.      * @return The itemStack with all the things set.
  216.      */
  217.     public ItemStack build() {
  218.         ItemStack itemStack = new MaterialData(material, data).toItemStack(amount);
  219.         final ItemStack FINAL_STACK = glowing ? addGlowEffect(itemStack) : itemStack;
  220.         if (durability != -1)
  221.             FINAL_STACK.setDurability((short) Math.min(durability, material.getMaxDurability()));
  222.         enchantments.keySet().stream().forEach(enchantment ->
  223.                 FINAL_STACK.addUnsafeEnchantment(enchantment, enchantments.get(enchantment)));
  224.         ItemMeta itemMeta = FINAL_STACK.getItemMeta();
  225.         if (displayName != null && !displayName.equals(""))
  226.             itemMeta.setDisplayName(displayName);
  227.         if (lore != null && !lore.isEmpty())
  228.             itemMeta.setLore(lore);
  229.         FINAL_STACK.setItemMeta(itemMeta);
  230.         return glowing ? addGlowEffect(FINAL_STACK) : FINAL_STACK;
  231.     }
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement