<tier-color-cleaned> fixed

This commit is contained in:
Roch Blonndiaux 2022-11-09 16:43:26 +01:00
parent ecf8fda224
commit 077a4ce0ef
2 changed files with 246 additions and 203 deletions

View File

@ -12,6 +12,7 @@ import net.Indyuce.mmoitems.stat.type.GemStoneStat;
import net.Indyuce.mmoitems.stat.type.NameData; import net.Indyuce.mmoitems.stat.type.NameData;
import net.Indyuce.mmoitems.stat.type.StatHistory; import net.Indyuce.mmoitems.stat.type.StatHistory;
import net.Indyuce.mmoitems.stat.type.StringStat; import net.Indyuce.mmoitems.stat.type.StringStat;
import net.Indyuce.mmoitems.util.ColorUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -19,90 +20,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()};
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 ItemTier tier = item.getMMOItem().getTier();
// Bake // Bake
String format = data.toString(); String format = data.toString()
.replace("<tier-name>", tier != null ? ChatColor.stripColor(tier.getName()) : "")
.replace("<tier-color>", tier != null ? ChatColor.getLastColors(tier.getName()) : "&f")
.replace("<tier-color-cleaned>", tier != null ? ColorUtils.stripDecoration(ChatColor.getLastColors(tier.getName())) : "&f");
ItemTier tier = item.getMMOItem().getTier(); if (tier != null) {
format = format.replace("<tier-name>", tier != null ? ChatColor.stripColor(tier.getName()) : ""); System.out.println("Format: " + format);
format = format.replace("<tier-color>", tier != null ? ChatColor.getLastColors(tier.getName()) : "&f"); System.out.println("Last Colors: " + ChatColor.getLastColors(tier.getName()) + " w: " + ColorUtils.stripDecoration(ChatColor.getLastColors(tier.getName())));
if (tier != null) System.out.println("===================\n");
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? // Is this upgradable?
format = cropUpgrade(format); format = cropUpgrade(format);
if (item.getMMOItem().hasUpgradeTemplate()) { format = appendUpgradeLevel(format, item.getMMOItem().getUpgradeLevel()); } if (item.getMMOItem().hasUpgradeTemplate())
format = appendUpgradeLevel(format, item.getMMOItem().getUpgradeLevel());
item.getMeta().setDisplayName(format); item.getMeta().setDisplayName(format);
// Force Stat History generation // Force Stat History generation
StatHistory.from(item.getMMOItem(), this); StatHistory.from(item.getMMOItem(), this);
// Add NBT // Add NBT
item.addItemTag(getAppliedNBT(data)); item.addItemTag(getAppliedNBT(data));
} }
@NotNull String cropUpgrade(@NotNull String format) { @NotNull
String suffix = MMOItems.plugin.getConfig().getString("item-upgrading.name-suffix", " &8(&e+#lvl#&8)"); String cropUpgrade(@NotNull String format) {
if (suffix == null || suffix.isEmpty()) { return format; } String suffix = MMOItems.plugin.getConfig().getString("item-upgrading.name-suffix", " &8(&e+#lvl#&8)");
if (suffix == null || suffix.isEmpty())
return format;
//MMOItems.getConsole().sendMessage("Level " + upgradeLevel); //MMOItems.getConsole().sendMessage("Level " + upgradeLevel);
//MMOItems.getConsole().sendMessage("Format " + format); //MMOItems.getConsole().sendMessage("Format " + format);
if (suffix != null) {
// Crop lvl // Crop lvl
int lvlOFFSET = suffix.indexOf("#lvl#"); int lvlOFFSET = suffix.indexOf("#lvl#");
if (lvlOFFSET < 0) { return format; } if (lvlOFFSET < 0)
String sB4 = suffix.substring(0, lvlOFFSET); return format;
String aFt = suffix.substring(lvlOFFSET + "#lvl#".length()); String sB4 = suffix.substring(0, lvlOFFSET);
String sB4_alt = sB4.replace("+", "-"); String aFt = suffix.substring(lvlOFFSET + "#lvl#".length());
String aFt_alt = aFt.replace("+", "-"); String sB4_alt = sB4.replace("+", "-");
String aFt_alt = aFt.replace("+", "-");
// Remove it // Remove it
if (format.contains(sB4)) { if (format.contains(sB4)) {
// Get offsets // Get offsets
int sB4_offset = format.indexOf(sB4); int sB4_offset = format.indexOf(sB4);
int aFt_offset = format.lastIndexOf(aFt); int aFt_offset = format.lastIndexOf(aFt);
// No after = to completion // No after = to completion
if (aFt_offset < 0) { aFt_offset = format.length(); } else { aFt_offset += aFt.length(); } if (aFt_offset < 0) {
aFt_offset = format.length();
} else {
aFt_offset += aFt.length();
}
// Remove that // Remove that
String beforePrefix = format.substring(0, sB4_offset); String beforePrefix = format.substring(0, sB4_offset);
String afterPrefix = format.substring(aFt_offset); String afterPrefix = format.substring(aFt_offset);
// Replace // Replace
format = beforePrefix + afterPrefix; } format = beforePrefix + afterPrefix;
}
// Remove it // Remove it
if (format.contains(sB4_alt)) { if (format.contains(sB4_alt)) {
// Get offsets // Get offsets
int sB4_offset = format.indexOf(sB4_alt); int sB4_offset = format.indexOf(sB4_alt);
int aFt_offset = format.lastIndexOf(aFt_alt); int aFt_offset = format.lastIndexOf(aFt_alt);
// No after = to completion // No after = to completion
if (aFt_offset < 0) { aFt_offset = format.length(); } else { aFt_offset += aFt_alt.length(); } if (aFt_offset < 0) {
aFt_offset = format.length();
} else {
aFt_offset += aFt_alt.length();
}
// Remove that // Remove that
String beforePrefix = format.substring(0, sB4_offset); String beforePrefix = format.substring(0, sB4_offset);
String afterPrefix = format.substring(aFt_offset); String afterPrefix = format.substring(aFt_offset);
// Replace // Replace
format = beforePrefix + afterPrefix; } 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 +137,200 @@ 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
} public static String levelPrefix(@NotNull String template, int toLevel) {
return template
.replace("#lvl#", String.valueOf(toLevel))
.replace("+-", "-");
}
@NotNull /**
public static String levelPrefix(@NotNull String template, int toLevel) { * This is not saved as a custom NBT data, instead it is stored as the name of the item itself.
return template * Alas this returns an empty list
.replace("#lvl#", String.valueOf(toLevel)) */
.replace("+-", "-"); @NotNull
} @Override
public ArrayList<ItemTag> getAppliedNBT(@NotNull StringData data) {
/** if (data instanceof NameData) {
* 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
*/
@NotNull
@Override
public ArrayList<ItemTag> getAppliedNBT(@NotNull StringData data) {
if (data instanceof NameData) { ArrayList<ItemTag> tags = new ArrayList<>();
ArrayList<ItemTag> tags = new ArrayList<>(); // Append those
tags.add(new ItemTag(getNBTPath(), ((NameData) data).getMainName()));
if (((NameData) data).hasPrefixes()) {
tags.add(((NameData) data).compressPrefixes(getNBTPath() + "_PRE"));
}
if (((NameData) data).hasSuffixes()) {
tags.add(((NameData) data).compressSuffixes(getNBTPath() + "_SUF"));
}
// Append those return tags;
tags.add(new ItemTag(getNBTPath(), ((NameData) data).getMainName())); }
if (((NameData) data).hasPrefixes()) { tags.add(((NameData) data).compressPrefixes(getNBTPath() + "_PRE"));}
if (((NameData) data).hasSuffixes()) { tags.add(((NameData) data).compressSuffixes(getNBTPath() + "_SUF"));}
return tags; // Thats it
} return new ArrayList<>();
}
// Thats it @Override
return new ArrayList<>(); public void whenLoaded(@NotNull ReadMMOItem mmoitem) {
}
@Override // Get tags
public void whenLoaded(@NotNull ReadMMOItem mmoitem) { ArrayList<ItemTag> relevantTags = new ArrayList<>();
boolean stored = false;
ItemTag mainName = ItemTag.getTagAtPath(getNBTPath(), mmoitem.getNBT(), SupportedNBTTagValues.STRING);
// Get tags //NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Loading name of \u00a7b" + mmoitem.getType() + " " + mmoitem.getId());
ArrayList<ItemTag> relevantTags = new ArrayList<>(); 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()); if (mainName != null) {
if (mainName != null) { // Ah yes
ItemTag prefixes = ItemTag.getTagAtPath(getNBTPath() + "_PRE", mmoitem.getNBT(), SupportedNBTTagValues.STRING);
ItemTag suffixes = ItemTag.getTagAtPath(getNBTPath() + "_SUF", mmoitem.getNBT(), SupportedNBTTagValues.STRING);
relevantTags.add(mainName);
relevantTags.add(prefixes);
relevantTags.add(suffixes);
// Ah yes // No need to evaluate anvil changes if the item has no display name
ItemTag prefixes = ItemTag.getTagAtPath(getNBTPath() + "_PRE", mmoitem.getNBT(), SupportedNBTTagValues.STRING); if (mmoitem.getNBT().getItem().getItemMeta().hasDisplayName()) {
ItemTag suffixes = ItemTag.getTagAtPath(getNBTPath() + "_SUF", mmoitem.getNBT(), SupportedNBTTagValues.STRING); stored = true;
relevantTags.add(mainName); }
relevantTags.add(prefixes);
relevantTags.add(suffixes);
// No need to evaluate anvil changes if the item has no display name } else {
if (mmoitem.getNBT().getItem().getItemMeta().hasDisplayName()) { stored = true; }
} else { // No need to continue if the item has no display name
if (!mmoitem.getNBT().getItem().getItemMeta().hasDisplayName()) {
return;
}
// No need to continue if the item has no display name //NME//MMOItems.log("\u00a7a\u00a2\u00a2\u00a2\u00a77 Older item, decrypting as main name as:\u00a7f " + cropUpgrade(mmoitem.getNBT().getItem().getItemMeta().getDisplayName()));
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())); // Add sole tag
relevantTags.add(new ItemTag(getNBTPath(), cropUpgrade(mmoitem.getNBT().getItem().getItemMeta().getDisplayName())));
}
// Add sole tag // Use that
relevantTags.add(new ItemTag(getNBTPath(), cropUpgrade(mmoitem.getNBT().getItem().getItemMeta().getDisplayName()))); NameData bakedData = (NameData) getLoadedNBT(relevantTags);
}
// Use that // Valid?
NameData bakedData = (NameData) getLoadedNBT(relevantTags); if (bakedData != null) {
// Valid? //NME//MMOItems.log("\u00a7e\u00a2\u00a2\u00a2\u00a77 Built:\u00a7f " + bakedData.toString());
if (bakedData != null) { /*
* Suppose we expect an item name with prefixes and suffixes,
* well, removing those should leave the bare name, right?
*
* If the player has renamed their item, this bare name will be somewhat
* different, and this is where those changes are updated.
*/
//NME//MMOItems.log("\u00a7e\u00a2\u00a2\u00a2\u00a77 Built:\u00a7f " + bakedData.toString()); @Nullable String itemName = null;
/* if (stored) {
* Suppose we expect an item name with prefixes and suffixes,
* well, removing those should leave the bare name, right?
*
* If the player has renamed their item, this bare name will be somewhat
* different, and this is where those changes are updated.
*/
@Nullable String itemName = null; // Could the player have renamed?
if (stored) { itemName = mmoitem.getNBT().getItem().getItemMeta().getDisplayName();
String colorless = ChatColor.stripColor(itemName);
// Could the player have renamed? //NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Comparing: " + itemName + " | " + colorless);
itemName = mmoitem.getNBT().getItem().getItemMeta().getDisplayName(); // By player
String colorless = ChatColor.stripColor(itemName); if (!itemName.equals(colorless)) {
//NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Not anvil");
itemName = null;
//NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Comparing: " + itemName + " | " + colorless); } else {
// By player //NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Replaced main with \u00a7b " + itemName);
if (!itemName.equals(colorless)) { bakedData.setString(itemName);
//NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Not anvil"); }
itemName = null;
} else { }
//NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Replaced main with \u00a7b " + itemName);
bakedData.setString(itemName);
}
} // Set
mmoitem.setData(this, bakedData);
// Set // Update in SH. Must happen after setting the data
mmoitem.setData(this, bakedData); if (stored && itemName != null) {
// Update in SH. Must happen after setting the data // History not prematurely loaded?
if (stored && itemName != null) { if (mmoitem.getStatHistory(this) == null) {
// History not prematurely loaded? // Also load history :think ing:
if (mmoitem.getStatHistory(this) == null) { ItemTag hisTag = ItemTag.getTagAtPath(ItemStackBuilder.history_keyword + getId(), mmoitem.getNBT(), SupportedNBTTagValues.STRING);
// Also load history :think ing: if (hisTag != null) {
ItemTag hisTag = ItemTag.getTagAtPath(ItemStackBuilder.history_keyword + getId(), mmoitem.getNBT(), SupportedNBTTagValues.STRING);
if (hisTag != null) { // Aye
StatHistory hist = StatHistory.fromNBTString(mmoitem, (String) hisTag.getValue());
// Aye // History valid? Record
StatHistory hist = StatHistory.fromNBTString(mmoitem, (String) hisTag.getValue()); if (hist != null) {
// History valid? Record // Original Data
if (hist != null) { NameData og = (NameData) hist.getOriginalData();
// Original Data // Overwrite
NameData og = (NameData) hist.getOriginalData(); og.setString(itemName);
// Overwrite // Load its stat history
og.setString(itemName); mmoitem.setStatHistory(this, hist);
// Load its stat history //NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Name History:");
mmoitem.setStatHistory(this, hist); //NME//hist.log();
}
}
}
}
}
}
//NME//MMOItems.log("\u00a7b\u00a2\u00a2\u00a2\u00a77 Name History:"); @Nullable
//NME//hist.log(); @Override
} public StringData getLoadedNBT(@NotNull ArrayList<ItemTag> storedTags) {
}
}
}
}
}
@Nullable // You got a double right
@Override ItemTag tg = ItemTag.getTagAtPath(getNBTPath(), storedTags);
public StringData getLoadedNBT(@NotNull ArrayList<ItemTag> storedTags) {
// You got a double right // Found righ
ItemTag tg = ItemTag.getTagAtPath(getNBTPath(), storedTags); if (tg != null) {
// Found righ // Get number
if (tg != null) { String value = (String) tg.getValue();
// Get number // That's it
String value = (String) tg.getValue(); NameData nd = new NameData(value);
// That's it nd.readPrefixes(ItemTag.getTagAtPath(getNBTPath() + "_PRE", storedTags));
NameData nd = new NameData(value); nd.readSuffixes(ItemTag.getTagAtPath(getNBTPath() + "_SUF", storedTags));
nd.readPrefixes(ItemTag.getTagAtPath(getNBTPath() + "_PRE", storedTags)); return nd;
nd.readSuffixes(ItemTag.getTagAtPath(getNBTPath() + "_SUF", storedTags)); }
return nd; // Fail
} return null;
}
// Fail @NotNull
return null; @Override
} public StringData getClearStatData() {
return new NameData("");
}
@NotNull @Override
@Override public StringData whenInitialized(Object object) {
public StringData getClearStatData() { return new NameData(object.toString());
return new NameData(""); }
}
@Override
public StringData whenInitialized(Object object) {
return new NameData(object.toString());
}
} }

View File

@ -0,0 +1,21 @@
package net.Indyuce.mmoitems.util;
import org.bukkit.ChatColor;
import org.jetbrains.annotations.NotNull;
import java.util.regex.Pattern;
/**
* mmoitems
* 09/11/2022
*
* @author Roch Blondiaux (Kiwix).
*/
public class ColorUtils {
private static final Pattern STRIP_DECORATION_PATTERN = Pattern.compile("(?i)" + '§' + "[K-O]");
public static @NotNull String stripDecoration(@NotNull String input) {
return "%s%s".formatted(ChatColor.RESET, STRIP_DECORATION_PATTERN.matcher(input).replaceAll("")).replace('§', '&');
}
}