From ae4ad43506aa321588e1b5aaacb117de90f09f1e Mon Sep 17 00:00:00 2001 From: Ryder Belserion Date: Sat, 27 Apr 2024 21:14:37 -0400 Subject: [PATCH] Add support for PlaceholderAPI in key displayname/lores This also allows you to use %keys% or %crate_opened% which are some of our internal placeholders. A bug is fixed as well where keys didn't have lores. --- paper/run/server.properties | 2 +- .../crazycrates/api/builders/ItemBuilder.java | 44 ++++--------------- .../crazycrates/api/objects/Crate.java | 16 ++++--- .../crazycrates/tasks/BukkitUserManager.java | 22 ++++++++++ .../tasks/crates/CrateManager.java | 4 +- 5 files changed, 43 insertions(+), 45 deletions(-) diff --git a/paper/run/server.properties b/paper/run/server.properties index cddba1a50..7974fabd7 100644 --- a/paper/run/server.properties +++ b/paper/run/server.properties @@ -1,5 +1,5 @@ #Minecraft server properties -#Sat Apr 27 14:28:38 EDT 2024 +#Sat Apr 27 21:09:45 EDT 2024 enable-jmx-monitoring=false level-seed= rcon.port=25575 diff --git a/paper/src/main/java/com/badbones69/crazycrates/api/builders/ItemBuilder.java b/paper/src/main/java/com/badbones69/crazycrates/api/builders/ItemBuilder.java index 44c9d8b37..6b56c6cd7 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/api/builders/ItemBuilder.java +++ b/paper/src/main/java/com/badbones69/crazycrates/api/builders/ItemBuilder.java @@ -67,7 +67,6 @@ public class ItemBuilder { // Display private String displayName = ""; private List displayLore = new ArrayList<>(); - private List componentLore = new ArrayList<>(); private int itemDamage = 0; // Model Data @@ -473,6 +472,13 @@ public ItemBuilder setTarget(Player target) { return this; } + /** + * @return the player object. + */ + public Player getTarget() { + return this.target; + } + /** * Get the item's name with all the placeholders added to it. * @@ -755,22 +761,6 @@ public ItemBuilder setLore(List lore) { return this; } - /** - * Set the lore of the item in the builder. This will auto force color in all the lores that contains color code. (, , , etc...) - * - * @param lore the lore of the item in the builder. - * @return the ItemBuilder with updated info. - */ - public ItemBuilder setComponentLore(List lore) { - if (lore != null) { - this.componentLore.clear(); - - lore.forEach(this::addLore); - } - - return this; - } - /** * Set the lore of the item with papi support in the builder. This will auto force color in all the lores that contains color code. (, , , etc...) * @@ -802,18 +792,6 @@ public ItemBuilder addLore(String line) { return this; } - /** - * Add a line to the current lore of the item. This will auto force color in the lore that contains color code. (, , , etc...) - * - * @param line the new line you wish to add. - * @return the ItemBuilder with updated info. - */ - public ItemBuilder addLore(Component line) { - if (line != null) this.componentLore.add(line); - - return this; - } - /** * Set the placeholders that are in the lore of the item. * @@ -845,10 +823,6 @@ public ItemBuilder addLorePlaceholder(String placeholder, String argument) { * @return the lore with all placeholders in it. */ public List getUpdatedLore() { - if (!this.componentLore.isEmpty()) { - return this.componentLore; - } - List newLore = new ArrayList<>(); for (String item : this.displayLore) { @@ -1111,11 +1085,11 @@ public ItemBuilder setGlow(boolean glow) { * @return the ItemStack as an ItemBuilder with all the info from the item. */ public static ItemBuilder convertItemStack(ItemStack item) { - return new ItemBuilder(item).setComponentLore(item.lore()).setAmount(item.getAmount()).addEnchantments(new HashMap<>(item.getEnchantments()), true); + return new ItemBuilder(item).setAmount(item.getAmount()).addEnchantments(new HashMap<>(item.getEnchantments()), true); } public static ItemBuilder convertItemStack(ItemStack item, Player player) { - return new ItemBuilder(item).setTarget(player).setComponentLore(item.lore()).setAmount(item.getAmount()).addEnchantments(new HashMap<>(item.getEnchantments()), true); + return new ItemBuilder(item).setTarget(player).setAmount(item.getAmount()).addEnchantments(new HashMap<>(item.getEnchantments()), true); } /** diff --git a/paper/src/main/java/com/badbones69/crazycrates/api/objects/Crate.java b/paper/src/main/java/com/badbones69/crazycrates/api/objects/Crate.java index b3eea42a3..df75d5d8e 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/api/objects/Crate.java +++ b/paper/src/main/java/com/badbones69/crazycrates/api/objects/Crate.java @@ -3,6 +3,8 @@ import com.badbones69.crazycrates.api.builders.ItemBuilder; import com.badbones69.crazycrates.api.builders.types.CrateTierMenu; import com.badbones69.crazycrates.api.enums.PersistentKeys; +import com.badbones69.crazycrates.tasks.BukkitUserManager; +import com.badbones69.crazycrates.tasks.crates.CrateManager; import com.badbones69.crazycrates.tasks.crates.effects.SoundEffect; import com.ryderbelserion.vital.files.FileManager; import com.ryderbelserion.vital.util.DyeUtil; @@ -78,6 +80,8 @@ public class Crate { private final @NotNull CrazyCrates plugin = JavaPlugin.getPlugin(CrazyCrates.class); + private final @NotNull BukkitUserManager userManager = this.plugin.getUserManager(); + private final @NotNull InventoryManager inventoryManager = this.plugin.getInventoryManager(); private final @NotNull FileManager fileManager = this.plugin.getFileManager(); @@ -95,9 +99,9 @@ public class Crate { * @param prizes The prizes that can be won. * @param file The crate file. */ - public Crate(String name, String previewName, CrateType crateType, ItemStack key, String keyName, List prizes, FileConfiguration file, int newPlayerKeys, List tiers, int maxMassOpen, int requiredKeys, List prizeMessage, List prizeCommands, CrateHologram hologram) { - this.emptyKey = ItemBuilder.convertItemStack(key).setName(keyName); - this.keyBuilder = ItemBuilder.convertItemStack(key).setName(keyName).setCrateName(name); + public Crate(String name, String previewName, CrateType crateType, ItemBuilder key, String keyName, List prizes, FileConfiguration file, int newPlayerKeys, List tiers, int maxMassOpen, int requiredKeys, List prizeMessage, List prizeCommands, CrateHologram hologram) { + this.emptyKey = key.setName(keyName); + this.keyBuilder = key.setName(keyName).setCrateName(name); this.keyName = keyName; this.file = file; @@ -499,7 +503,7 @@ public ItemStack getKey() { * @return the key as an item stack. */ public ItemStack getKey(Player player) { - return this.keyBuilder.setTarget(player).build(); + return this.userManager.addPlaceholders(this.keyBuilder.setTarget(player), this).build(); } /** @@ -519,9 +523,7 @@ public ItemStack getKey(int amount) { * @return the key as an item stack. */ public ItemStack getKey(int amount, Player player) { - ItemBuilder key = this.keyBuilder.setTarget(player).setAmount(amount); - - return key.build(); + return this.userManager.addPlaceholders(this.keyBuilder.setTarget(player).setAmount(amount), this).build(); } /** diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/BukkitUserManager.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/BukkitUserManager.java index 833f0c29d..f0c04e597 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/BukkitUserManager.java +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/BukkitUserManager.java @@ -1,6 +1,7 @@ package com.badbones69.crazycrates.tasks; import ch.jalu.configme.SettingsManager; +import com.badbones69.crazycrates.api.builders.ItemBuilder; import com.badbones69.crazycrates.api.utils.ItemUtils; import com.badbones69.crazycrates.tasks.crates.CrateManager; import org.bukkit.plugin.java.JavaPlugin; @@ -21,6 +22,8 @@ import us.crazycrew.crazycrates.api.users.UserManager; import com.badbones69.crazycrates.api.enums.Messages; import com.badbones69.crazycrates.api.utils.MiscUtils; + +import java.text.NumberFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -617,4 +620,23 @@ public void addOpenedCrate(UUID uuid, String crateName) { private boolean isCrateInvalid(String crateName) { return crateName.isBlank() || this.crateManager.getCrateFromName(crateName) == null; } + + /** + * Adds internal placeholders to the itembuilder. + * + * @param itemBuilder the itembuilder + * @param crate the crate + * @return the itembuilder + */ + public ItemBuilder addPlaceholders(ItemBuilder itemBuilder, Crate crate) { + UUID uuid = itemBuilder.getTarget().getUniqueId(); + String name = crate.getName(); + + itemBuilder.addLorePlaceholder("%keys%", NumberFormat.getNumberInstance().format(getVirtualKeys(uuid, name))) + .addLorePlaceholder("%keys_physical%", NumberFormat.getNumberInstance().format(getPhysicalKeys(uuid, name))) + .addLorePlaceholder("%keys_total%", NumberFormat.getNumberInstance().format(getTotalKeys(uuid, name))) + .addLorePlaceholder("%crate_opened%", NumberFormat.getNumberInstance().format(getCrateOpened(uuid, name))); + + return itemBuilder; + } } \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java index d9554a457..d2bfc5f8a 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java @@ -1228,14 +1228,14 @@ public List getCrateSchematics() { } // Internal methods. - private ItemStack getKey(FileConfiguration file) { + private ItemBuilder getKey(FileConfiguration file) { String name = file.getString("Crate.PhysicalKey.Name"); List lore = file.getStringList("Crate.PhysicalKey.Lore"); String id = file.getString("Crate.PhysicalKey.Item", "TRIPWIRE_HOOK"); boolean glowing = file.getBoolean("Crate.PhysicalKey.Glowing", true); boolean hideFlags = file.getBoolean("Crate.PhysicalKey.HideItemFlags", false); - return new ItemBuilder().setMaterial(id).setName(name).setLore(lore).setGlow(glowing).hideItemFlags(hideFlags).build(); + return new ItemBuilder().setMaterial(id).setName(name).setLore(lore).setGlow(glowing).hideItemFlags(hideFlags); } // Cleans the data file.