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.database.NPCDataManager;
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.util.*;
import me.night.nullvalkyrie.commands.*;

View file

@ -1,7 +1,7 @@
package me.night.nullvalkyrie.commands;
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.command.CommandSender;
import org.bukkit.entity.Player;

View file

@ -1,7 +1,7 @@
package me.night.nullvalkyrie.commands;
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.command.CommandSender;
import org.bukkit.entity.Player;

View file

@ -1,120 +1,122 @@
package me.night.nullvalkyrie.items;
import me.night.nullvalkyrie.Main;
import me.night.nullvalkyrie.enums.Rarity;
import me.night.nullvalkyrie.util.Util;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.io.File;
import java.util.*;
import static me.night.nullvalkyrie.database.CustomWeaponsDataManager.getWeapon;
public class CustomItemManager {
public static final HashMap<String, NamespacedKey> keys = new HashMap<>();
public static ItemStack produceItem(String itemName) {
HashMap<String, Object> weapon = getWeapon(itemName);
ItemStack item = new ItemStack((Material) weapon.get("Material"));
List<String> propertiesList = new ArrayList<>();
List<String> itemAbility = new ArrayList<>();
HashMap<String, Object> enchants = (HashMap<String, Object>) weapon.get("Enchants");
HashMap<String, Object> attributes = (HashMap<String, Object>) weapon.get("Attributes");
for (String enchant : enchants.keySet())
item.addUnsafeEnchantment(Enchantment.getByKey(NamespacedKey.minecraft(enchant)), (Integer) enchants.get(enchant));
HashMap<String, Object> lore = (HashMap<String, Object>) weapon.get("Lore");
HashMap<String, Object> ability = (HashMap<String, Object>) lore.get("Ability");
HashMap<String, Object> properties = (HashMap<String, Object>) lore.get("Properties");
for (String p : properties.keySet())
if ((int) properties.get(p) > 0)
propertiesList.add(ChatColor.GRAY + Util.capitalize(p) + ": " + ChatColor.RED + "+" + properties.get(p));
itemAbility.add(ChatColor.GOLD + "Item Ability: " + ability.get("Name"));
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.setUnbreakable(true);
ArrayList<String> loreList = new ArrayList<>(propertiesList);
loreList.add("");
ArrayList<String> enchantmentList = new ArrayList<>();
for (Enchantment enchantment : item.getEnchantments().keySet()) {
List<String> split = Arrays.asList(Arrays.asList(enchantment.getKey().toString().split(":")).get(1).split("_"));
StringBuilder builder = new StringBuilder();
for (String strings : split) {
String formatted = Util.capitalize(strings);
if (split.size() > 1) {
if (strings.equals(split.get(split.size() - 1))) builder.append(formatted);
else {
builder.append(formatted);
builder.append(" ");
}
} else builder.append(formatted);
}
enchantmentList.add(builder + " " + item.getEnchantmentLevel(enchantment));
}
loreList.add(ChatColor.BLUE + String.join(", ", enchantmentList));
loreList.add("");
loreList.addAll(itemAbility);
loreList.add("");
loreList.add(Rarity.getRarity((String) weapon.get("Rarity")).getDisplay());
itemMeta.setLore(loreList);
for (String attribute : attributes.keySet()) {
if (attribute.equals("damage")) {
AttributeModifier p = new AttributeModifier(UUID.randomUUID(), "generic.attackDamage", (Double) attributes.get(attribute), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND);
itemMeta.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE, p);
} 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()) {
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
NamespacedKey key1 = new NamespacedKey(Main.getPlugin(Main.class), key);
keys.put(Rarity.getRarity((String) weapon.get("Rarity")).getColor() + weapon.get("Name") + "." + key, key1);
container.set(key1, PersistentDataType.INTEGER, (int) pdcdata.get(key));
}
item.setItemMeta(itemMeta);
HashMap<String, Object> recipes = (HashMap<String, Object>) weapon.get("Recipes");
if (recipes.get("Shape") != null) {
List<String> shapes = (List<String>) recipes.get("Shape");
HashMap<String, String> ind = (HashMap<String, String>) recipes.get("Ingredients");
HashMap<Character, Material> indgredients = new HashMap<>();
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;
}
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);
recipe.shape(shapes.get(0), shapes.get(1), shapes.get(2));
List<Character> abcs = List.of('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I');
for (int ei = 0; ei < amount; ei++)
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);
else Main.getPlugin(Main.class).saveResource(path, true);
}
}
package me.night.nullvalkyrie.entities.items;
import me.night.nullvalkyrie.Main;
import me.night.nullvalkyrie.enums.Rarity;
import me.night.nullvalkyrie.util.Util;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import java.io.File;
import java.util.*;
import static me.night.nullvalkyrie.database.CustomWeaponsDataManager.getWeapon;
public class CustomItemManager {
public static final HashMap<String, NamespacedKey> keys = new HashMap<>();
public static ItemStack produceItem(String itemName) {
HashMap<String, Object> weapon = getWeapon(itemName);
ItemStack item = new ItemStack((Material) weapon.get("Material"));
List<String> propertiesList = new ArrayList<>();
List<String> itemAbility = new ArrayList<>();
HashMap<String, Object> enchants = (HashMap<String, Object>) weapon.get("Enchants");
HashMap<String, Object> attributes = (HashMap<String, Object>) weapon.get("Attributes");
for (String enchant : enchants.keySet())
item.addUnsafeEnchantment(Enchantment.getByKey(NamespacedKey.minecraft(enchant)), (Integer) enchants.get(enchant));
HashMap<String, Object> lore = (HashMap<String, Object>) weapon.get("Lore");
HashMap<String, Object> ability = (HashMap<String, Object>) lore.get("Ability");
HashMap<String, Object> properties = (HashMap<String, Object>) lore.get("Properties");
for (String p : properties.keySet())
if ((int) properties.get(p) > 0)
propertiesList.add(ChatColor.GRAY + Util.capitalize(p) + ": " + ChatColor.RED + "+" + properties.get(p));
if (ability.get("Name") != null) {
itemAbility.add(ChatColor.GOLD + "Item Ability: " + ability.get("Name"));
for (String line : (List<String>) ability.get("Details"))
itemAbility.add(ChatColor.GRAY + line);
}
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta == null) return item;
itemMeta.setDisplayName(Rarity.getRarity((String) weapon.get("Rarity")).getColor() + weapon.get("Name"));
itemMeta.setUnbreakable(true);
ArrayList<String> loreList = new ArrayList<>(propertiesList);
loreList.add("");
ArrayList<String> enchantmentList = new ArrayList<>();
for (Enchantment enchantment : item.getEnchantments().keySet()) {
List<String> split = Arrays.asList(Arrays.asList(enchantment.getKey().toString().split(":")).get(1).split("_"));
StringBuilder builder = new StringBuilder();
for (String strings : split) {
String formatted = Util.capitalize(strings);
if (split.size() > 1) {
if (strings.equals(split.get(split.size() - 1))) builder.append(formatted);
else {
builder.append(formatted);
builder.append(" ");
}
} else builder.append(formatted);
}
enchantmentList.add(builder + " " + item.getEnchantmentLevel(enchantment));
}
loreList.add(ChatColor.BLUE + String.join(", ", enchantmentList));
loreList.add("");
loreList.addAll(itemAbility);
loreList.add("");
loreList.add(Rarity.getRarity((String) weapon.get("Rarity")).getDisplay());
itemMeta.setLore(loreList);
for (String attribute : attributes.keySet()) {
if (attribute.equals("damage")) {
AttributeModifier p = new AttributeModifier(UUID.randomUUID(), "generic.attackDamage", (Double) attributes.get(attribute), AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND);
itemMeta.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE, p);
} 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()) {
PersistentDataContainer container = itemMeta.getPersistentDataContainer();
NamespacedKey key1 = new NamespacedKey(Main.getPlugin(Main.class), key);
keys.put(Rarity.getRarity((String) weapon.get("Rarity")).getColor() + weapon.get("Name") + "." + key, key1);
container.set(key1, PersistentDataType.INTEGER, (int) pdcdata.get(key));
}
item.setItemMeta(itemMeta);
HashMap<String, Object> recipes = (HashMap<String, Object>) weapon.get("Recipes");
if (recipes.get("Shape") != null) {
List<String> shapes = (List<String>) recipes.get("Shape");
HashMap<String, String> ind = (HashMap<String, String>) recipes.get("Ingredients");
HashMap<Character, Material> indgredients = new HashMap<>();
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;
}
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);
recipe.shape(shapes.get(0), shapes.get(1), shapes.get(2));
List<Character> abcs = List.of('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I');
for (int ei = 0; ei < amount; ei++)
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);
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 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.events.PacketContainer;
import com.comphenix.protocol.wrappers.BlockPosition;
import me.night.nullvalkyrie.items.CustomItemManager;
import me.night.nullvalkyrie.items.Pickaxe;
import me.night.nullvalkyrie.entities.items.CustomItemManager;
import me.night.nullvalkyrie.entities.items.Pickaxe;
import me.night.nullvalkyrie.enums.Rarity;
import me.night.nullvalkyrie.Main;
import net.md_5.bungee.api.ChatMessageType;

View file

@ -1,7 +1,7 @@
package me.night.nullvalkyrie.ui.inventory;
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.Material;
import org.bukkit.entity.Player;

View file

@ -1,6 +1,6 @@
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 FileManager() {

View file

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

View file

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