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,7 +21,10 @@ 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) {
return false;
}
String commandName = args[0]; String commandName = args[0];
if (commandName.equalsIgnoreCase("reload")) { if (commandName.equalsIgnoreCase("reload")) {
return new ReloadCommand().onCommand(sender, command, label, args); return new ReloadCommand().onCommand(sender, command, label, args);
@ -36,27 +39,84 @@ public class BlackSmithConfigCommand implements CommandExecutor {
return false; return false;
} }
if (args.length == 1) { //Find which global setting the user has specified, if any
//TODO: Display the current value of the setting GlobalSetting detectedGlobalSetting = null;
} else if (args.length == 2 && isSpecialCase(commandName)) {
//TODO: Display the current value for the specified setting and material
}
if (isSpecialCase(settings, commandName, args)) {
return true;
}
for (GlobalSetting globalSetting : GlobalSetting.values()) { for (GlobalSetting globalSetting : GlobalSetting.values()) {
if (commandName.equalsIgnoreCase(globalSetting.getCommandName())) { if (commandName.equalsIgnoreCase(globalSetting.getCommandName())) {
settings.changeValue(globalSetting, args[1]); detectedGlobalSetting = globalSetting;
return true; break;
} }
} }
//Find which npc setting the user has specified, if any
NPCSetting detectedNPCSetting = null;
for (NPCSetting npcSetting : NPCSetting.values()) { for (NPCSetting npcSetting : NPCSetting.values()) {
if (commandName.equalsIgnoreCase(npcSetting.getCommandName())) { if (commandName.equalsIgnoreCase(npcSetting.getCommandName())) {
settings.changeValue(npcSetting, args[1]); 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;
}
sender.sendMessage(String.format("Current value of %s for material %s: %s", setting.getCommandName(),
material, settings.getBasePrice(material)));
return true;
} else if (setting == GlobalSetting.PRICE_PER_DURABILITY_POINT) {
Material material = Material.matchMaterial(selector);
if (material == null) {
return false;
}
sender.sendMessage(String.format("Current value of %s for material %s: %s", setting.getCommandName(),
material, settings.getPricePerDurabilityPoint(material)));
} else if (setting == GlobalSetting.ENCHANTMENT_COST) {
Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(selector));
if (enchantment == null) {
return false;
}
sender.sendMessage(String.format("Current value of %s for enchantment %s: %s", setting.getCommandName(),
enchantment, settings.getEnchantmentCost(enchantment)));
} }
return false; return false;
} }
@ -74,26 +134,38 @@ public class BlackSmithConfigCommand implements CommandExecutor {
} }
/** /**
* 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
* *