Advertisement
Green_Panda

ItemAPI

Jul 12th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.79 KB | None | 0 0
  1. package <YourPackage>;
  2.  
  3. import com.sun.istack.internal.NotNull;
  4. import org.bukkit.*;
  5. import org.bukkit.Color;
  6. import org.bukkit.attribute.Attribute;
  7. import org.bukkit.attribute.AttributeModifier;
  8. import org.bukkit.block.banner.Pattern;
  9. import org.bukkit.enchantments.Enchantment;
  10. import org.bukkit.entity.EntityType;
  11. import org.bukkit.entity.TropicalFish;
  12. import org.bukkit.inventory.ItemFlag;
  13. import org.bukkit.inventory.ItemStack;
  14. import org.bukkit.inventory.meta.*;
  15. import org.bukkit.inventory.meta.BookMeta.Generation;
  16. import org.bukkit.map.MapView;
  17. import org.bukkit.material.MaterialData;
  18. import org.bukkit.potion.PotionData;
  19. import org.bukkit.potion.PotionEffect;
  20. import org.bukkit.potion.PotionEffectType;
  21.  
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Objects;
  25.  
  26. /**
  27.  * Created by Green_Panda
  28.  * Class create at 12.07.2020 15:36
  29.  *
  30.  * Terms Of Use:
  31.  *  - You mustn't use this class in profitable projects.
  32.  *    This forbids the usage in projects that are for example aimed to be used on a Server that earns any sort of money and ones that are directly sold to any market.
  33.  *  - You mustn't claim it as your own work or change the author.
  34.  *    This includes classes that are based off of this code. Those have to include a credit to the original author.
  35.  *
  36.  * Feedback:
  37.  *  - If you want to give feedback or add a special function to this API for everyone to use, then please write a comment.
  38.  *
  39.  *  Support:
  40.  *  - If you need support, go to my discord server: https://discord.com/invite/WMFDPbh
  41.  */
  42.  
  43. public class ItemAPI {
  44.  
  45.     private ItemStack item;
  46.     private ItemMeta itemMeta;
  47.  
  48.     public ItemAPI(Material material) {
  49.         new ItemAPI(material, (byte) 0);
  50.     }
  51.     public ItemAPI(Material material, byte subid) {
  52.         this.item = new ItemStack(material, subid);
  53.         this.itemMeta = item.getItemMeta();
  54.     }
  55.     public ItemAPI(ItemStack item) {
  56.         this.item = item;
  57.         this.itemMeta = item.getItemMeta();
  58.     }
  59.  
  60.     // -------------------------------- ItemMeta -------------------------------- \\
  61.     public ItemAPI setAmount(@NotNull int amount) {
  62.         item.setAmount(amount);
  63.         return this;
  64.     }
  65.     @Deprecated public ItemAPI setData(@NotNull MaterialData data) {
  66.         item.setData(data);
  67.         return this;
  68.     }
  69.     public ItemAPI setType(@NotNull Material type) {
  70.         item.setType(type);
  71.         return this;
  72.     }
  73.     public ItemAPI setDisplayName(@NotNull String name) {
  74.         itemMeta.setDisplayName(name);
  75.         return this;
  76.     }
  77.     public ItemAPI setCustomModelData(@NotNull int data) {
  78.         itemMeta.setCustomModelData(data);
  79.         return this;
  80.     }
  81.     public ItemAPI setLocalizedName(@NotNull String name) {
  82.         itemMeta.setLocalizedName(name);
  83.         return this;
  84.     }
  85.     public ItemAPI setUnbreakable(@NotNull boolean unbreakable) {
  86.         itemMeta.setUnbreakable(unbreakable);
  87.         return this;
  88.     }
  89.     public ItemAPI addLoreLine(@NotNull String line) {
  90.         Objects.requireNonNull(itemMeta.getLore()).add(line);
  91.         return this;
  92.     }
  93.     public ItemAPI addLoreLines(@NotNull String...lines) {
  94.         for (String line : lines) {
  95.             addLoreLine(line);
  96.         }
  97.         return this;
  98.     }
  99.     public ItemAPI addEnchantment(@NotNull Enchantment enchantment, Integer lvl, @NotNull boolean b) {
  100.         itemMeta.addEnchant(enchantment, lvl, b);
  101.         return this;
  102.     }
  103.     public ItemAPI addEnchantment(@NotNull Enchantment enchantment, Integer lvl) {
  104.         addEnchantment(enchantment, lvl, false);
  105.         return this;
  106.     }
  107.     public ItemAPI addEnchantments(@NotNull Map<Enchantment, Integer> enchants) {
  108.         enchants.forEach(this::addEnchantment);
  109.         return this;
  110.     }
  111.     public ItemAPI addItemFlags(@NotNull ItemFlag...flag) {
  112.         itemMeta.addItemFlags(flag);
  113.         return this;
  114.     }
  115.     public ItemAPI addAtributeModifier(@NotNull Attribute attribute, @NotNull AttributeModifier modefier) {
  116.         itemMeta.addAttributeModifier(attribute, modefier);
  117.         return this;
  118.     }
  119.     // -------------------------------- ItemMeta -------------------------------- \\
  120.  
  121.     // -------------------------------- BookMeta -------------------------------- \\
  122.     public ItemAPI addBookPage(@NotNull String...strings) {
  123.         ((BookMeta) itemMeta).addPage(strings);
  124.         return this;
  125.     }
  126.     public ItemAPI setBookAuthor(@NotNull String author) {
  127.         ((BookMeta) itemMeta).setAuthor(author);
  128.         return this;
  129.     }
  130.     public ItemAPI setBookGeneration(@NotNull Generation generation) {
  131.         ((BookMeta) itemMeta).setGeneration(generation);
  132.         return this;
  133.     }
  134.     public ItemAPI setBookPage(int i, @NotNull String page) {
  135.         ((BookMeta) itemMeta).setPage(i, page);
  136.         return this;
  137.     }
  138.     public ItemAPI setBookPages(@NotNull String...pages) {
  139.         ((BookMeta) itemMeta).setPages(pages);
  140.         return this;
  141.     }
  142.     public ItemAPI setBookPages(@NotNull List<String> pages) {
  143.         ((BookMeta) itemMeta).setPages(pages);
  144.         return this;
  145.     }
  146.     public ItemAPI setBookTitle(@NotNull String title) {
  147.         ((BookMeta) itemMeta).setTitle(title);
  148.         return this;
  149.     }
  150.     public ItemAPI removeBookPage(int i) {
  151.         List<String> pages = ((BookMeta) itemMeta).getPages();
  152.         pages.remove(i);
  153.         setBookPages(pages);
  154.         return this;
  155.     }
  156.     public ItemAPI removeBookPages(@NotNull int...indexes) {
  157.         List<String> pages = ((BookMeta) itemMeta).getPages();
  158.         for (int i : indexes) {
  159.             pages.remove(i);
  160.         }
  161.         setBookPages(pages);
  162.         return this;
  163.     }
  164.     // -------------------------------- BookMeta -------------------------------- \\
  165.  
  166.     // --------------------------- KnowledgeBookMeta ---------------------------- \\
  167.     public ItemAPI setKnowledgeBookRecipes(@NotNull List<NamespacedKey> recipes) {
  168.         ((KnowledgeBookMeta) itemMeta).setRecipes(recipes);
  169.         return this;
  170.     }
  171.     public ItemAPI addKnowledgeBookRecipe(@NotNull NamespacedKey recipe) {
  172.         List<NamespacedKey> recipes = ((KnowledgeBookMeta) itemMeta).getRecipes();
  173.         recipes.add(recipe);
  174.         ((KnowledgeBookMeta) itemMeta).setRecipes(recipes);
  175.         return this;
  176.     }
  177.     public ItemAPI addKnowledgeBookRecipes(@NotNull NamespacedKey...recipes) {
  178.         for (NamespacedKey recipe : recipes) {
  179.             addKnowledgeBookRecipe(recipe);
  180.         }
  181.         return this;
  182.     }
  183.     public ItemAPI removeKnowledgeBookRecipe(@NotNull NamespacedKey recipe) {
  184.         List<NamespacedKey> recipes = ((KnowledgeBookMeta) itemMeta).getRecipes();
  185.         recipes.remove(recipe);
  186.         ((KnowledgeBookMeta) itemMeta).setRecipes(recipes);
  187.         return this;
  188.     }
  189.     public ItemAPI removeKnowledgeBookRecipes(@NotNull NamespacedKey...recipes) {
  190.         for (NamespacedKey recipe : recipes) {
  191.             addKnowledgeBookRecipe(recipe);
  192.         }
  193.         return this;
  194.     }
  195.     // --------------------------- KnowledgeBookMeta ---------------------------- \\
  196.  
  197.     // ------------------------------- PotionMeta ------------------------------- \\
  198.     public ItemAPI setPotionColor(@NotNull Color color) {
  199.         ((PotionMeta) itemMeta).setColor(color);
  200.         return this;
  201.     }
  202.     public ItemAPI setBasePotionData(@NotNull PotionData data) {
  203.         ((PotionMeta) itemMeta).setBasePotionData(data);
  204.         return this;
  205.     }
  206.     public ItemAPI addCustomPotionEffect(@NotNull PotionEffect effect, @NotNull boolean b) {
  207.         ((PotionMeta) itemMeta).addCustomEffect(effect, b);
  208.         return this;
  209.     }
  210.     public ItemAPI addCustomPotionEffects(@NotNull Map<PotionEffect, Boolean> effects) {
  211.         effects.forEach(this::addCustomPotionEffect);
  212.         return this;
  213.     }
  214.     public ItemAPI removeCustomPotionEffect(@NotNull PotionEffectType effect) {
  215.         ((PotionMeta) itemMeta).removeCustomEffect(effect);
  216.         return this;
  217.     }
  218.     // ------------------------------- PotionMeta ------------------------------- \\
  219.  
  220.     // ------------------------- EnchantmentStorageMeta ------------------------- \\
  221.     public ItemAPI addStoredEnchantment(@NotNull Enchantment ench, int lvl, @NotNull boolean ignore) {
  222.         ((EnchantmentStorageMeta) itemMeta).addStoredEnchant(ench, lvl, ignore);
  223.         return this;
  224.     }
  225.     public ItemAPI addStoredEnchantment(@NotNull Map<Enchantment, Integer> enchants) {
  226.         enchants.forEach((ench, lvl) -> {
  227.             ((EnchantmentStorageMeta) itemMeta).addStoredEnchant(ench, lvl, false);
  228.         });
  229.         return this;
  230.     }
  231.     public ItemAPI removeStoredEnchantment(@NotNull Enchantment ench) {
  232.         ((EnchantmentStorageMeta) itemMeta).removeStoredEnchant(ench);
  233.         return this;
  234.     }
  235.     // ------------------------- EnchantmentStorageMeta ------------------------- \\
  236.  
  237.     // ------------------------------ SpawnEggMeta ------------------------------ \\
  238.     @Deprecated public ItemAPI setSpawnEggType(@NotNull EntityType type) {
  239.         ((SpawnEggMeta) itemMeta).setSpawnedType(type);
  240.         return this;
  241.     }
  242.     // ------------------------------ SpawnEggMeta ------------------------------ \\
  243.  
  244.     // ------------------------------ FireworkMeta ------------------------------ \\
  245.     public ItemAPI addFireworkEffect(@NotNull FireworkEffect effect) {
  246.         ((FireworkMeta) itemMeta).addEffect(effect);
  247.         return this;
  248.     }
  249.     public ItemAPI addFireWorkEffects(@NotNull FireworkEffect...effects) {
  250.         for (FireworkEffect effect : effects) {
  251.             addFireworkEffect(effect);
  252.         }
  253.         return this;
  254.     }
  255.     public ItemAPI removeFireworkEffect(int i) {
  256.         ((FireworkMeta) itemMeta).removeEffect(i);
  257.         return this;
  258.     }
  259.     public ItemAPI removeFireworkEffects(@NotNull int...indexes) {
  260.         for (int i : indexes) {
  261.             removeFireworkEffect(i);
  262.         }
  263.         return this;
  264.     }
  265.     public ItemAPI setPower(int power) {
  266.         ((FireworkMeta) itemMeta).setPower(power);
  267.         return this;
  268.     }
  269.     // ------------------------------ FireworkMeta ------------------------------ \\
  270.  
  271.     // ------------------------------- BannerMeta ------------------------------- \\
  272.     public ItemAPI addBannerPattern(@NotNull Pattern pattern) {
  273.         ((BannerMeta) itemMeta).addPattern(pattern);
  274.         return this;
  275.     }
  276.     public ItemAPI addBannerPatterns(@NotNull Pattern...patterns) {
  277.         for (Pattern pattern : patterns) {
  278.             addBannerPattern(pattern);
  279.         }
  280.         return this;
  281.     }
  282.     public ItemAPI removeBannerPattern(int i) {
  283.         ((BannerMeta) itemMeta).removePattern(i);
  284.         return this;
  285.     }
  286.     public ItemAPI removeBannerPatterns(@NotNull int...indexes) {
  287.         for (int i : indexes) {
  288.             removeBannerPattern(i);
  289.         }
  290.         return this;
  291.     }
  292.     public ItemAPI setBannerPattern(int i, @NotNull Pattern pattern) {
  293.         ((BannerMeta) itemMeta).setPattern(i, pattern);
  294.         return this;
  295.     }
  296.     public ItemAPI setBannerPatterns(@NotNull List<Pattern> patterns) {
  297.         ((BannerMeta) itemMeta).setPatterns(patterns);
  298.         return this;
  299.     }
  300.     // ------------------------------- BannerMeta ------------------------------- \\
  301.  
  302.     // ---------------------------- LeatherArmorMeta ---------------------------- \\
  303.     public ItemAPI setLeatherColor(@NotNull Color color) {
  304.         ((LeatherArmorMeta) itemMeta).setColor(color);
  305.         return this;
  306.     }
  307.     // ---------------------------- LeatherArmorMeta ---------------------------- \\
  308.  
  309.     // ------------------------------ CrossbowMeta ------------------------------ \\
  310.     public ItemAPI setChargedCrossbowProjectiles(@NotNull List<ItemStack> projectiles) {
  311.         ((CrossbowMeta) itemMeta).setChargedProjectiles(projectiles);
  312.         return this;
  313.     }
  314.     public ItemAPI addChargedCrossbowProjectile(@NotNull ItemStack projectile) {
  315.         ((CrossbowMeta) itemMeta).addChargedProjectile(projectile);
  316.         return this;
  317.     }
  318.     public ItemAPI addChargedCrossbowProjectiles(@NotNull ItemStack...projectiles) {
  319.         for (ItemStack projectile : projectiles) {
  320.             addChargedCrossbowProjectile(projectile);
  321.         }
  322.         return this;
  323.     }
  324.     // ------------------------------ CrossbowMeta ------------------------------ \\
  325.  
  326.     // -------------------------------- SkullMeta ------------------------------- \\
  327.     @Deprecated public ItemAPI setSkullOwner(@NotNull String owner) {
  328.         ((SkullMeta) itemMeta).setOwner(owner);
  329.         return this;
  330.     }
  331.     public ItemAPI setSkullOwningPlayer(@NotNull OfflinePlayer player) {
  332.         ((SkullMeta) itemMeta).setOwningPlayer(player);
  333.         return this;
  334.     }
  335.     // -------------------------------- SkullMeta ------------------------------- \\
  336.  
  337.     // --------------------------- SuspiciousStewMeta --------------------------- \\
  338.     public ItemAPI setCustomStewEffect(@NotNull Map<PotionEffect, Boolean> effects) {
  339.         ((SuspiciousStewMeta) itemMeta).getCustomEffects().clear();
  340.         addCustomPotionEffects(effects);
  341.         return this;
  342.     }
  343.     public ItemAPI addCustomStewEffect(@NotNull PotionEffect effect, @NotNull boolean overwrite) {
  344.         ((SuspiciousStewMeta) itemMeta).addCustomEffect(effect, overwrite);
  345.         return this;
  346.     }
  347.     public ItemAPI addCustomStewEffects(@NotNull Map<PotionEffect, Boolean> effects) {
  348.         effects.forEach(this::addCustomStewEffect);
  349.         return this;
  350.     }
  351.     public ItemAPI removeCustomStewEffect(@NotNull PotionEffectType type) {
  352.         ((SuspiciousStewMeta) itemMeta).removeCustomEffect(type);
  353.         return this;
  354.     }
  355.     public ItemAPI removeCustomStewEffects(@NotNull PotionEffectType...types) {
  356.         for (PotionEffectType type : types) {
  357.             removeCustomStewEffect(type);
  358.         }
  359.         return this;
  360.     }
  361.     // --------------------------- SuspiciousStewMeta --------------------------- \\
  362.  
  363.     // ------------------------- TropicalFishBucketMeta ------------------------- \\
  364.     public ItemAPI setTropicalFishBodyColor(@NotNull DyeColor color) {
  365.         ((TropicalFishBucketMeta) itemMeta).setBodyColor(color);
  366.         return this;
  367.     }
  368.     public ItemAPI setTropicalFishPattern(@NotNull TropicalFish.Pattern pattern) {
  369.         ((TropicalFishBucketMeta) itemMeta).setPattern(pattern);
  370.         return this;
  371.     }
  372.     public ItemAPI setTropicalFishPatternColor(@NotNull DyeColor color) {
  373.         ((TropicalFishBucketMeta) itemMeta).setPatternColor(color);
  374.         return this;
  375.     }
  376.     // ------------------------- TropicalFishBucketMeta ------------------------- \\
  377.  
  378.     // --------------------------------- MapMeta -------------------------------- \\
  379.     public ItemAPI setMapColor(@NotNull Color color) {
  380.         ((MapMeta) itemMeta).setColor(color);
  381.         return this;
  382.     }
  383.     public ItemAPI setMapLocationName(@NotNull String name) {
  384.         ((MapMeta) itemMeta).setLocationName(name);
  385.         return this;
  386.     }
  387.     @Deprecated public ItemAPI setMapId(int id) {
  388.         ((MapMeta) itemMeta).setMapId(id);
  389.         return this;
  390.     }
  391.     public ItemAPI setMapView(@NotNull MapView view) {
  392.         ((MapMeta) itemMeta).setMapView(view);
  393.         return this;
  394.     }
  395.     public ItemAPI setMapScaling(@NotNull boolean value) {
  396.         ((MapMeta) itemMeta).setScaling(value);
  397.         return this;
  398.     }
  399.     // --------------------------------- MapMeta -------------------------------- \\
  400.  
  401.     // ---------------------------- FireworkEffectMeta -------------------------- \\
  402.     public ItemAPI setFireworkEffect(@NotNull FireworkEffect effect) {
  403.         ((FireworkEffectMeta) itemMeta).setEffect(effect);
  404.         return this;
  405.     }
  406.     // ---------------------------- FireworkEffectMeta -------------------------- \\
  407.  
  408.     public ItemStack build() {
  409.         item.setItemMeta(itemMeta);
  410.         return item;
  411.     }
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement