From 5a1643c59d386e3b46de4985344e0f6c8c8a0c29 Mon Sep 17 00:00:00 2001 From: Brianna Date: Fri, 17 Jul 2020 20:47:29 -0500 Subject: [PATCH] Moved kit keys to nbt. --- .../songoda/ultimatekits/UltimateKits.java | 4 +- .../com/songoda/ultimatekits/key/Key.java | 56 +++++++++++-------- .../com/songoda/ultimatekits/kit/Kit.java | 28 +++++----- .../ultimatekits/settings/Settings.java | 2 + src/main/resources/en_US.lang | 29 ++++------ 5 files changed, 65 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/songoda/ultimatekits/UltimateKits.java b/src/main/java/com/songoda/ultimatekits/UltimateKits.java index 9a21c44..52b1e43 100644 --- a/src/main/java/com/songoda/ultimatekits/UltimateKits.java +++ b/src/main/java/com/songoda/ultimatekits/UltimateKits.java @@ -301,8 +301,9 @@ public class UltimateKits extends SongodaPlugin { for (String keyName : keyFile.getConfigurationSection("Keys").getKeys(false)) { int amt = keyFile.getInt("Keys." + keyName + ".Item Amount"); int kitAmount = keyFile.getInt("Keys." + keyName + ".Amount of kit received"); + boolean enchanted = keyFile.getBoolean("Keys." + keyName + ".Enchanted"); - Key key = new Key(keyName, amt, kitAmount); + Key key = new Key(keyName, amt, kitAmount, enchanted); keyManager.addKey(key); } } @@ -507,6 +508,7 @@ public class UltimateKits extends SongodaPlugin { keyFile.set("Keys.Ultra.Amount of kit received", 1); keyFile.set("Keys.Insane.Item Amount", -1); keyFile.set("Keys.Insane.Amount of kit received", 2); + keyFile.set("Keys.Insane.Enchanted", true); } private void checkCrateDefaults() { diff --git a/src/main/java/com/songoda/ultimatekits/key/Key.java b/src/main/java/com/songoda/ultimatekits/key/Key.java index 859e6f3..b538148 100644 --- a/src/main/java/com/songoda/ultimatekits/key/Key.java +++ b/src/main/java/com/songoda/ultimatekits/key/Key.java @@ -1,57 +1,62 @@ package com.songoda.ultimatekits.key; +import com.songoda.core.nms.NmsManager; +import com.songoda.core.nms.nbt.NBTItem; +import com.songoda.core.utils.ItemUtils; import com.songoda.core.utils.TextUtils; import com.songoda.ultimatekits.UltimateKits; import com.songoda.ultimatekits.kit.Kit; -import org.bukkit.Material; -import org.bukkit.enchantments.Enchantment; +import com.songoda.ultimatekits.settings.Settings; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; import java.util.List; -import org.bukkit.ChatColor; public class Key { // The name of the key. - private String name; + private final String name; // The amount of items this key will give you. -1 is all; - private int amount; + private final int amount; + + // Should the key be enchanted? + private final boolean enchanted; // The amount of kit given when the key is used. - private int kitAmount; + private final int kitAmount; - public Key(String name, int amount, int kitAmount) { + public Key(String name, int amount, int kitAmount, boolean enchanted) { this.name = name; this.amount = amount; this.kitAmount = kitAmount; + this.enchanted = enchanted; } - - public ItemStack getKeyItem(Kit kit, int amt) { + public ItemStack getKeyItem(Kit kit, int amount) { UltimateKits plugin = UltimateKits.getInstance(); - ItemStack is = new ItemStack(Material.TRIPWIRE_HOOK, amt); + ItemStack item = Settings.KEY_MATERIAL.getMaterial().getItem(); + item.setAmount(amount); - String kitName; - if (kit != null) - kitName = TextUtils.formatText(kit.getName(), true); - else - kitName = "Any"; + String kitName = kit != null ? TextUtils.formatText(kit.getName(), true) + : plugin.getLocale().getMessage("general.type.any").getMessage(); - ItemMeta meta = is.getItemMeta(); + ItemMeta meta = item.getItemMeta(); meta.setDisplayName(plugin.getLocale().getMessage("interface.key.title") .processPlaceholder("kit", kitName).getMessage()); - meta.addEnchant(Enchantment.DURABILITY, 1, true); + if (enchanted) + ItemUtils.addGlow(item); + List lore = new ArrayList<>(); - lore.add(ChatColor.YELLOW + name + " " + ChatColor.WHITE + "Key"); + lore.add(plugin.getLocale().getMessage("interface.key.name") + .processPlaceholder("name", name).getMessage()); String desc1 = plugin.getLocale().getMessage("interface.key.description1") .processPlaceholder("kit", kitName).getMessage(); - if (kitName.equals("Any")) + if (kit == null) desc1 = desc1.replaceAll("\\[.*?\\]", ""); else desc1 = desc1.replace("[", "").replace("]", ""); @@ -66,12 +71,15 @@ public class Key { .processPlaceholder("amt", this.kitAmount).getMessage()); meta.setLore(lore); - is.setItemMeta(meta); + item.setItemMeta(meta); - return is; + NBTItem nbtItem = NmsManager.getNbt().of(item); + nbtItem.set("key", name); + nbtItem.set("kit", kit == null ? "ANY" : kit.getName()); + + return nbtItem.finish(); } - public String getName() { return name; } @@ -83,4 +91,8 @@ public class Key { public int getKitAmount() { return kitAmount; } + + public boolean isEnchanted() { + return enchanted; + } } diff --git a/src/main/java/com/songoda/ultimatekits/kit/Kit.java b/src/main/java/com/songoda/ultimatekits/kit/Kit.java index 157b449..36c8e08 100644 --- a/src/main/java/com/songoda/ultimatekits/kit/Kit.java +++ b/src/main/java/com/songoda/ultimatekits/kit/Kit.java @@ -7,6 +7,8 @@ import com.songoda.core.configuration.Config; import com.songoda.core.gui.Gui; import com.songoda.core.gui.GuiManager; import com.songoda.core.hooks.EconomyManager; +import com.songoda.core.nms.NmsManager; +import com.songoda.core.nms.nbt.NBTItem; import com.songoda.core.utils.ItemUtils; import com.songoda.core.utils.TextUtils; import com.songoda.ultimatekits.UltimateKits; @@ -99,27 +101,25 @@ public class Kit { public void processKeyUse(Player player) { ItemStack item = player.getItemInHand(); - if (item.getType() != Material.TRIPWIRE_HOOK || !item.hasItemMeta()) { + NBTItem nbtItem = NmsManager.getNbt().of(item); + + if (!nbtItem.has("key") || !nbtItem.has("kit")) return; - } - Key key = plugin.getKeyManager().getKey(ChatColor.stripColor(item.getItemMeta().getLore().get(0)).replace(" Key", "")); - // This is some legacy support crap. - String title = plugin.getLocale().getMessage("interface.key.title") - .processPlaceholder("kit", name).getMessage(); - if (title.startsWith(ChatColor.COLOR_CHAR + "f")) - title = title.substring(2); + String keyName = nbtItem.getNBTObject("key").asString(); + String kitName = nbtItem.getNBTObject("kit").asString(); - String titleAny = plugin.getLocale().getMessage("interface.key.title") - .processPlaceholder("kit", "Any").getMessage(); - if (titleAny.startsWith(ChatColor.COLOR_CHAR + "f")) - titleAny = titleAny.substring(2); + boolean any = kitName.equals("ANY"); + Key key = plugin.getKeyManager().getKey(keyName); - if (!item.getItemMeta().getDisplayName().equals(title) - && !item.getItemMeta().getDisplayName().equals(titleAny)) { + if (key == null && !any) + return; + + if (!any && !kitName.equals(name)) { plugin.getLocale().getMessage("event.crate.wrongkey").sendPrefixedMessage(player); return; } + if (giveKit(player, key)) { plugin.getLocale().getMessage("event.key.success") .processPlaceholder("kit", name).sendPrefixedMessage(player); diff --git a/src/main/java/com/songoda/ultimatekits/settings/Settings.java b/src/main/java/com/songoda/ultimatekits/settings/Settings.java index 1a5785a..8b8346f 100644 --- a/src/main/java/com/songoda/ultimatekits/settings/Settings.java +++ b/src/main/java/com/songoda/ultimatekits/settings/Settings.java @@ -22,6 +22,8 @@ public class Settings { public static final ConfigSetting CHANCE_IN_PREVIEW = new ConfigSetting(config, "Main.Display Chance In Preview", true); public static final ConfigSetting CURRENCY_SYMBOL = new ConfigSetting(config, "Main.Currency Symbol", "$"); public static final ConfigSetting STARTER_KIT = new ConfigSetting(config, "Main.Starter Kit", "none"); + public static final ConfigSetting KEY_MATERIAL = new ConfigSetting(config, "Main.Key Material", "TRIPWIRE_HOOK", + "What type of material should be used for kit keys?"); public static final ConfigSetting ECONOMY_PLUGIN = new ConfigSetting(config, "Main.Economy", EconomyManager.getEconomy() == null ? "Vault" : EconomyManager.getEconomy().getName(), "Which economy plugin should be used?", diff --git a/src/main/resources/en_US.lang b/src/main/resources/en_US.lang index b355918..e82214b 100644 --- a/src/main/resources/en_US.lang +++ b/src/main/resources/en_US.lang @@ -10,9 +10,8 @@ general: money: '&6Money' link: '&9Link' free: Free - + any: Any # Interface Messages - interface: selector: kit: '&c%kit%' @@ -51,19 +50,19 @@ interface: 'no': '&c&lNo' key: title: '&5%kit% &fKit Key' - description1: '&rRight-Click on [a ]&c&l%kit%&r kit' - description2: '&rand receive its contents!' - description3: '&rand receive some of its contents!' - description4: '&rGives kit &c&l%amt% &rtimes.' + name: '&e%name% &fKey' + description1: '&fRight-Click on [a ]&c&l%kit%&f kit' + description2: '&fand receive its contents!' + description3: '&fand receive some of its contents!' + description4: '&fGives kit &c&l%amt% &ftimes.' crate: title: '&5%kit% &f%crate% Crate' - description1: '&rRight-Click to open' - description2: '&rand receive its contents!' - description3: '&rand receive some of its contents!' - description4: '&rLeft-Click to preview' - -# Administrative interfaces + description1: '&fRight-Click to open' + description2: '&fand receive its contents!' + description3: '&fand receive some of its contents!' + description4: '&fLeft-Click to preview' + # Administrative interfaces kitblock: title: '&8This contains &a%kit%' switchtype: '&5&lSwitch kit type' @@ -164,9 +163,7 @@ interface: switchtokitfunctionslore: '&7Click to switch back|&7to the kit functions.' itemfunctionlore: '&7Display Item: &6%item%|&7Display Name: &6%name%|&7Display Lore: &6%lore%||&7Left-Click: &6To set a display item.|&7Middle-Click: &6To set a display name.|&7Right-Click: &6To set display lore.|&7Shift-Click: &6To set chance.||&7Display options only show up on display.|&7This can be useful if you want to explain|&7What an item does without putting it in the|&7permanent lore.||&6Leave function mode to move items.' saved: '&8Changes to &a%kit% &8saved successfully.' - # Command Messages - command: general: noperms: '&cYou do not have permission to do that!' @@ -179,9 +176,7 @@ command: crate: given: '&9Gave &7%player% &9crate &7%crate% &9for kit &7%kit%.' doesntexist: '&cThis crate is not loaded.' - # Event Messages - event: preview: kit: '&9You are now previewing kit &7%kit%&9.' @@ -208,4 +203,4 @@ event: cancelled: '&cPurchase Canceled.' key: given: '&9You have received a &a%kit% &9kit key.' - success: '&9You have successfully redeemed a key for the kit &7%kit%&9.' \ No newline at end of file + success: '&9You have successfully redeemed a key for the kit &7%kit%&9.'