From d763a05158c72f59c93147316c3e7db678050243 Mon Sep 17 00:00:00 2001 From: NK Date: Tue, 6 Dec 2022 12:33:46 +0000 Subject: [PATCH] edit on custom item manager to accept pdc and recipes --- .../nullvalkyrie/commands/BetaCommand.java | 2 - .../database/CustomWeaponsDataManager.java | 18 +++++++ .../events/listeners/CustomItemEvents.java | 2 +- .../events/listeners/ServerEvents.java | 2 +- .../nullvalkyrie/items/CustomItemManager.java | 52 +++++++++++-------- 5 files changed, 49 insertions(+), 27 deletions(-) diff --git a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java index f7145cc..453ec37 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java @@ -1,6 +1,5 @@ package me.night.nullvalkyrie.commands; -import me.night.nullvalkyrie.database.UserDataManager; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -15,7 +14,6 @@ public class BetaCommand extends Command { @Override public void onCommand(CommandSender sender, String[] args) { if (sender instanceof Player player) { - UserDataManager.updateUserBank(player.getUniqueId().toString(), 10); } } diff --git a/src/main/java/me/night/nullvalkyrie/database/CustomWeaponsDataManager.java b/src/main/java/me/night/nullvalkyrie/database/CustomWeaponsDataManager.java index c89a22e..9e05221 100644 --- a/src/main/java/me/night/nullvalkyrie/database/CustomWeaponsDataManager.java +++ b/src/main/java/me/night/nullvalkyrie/database/CustomWeaponsDataManager.java @@ -11,6 +11,7 @@ import java.util.List; public class CustomWeaponsDataManager { + public static HashMap getWeapon(String itemName) { HashMap item = new HashMap<>(); try (MongoCursor cursor = DatabaseManager.getCustomWeaponsDB().find(Filters.eq("Name", itemName)).cursor()) { @@ -37,6 +38,21 @@ public class CustomWeaponsDataManager { HashMap attr = new HashMap<>(); for (String a : enchants.keySet()) ench.put(a, enchants.get(a)); for (String a : attributes.keySet()) attr.put(a, attributes.get(a)); + Document pdc = (Document) doc.get("PDC"); + HashMap pdcdata = new HashMap<>(); + for (String i : pdc.keySet()) + pdcdata.put(i, pdc.get(i)); + Document recipe = (Document) doc.get("Recipes"); + HashMap recipes = new HashMap<>(); + Document ing = (Document) recipe.get("Ingredients"); + HashMap ingredients = new HashMap<>(); + for (String i : ing.keySet()) + ingredients.put(i, ing.getString(i)); + List shapes = new ArrayList<>(); + if (recipe.get("Shapes") != null) for (String s : (List) recipe.get("Shapes")) shapes.add(s); + recipes.put("Shape", shapes); + recipes.put("Amount", recipe.getInteger("Amount")); + recipes.put("Ingredients", ingredients); item.put("Name", name); item.put("Material", Material.matchMaterial(doc.getString("Material"))); item.put("Type", doc.getString("Type")); @@ -44,6 +60,8 @@ public class CustomWeaponsDataManager { item.put("Lore", lores); item.put("Enchants", ench); item.put("Attributes", attr); + item.put("PDC", pdcdata); + item.put("Recipes", recipes); } return item; } diff --git a/src/main/java/me/night/nullvalkyrie/events/listeners/CustomItemEvents.java b/src/main/java/me/night/nullvalkyrie/events/listeners/CustomItemEvents.java index 3e54a86..60cf040 100644 --- a/src/main/java/me/night/nullvalkyrie/events/listeners/CustomItemEvents.java +++ b/src/main/java/me/night/nullvalkyrie/events/listeners/CustomItemEvents.java @@ -96,7 +96,7 @@ public class CustomItemEvents implements Listener { NamespacedKey ammoKey = CustomItemManager.keys.get(name + ".ammo"); int ammo = container.get(ammoKey, PersistentDataType.INTEGER); container.set(ammoKey, PersistentDataType.INTEGER, ammo - 1); - int max = container.get(CustomItemManager.keys.get(name + ".maxload"), PersistentDataType.INTEGER); + int max = container.get(CustomItemManager.keys.get(name + ".max"), PersistentDataType.INTEGER); weapon.setItemMeta(weaponMeta); e.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', "&6AK-47 ( " + (ammo - 1) + "/ " + max + " )"))); diff --git a/src/main/java/me/night/nullvalkyrie/events/listeners/ServerEvents.java b/src/main/java/me/night/nullvalkyrie/events/listeners/ServerEvents.java index cad0798..8cfd575 100644 --- a/src/main/java/me/night/nullvalkyrie/events/listeners/ServerEvents.java +++ b/src/main/java/me/night/nullvalkyrie/events/listeners/ServerEvents.java @@ -42,7 +42,7 @@ public class ServerEvents implements Listener { @EventHandler public void onClickHologram(InteractHologramEvent e) { if (e.getHologram().getCustomName().equals(ChatColor.GOLD + "Click me to change!!!")) { - System.out.println(true); + // TODO: change hologram things } } } diff --git a/src/main/java/me/night/nullvalkyrie/items/CustomItemManager.java b/src/main/java/me/night/nullvalkyrie/items/CustomItemManager.java index 5ed9635..b09b89f 100644 --- a/src/main/java/me/night/nullvalkyrie/items/CustomItemManager.java +++ b/src/main/java/me/night/nullvalkyrie/items/CustomItemManager.java @@ -3,6 +3,7 @@ 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; @@ -12,7 +13,10 @@ import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.ShapedRecipe; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; import java.io.File; import java.util.*; @@ -40,10 +44,11 @@ public class CustomItemManager { itemAbility.add(ChatColor.GOLD + "Item Ability: " + ability.get("Name")); for (String line : (List) ability.get("Details")) itemAbility.add(ChatColor.GRAY + line); - //recipe + ItemMeta itemMeta = item.getItemMeta(); itemMeta.setDisplayName(Rarity.getRarity((String) weapon.get("Rarity")).getColor() + weapon.get("Name")); itemMeta.setUnbreakable(true); + ArrayList loreList = new ArrayList<>(); loreList.addAll(propertiesList); loreList.add(""); @@ -79,32 +84,33 @@ public class CustomItemManager { } } itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE, ItemFlag.HIDE_ENCHANTS); -// for (String key : fileConfig.getKeys(true)) { -// if (key.startsWith("pdc.")) { -// String property = Arrays.asList(key.split("\\.")).get(1); -// if (property.equals("ammo")) { -// PersistentDataContainer container = itemMeta.getPersistentDataContainer(); -// NamespacedKey key1 = new NamespacedKey(main, "ammo"); -// keys.put(Rarity.getRarity(fileConfig.getString("rarity")).getColor() + fileConfig.getString("name") + "." + property, key1); -// container.set(key1, PersistentDataType.INTEGER, fileConfig.getInt(key)); -// } else if (property.equals("maxload")) { -// PersistentDataContainer container = itemMeta.getPersistentDataContainer(); -// NamespacedKey key2 = new NamespacedKey(main, "maxload"); -// keys.put(Rarity.getRarity(fileConfig.getString("rarity")).getColor() + fileConfig.getString("name") + "." + property, key2); -// container.set(key2, PersistentDataType.INTEGER, fileConfig.getInt(key)); -// } -// } -// } + HashMap pdcdata = (HashMap) 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 recipes = (HashMap) weapon.get("Recipes"); + List shapes = (List) recipes.get("Shape"); + HashMap ind = (HashMap) recipes.get("Ingredients"); + HashMap 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(NamespacedKey key, ItemStack i, int ingredient, String shape1, String shape2, String shape3, List ingredients) { -// ShapedRecipe wither_sword_recipe = new ShapedRecipe(new NamespacedKey(main, "widow_sword"), widow_sword); -// wither_sword_recipe.shape(" A ", " A "," B "); -// wither_sword_recipe.setIngredient('A', Material.IRON_INGOT); -// wither_sword_recipe.setIngredient('B', Material.STICK); -// Bukkit.addRecipe(wither_sword_recipe); + public static void setItemRecipe(String key, ItemStack i, List shapes, HashMap 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 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) {