Skip to content

Commit

Permalink
Add support for PlaceholderAPI in key displayname/lores
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ryderbelserion committed Apr 28, 2024
1 parent 7d20f7a commit ae4ad43
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 45 deletions.
2 changes: 1 addition & 1 deletion paper/run/server.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public class ItemBuilder {
// Display
private String displayName = "";
private List<String> displayLore = new ArrayList<>();
private List<Component> componentLore = new ArrayList<>();
private int itemDamage = 0;

// Model Data
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -755,22 +761,6 @@ public ItemBuilder setLore(List<String> 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. (<green>, <red>, <gray>, etc...)
*
* @param lore the lore of the item in the builder.
* @return the ItemBuilder with updated info.
*/
public ItemBuilder setComponentLore(List<Component> 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. (<green>, <red>, <gray>, etc...)
*
Expand Down Expand Up @@ -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. (<green>, <red>, <gray>, 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.
*
Expand Down Expand Up @@ -845,10 +823,6 @@ public ItemBuilder addLorePlaceholder(String placeholder, String argument) {
* @return the lore with all placeholders in it.
*/
public List<Component> getUpdatedLore() {
if (!this.componentLore.isEmpty()) {
return this.componentLore;
}

List<Component> newLore = new ArrayList<>();

for (String item : this.displayLore) {
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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<Prize> prizes, FileConfiguration file, int newPlayerKeys, List<Tier> tiers, int maxMassOpen, int requiredKeys, List<String> prizeMessage, List<String> 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<Prize> prizes, FileConfiguration file, int newPlayerKeys, List<Tier> tiers, int maxMassOpen, int requiredKeys, List<String> prizeMessage, List<String> prizeCommands, CrateHologram hologram) {
this.emptyKey = key.setName(keyName);
this.keyBuilder = key.setName(keyName).setCrateName(name);
this.keyName = keyName;

this.file = file;
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1228,14 +1228,14 @@ public List<CrateSchematic> getCrateSchematics() {
}

// Internal methods.
private ItemStack getKey(FileConfiguration file) {
private ItemBuilder getKey(FileConfiguration file) {
String name = file.getString("Crate.PhysicalKey.Name");
List<String> 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.
Expand Down

0 comments on commit ae4ad43

Please sign in to comment.