Fixes reforge-able items and displaying color formatting codes

This commit is contained in:
Kristian Knarvik 2022-10-02 21:03:17 +02:00
parent a6e9163dbd
commit f058f4eec8
6 changed files with 27 additions and 28 deletions

View File

@ -8,8 +8,8 @@ import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.manager.ItemType; import net.knarcraft.blacksmith.manager.ItemType;
import net.knarcraft.blacksmith.manager.Message; import net.knarcraft.blacksmith.manager.Message;
import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.MessageFormatter;
import net.knarcraft.blacksmith.util.TypeValidationHelper; import net.knarcraft.blacksmith.util.TypeValidationHelper;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -67,17 +67,20 @@ public class BlackSmithConfigCommand implements CommandExecutor {
if (args.length == 1) { if (args.length == 1) {
//TODO: See if there's a way to remove this duplication //TODO: See if there's a way to remove this duplication
String rawValue;
String correctCommandName;
if (detectedGlobalSetting != null) { if (detectedGlobalSetting != null) {
displaySuccessMessage(sender, Message.getCurrentValueMessage(detectedGlobalSetting.getCommandName(), rawValue = String.valueOf(settings.getRawValue(detectedGlobalSetting));
MessageFormatter.escapeColorCodes(String.valueOf(settings.getRawValue(detectedGlobalSetting))))); correctCommandName = detectedGlobalSetting.getCommandName();
return true;
} else if (detectedNPCSetting != null) { } else if (detectedNPCSetting != null) {
displaySuccessMessage(sender, Message.getCurrentValueMessage(detectedNPCSetting.getCommandName(), rawValue = String.valueOf(settings.getRawValue(detectedNPCSetting));
MessageFormatter.escapeColorCodes(String.valueOf(settings.getRawValue(detectedNPCSetting))))); correctCommandName = detectedNPCSetting.getCommandName();
return true;
} else { } else {
return false; return false;
} }
displaySuccessMessage(sender, Message.getCurrentValueMessage(correctCommandName, rawValue));
sender.sendMessage("Raw value: " + rawValue.replace(ChatColor.COLOR_CHAR, '&'));
return true;
} else if (args.length == 2 && isSpecialCase(commandName)) { } else if (args.length == 2 && isSpecialCase(commandName)) {
if (displaySpecialCaseValue(args[1], sender, detectedGlobalSetting, settings)) { if (displaySpecialCaseValue(args[1], sender, detectedGlobalSetting, settings)) {
return true; return true;

View File

@ -58,8 +58,9 @@ public class BlackSmithEditCommand implements CommandExecutor {
CommandSender sender) { CommandSender sender) {
if (newValue == null) { if (newValue == null) {
//Display the current value of the setting //Display the current value of the setting
displaySuccessMessage(sender, Message.getCurrentValueMessage(npcSetting.getCommandName(), String rawValue = String.valueOf(blacksmithTrait.getSettings().getRawValue(npcSetting));
MessageFormatter.escapeColorCodes(String.valueOf(blacksmithTrait.getSettings().getRawValue(npcSetting))))); displaySuccessMessage(sender, Message.getCurrentValueMessage(npcSetting.getCommandName(), rawValue));
sender.sendMessage("Raw value: " + rawValue.replace(ChatColor.COLOR_CHAR, '&'));
return true; return true;
} else { } else {
boolean isValidType = TypeValidationHelper.isValid(npcSetting.getValueType(), newValue, sender); boolean isValidType = TypeValidationHelper.isValid(npcSetting.getValueType(), newValue, sender);

View File

@ -24,8 +24,10 @@ public enum NPCSetting {
-----------*/ -----------*/
BUSY_WITH_PLAYER_MESSAGE("defaults.messages.busy-with-player", SettingValueType.STRING, BUSY_WITH_PLAYER_MESSAGE("defaults.messages.busy-with-player", SettingValueType.STRING,
"&cI'm busy at the moment. Come back later!", "busyPlayerMessage"), "&cI'm busy at the moment. Come back later!", "busyPlayerMessage"),
//TODO: Add placeholder for remaining time?
BUSY_WITH_REFORGE_MESSAGE("defaults.messages.busy-with-reforge", SettingValueType.STRING, BUSY_WITH_REFORGE_MESSAGE("defaults.messages.busy-with-reforge", SettingValueType.STRING,
"&cI'm working on it. Be patient!", "busyReforgeMessage"), "&cI'm working on it. Be patient!", "busyReforgeMessage"),
//TODO: Add placeholder for remaining time?
COOL_DOWN_UNEXPIRED_MESSAGE("defaults.messages.cool-down-not-expired", SettingValueType.STRING, COOL_DOWN_UNEXPIRED_MESSAGE("defaults.messages.cool-down-not-expired", SettingValueType.STRING,
"&cYou've already had your chance! Give me a break!", "coolDownUnexpiredMessage"), "&cYou've already had your chance! Give me a break!", "coolDownUnexpiredMessage"),
COST_MESSAGE( COST_MESSAGE(

View File

@ -64,8 +64,11 @@ public class NPCSettings {
public void changeSetting(NPCSetting setting, Object newValue) { public void changeSetting(NPCSetting setting, Object newValue) {
if (setting == NPCSetting.REFORGE_ABLE_ITEMS) { if (setting == NPCSetting.REFORGE_ABLE_ITEMS) {
newValue = replaceReforgeAblePlaceholders(newValue); newValue = replaceReforgeAblePlaceholders(newValue);
currentValues.put(setting, newValue);
updateReforgeAbleItems();
} else {
currentValues.put(setting, newValue);
} }
currentValues.put(setting, newValue);
} }
/** /**
@ -346,20 +349,20 @@ public class NPCSettings {
*/ */
private void updateReforgeAbleItems() { private void updateReforgeAbleItems() {
this.reforgeAbleItems.clear(); this.reforgeAbleItems.clear();
List<?> reforgeAbleItems = (List<?>) currentValues.get(NPCSetting.REFORGE_ABLE_ITEMS); String newReforgeAbleItems = (String) currentValues.get(NPCSetting.REFORGE_ABLE_ITEMS);
if (reforgeAbleItems == null) { if (newReforgeAbleItems == null) {
return; return;
} }
for (Object item : reforgeAbleItems) { for (String item : newReforgeAbleItems.split(",")) {
if (item == null) { if (item == null || item.equalsIgnoreCase("null")) {
continue; continue;
} }
Material material = Material.matchMaterial(String.valueOf(item)); Material material = Material.matchMaterial(item.replace('-', '_'));
if (material != null && BlacksmithTrait.isRepairable(new ItemStack(material, 1))) { if (material != null && BlacksmithTrait.isRepairable(new ItemStack(material, 1))) {
this.reforgeAbleItems.add(material); this.reforgeAbleItems.add(material);
} else { } else {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to verify " + material + BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to verify " + item +
" as a valid reforge-able item"); " as a valid reforge-able item");
} }
} }

View File

@ -43,7 +43,8 @@ public enum SmithPreset {
*/ */
public static String replacePlaceholder(String possiblePlaceholder) { public static String replacePlaceholder(String possiblePlaceholder) {
for (SmithPreset smithPreset : SmithPreset.values()) { for (SmithPreset smithPreset : SmithPreset.values()) {
if (possiblePlaceholder.equalsIgnoreCase("preset:" + smithPreset.name())) { if (possiblePlaceholder.replace('-', '_').equalsIgnoreCase("preset:" +
smithPreset.name())) {
return String.join(",", smithPreset.getMaterialNames()); return String.join(",", smithPreset.getMaterialNames());
} }
} }

View File

@ -49,17 +49,6 @@ public final class MessageFormatter {
sender.sendMessage(ChatColor.DARK_RED + getFormattedMessage(message)); sender.sendMessage(ChatColor.DARK_RED + getFormattedMessage(message));
} }
/**
* Escapes color codes to prevent them from being shown
*
* @param input <p>The input string to escape color codes for</p>
* @return <p>The input string with color codes escaped</p>
*/
public static String escapeColorCodes(String input) {
//TODO: Find a working way of escaping color codes
return translateColors(input).replace(String.valueOf(ChatColor.COLOR_CHAR), "&&&");
}
/** /**
* Gets the formatted version of any chat message * Gets the formatted version of any chat message
* *