Adds ability to display current value of blacksmith commands

This commit is contained in:
Kristian Knarvik 2022-09-19 15:05:22 +02:00
parent f3169c9255
commit 3cfa7a2a0a
2 changed files with 131 additions and 39 deletions

View File

@ -21,79 +21,151 @@ public class BlackSmithConfigCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) { @NotNull String[] args) {
if (args.length > 0) { if (args.length == 0) {
String commandName = args[0]; return false;
if (commandName.equalsIgnoreCase("reload")) { }
return new ReloadCommand().onCommand(sender, command, label, args);
}
GlobalSettings settings = BlacksmithPlugin.getInstance().getSettings();
//Changing reforge-able items' default isn't recommended String commandName = args[0];
if (commandName.equalsIgnoreCase(NPCSetting.REFORGE_ABLE_ITEMS.getCommandName())) { if (commandName.equalsIgnoreCase("reload")) {
sender.sendMessage(ChatColor.DARK_RED + "Changing reforge-able items globally will make every new " + return new ReloadCommand().onCommand(sender, command, label, args);
"blacksmith unable to re-forge anything not in the list, unless it's changed for the " + }
"individual NPC. If you really want to change this, change it manually."); GlobalSettings settings = BlacksmithPlugin.getInstance().getSettings();
//Changing reforge-able items' default isn't recommended
if (commandName.equalsIgnoreCase(NPCSetting.REFORGE_ABLE_ITEMS.getCommandName())) {
sender.sendMessage(ChatColor.DARK_RED + "Changing reforge-able items globally will make every new " +
"blacksmith unable to re-forge anything not in the list, unless it's changed for the " +
"individual NPC. If you really want to change this, change it manually.");
return false;
}
//Find which global setting the user has specified, if any
GlobalSetting detectedGlobalSetting = null;
for (GlobalSetting globalSetting : GlobalSetting.values()) {
if (commandName.equalsIgnoreCase(globalSetting.getCommandName())) {
detectedGlobalSetting = globalSetting;
break;
}
}
//Find which npc setting the user has specified, if any
NPCSetting detectedNPCSetting = null;
for (NPCSetting npcSetting : NPCSetting.values()) {
if (commandName.equalsIgnoreCase(npcSetting.getCommandName())) {
detectedNPCSetting = npcSetting;
break;
}
}
if (args.length == 1) {
if (detectedGlobalSetting != null) {
sender.sendMessage(String.format("Current value of %s: %s", detectedGlobalSetting.getCommandName(),
settings.getRawValue(detectedGlobalSetting)));
} else if (detectedNPCSetting != null) {
sender.sendMessage(String.format("Current value of %s: %s", detectedNPCSetting.getCommandName(),
settings.getRawValue(detectedNPCSetting)));
}
return true;
} else if (args.length == 2 && isSpecialCase(commandName)) {
return displaySpecialCaseValue(args[1], sender, detectedGlobalSetting, settings);
}
if (updateSpecialCase(settings, commandName, args)) {
return true;
}
if (detectedGlobalSetting != null) {
settings.changeValue(detectedGlobalSetting, args[1]);
return true;
} else if (detectedNPCSetting != null) {
settings.changeValue(detectedNPCSetting, args[1]);
return true;
}
return false;
}
/**
* Displays the current value of a special-case configuration value
*
* @param selector <p>The selector specifying which material/enchantment to display information about</p>
* @param sender <p>The sender to display the value to</p>
* @param setting <p>The setting to display</p>
* @param settings <p>The settings object to query</p>
* @return <p>True if the value was successfully displayed</p>
*/
private boolean displaySpecialCaseValue(String selector, CommandSender sender, GlobalSetting setting,
GlobalSettings settings) {
if (setting == GlobalSetting.BASE_PRICE) {
Material material = Material.matchMaterial(selector);
if (material == null) {
return false; return false;
} }
sender.sendMessage(String.format("Current value of %s for material %s: %s", setting.getCommandName(),
if (args.length == 1) { material, settings.getBasePrice(material)));
//TODO: Display the current value of the setting return true;
} else if (args.length == 2 && isSpecialCase(commandName)) { } else if (setting == GlobalSetting.PRICE_PER_DURABILITY_POINT) {
//TODO: Display the current value for the specified setting and material Material material = Material.matchMaterial(selector);
if (material == null) {
return false;
} }
sender.sendMessage(String.format("Current value of %s for material %s: %s", setting.getCommandName(),
if (isSpecialCase(settings, commandName, args)) { material, settings.getPricePerDurabilityPoint(material)));
return true; } else if (setting == GlobalSetting.ENCHANTMENT_COST) {
} Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(selector));
if (enchantment == null) {
for (GlobalSetting globalSetting : GlobalSetting.values()) { return false;
if (commandName.equalsIgnoreCase(globalSetting.getCommandName())) {
settings.changeValue(globalSetting, args[1]);
return true;
}
}
for (NPCSetting npcSetting : NPCSetting.values()) {
if (commandName.equalsIgnoreCase(npcSetting.getCommandName())) {
settings.changeValue(npcSetting, args[1]);
}
} }
sender.sendMessage(String.format("Current value of %s for enchantment %s: %s", setting.getCommandName(),
enchantment, settings.getEnchantmentCost(enchantment)));
} }
return false; return false;
} }
/** /**
* Gets whether a command name matches a special case command * Gets whether a command name matches a special case command
* *
* @param commandName <p>The command specified</p> * @param commandName <p>The command specified</p>
* @return <p>True if the command is a special case</p> * @return <p>True if the command is a special case</p>
*/ */
private boolean isSpecialCase(String commandName) { private boolean isSpecialCase(String commandName) {
return commandName.equalsIgnoreCase(GlobalSetting.BASE_PRICE.getCommandName()) || return commandName.equalsIgnoreCase(GlobalSetting.BASE_PRICE.getCommandName()) ||
commandName.equalsIgnoreCase(GlobalSetting.PRICE_PER_DURABILITY_POINT.getCommandName()) || commandName.equalsIgnoreCase(GlobalSetting.PRICE_PER_DURABILITY_POINT.getCommandName()) ||
commandName.equalsIgnoreCase(GlobalSetting.ENCHANTMENT_COST.getCommandName()); commandName.equalsIgnoreCase(GlobalSetting.ENCHANTMENT_COST.getCommandName());
} }
/** /**
* Gets whether the command could be processed as one of the three special cases * Updates a special-case configuration value if a special-case is encountered
* *
* @param settings <p>The settings to modify</p> * @param settings <p>The settings to modify</p>
* @param commandName <p>The sub-command the player specified</p> * @param commandName <p>The sub-command the player specified</p>
* @param args <p>All arguments given</p> * @param args <p>All arguments given</p>
* @return <p>True if already handled as a special case</p> * @return <p>True if already handled as a special case</p>
*/ */
private boolean isSpecialCase(GlobalSettings settings, String commandName, String[] args) { private boolean updateSpecialCase(GlobalSettings settings, String commandName, String[] args) {
if (commandName.equalsIgnoreCase(GlobalSetting.BASE_PRICE.getCommandName())) { if (commandName.equalsIgnoreCase(GlobalSetting.BASE_PRICE.getCommandName())) {
settings.setBasePrice(Material.matchMaterial(args[1]), Double.parseDouble(args[2])); Material material = Material.matchMaterial(args[1]);
if (material == null) {
return false;
}
settings.setBasePrice(material, Double.parseDouble(args[2]));
return true; return true;
} }
if (commandName.equalsIgnoreCase(GlobalSetting.PRICE_PER_DURABILITY_POINT.getCommandName())) { if (commandName.equalsIgnoreCase(GlobalSetting.PRICE_PER_DURABILITY_POINT.getCommandName())) {
settings.setPricePerDurabilityPoint(Material.matchMaterial(args[1]), Double.parseDouble(args[2])); Material material = Material.matchMaterial(args[1]);
if (material == null) {
return false;
}
settings.setPricePerDurabilityPoint(material, Double.parseDouble(args[2]));
return true; return true;
} }
if (commandName.equalsIgnoreCase(GlobalSetting.ENCHANTMENT_COST.getCommandName())) { if (commandName.equalsIgnoreCase(GlobalSetting.ENCHANTMENT_COST.getCommandName())) {
settings.setEnchantmentCost(Enchantment.getByKey(NamespacedKey.minecraft(args[1])), Double.parseDouble(args[2])); Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(args[1]));
if (enchantment == null) {
return false;
}
settings.setEnchantmentCost(enchantment, Double.parseDouble(args[2]));
return true; return true;
} }

View File

@ -85,6 +85,26 @@ public class GlobalSettings {
save(); save();
} }
/**
* Gets the current raw value of the given global setting
*
* @param globalSetting <p>The setting to get</p>
* @return <p>The current raw setting value</p>
*/
public Object getRawValue(GlobalSetting globalSetting) {
return globalSettings.get(globalSetting);
}
/**
* Gets the current raw value of the given default NPC setting
*
* @param npcSetting <p>The setting to get</p>
* @return <p>The current raw setting value</p>
*/
public Object getRawValue(NPCSetting npcSetting) {
return defaultNPCSettings.get(npcSetting);
}
/** /**
* Sets the enchantment cost for the given enchantment * Sets the enchantment cost for the given enchantment
* *