enchantment manager

This commit is contained in:
NK 2022-12-19 20:27:16 +00:00
parent c89d48910c
commit 0f39c1bc73
11 changed files with 263 additions and 229 deletions

View file

@ -6,7 +6,7 @@ import me.night.nullvalkyrie.tasks.AlwaysDayTask;
import me.night.nullvalkyrie.ui.inventory.InventoryListener; import me.night.nullvalkyrie.ui.inventory.InventoryListener;
import me.night.nullvalkyrie.database.NPCDataManager; import me.night.nullvalkyrie.database.NPCDataManager;
import me.night.nullvalkyrie.discord.DiscordClientManager; import me.night.nullvalkyrie.discord.DiscordClientManager;
import me.night.nullvalkyrie.enchantments.EnchantmentManager; import me.night.nullvalkyrie.util.enchantments.EnchantmentManager;
import me.night.nullvalkyrie.ui.player.ScoreboardListener; import me.night.nullvalkyrie.ui.player.ScoreboardListener;
import me.night.nullvalkyrie.util.*; import me.night.nullvalkyrie.util.*;
import me.night.nullvalkyrie.commands.*; import me.night.nullvalkyrie.commands.*;

View file

@ -1,7 +1,7 @@
package me.night.nullvalkyrie.commands; package me.night.nullvalkyrie.commands;
import me.night.nullvalkyrie.database.CustomWeaponsDataManager; import me.night.nullvalkyrie.database.CustomWeaponsDataManager;
import me.night.nullvalkyrie.items.CustomItemManager; import me.night.nullvalkyrie.entities.items.CustomItemManager;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;

View file

@ -1,7 +1,7 @@
package me.night.nullvalkyrie.commands; package me.night.nullvalkyrie.commands;
import me.night.nullvalkyrie.database.CustomWeaponsDataManager; import me.night.nullvalkyrie.database.CustomWeaponsDataManager;
import me.night.nullvalkyrie.items.CustomItemManager; import me.night.nullvalkyrie.entities.items.CustomItemManager;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;

View file

@ -1,120 +1,122 @@
package me.night.nullvalkyrie.items; package me.night.nullvalkyrie.entities.items;
import me.night.nullvalkyrie.Main; import me.night.nullvalkyrie.Main;
import me.night.nullvalkyrie.enums.Rarity; import me.night.nullvalkyrie.enums.Rarity;
import me.night.nullvalkyrie.util.Util; import me.night.nullvalkyrie.util.Util;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute; import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier; import org.bukkit.attribute.AttributeModifier;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.*; import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
import java.io.File; import java.io.File;
import java.util.*; import java.util.*;
import static me.night.nullvalkyrie.database.CustomWeaponsDataManager.getWeapon; import static me.night.nullvalkyrie.database.CustomWeaponsDataManager.getWeapon;
public class CustomItemManager { public class CustomItemManager {
public static final HashMap<String, NamespacedKey> keys = new HashMap<>(); public static final HashMap<String, NamespacedKey> keys = new HashMap<>();
public static ItemStack produceItem(String itemName) { public static ItemStack produceItem(String itemName) {
HashMap<String, Object> weapon = getWeapon(itemName); HashMap<String, Object> weapon = getWeapon(itemName);
ItemStack item = new ItemStack((Material) weapon.get("Material")); ItemStack item = new ItemStack((Material) weapon.get("Material"));
List<String> propertiesList = new ArrayList<>(); List<String> propertiesList = new ArrayList<>();
List<String> itemAbility = new ArrayList<>(); List<String> itemAbility = new ArrayList<>();
HashMap<String, Object> enchants = (HashMap<String, Object>) weapon.get("Enchants"); HashMap<String, Object> enchants = (HashMap<String, Object>) weapon.get("Enchants");
HashMap<String, Object> attributes = (HashMap<String, Object>) weapon.get("Attributes"); HashMap<String, Object> attributes = (HashMap<String, Object>) weapon.get("Attributes");
for (String enchant : enchants.keySet()) for (String enchant : enchants.keySet())
item.addUnsafeEnchantment(Enchantment.getByKey(NamespacedKey.minecraft(enchant)), (Integer) enchants.get(enchant)); item.addUnsafeEnchantment(Enchantment.getByKey(NamespacedKey.minecraft(enchant)), (Integer) enchants.get(enchant));
HashMap<String, Object> lore = (HashMap<String, Object>) weapon.get("Lore"); HashMap<String, Object> lore = (HashMap<String, Object>) weapon.get("Lore");
HashMap<String, Object> ability = (HashMap<String, Object>) lore.get("Ability"); HashMap<String, Object> ability = (HashMap<String, Object>) lore.get("Ability");
HashMap<String, Object> properties = (HashMap<String, Object>) lore.get("Properties"); HashMap<String, Object> properties = (HashMap<String, Object>) lore.get("Properties");
for (String p : properties.keySet()) for (String p : properties.keySet())
if ((int) properties.get(p) > 0) if ((int) properties.get(p) > 0)
propertiesList.add(ChatColor.GRAY + Util.capitalize(p) + ": " + ChatColor.RED + "+" + properties.get(p)); propertiesList.add(ChatColor.GRAY + Util.capitalize(p) + ": " + ChatColor.RED + "+" + properties.get(p));
itemAbility.add(ChatColor.GOLD + "Item Ability: " + ability.get("Name")); if (ability.get("Name") != null) {
for (String line : (List<String>) ability.get("Details")) itemAbility.add(ChatColor.GOLD + "Item Ability: " + ability.get("Name"));
itemAbility.add(ChatColor.GRAY + line); for (String line : (List<String>) ability.get("Details"))
itemAbility.add(ChatColor.GRAY + line);
ItemMeta itemMeta = item.getItemMeta(); }
itemMeta.setDisplayName(Rarity.getRarity((String) weapon.get("Rarity")).getColor() + weapon.get("Name")); ItemMeta itemMeta = item.getItemMeta();
itemMeta.setUnbreakable(true); if (itemMeta == null) return item;
itemMeta.setDisplayName(Rarity.getRarity((String) weapon.get("Rarity")).getColor() + weapon.get("Name"));
ArrayList<String> loreList = new ArrayList<>(propertiesList); itemMeta.setUnbreakable(true);
loreList.add("");
ArrayList<String> enchantmentList = new ArrayList<>(); ArrayList<String> loreList = new ArrayList<>(propertiesList);
for (Enchantment enchantment : item.getEnchantments().keySet()) { loreList.add("");
List<String> split = Arrays.asList(Arrays.asList(enchantment.getKey().toString().split(":")).get(1).split("_")); ArrayList<String> enchantmentList = new ArrayList<>();
StringBuilder builder = new StringBuilder(); for (Enchantment enchantment : item.getEnchantments().keySet()) {
for (String strings : split) { List<String> split = Arrays.asList(Arrays.asList(enchantment.getKey().toString().split(":")).get(1).split("_"));
String formatted = Util.capitalize(strings); StringBuilder builder = new StringBuilder();
if (split.size() > 1) { for (String strings : split) {
if (strings.equals(split.get(split.size() - 1))) builder.append(formatted); String formatted = Util.capitalize(strings);
else { if (split.size() > 1) {
builder.append(formatted); if (strings.equals(split.get(split.size() - 1))) builder.append(formatted);
builder.append(" "); else {
} builder.append(formatted);
} else builder.append(formatted); builder.append(" ");
} }
enchantmentList.add(builder + " " + item.getEnchantmentLevel(enchantment)); } else builder.append(formatted);
} }
loreList.add(ChatColor.BLUE + String.join(", ", enchantmentList)); enchantmentList.add(builder + " " + item.getEnchantmentLevel(enchantment));
loreList.add(""); }
loreList.addAll(itemAbility); loreList.add(ChatColor.BLUE + String.join(", ", enchantmentList));
loreList.add(""); loreList.add("");
loreList.add(Rarity.getRarity((String) weapon.get("Rarity")).getDisplay()); loreList.addAll(itemAbility);
itemMeta.setLore(loreList); loreList.add("");
for (String attribute : attributes.keySet()) { loreList.add(Rarity.getRarity((String) weapon.get("Rarity")).getDisplay());
if (attribute.equals("damage")) { itemMeta.setLore(loreList);
AttributeModifier p = new AttributeModifier(UUID.randomUUID(), "generic.attackDamage", (Double) attributes.get(attribute), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND); for (String attribute : attributes.keySet()) {
itemMeta.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE, p); if (attribute.equals("damage")) {
} else if (attribute.equals("moveSpeed")) { AttributeModifier p = new AttributeModifier(UUID.randomUUID(), "generic.attackDamage", (Double) attributes.get(attribute), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND);
AttributeModifier s = new AttributeModifier(UUID.randomUUID(), "generic.movementSpeed", (Double) attributes.get(attribute), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND); itemMeta.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE, p);
itemMeta.addAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED, s); } else if (attribute.equals("moveSpeed")) {
} AttributeModifier s = new AttributeModifier(UUID.randomUUID(), "generic.movementSpeed", (Double) attributes.get(attribute), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND);
} itemMeta.addAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED, s);
itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE, ItemFlag.HIDE_ENCHANTS); }
HashMap<String, Object> pdcdata = (HashMap<String, Object>) weapon.get("PDC"); }
for (String key : pdcdata.keySet()) { itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE, ItemFlag.HIDE_ENCHANTS);
PersistentDataContainer container = itemMeta.getPersistentDataContainer(); HashMap<String, Object> pdcdata = (HashMap<String, Object>) weapon.get("PDC");
NamespacedKey key1 = new NamespacedKey(Main.getPlugin(Main.class), key); for (String key : pdcdata.keySet()) {
keys.put(Rarity.getRarity((String) weapon.get("Rarity")).getColor() + weapon.get("Name") + "." + key, key1); PersistentDataContainer container = itemMeta.getPersistentDataContainer();
container.set(key1, PersistentDataType.INTEGER, (int) pdcdata.get(key)); NamespacedKey key1 = new NamespacedKey(Main.getPlugin(Main.class), key);
} keys.put(Rarity.getRarity((String) weapon.get("Rarity")).getColor() + weapon.get("Name") + "." + key, key1);
item.setItemMeta(itemMeta); container.set(key1, PersistentDataType.INTEGER, (int) pdcdata.get(key));
HashMap<String, Object> recipes = (HashMap<String, Object>) weapon.get("Recipes"); }
if (recipes.get("Shape") != null) { item.setItemMeta(itemMeta);
List<String> shapes = (List<String>) recipes.get("Shape"); HashMap<String, Object> recipes = (HashMap<String, Object>) weapon.get("Recipes");
HashMap<String, String> ind = (HashMap<String, String>) recipes.get("Ingredients"); if (recipes.get("Shape") != null) {
HashMap<Character, Material> indgredients = new HashMap<>(); List<String> shapes = (List<String>) recipes.get("Shape");
for (String i : ind.keySet()) HashMap<String, String> ind = (HashMap<String, String>) recipes.get("Ingredients");
indgredients.put(i.charAt(0), Material.matchMaterial(ind.get(i))); HashMap<Character, Material> indgredients = new HashMap<>();
setItemRecipe((String) weapon.get("Name"), item, shapes, indgredients, (int) recipes.get("Amount")); for (String i : ind.keySet())
indgredients.put(i.charAt(0), Material.matchMaterial(ind.get(i)));
} setItemRecipe((String) weapon.get("Name"), item, shapes, indgredients, (int) recipes.get("Amount"));
return item;
} }
return item;
public static void setItemRecipe(String key, ItemStack i, List<String> shapes, HashMap<Character, Material> ingredients, int amount) { }
NamespacedKey nsk = new NamespacedKey(Main.getPlugin(Main.class), key.replaceAll("\\s", ""));
ShapedRecipe recipe = new ShapedRecipe(nsk, i); public static void setItemRecipe(String key, ItemStack i, List<String> shapes, HashMap<Character, Material> ingredients, int amount) {
recipe.shape(shapes.get(0), shapes.get(1), shapes.get(2)); NamespacedKey nsk = new NamespacedKey(Main.getPlugin(Main.class), key.replaceAll("\\s", ""));
List<Character> abcs = List.of('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'); ShapedRecipe recipe = new ShapedRecipe(nsk, i);
for (int ei = 0; ei < amount; ei++) recipe.shape(shapes.get(0), shapes.get(1), shapes.get(2));
recipe.setIngredient(abcs.get(ei), ingredients.get(abcs.get(ei))); List<Character> abcs = List.of('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I');
if (Bukkit.getRecipe(nsk) != null) Bukkit.removeRecipe(nsk); for (int ei = 0; ei < amount; ei++)
Bukkit.addRecipe(recipe); recipe.setIngredient(abcs.get(ei), ingredients.get(abcs.get(ei)));
} if (Bukkit.getRecipe(nsk) != null) Bukkit.removeRecipe(nsk);
Bukkit.addRecipe(recipe);
public static void updateYamlFilesToPlugin(String path) { }
File file = new File(Main.getPlugin(Main.class).getDataFolder(), path);
if (!file.exists()) Main.getPlugin(Main.class).saveResource(path, true); public static void updateYamlFilesToPlugin(String path) {
else Main.getPlugin(Main.class).saveResource(path, true); File file = new File(Main.getPlugin(Main.class).getDataFolder(), path);
} if (!file.exists()) Main.getPlugin(Main.class).saveResource(path, true);
} else Main.getPlugin(Main.class).saveResource(path, true);
}
}

View file

@ -1,4 +1,4 @@
package me.night.nullvalkyrie.items; package me.night.nullvalkyrie.entities.items;
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ArrayListMultimap;
import org.bukkit.Material; import org.bukkit.Material;

View file

@ -0,0 +1,34 @@
package me.night.nullvalkyrie.enums;
import me.night.nullvalkyrie.util.enchantments.EnchantmentManager;
import org.bukkit.enchantments.Enchantment;
public enum Enchantments {
THUNDERBOLT("thunderbolt", "ThunderBolt", 5),
SMELTING_TOUCH("smelting-touch", "Smelting Touch", 1);
public final String namespacekey;
public final String name;
public final int maxLevel;
Enchantments(String namespacekey, String name, int maxLevel) {
this.namespacekey = namespacekey;
this.name = name;
this.maxLevel = maxLevel;
}
public String getNamespacekey() {
return namespacekey;
}
public String getName() {
return name;
}
public int getMaxLevel() {
return maxLevel;
}
public Enchantment getEnchant() {
return EnchantmentManager.enchants.get(this.ordinal());
}
}

View file

@ -5,8 +5,8 @@ import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.BlockPosition; import com.comphenix.protocol.wrappers.BlockPosition;
import me.night.nullvalkyrie.items.CustomItemManager; import me.night.nullvalkyrie.entities.items.CustomItemManager;
import me.night.nullvalkyrie.items.Pickaxe; import me.night.nullvalkyrie.entities.items.Pickaxe;
import me.night.nullvalkyrie.enums.Rarity; import me.night.nullvalkyrie.enums.Rarity;
import me.night.nullvalkyrie.Main; import me.night.nullvalkyrie.Main;
import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.ChatMessageType;

View file

@ -1,7 +1,7 @@
package me.night.nullvalkyrie.ui.inventory; package me.night.nullvalkyrie.ui.inventory;
import me.night.nullvalkyrie.database.ShopDataManager; import me.night.nullvalkyrie.database.ShopDataManager;
import me.night.nullvalkyrie.items.CustomItemManager; import me.night.nullvalkyrie.entities.items.CustomItemManager;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;

View file

@ -1,6 +1,6 @@
package me.night.nullvalkyrie.util; package me.night.nullvalkyrie.util;
import static me.night.nullvalkyrie.items.CustomItemManager.updateYamlFilesToPlugin; import static me.night.nullvalkyrie.entities.items.CustomItemManager.updateYamlFilesToPlugin;
public class FileManager { public class FileManager {
public FileManager() { public FileManager() {

View file

@ -1,58 +1,58 @@
package me.night.nullvalkyrie.enchantments; package me.night.nullvalkyrie.util.enchantments;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentTarget; import org.bukkit.enchantments.EnchantmentTarget;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class CustomEnchantment extends Enchantment { public class CustomEnchantment extends Enchantment {
private final String name; private final String name;
private final int max; private final int max;
public CustomEnchantment(String namespace, String name, int lvl) { public CustomEnchantment(String namespace, String name, int lvl) {
super(NamespacedKey.minecraft(namespace)); super(NamespacedKey.minecraft(namespace));
this.name = name; this.name = name;
this.max = lvl; this.max = lvl;
} }
@Override @Override
public @NotNull String getName() { public @NotNull String getName() {
return name; return name;
} }
@Override @Override
public int getMaxLevel() { public int getMaxLevel() {
return max; return max;
} }
@Override @Override
public int getStartLevel() { public int getStartLevel() {
return 1; return 1;
} }
@Override @Override
public @NotNull EnchantmentTarget getItemTarget() { public @NotNull EnchantmentTarget getItemTarget() {
return null; return null;
} }
@Override @Override
public boolean isTreasure() { public boolean isTreasure() {
return false; return false;
} }
@Override @Override
public boolean isCursed() { public boolean isCursed() {
return false; return false;
} }
@Override @Override
public boolean conflictsWith(@NotNull Enchantment other) { public boolean conflictsWith(@NotNull Enchantment other) {
return false; return false;
} }
@Override @Override
public boolean canEnchantItem(@NotNull ItemStack item) { public boolean canEnchantItem(@NotNull ItemStack item) {
return true; return true;
} }
} }

View file

@ -1,43 +1,41 @@
package me.night.nullvalkyrie.enchantments; package me.night.nullvalkyrie.util.enchantments;
import org.bukkit.enchantments.Enchantment; import me.night.nullvalkyrie.enums.Enchantments;
import org.bukkit.enchantments.Enchantment;
import java.lang.reflect.Field;
import java.util.ArrayList; import java.lang.reflect.Field;
import java.util.Arrays; import java.util.ArrayList;
import java.util.List; import java.util.Arrays;
import java.util.List;
public class EnchantmentManager {
public static final Enchantment ThunderBolt = new CustomEnchantment("thunderbolt", "ThunderBolt", 5); public class EnchantmentManager {
public static final Enchantment SmeltingTouch = new CustomEnchantment("smelting-touch", "Smelting Touch", 1); public static List<Enchantment> enchants = new ArrayList<>();
public static void register() {
public static void register() { List<Boolean> registeredList = new ArrayList<>();
List<Enchantment> enchants = new ArrayList<>(); for (Enchantments enchantment : Enchantments.values()) {
enchants.add(ThunderBolt); Enchantment en = new CustomEnchantment(enchantment.getNamespacekey(), enchantment.getName(), enchantment.getMaxLevel());
enchants.add(SmeltingTouch); enchants.add(en);
List<Boolean> registeredList = new ArrayList<>(); registeredList.add(Arrays.stream(Enchantment.values()).toList().contains(en));
for (Enchantment enchant : enchants) { }
registeredList.add(Arrays.stream(Enchantment.values()).toList().contains(enchant)); for (int counter = 0; counter < registeredList.size(); counter++) {
} if (!registeredList.get(counter)) registerEnchantment(enchants.get(counter));
for (int counter = 0; counter < registeredList.size(); counter++) { }
if (!registeredList.get(counter)) registerEnchantment(enchants.get(counter));
} }
} public static void registerEnchantment(Enchantment en) {
try {
public static void registerEnchantment(Enchantment en) { try {
try { Field f = Enchantment.class.getDeclaredField("acceptingNew");
try { f.setAccessible(true);
Field f = Enchantment.class.getDeclaredField("acceptingNew"); f.set(null, true);
f.setAccessible(true); } catch (Exception e) {
f.set(null, true); e.printStackTrace();
} catch (Exception e) { }
e.printStackTrace(); Enchantment.registerEnchantment(en);
} } catch (Exception e) {
Enchantment.registerEnchantment(en); e.printStackTrace();
} catch (Exception e) { }
e.printStackTrace(); }
}
} }
}