NullValkyrie

Minecraft 1.19 multipurpose plugin for spigot servers with a lot of features where most modern servers have.
git clone https://codeberg.org/night0721/NullValkyrie
Log | Files | Refs | README | LICENSE

CustomItemManager.java (6590B)


      1 package me.night.nullvalkyrie.entities.items;
      2 
      3 import me.night.nullvalkyrie.NullValkyrie;
      4 import me.night.nullvalkyrie.database.CustomWeaponsDataManager;
      5 import me.night.nullvalkyrie.entities.miners.Rarity;
      6 import me.night.nullvalkyrie.util.Util;
      7 import org.bukkit.Bukkit;
      8 import org.bukkit.ChatColor;
      9 import org.bukkit.Material;
     10 import org.bukkit.NamespacedKey;
     11 import org.bukkit.attribute.Attribute;
     12 import org.bukkit.attribute.AttributeModifier;
     13 import org.bukkit.enchantments.Enchantment;
     14 import org.bukkit.inventory.*;
     15 import org.bukkit.inventory.meta.ItemMeta;
     16 import org.bukkit.persistence.PersistentDataContainer;
     17 import org.bukkit.persistence.PersistentDataType;
     18 
     19 import java.util.*;
     20 
     21 public class CustomItemManager {
     22     public static final HashMap<String, NamespacedKey> keys = new HashMap<>();
     23 
     24     @SuppressWarnings("unchecked")
     25     public static ItemStack produceItem(String itemName) {
     26         HashMap<String, Object> weapon = new CustomWeaponsDataManager().getWeapon(itemName);
     27         ItemStack item = new ItemStack((Material) weapon.get("Material"));
     28         List<String> propertiesList = new ArrayList<>();
     29         List<String> itemAbility = new ArrayList<>();
     30         HashMap<String, Object> enchants = (HashMap<String, Object>) weapon.get("Enchants");
     31         HashMap<String, Object> attributes = (HashMap<String, Object>) weapon.get("Attributes");
     32         for (String enchant : enchants.keySet())
     33             item.addUnsafeEnchantment(Objects.requireNonNull(Enchantment.getByKey(NamespacedKey.minecraft(enchant))), (Integer) enchants.get(enchant));
     34         HashMap<String, Object> lore = (HashMap<String, Object>) weapon.get("Lore");
     35         HashMap<String, Object> ability = (HashMap<String, Object>) lore.get("Ability");
     36         HashMap<String, Object> properties = (HashMap<String, Object>) lore.get("Properties");
     37         for (String p : properties.keySet())
     38             if ((int) properties.get(p) > 0)
     39                 propertiesList.add(ChatColor.GRAY + Util.capitalize(p) + ": " + ChatColor.RED + "+" + properties.get(p));
     40         if (ability.get("Name") != null) {
     41             itemAbility.add(ChatColor.GOLD + "Item Ability: " + ability.get("Name"));
     42             for (String line : (List<String>) ability.get("Details"))
     43                 itemAbility.add(ChatColor.GRAY + line);
     44         }
     45         ItemMeta itemMeta = item.getItemMeta();
     46         if (itemMeta == null) return item;
     47         itemMeta.setDisplayName(Rarity.getRarity((String) weapon.get("Rarity")).getColor() + weapon.get("Name"));
     48         itemMeta.setUnbreakable(true);
     49 
     50         ArrayList<String> loreList = new ArrayList<>(propertiesList);
     51         loreList.add("");
     52         ArrayList<String> enchantmentList = new ArrayList<>();
     53         for (Enchantment enchantment : item.getEnchantments().keySet()) {
     54             List<String> split = Arrays.asList(Arrays.asList(enchantment.getKey().toString().split(":")).get(1).split("_"));
     55             StringBuilder builder = new StringBuilder();
     56             for (String strings : split) {
     57                 String formatted = Util.capitalize(strings);
     58                 if (split.size() > 1) {
     59                     if (strings.equals(split.get(split.size() - 1))) builder.append(formatted);
     60                     else {
     61                         builder.append(formatted);
     62                         builder.append(" ");
     63                     }
     64                 } else builder.append(formatted);
     65             }
     66             enchantmentList.add(builder + " " + item.getEnchantmentLevel(enchantment));
     67         }
     68         loreList.add(ChatColor.BLUE + String.join(", ", enchantmentList));
     69         loreList.add("");
     70         loreList.addAll(itemAbility);
     71         loreList.add("");
     72         loreList.add(Rarity.getRarity((String) weapon.get("Rarity")).getDisplay());
     73         itemMeta.setLore(loreList);
     74         for (String attribute : attributes.keySet()) {
     75             if (attribute.equals("damage")) {
     76                 AttributeModifier p = new AttributeModifier(UUID.randomUUID(), "generic.attackDamage", (Double) attributes.get(attribute), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND);
     77                 itemMeta.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE, p);
     78             } else if (attribute.equals("moveSpeed")) {
     79                 AttributeModifier s = new AttributeModifier(UUID.randomUUID(), "generic.movementSpeed", (Double) attributes.get(attribute), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND);
     80                 itemMeta.addAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED, s);
     81             }
     82         }
     83         itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE, ItemFlag.HIDE_ENCHANTS);
     84         HashMap<String, Object> pdcdata = (HashMap<String, Object>) weapon.get("PDC");
     85         for (String key : pdcdata.keySet()) {
     86             PersistentDataContainer container = itemMeta.getPersistentDataContainer();
     87             NamespacedKey key1 = new NamespacedKey(NullValkyrie.getPlugin(NullValkyrie.class), key);
     88             keys.put(Rarity.getRarity((String) weapon.get("Rarity")).getColor() + weapon.get("Name") + "." + key, key1);
     89             container.set(key1, PersistentDataType.INTEGER, (int) pdcdata.get(key));
     90         }
     91         item.setItemMeta(itemMeta);
     92         HashMap<String, Object> recipes = (HashMap<String, Object>) weapon.get("Recipes");
     93         if (recipes.get("Shape") != null) {
     94             List<String> shapes = (List<String>) recipes.get("Shape");
     95             HashMap<String, String> ind = (HashMap<String, String>) recipes.get("Ingredients");
     96             HashMap<Character, Material> indgredients = new HashMap<>();
     97             for (String i : ind.keySet())
     98                 indgredients.put(i.charAt(0), Material.matchMaterial(ind.get(i)));
     99             setItemRecipe((String) weapon.get("Name"), item, shapes, indgredients, (int) recipes.get("Amount"));
    100 
    101         }
    102         return item;
    103     }
    104 
    105     public static void setItemRecipe(String key, ItemStack i, List<String> shapes, HashMap<Character, Material> ingredients, int amount) {
    106         NamespacedKey nsk = new NamespacedKey(NullValkyrie.getPlugin(NullValkyrie.class), key.replaceAll("\\s", ""));
    107         ShapedRecipe recipe = new ShapedRecipe(nsk, i);
    108         recipe.shape(shapes.get(0), shapes.get(1), shapes.get(2));
    109         List<Character> abcs = List.of('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I');
    110         for (int ei = 0; ei < amount; ei++)
    111             recipe.setIngredient(abcs.get(ei), ingredients.get(abcs.get(ei)));
    112         if (Bukkit.getRecipe(nsk) != null) Bukkit.removeRecipe(nsk);
    113         Bukkit.addRecipe(recipe);
    114     }
    115 }