Added description placeholder support

This commit is contained in:
Auxilor 2021-08-04 18:20:24 +01:00
parent a865a305ae
commit df6d724950
3 changed files with 126 additions and 18 deletions

View File

@ -139,7 +139,7 @@ public class EnchantDisplay extends DisplayModule {
lore.add(Display.PREFIX + name); lore.add(Display.PREFIX + name);
if (!options.getDescriptionOptions().isShowingAtBottom()) { if (!options.getDescriptionOptions().isShowingAtBottom()) {
if (enchantments.size() <= options.getDescriptionOptions().getThreshold() && options.getDescriptionOptions().isEnabled()) { if (enchantments.size() <= options.getDescriptionOptions().getThreshold() && options.getDescriptionOptions().isEnabled()) {
lore.addAll(EnchantmentCache.getEntry(enchantment).getDescription()); lore.addAll(EnchantmentCache.getEntry(enchantment).getDescription(level));
} }
} }
}); });
@ -163,8 +163,8 @@ public class EnchantDisplay extends DisplayModule {
if (options.getDescriptionOptions().isShowingAtBottom()) { if (options.getDescriptionOptions().isShowingAtBottom()) {
if (enchantments.size() <= options.getDescriptionOptions().getThreshold() && options.getDescriptionOptions().isEnabled()) { if (enchantments.size() <= options.getDescriptionOptions().getThreshold() && options.getDescriptionOptions().isEnabled()) {
for (Enchantment enchantment : enchantments.keySet()) { for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
lore.addAll(EnchantmentCache.getEntry(enchantment).getDescription()); lore.addAll(EnchantmentCache.getEntry(entry.getKey()).getDescription(entry.getValue()));
} }
} }
} }

View File

@ -18,6 +18,7 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentWrapper; import org.bukkit.enchantments.EnchantmentWrapper;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -88,7 +89,7 @@ public class EnchantmentCache {
enchantment, enchantment,
"&4INVALID ENCHANTMENT", "&4INVALID ENCHANTMENT",
"INVALID", "INVALID",
Collections.singletonList(Display.PREFIX + "INVALID ENCHANTMENT: " + enchantment.getClass().getName()), new HashMap<>(Map.of(1, Collections.singletonList(Display.PREFIX + "INVALID ENCHANTMENT: " + enchantment.getClass().getName()))),
EnchantmentType.NORMAL, EnchantmentType.NORMAL,
EnchantmentRarity.getByName(PLUGIN.getConfigYml().getString("rarity.vanilla-rarity")) EnchantmentRarity.getByName(PLUGIN.getConfigYml().getString("rarity.vanilla-rarity"))
)); ));
@ -143,10 +144,32 @@ public class EnchantmentCache {
name = StringUtils.format(name); name = StringUtils.format(name);
description.replaceAll(line -> Display.PREFIX + PLUGIN.getDisplayModule().getOptions().getDescriptionOptions().getColor() + line); description.replaceAll(line -> Display.PREFIX + PLUGIN.getDisplayModule().getOptions().getDescriptionOptions().getColor() + line);
CACHE.put(enchantment.getKey(), new CacheEntry(enchantment, name, rawName, description, type, rarity));
Map<Integer, List<String>> levelDescription = new HashMap<>();
if (enchantment instanceof EcoEnchant ecoEnchant) {
for (int i = 1; i <= ecoEnchant.getMaxLevel(); i++) {
List<String> levelDesc = new ArrayList<>();
for (String s : ecoEnchant.getWrappedDescription()) {
levelDesc.add(s.replace("%value%", ecoEnchant.getPlaceholder(i)));
}
levelDescription.put(
i,
levelDesc
);
}
} else {
for (int i = 1; i <= enchantment.getMaxLevel(); i++) {
levelDescription.put(i, description);
}
}
CACHE.put(enchantment.getKey(), new CacheEntry(enchantment, name, rawName, levelDescription, type, rarity));
} }
@ToString @ToString
@SuppressWarnings("DeprecatedIsStillUsed")
public static final class CacheEntry { public static final class CacheEntry {
/** /**
* The enchantment that this cache is for. * The enchantment that this cache is for.
@ -169,14 +192,12 @@ public class EnchantmentCache {
/** /**
* The description, line-wrapped. * The description, line-wrapped.
*/ */
@Getter private final Map<Integer, List<String>> description;
private final List<String> description;
/** /**
* The description, not line-wrapped or colorized. * The description, not line-wrapped or colorized.
*/ */
@Getter private final Map<Integer, String> stringDescription;
private final String stringDescription;
/** /**
* The type of the enchantment. * The type of the enchantment.
@ -193,7 +214,7 @@ public class EnchantmentCache {
private CacheEntry(@NotNull final Enchantment enchantment, private CacheEntry(@NotNull final Enchantment enchantment,
@NotNull final String name, @NotNull final String name,
@NotNull final String rawName, @NotNull final String rawName,
@NotNull final List<String> description, @NotNull final Map<Integer, List<String>> description,
@NotNull final EnchantmentType type, @NotNull final EnchantmentType type,
@NotNull final EnchantmentRarity rarity) { @NotNull final EnchantmentRarity rarity) {
this.enchantment = enchantment; this.enchantment = enchantment;
@ -202,17 +223,94 @@ public class EnchantmentCache {
this.description = description; this.description = description;
this.type = type; this.type = type;
this.rarity = rarity; this.rarity = rarity;
this.stringDescription = new HashMap<>();
StringBuilder descriptionBuilder = new StringBuilder(); for (Integer level : description.keySet()) {
StringBuilder descriptionBuilder = new StringBuilder();
description.forEach(s -> { for (String s : description.get(level)) {
descriptionBuilder.append(s); descriptionBuilder.append(s);
descriptionBuilder.append(" "); descriptionBuilder.append(" ");
}); }
String processedStringDescription = descriptionBuilder.toString(); String processedStringDescription = descriptionBuilder.toString();
processedStringDescription = processedStringDescription.replace(Display.PREFIX, ""); processedStringDescription = processedStringDescription.replace(Display.PREFIX, "");
this.stringDescription = processedStringDescription.replaceAll(PLUGIN.getDisplayModule().getOptions().getDescriptionOptions().getColor(), ""); stringDescription.put(level, processedStringDescription.replaceAll(PLUGIN.getDisplayModule().getOptions().getDescriptionOptions().getColor(), ""));
}
}
/**
* Get the description of an enchantment at a certain level.
*
* @param level The level.
* @return The description, wrapped and formatted.
*/
public List<String> getDescription(final int level) {
List<String> description = this.description.get(level);
if (description == null) {
if (enchantment instanceof EcoEnchant enchant) {
List<String> levelDesc = new ArrayList<>();
for (String s : enchant.getWrappedDescription()) {
levelDesc.add(s.replace("%value%", enchant.getPlaceholder(level)));
}
this.description.put(
level,
levelDesc
);
} else {
this.description.put(level, this.description.get(0));
}
return getDescription(level);
} else {
return description;
}
}
/**
* Get the description of an enchantment at a certain level.
*
* @param level The level.
* @return The description, unwrapped and unformatted.
*/
public String getStringDescription(final int level) {
String stringDesc = this.stringDescription.get(level);
if (stringDesc == null) {
StringBuilder descriptionBuilder = new StringBuilder();
for (String s : description.get(level)) {
descriptionBuilder.append(s);
descriptionBuilder.append(" ");
}
String processedStringDescription = descriptionBuilder.toString();
processedStringDescription = processedStringDescription.replace(Display.PREFIX, "");
stringDescription.put(level, processedStringDescription.replaceAll(PLUGIN.getDisplayModule().getOptions().getDescriptionOptions().getColor(), ""));
return getStringDescription(level);
} else {
return stringDesc;
}
}
/**
* Get the description of an enchantment at level 1.
*
* @return The description, wrapped and formatted.
*/
@Deprecated
public List<String> getDescription() {
return getDescription(1);
}
/**
* Get the description of an enchantment at level 1.
*
* @return The description, unwrapped and unformatted.
*/
@Deprecated
public String getStringDescription() {
return getStringDescription(1);
} }
} }
} }

View File

@ -238,6 +238,16 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Watche
EnchantmentUtils.register(this); EnchantmentUtils.register(this);
} }
/**
* Format the %value% placeholder in description lore.
*
* @param level The level of the enchantment.
* @return The placeholder.
*/
public String getPlaceholder(final int level) {
return "unknown";
}
/** /**
* Get description of enchantment line-wrapped. * Get description of enchantment line-wrapped.
* *