From ff2aec18c17e885eb247059a49f416ac75fe87e1 Mon Sep 17 00:00:00 2001 From: NK Date: Wed, 30 Nov 2022 12:40:29 +0000 Subject: [PATCH] fixing in custom item manager --- src/main/java/me/night/nullvalkyrie/Main.java | 1 - .../nullvalkyrie/commands/MinerCommand.java | 6 +- .../nullvalkyrie/commands/ShopCommand.java | 5 +- .../nullvalkyrie/commands/UtilCommand.java | 10 +-- .../nullvalkyrie/events/CustomItemEvents.java | 4 +- .../nullvalkyrie/items/CustomItemManager.java | 66 ++++++++----------- src/main/resources/customEnchants.yml | 0 7 files changed, 35 insertions(+), 57 deletions(-) delete mode 100644 src/main/resources/customEnchants.yml diff --git a/src/main/java/me/night/nullvalkyrie/Main.java b/src/main/java/me/night/nullvalkyrie/Main.java index 638efce..025da1f 100644 --- a/src/main/java/me/night/nullvalkyrie/Main.java +++ b/src/main/java/me/night/nullvalkyrie/Main.java @@ -6,7 +6,6 @@ import me.night.nullvalkyrie.database.NPCDataManager; import me.night.nullvalkyrie.discord.DiscordClientManager; import me.night.nullvalkyrie.enchantments.EnchantmentManager; import me.night.nullvalkyrie.events.*; -import me.night.nullvalkyrie.items.CustomItemManager; import me.night.nullvalkyrie.npc.*; import me.night.nullvalkyrie.ui.ScoreboardListener; import me.night.nullvalkyrie.util.*; diff --git a/src/main/java/me/night/nullvalkyrie/commands/MinerCommand.java b/src/main/java/me/night/nullvalkyrie/commands/MinerCommand.java index 371b007..e19a59a 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/MinerCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/MinerCommand.java @@ -24,12 +24,10 @@ public class MinerCommand extends Command { @Override public void onCommand(CommandSender sender, String[] args) { - if (sender instanceof Player) { + if (sender instanceof Player player) { if (args.length == 0) { - Player player = (Player) sender; new MinerGUI(player); int seconds = (int) (new Date().getTime() - MinerDataManager.getLastClaim(1)) / 1000; - System.out.println("Seconds: " + seconds); generate(50, seconds); } else if (args[0].equalsIgnoreCase("new")) { String name = args[1]; @@ -41,7 +39,7 @@ public class MinerCommand extends Command { } else if (args[0].equalsIgnoreCase("claim")) { String minerIndex = args[1]; MinerDataManager.setLastClaim(Long.parseLong(minerIndex)); - System.out.println("Done"); + player.sendMessage("Claimed"); } } diff --git a/src/main/java/me/night/nullvalkyrie/commands/ShopCommand.java b/src/main/java/me/night/nullvalkyrie/commands/ShopCommand.java index e82432a..348f75b 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/ShopCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/ShopCommand.java @@ -14,7 +14,6 @@ import java.util.List; public class ShopCommand extends Command { private final FileConfiguration file = CustomItemManager.loadConfig("shop.yml"); - private Inventory inv; public ShopCommand() { super("7elven", @@ -26,10 +25,10 @@ public class ShopCommand extends Command { @Override public void onCommand(CommandSender sender, String[] args) { - inv = Bukkit.createInventory(null, 45, ChatColor.GREEN + "7-Eleven 24/7"); + Inventory inv = Bukkit.createInventory(null, 45, ChatColor.GREEN + "7-Eleven 24/7"); int counter = 0; for (String c : file.getKeys(false)) { - ItemStack item = CustomItemManager.getItem(file.getString(c + ".name")).clone(); + ItemStack item = CustomItemManager.produceItem(file.getString(c + ".name")).clone(); ItemMeta itemMeta = item.getItemMeta(); List lore = itemMeta.getLore(); lore.add("Price (BIN): " + file.getString(c + ".price")); diff --git a/src/main/java/me/night/nullvalkyrie/commands/UtilCommand.java b/src/main/java/me/night/nullvalkyrie/commands/UtilCommand.java index 9bb4304..e3b0662 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/UtilCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/UtilCommand.java @@ -13,13 +13,7 @@ import java.util.*; public class UtilCommand extends Command { public UtilCommand() { - super( - "util", - new String[]{}, - "Give you a tool", - "" - - ); + super("util", new String[]{}, "Give you a tool", ""); } @Override @@ -36,7 +30,7 @@ public class UtilCommand extends Command { } } - ItemStack item = CustomItemManager.getItem(s.toString()); + ItemStack item = CustomItemManager.produceItem(s.toString()); if (item.hasItemMeta()) { player.getInventory().addItem(item); } else { diff --git a/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java b/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java index d5b0190..04c61be 100644 --- a/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java +++ b/src/main/java/me/night/nullvalkyrie/events/CustomItemEvents.java @@ -296,8 +296,8 @@ public class CustomItemEvents implements Listener { bread.addIngredient(new ItemStack(Material.EMERALD, 10)); recipes.add(bread); - MerchantRecipe tntStick = new MerchantRecipe(CustomItemManager.getItem("Terminator"), 10); - tntStick.addIngredient(CustomItemManager.getItem("Widow Sword")); + MerchantRecipe tntStick = new MerchantRecipe(CustomItemManager.produceItem("Terminator"), 10); + tntStick.addIngredient(CustomItemManager.produceItem("Widow Sword")); recipes.add(tntStick); Merchant merchant = Bukkit.createMerchant("Exchange here"); merchant.setRecipes(recipes); diff --git a/src/main/java/me/night/nullvalkyrie/items/CustomItemManager.java b/src/main/java/me/night/nullvalkyrie/items/CustomItemManager.java index 68fbd14..bae764a 100644 --- a/src/main/java/me/night/nullvalkyrie/items/CustomItemManager.java +++ b/src/main/java/me/night/nullvalkyrie/items/CustomItemManager.java @@ -22,51 +22,42 @@ import java.util.*; import static me.night.nullvalkyrie.database.CustomWeaponsDataManager.getWeapon; - public class CustomItemManager { - private static final HashMap weapons = new HashMap<>(); public static HashMap keys = new HashMap<>(); - private static Main main; - - public CustomItemManager(Main main) { - CustomItemManager.main = main; - } public static ItemStack produceItem(String itemName) { HashMap weapon = getWeapon(itemName); - ItemStack item = new ItemStack((Material) weapon.get("Material")); - List properties = new ArrayList<>(); + List propertiesList = new ArrayList<>(); List itemAbility = new ArrayList<>(); HashMap enchants = (HashMap) weapon.get("Enchants"); HashMap attributes = (HashMap) weapon.get("Attributes"); - for (String enchant : enchants.keySet()) { + for (String enchant : enchants.keySet()) item.addUnsafeEnchantment(Enchantment.getByKey(NamespacedKey.minecraft(enchant)), (Integer) enchants.get(enchant)); - } - HashMap lo = (HashMap) weapon.get("Lore"); - HashMap abi = (HashMap) lo.get("Ability"); - HashMap prob = (HashMap) lo.get("Properties"); - for (String p : prob.keySet()) - if ((int) prob.get(p) > 0) - properties.add(ChatColor.GRAY + Util.capitalize(p) + ": " + ChatColor.RED + "+" + prob.get(p)); - itemAbility.add(ChatColor.GOLD + "Item Ability: " + abi.get("Name")); - for (String line : (List) abi.get("Details")) + HashMap lore = (HashMap) weapon.get("Lore"); + HashMap ability = (HashMap) lore.get("Ability"); + HashMap properties = (HashMap) 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) 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 lore = new ArrayList<>(); - lore.addAll(properties); - lore.add(""); + ArrayList loreList = new ArrayList<>(); + loreList.addAll(propertiesList); + loreList.add(""); ArrayList enchantmentList = new ArrayList<>(); for (Enchantment enchantment : item.getEnchantments().keySet()) { - List splitted = Arrays.asList(Arrays.asList(enchantment.getKey().toString().split(":")).get(1).split("_")); + List split = Arrays.asList(Arrays.asList(enchantment.getKey().toString().split(":")).get(1).split("_")); StringBuilder builder = new StringBuilder(); - for (String strings : splitted) { + for (String strings : split) { String formatted = Util.capitalize(strings); - if (splitted.size() > 1) { - if (strings.equals(splitted.get(splitted.size() - 1))) builder.append(formatted); + if (split.size() > 1) { + if (strings.equals(split.get(split.size() - 1))) builder.append(formatted); else { builder.append(formatted); builder.append(" "); @@ -75,12 +66,12 @@ public class CustomItemManager { } enchantmentList.add(builder + " " + item.getEnchantmentLevel(enchantment)); } - lore.add(ChatColor.BLUE + String.join(", ", enchantmentList)); - lore.add(""); - lore.addAll(itemAbility); - lore.add(""); - lore.add(Rarity.getRarity((String) weapon.get("Rarity")).getDisplay()); - itemMeta.setLore(lore); + 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); @@ -121,7 +112,7 @@ public class CustomItemManager { } public static YamlConfiguration loadConfig(String path) { - File f = new File(main.getDataFolder(), path); + File f = new File(Main.getPlugin(Main.class).getDataFolder(), path); if (!f.exists()) { try { f.createNewFile(); @@ -133,13 +124,10 @@ public class CustomItemManager { return YamlConfiguration.loadConfiguration(f); } - public static ItemStack getItem(String name) { - return weapons.get(name); - } public static void updateYamlFilesToPlugin(String path) { - File file = new File(main.getDataFolder(), path); - if (!file.exists()) main.saveResource(path, true); - else main.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); } } diff --git a/src/main/resources/customEnchants.yml b/src/main/resources/customEnchants.yml deleted file mode 100644 index e69de29..0000000