Tier placeholders fix

This commit is contained in:
Roch Blonndiaux 2022-12-07 15:48:34 +01:00
parent 192804f504
commit 5e55049369
2 changed files with 243 additions and 212 deletions

View File

@ -18,7 +18,9 @@ import java.util.List;
import java.util.Random; import java.util.Random;
public class ItemTier { public class ItemTier {
private final String name, id; private final String id;
private final String name;
private final String unparsedName;
private final UnidentificationInfo unidentificationInfo; private final UnidentificationInfo unidentificationInfo;
// Deconstruction // Deconstruction
@ -44,10 +46,10 @@ public class ItemTier {
* @param config Configuration section to get all values from * @param config Configuration section to get all values from
*/ */
public ItemTier(@NotNull ConfigurationSection config) { public ItemTier(@NotNull ConfigurationSection config) {
// The name and ID, crucial parts. // The name and ID, crucial parts.
id = config.getName().toUpperCase().replace("-", "_"); this.id = config.getName().toUpperCase().replace("-", "_");
name = MythicLib.plugin.parseColors(config.getString("name")); this.unparsedName = config.getString("name");
this.name = MythicLib.plugin.parseColors(unparsedName);
// Deconstruct and Unidentification // Deconstruct and Unidentification
deconstructTable = config.contains("deconstruct-item") ? new DropTable(config.getConfigurationSection("deconstruct-item")) : null; deconstructTable = config.contains("deconstruct-item") ? new DropTable(config.getConfigurationSection("deconstruct-item")) : null;
@ -89,8 +91,8 @@ public class ItemTier {
/** /**
* @return Reads the deconstruction drop table. This may return a list * @return Reads the deconstruction drop table. This may return a list
* containing multiple items and they should all be added to the * containing multiple items and they should all be added to the
* player's inventory * player's inventory
*/ */
public List<ItemStack> getDeconstructedLoot(@NotNull PlayerData player) { public List<ItemStack> getDeconstructedLoot(@NotNull PlayerData player) {
//noinspection ConstantConditions //noinspection ConstantConditions
@ -119,7 +121,7 @@ public class ItemTier {
/** /**
* @return If the item tier has a modifier capacity ie if this tier let * @return If the item tier has a modifier capacity ie if this tier let
* generated items have modifiers * generated items have modifiers
*/ */
public boolean hasCapacity() { public boolean hasCapacity() {
return capacity != null; return capacity != null;
@ -127,8 +129,8 @@ public class ItemTier {
/** /**
* @return The formula for modifier capacity which can be then rolled to * @return The formula for modifier capacity which can be then rolled to
* generate a random amount of modifier capacity when generating a * generate a random amount of modifier capacity when generating a
* random item * random item
*/ */
@Nullable @Nullable
public NumericStatFormula getModifierCapacity() { public NumericStatFormula getModifierCapacity() {
@ -140,6 +142,10 @@ public class ItemTier {
return unidentificationInfo; return unidentificationInfo;
} }
public @NotNull String getUnparsedName() {
return unparsedName;
}
public static class UnidentificationInfo { public static class UnidentificationInfo {
@NotNull @NotNull
private final String name, prefix; private final String name, prefix;

View File

@ -1,7 +1,9 @@
package net.Indyuce.mmoitems.stat; package net.Indyuce.mmoitems.stat;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.item.ItemTag; import io.lumine.mythic.lib.api.item.ItemTag;
import io.lumine.mythic.lib.api.item.SupportedNBTTagValues; import io.lumine.mythic.lib.api.item.SupportedNBTTagValues;
import io.lumine.mythic.lib.comp.adventure.AdventureParser;
import io.lumine.mythic.lib.version.VersionMaterial; import io.lumine.mythic.lib.version.VersionMaterial;
import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ItemTier; import net.Indyuce.mmoitems.api.ItemTier;
@ -19,90 +21,104 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
public class DisplayName extends StringStat implements GemStoneStat { public class DisplayName extends StringStat implements GemStoneStat {
private final String[] cleanFilter = {ChatColor.BOLD.toString(), ChatColor.ITALIC.toString(), ChatColor.UNDERLINE.toString(), ChatColor.STRIKETHROUGH.toString(), ChatColor.MAGIC.toString()}; private final String[] cleanFilter = {ChatColor.BOLD.toString(), ChatColor.ITALIC.toString(), ChatColor.UNDERLINE.toString(), ChatColor.STRIKETHROUGH.toString(), ChatColor.MAGIC.toString()};
public DisplayName() { public DisplayName() {
super("NAME", VersionMaterial.OAK_SIGN.toMaterial(), "Display Name", new String[] { "The item display name." }, super("NAME", VersionMaterial.OAK_SIGN.toMaterial(), "Display Name", new String[]{"The item display name."},
new String[] { "all" }); new String[]{"all"});
} }
@Override @Override
public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StringData data) { public void whenApplied(@NotNull ItemStackBuilder item, @NotNull StringData data) {
final AdventureParser parser = MythicLib.plugin.getAdventureParser();
// Bake
String format = data.toString();
// Bake ItemTier tier = item.getMMOItem().getTier();
String format = data.toString(); format = format.replace("<tier-name>", tier != null ? parser.stripColors(tier.getUnparsedName()) : "");
format = format.replace("<tier-color>", tier != null ? parser.lastColor(tier.getUnparsedName(), true) : "&f");
format = format.replace("<tier-color-cleaned>", tier != null ? parser.lastColor(tier.getUnparsedName(), false) : "");
ItemTier tier = item.getMMOItem().getTier(); // Is this upgradable?
format = format.replace("<tier-name>", tier != null ? ChatColor.stripColor(tier.getName()) : ""); format = cropUpgrade(format);
format = format.replace("<tier-color>", tier != null ? ChatColor.getLastColors(tier.getName()) : "&f"); if (item.getMMOItem().hasUpgradeTemplate()) {
if (tier != null) format = appendUpgradeLevel(format, item.getMMOItem().getUpgradeLevel());
for (String filter : cleanFilter) }
if (ChatColor.getLastColors(tier.getName()).contains(filter))
format = format.replace("<tier-color-cleaned>", ChatColor.getLastColors(tier.getName().replace(filter, "")));
// Is this upgradable? item.getMeta().setDisplayName(format);
format = cropUpgrade(format);
if (item.getMMOItem().hasUpgradeTemplate()) { format = appendUpgradeLevel(format, item.getMMOItem().getUpgradeLevel()); }
item.getMeta().setDisplayName(format); // Force Stat History generation
StatHistory.from(item.getMMOItem(), this);
// Force Stat History generation // Add NBT
StatHistory.from(item.getMMOItem(), this); item.addItemTag(getAppliedNBT(data));
}
// Add NBT @NotNull
item.addItemTag(getAppliedNBT(data)); String cropUpgrade(@NotNull String format) {
} String suffix = MMOItems.plugin.getConfig().getString("item-upgrading.name-suffix", " &8(&e+#lvl#&8)");
if (suffix == null || suffix.isEmpty()) {
return format;
}
@NotNull String cropUpgrade(@NotNull String format) { //MMOItems.getConsole().sendMessage("Level " + upgradeLevel);
String suffix = MMOItems.plugin.getConfig().getString("item-upgrading.name-suffix", " &8(&e+#lvl#&8)"); //MMOItems.getConsole().sendMessage("Format " + format);
if (suffix == null || suffix.isEmpty()) { return format; }
//MMOItems.getConsole().sendMessage("Level " + upgradeLevel); if (suffix != null) {
//MMOItems.getConsole().sendMessage("Format " + format);
if (suffix != null) { // Crop lvl
int lvlOFFSET = suffix.indexOf("#lvl#");
if (lvlOFFSET < 0) {
return format;
}
String sB4 = suffix.substring(0, lvlOFFSET);
String aFt = suffix.substring(lvlOFFSET + "#lvl#".length());
String sB4_alt = sB4.replace("+", "-");
String aFt_alt = aFt.replace("+", "-");
// Crop lvl // Remove it
int lvlOFFSET = suffix.indexOf("#lvl#"); if (format.contains(sB4)) {
if (lvlOFFSET < 0) { return format; }
String sB4 = suffix.substring(0, lvlOFFSET);
String aFt = suffix.substring(lvlOFFSET + "#lvl#".length());
String sB4_alt = sB4.replace("+", "-");
String aFt_alt = aFt.replace("+", "-");
// Remove it // Get offsets
if (format.contains(sB4)) { int sB4_offset = format.indexOf(sB4);
int aFt_offset = format.lastIndexOf(aFt);
// Get offsets // No after = to completion
int sB4_offset = format.indexOf(sB4); if (aFt_offset < 0) {
int aFt_offset = format.lastIndexOf(aFt); aFt_offset = format.length();
} else {
aFt_offset += aFt.length();
}
// No after = to completion // Remove that
if (aFt_offset < 0) { aFt_offset = format.length(); } else { aFt_offset += aFt.length(); } String beforePrefix = format.substring(0, sB4_offset);
String afterPrefix = format.substring(aFt_offset);
// Remove that // Replace
String beforePrefix = format.substring(0, sB4_offset); format = beforePrefix + afterPrefix;
String afterPrefix = format.substring(aFt_offset); }
// Replace // Remove it
format = beforePrefix + afterPrefix; } if (format.contains(sB4_alt)) {
// Remove it // Get offsets
if (format.contains(sB4_alt)) { int sB4_offset = format.indexOf(sB4_alt);
int aFt_offset = format.lastIndexOf(aFt_alt);
// Get offsets // No after = to completion
int sB4_offset = format.indexOf(sB4_alt); if (aFt_offset < 0) {
int aFt_offset = format.lastIndexOf(aFt_alt); aFt_offset = format.length();
} else {
aFt_offset += aFt_alt.length();
}
// No after = to completion // Remove that
if (aFt_offset < 0) { aFt_offset = format.length(); } else { aFt_offset += aFt_alt.length(); } String beforePrefix = format.substring(0, sB4_offset);
String afterPrefix = format.substring(aFt_offset);
// Remove that // Replace
String beforePrefix = format.substring(0, sB4_offset); format = beforePrefix + afterPrefix;
String afterPrefix = format.substring(aFt_offset); }
// Replace
format = beforePrefix + afterPrefix; }
/*/ Bake old indices for removal /*/ Bake old indices for removal
ArrayList<String> oldSuffixii = new ArrayList<>(); boolean negativity = false; ArrayList<String> oldSuffixii = new ArrayList<>(); boolean negativity = false;
@ -122,193 +138,202 @@ public class DisplayName extends StringStat implements GemStoneStat {
//MMOItems.getConsole().sendMessage("Edited " + format); //MMOItems.getConsole().sendMessage("Edited " + format);
} //*/ } //*/
//MMOItems.getConsole().sendMessage("Final " + format); //MMOItems.getConsole().sendMessage("Final " + format);
} }
return format; return format;
} }
@NotNull @NotNull
public static String appendUpgradeLevel(@NotNull String format, int lvl) { public static String appendUpgradeLevel(@NotNull String format, int lvl) {
String suffix = MMOItems.plugin.getConfig().getString("item-upgrading.name-suffix"); String suffix = MMOItems.plugin.getConfig().getString("item-upgrading.name-suffix");
if (suffix != null && lvl != 0) { if (suffix != null && lvl != 0) {
String actSuffix = levelPrefix(suffix, lvl); String actSuffix = levelPrefix(suffix, lvl);
return format + actSuffix; return format + actSuffix;
} }
return format; return format;
} }
@NotNull @NotNull
public static String levelPrefix(@NotNull String template, int toLevel) { public static String levelPrefix(@NotNull String template, int toLevel) {
return template return template
.replace("#lvl#", String.valueOf(toLevel)) .replace("#lvl#", String.valueOf(toLevel))
.replace("+-", "-"); .replace("+-", "-");
} }
/** /**
* This is not saved as a custom NBT data, instead it is stored as the name of the item itself. * This is not saved as a custom NBT data, instead it is stored as the name of the item itself.
* Alas this returns an empty list * Alas this returns an empty list
*/ */
@NotNull @NotNull
@Override @Override
public ArrayList<ItemTag> getAppliedNBT(@NotNull StringData data) { public ArrayList<ItemTag> getAppliedNBT(@NotNull StringData data) {
if (data instanceof NameData) { if (data instanceof NameData) {
ArrayList<ItemTag> tags = new ArrayList<>(); ArrayList<ItemTag> tags = new ArrayList<>();
// Append those // Append those
tags.add(new ItemTag(getNBTPath(), ((NameData) data).getMainName())); tags.add(new ItemTag(getNBTPath(), ((NameData) data).getMainName()));
if (((NameData) data).hasPrefixes()) { tags.add(((NameData) data).compressPrefixes(getNBTPath() + "_PRE"));} if (((NameData) data).hasPrefixes()) {
if (((NameData) data).hasSuffixes()) { tags.add(((NameData) data).compressSuffixes(getNBTPath() + "_SUF"));} tags.add(((NameData) data).compressPrefixes(getNBTPath() + "_PRE"));
}
if (((NameData) data).hasSuffixes()) {
tags.add(((NameData) data).compressSuffixes(getNBTPath() + "_SUF"));
}
return tags; return tags;
} }
// Thats it // Thats it
return new ArrayList<>(); return new ArrayList<>();
} }
@Override @Override
public void whenLoaded(@NotNull ReadMMOItem mmoitem) { public void whenLoaded(@NotNull ReadMMOItem mmoitem) {
// Get tags // Get tags
ArrayList<ItemTag> relevantTags = new ArrayList<>(); boolean stored = false; ArrayList<ItemTag> relevantTags = new ArrayList<>();
ItemTag mainName = ItemTag.getTagAtPath(getNBTPath(), mmoitem.getNBT(), SupportedNBTTagValues.STRING); boolean stored = false;
ItemTag mainName = ItemTag.getTagAtPath(getNBTPath(), mmoitem.getNBT(), SupportedNBTTagValues.STRING);
//NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Loading name of \u00a7b" + mmoitem.getType() + " " + mmoitem.getId()); //NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Loading name of \u00a7b" + mmoitem.getType() + " " + mmoitem.getId());
if (mainName != null) { if (mainName != null) {
// Ah yes // Ah yes
ItemTag prefixes = ItemTag.getTagAtPath(getNBTPath() + "_PRE", mmoitem.getNBT(), SupportedNBTTagValues.STRING); ItemTag prefixes = ItemTag.getTagAtPath(getNBTPath() + "_PRE", mmoitem.getNBT(), SupportedNBTTagValues.STRING);
ItemTag suffixes = ItemTag.getTagAtPath(getNBTPath() + "_SUF", mmoitem.getNBT(), SupportedNBTTagValues.STRING); ItemTag suffixes = ItemTag.getTagAtPath(getNBTPath() + "_SUF", mmoitem.getNBT(), SupportedNBTTagValues.STRING);
relevantTags.add(mainName); relevantTags.add(mainName);
relevantTags.add(prefixes); relevantTags.add(prefixes);
relevantTags.add(suffixes); relevantTags.add(suffixes);
// No need to evaluate anvil changes if the item has no display name // No need to evaluate anvil changes if the item has no display name
if (mmoitem.getNBT().getItem().getItemMeta().hasDisplayName()) { stored = true; } if (mmoitem.getNBT().getItem().getItemMeta().hasDisplayName()) {
stored = true;
}
} else { } else {
// No need to continue if the item has no display name // No need to continue if the item has no display name
if (!mmoitem.getNBT().getItem().getItemMeta().hasDisplayName()) { return; } if (!mmoitem.getNBT().getItem().getItemMeta().hasDisplayName()) {
return;
}
//NME//MMOItems.log("\u00a7a\u00a2\u00a2\u00a2\u00a77 Older item, decrypting as main name as:\u00a7f " + cropUpgrade(mmoitem.getNBT().getItem().getItemMeta().getDisplayName())); //NME//MMOItems.log("\u00a7a\u00a2\u00a2\u00a2\u00a77 Older item, decrypting as main name as:\u00a7f " + cropUpgrade(mmoitem.getNBT().getItem().getItemMeta().getDisplayName()));
// Add sole tag // Add sole tag
relevantTags.add(new ItemTag(getNBTPath(), cropUpgrade(mmoitem.getNBT().getItem().getItemMeta().getDisplayName()))); relevantTags.add(new ItemTag(getNBTPath(), cropUpgrade(mmoitem.getNBT().getItem().getItemMeta().getDisplayName())));
} }
// Use that // Use that
NameData bakedData = (NameData) getLoadedNBT(relevantTags); NameData bakedData = (NameData) getLoadedNBT(relevantTags);
// Valid? // Valid?
if (bakedData != null) { if (bakedData != null) {
//NME//MMOItems.log("\u00a7e\u00a2\u00a2\u00a2\u00a77 Built:\u00a7f " + bakedData.toString()); //NME//MMOItems.log("\u00a7e\u00a2\u00a2\u00a2\u00a77 Built:\u00a7f " + bakedData.toString());
/* /*
* Suppose we expect an item name with prefixes and suffixes, * Suppose we expect an item name with prefixes and suffixes,
* well, removing those should leave the bare name, right? * well, removing those should leave the bare name, right?
* *
* If the player has renamed their item, this bare name will be somewhat * If the player has renamed their item, this bare name will be somewhat
* different, and this is where those changes are updated. * different, and this is where those changes are updated.
*/ */
@Nullable String itemName = null; @Nullable String itemName = null;
if (stored) { if (stored) {
// Could the player have renamed? // Could the player have renamed?
itemName = mmoitem.getNBT().getItem().getItemMeta().getDisplayName(); itemName = mmoitem.getNBT().getItem().getItemMeta().getDisplayName();
String colorless = ChatColor.stripColor(itemName); String colorless = ChatColor.stripColor(itemName);
//NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Comparing: " + itemName + " | " + colorless); //NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Comparing: " + itemName + " | " + colorless);
// By player // By player
if (!itemName.equals(colorless)) { if (!itemName.equals(colorless)) {
//NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Not anvil"); //NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Not anvil");
itemName = null; itemName = null;
} else { } else {
//NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Replaced main with \u00a7b " + itemName); //NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Replaced main with \u00a7b " + itemName);
bakedData.setString(itemName); bakedData.setString(itemName);
} }
} }
// Set // Set
mmoitem.setData(this, bakedData); mmoitem.setData(this, bakedData);
// Update in SH. Must happen after setting the data // Update in SH. Must happen after setting the data
if (stored && itemName != null) { if (stored && itemName != null) {
// History not prematurely loaded? // History not prematurely loaded?
if (mmoitem.getStatHistory(this) == null) { if (mmoitem.getStatHistory(this) == null) {
// Also load history :think ing: // Also load history :think ing:
ItemTag hisTag = ItemTag.getTagAtPath(ItemStackBuilder.history_keyword + getId(), mmoitem.getNBT(), SupportedNBTTagValues.STRING); ItemTag hisTag = ItemTag.getTagAtPath(ItemStackBuilder.history_keyword + getId(), mmoitem.getNBT(), SupportedNBTTagValues.STRING);
if (hisTag != null) { if (hisTag != null) {
// Aye // Aye
StatHistory hist = StatHistory.fromNBTString(mmoitem, (String) hisTag.getValue()); StatHistory hist = StatHistory.fromNBTString(mmoitem, (String) hisTag.getValue());
// History valid? Record // History valid? Record
if (hist != null) { if (hist != null) {
// Original Data // Original Data
NameData og = (NameData) hist.getOriginalData(); NameData og = (NameData) hist.getOriginalData();
// Overwrite // Overwrite
og.setString(itemName); og.setString(itemName);
// Load its stat history // Load its stat history
mmoitem.setStatHistory(this, hist); mmoitem.setStatHistory(this, hist);
//NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Name History:"); //NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Name History:");
//NME//hist.log(); //NME//hist.log();
} }
} }
} }
} }
} }
} }
@Nullable @Nullable
@Override @Override
public StringData getLoadedNBT(@NotNull ArrayList<ItemTag> storedTags) { public StringData getLoadedNBT(@NotNull ArrayList<ItemTag> storedTags) {
// You got a double right // You got a double right
ItemTag tg = ItemTag.getTagAtPath(getNBTPath(), storedTags); ItemTag tg = ItemTag.getTagAtPath(getNBTPath(), storedTags);
// Found righ // Found righ
if (tg != null) { if (tg != null) {
// Get number // Get number
String value = (String) tg.getValue(); String value = (String) tg.getValue();
// That's it // That's it
NameData nd = new NameData(value); NameData nd = new NameData(value);
nd.readPrefixes(ItemTag.getTagAtPath(getNBTPath() + "_PRE", storedTags)); nd.readPrefixes(ItemTag.getTagAtPath(getNBTPath() + "_PRE", storedTags));
nd.readSuffixes(ItemTag.getTagAtPath(getNBTPath() + "_SUF", storedTags)); nd.readSuffixes(ItemTag.getTagAtPath(getNBTPath() + "_SUF", storedTags));
return nd; return nd;
} }
// Fail // Fail
return null; return null;
} }
@NotNull @NotNull
@Override @Override
public StringData getClearStatData() { public StringData getClearStatData() {
return new NameData(""); return new NameData("");
} }
@Override @Override
public StringData whenInitialized(Object object) { public StringData whenInitialized(Object object) {
return new NameData(object.toString()); return new NameData(object.toString());
} }
} }