From 4976484d914b45594525a1477736cbdedf9834a8 Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Sun, 2 May 2021 16:18:35 -0400 Subject: [PATCH] Unformat item lore and name when serializing items (#4095) Co-authored-by: Mariell Hoversholm --- .../essentials/items/AbstractItemDb.java | 5 +++-- .../earth2me/essentials/utils/FormatUtil.java | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/items/AbstractItemDb.java b/Essentials/src/main/java/com/earth2me/essentials/items/AbstractItemDb.java index c5e2339bd..e35f13313 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/items/AbstractItemDb.java +++ b/Essentials/src/main/java/com/earth2me/essentials/items/AbstractItemDb.java @@ -2,6 +2,7 @@ package com.earth2me.essentials.items; import com.earth2me.essentials.IConf; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.MaterialUtil; import com.earth2me.essentials.utils.VersionUtil; import net.ess3.api.IEssentials; @@ -203,7 +204,7 @@ public abstract class AbstractItemDb implements IConf, net.ess3.api.IItemDb { if (is.hasItemMeta()) { final ItemMeta meta = is.getItemMeta(); if (meta.hasDisplayName()) { - sb.append("name:").append(meta.getDisplayName().replaceAll(" ", "_")).append(" "); + sb.append("name:").append(FormatUtil.unformatString(meta.getDisplayName()).replace(" ", "_")).append(" "); } if (meta.hasLore()) { @@ -216,7 +217,7 @@ public abstract class AbstractItemDb implements IConf, net.ess3.api.IItemDb { sb.append("|"); } first = false; - sb.append(s.replaceAll(" ", "_")); + sb.append(FormatUtil.unformatString(s).replace(" ", "_")); } sb.append(" "); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/utils/FormatUtil.java b/Essentials/src/main/java/com/earth2me/essentials/utils/FormatUtil.java index f63b75850..09d7c1a82 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/utils/FormatUtil.java +++ b/Essentials/src/main/java/com/earth2me/essentials/utils/FormatUtil.java @@ -148,16 +148,27 @@ public final class FormatUtil { return builder.toString(); } - public static String unformatString(final IUser user, final String permBase, String message) { + public static String unformatString(final String message) { + if (message == null) { + return null; + } + return unformatString(message, EnumSet.allOf(ChatColor.class), true); + } + + public static String unformatString(final IUser user, final String permBase, final String message) { + if (message == null) { + return null; + } + return unformatString(message, getSupported(user, permBase), user.isAuthorized(permBase + ".rgb")); + } + + public static String unformatString(String message, final EnumSet supported, boolean rgb) { if (message == null) { return null; } - final EnumSet supported = getSupported(user, permBase); - // RGB Codes final StringBuffer rgbBuilder = new StringBuffer(); final Matcher rgbMatcher = STRIP_RGB_PATTERN.matcher(message); - final boolean rgb = user.isAuthorized(permBase + ".rgb"); while (rgbMatcher.find()) { final String code = rgbMatcher.group(1).replace(String.valueOf(ChatColor.COLOR_CHAR), ""); if (rgb) {