Implements the scrapper global edit command

Changes setting quite a bit to avoid code duplication
This commit is contained in:
2023-11-16 01:17:27 +01:00
parent f3f3f66c38
commit 4f885135e3
18 changed files with 716 additions and 791 deletions

View File

@@ -3,15 +3,14 @@ package net.knarcraft.blacksmith.command.blacksmith;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.command.ReloadCommand;
import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSetting;
import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSetting;
import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting;
import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSettings;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.blacksmith.formatting.ItemType;
import net.knarcraft.blacksmith.util.ConfigCommandHelper;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.ItemHelper;
import net.knarcraft.blacksmith.util.TypeValidationHelper;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@@ -20,12 +19,8 @@ import org.bukkit.enchantments.Enchantment;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage;
/**
* The command used for changing global configuration options
* The command used for changing global blacksmith configuration options
*/
public class BlackSmithConfigCommand implements CommandExecutor {
@@ -42,124 +37,36 @@ public class BlackSmithConfigCommand implements CommandExecutor {
}
GlobalBlacksmithSettings settings = BlacksmithPlugin.getInstance().getGlobalBlacksmithSettings();
//Find which global setting the user has specified, if any
GlobalBlacksmithSetting detectedGlobalBlacksmithSetting = null;
for (GlobalBlacksmithSetting globalBlacksmithSetting : GlobalBlacksmithSetting.values()) {
if (commandName.equalsIgnoreCase(globalBlacksmithSetting.getCommandName())) {
detectedGlobalBlacksmithSetting = globalBlacksmithSetting;
break;
}
}
//Find which npc setting the user has specified, if any
BlacksmithNPCSetting detectedBlacksmithNPCSetting = null;
for (BlacksmithNPCSetting blacksmithNpcSetting : BlacksmithNPCSetting.values()) {
if (commandName.equalsIgnoreCase(blacksmithNpcSetting.getCommandName())) {
detectedBlacksmithNPCSetting = blacksmithNpcSetting;
break;
}
//Find which setting the user has specified, if any
BlacksmithSetting detectedSetting = BlacksmithSetting.getSetting(commandName);
if (detectedSetting == null) {
return false;
}
//Display the current value of a setting
if (args.length == 1) {
return displayCurrentValue(detectedGlobalBlacksmithSetting, detectedBlacksmithNPCSetting, settings, sender);
ConfigCommandHelper.displayCurrentValue(detectedSetting, settings, sender);
return true;
} else if (args.length == 2 && isSpecialCase(commandName)) {
if (displaySpecialCaseValue(args[1], sender, detectedGlobalBlacksmithSetting, settings)) {
if (displaySpecialCaseValue(args[1], sender, detectedSetting, settings)) {
return true;
}
}
//Update the value of a special-case setting
if (args.length == 3 && updateSpecialCase(settings, detectedGlobalBlacksmithSetting, args, sender)) {
if (args.length == 3 && updateSpecialCase(settings, detectedSetting, args, sender)) {
return true;
}
//Change the value of the specified setting
if ((detectedGlobalBlacksmithSetting != null &&
TypeValidationHelper.isValid(detectedGlobalBlacksmithSetting.getValueType(), args[1], sender)) ||
(detectedBlacksmithNPCSetting != null &&
TypeValidationHelper.isValid(detectedBlacksmithNPCSetting.getValueType(), args[1], sender))) {
return changeValue(args, detectedGlobalBlacksmithSetting, detectedBlacksmithNPCSetting, settings, sender);
} else {
return false;
}
}
/**
* Changes the value of the setting defined in the user's input
*
* @param args <p>The arguments given by the user</p>
* @param detectedGlobalBlacksmithSetting <p>The global setting recognized from the input, if any</p>
* @param detectedBlacksmithNPCSetting <p>The NPC setting recognized from the input, if any</p>
* @param settings <p>The global settings object to get settings from</p>
* @param sender <p>The command sender to display any output to</p>
* @return <p>True if the value was successfully changed</p>
*/
private boolean changeValue(@NotNull String[] args, @Nullable GlobalBlacksmithSetting detectedGlobalBlacksmithSetting,
@Nullable BlacksmithNPCSetting detectedBlacksmithNPCSetting,
@NotNull GlobalBlacksmithSettings settings, @NotNull CommandSender sender) {
String newValue = args[1];
if (detectedGlobalBlacksmithSetting != null) {
settings.changeValue(detectedGlobalBlacksmithSetting, newValue);
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
getValueChangedMessage(detectedGlobalBlacksmithSetting.getCommandName(), newValue));
return true;
} else if (detectedBlacksmithNPCSetting != null) {
//This makes sure all arguments are treated as a sentence
if (detectedBlacksmithNPCSetting.getValueType() == SettingValueType.STRING) {
newValue = String.join(" ", Arrays.asList(args).subList(1, args.length));
}
settings.changeValue(detectedBlacksmithNPCSetting, newValue);
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
getValueChangedMessage(detectedBlacksmithNPCSetting.getCommandName(), newValue));
if (TypeValidationHelper.isValid(detectedSetting.getValueType(), args[1], sender)) {
ConfigCommandHelper.changeValue(args, detectedSetting, settings, sender);
return true;
} else {
return false;
}
}
/**
* Displays the current value of the selected setting
*
* @param detectedGlobalBlacksmithSetting <p>The global setting recognized from the input, if any</p>
* @param detectedBlacksmithNPCSetting <p>The NPC setting recognized from the input, if any</p>
* @param settings <p>The global settings object to get settings from</p>
* @param sender <p>The command sender to display any output to</p>
* @return <p>True if a settings was successfully displayed</p>
*/
private boolean displayCurrentValue(@Nullable GlobalBlacksmithSetting detectedGlobalBlacksmithSetting,
@Nullable BlacksmithNPCSetting detectedBlacksmithNPCSetting,
@NotNull GlobalBlacksmithSettings settings, @NotNull CommandSender sender) {
String settingValue;
String correctCommandName;
boolean printRawValue = false;
//Find the value of the specified setting
//TODO: See if there's a way to remove this duplication
if (detectedGlobalBlacksmithSetting != null) {
settingValue = String.valueOf(settings.getRawValue(detectedGlobalBlacksmithSetting));
correctCommandName = detectedGlobalBlacksmithSetting.getCommandName();
} else if (detectedBlacksmithNPCSetting != null) {
settingValue = String.valueOf(settings.getRawValue(detectedBlacksmithNPCSetting));
correctCommandName = detectedBlacksmithNPCSetting.getCommandName();
//For messages, print an additional raw value showing which color codes are used
if (detectedBlacksmithNPCSetting.getPath().startsWith("defaults.messages")) {
printRawValue = true;
}
} else {
return false;
}
//Display the current value of the setting
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
BlacksmithTranslatableMessage.getCurrentValueMessage(correctCommandName, settingValue));
//Print the value with any colors displayed as &a-f0-9
if (printRawValue) {
sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage(
settingValue.replace(ChatColor.COLOR_CHAR, '&')));
}
return true;
}
/**
* Displays the current value of a special-case configuration value
*
@@ -170,15 +77,15 @@ public class BlackSmithConfigCommand implements CommandExecutor {
* @return <p>True if the value was successfully displayed</p>
*/
private boolean displaySpecialCaseValue(@NotNull String selector, @NotNull CommandSender sender,
@Nullable GlobalBlacksmithSetting setting,
@Nullable BlacksmithSetting setting,
@NotNull GlobalBlacksmithSettings settings) {
if (setting == GlobalBlacksmithSetting.BASE_PRICE || setting == GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT) {
if (setting == BlacksmithSetting.BASE_PRICE || setting == BlacksmithSetting.PRICE_PER_DURABILITY_POINT) {
Material material = InputParsingHelper.matchMaterial(selector);
if (material == null) {
return false;
}
String currentValue;
if (setting == GlobalBlacksmithSetting.BASE_PRICE) {
if (setting == BlacksmithSetting.BASE_PRICE) {
currentValue = String.valueOf(settings.getBasePrice(material));
} else {
currentValue = String.valueOf(settings.getPricePerDurabilityPoint(material));
@@ -187,7 +94,7 @@ public class BlackSmithConfigCommand implements CommandExecutor {
BlacksmithTranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(),
ItemType.MATERIAL, material.name(), currentValue));
return true;
} else if (setting == GlobalBlacksmithSetting.ENCHANTMENT_COST) {
} else if (setting == BlacksmithSetting.ENCHANTMENT_COST) {
Enchantment enchantment = InputParsingHelper.matchEnchantment(selector);
if (enchantment == null) {
return false;
@@ -209,22 +116,22 @@ public class BlackSmithConfigCommand implements CommandExecutor {
* @return <p>True if the command is a special case</p>
*/
private boolean isSpecialCase(@NotNull String commandName) {
return commandName.equalsIgnoreCase(GlobalBlacksmithSetting.BASE_PRICE.getCommandName()) ||
commandName.equalsIgnoreCase(GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT.getCommandName()) ||
commandName.equalsIgnoreCase(GlobalBlacksmithSetting.ENCHANTMENT_COST.getCommandName());
return commandName.equalsIgnoreCase(BlacksmithSetting.BASE_PRICE.getCommandName()) ||
commandName.equalsIgnoreCase(BlacksmithSetting.PRICE_PER_DURABILITY_POINT.getCommandName()) ||
commandName.equalsIgnoreCase(BlacksmithSetting.ENCHANTMENT_COST.getCommandName());
}
/**
* Updates a special-case configuration value if a special-case is encountered
*
* @param settings <p>The settings to modify</p>
* @param detectedGlobalBlacksmithSetting <p>The global setting specified</p>
* @param args <p>All arguments given</p>
* @param sender <p>The command sender to notify if successful</p>
* @param settings <p>The settings to modify</p>
* @param blacksmithSetting <p>The setting specified</p>
* @param args <p>All arguments given</p>
* @param sender <p>The command sender to notify if successful</p>
* @return <p>True if already handled as a special case</p>
*/
private boolean updateSpecialCase(@NotNull GlobalBlacksmithSettings settings,
@Nullable GlobalBlacksmithSetting detectedGlobalBlacksmithSetting,
@Nullable BlacksmithSetting blacksmithSetting,
@NotNull String[] args,
CommandSender sender) {
if (InputParsingHelper.isEmpty(args[2])) {
@@ -235,10 +142,10 @@ public class BlackSmithConfigCommand implements CommandExecutor {
double newPrice = Double.parseDouble(args[2]);
String newValue = String.valueOf(newPrice);
if (detectedGlobalBlacksmithSetting == GlobalBlacksmithSetting.BASE_PRICE ||
detectedGlobalBlacksmithSetting == GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT) {
return updatePriceSpecialCase(settings, detectedGlobalBlacksmithSetting, args[1], newPrice, sender);
} else if (detectedGlobalBlacksmithSetting == GlobalBlacksmithSetting.ENCHANTMENT_COST) {
if (blacksmithSetting == BlacksmithSetting.BASE_PRICE ||
blacksmithSetting == BlacksmithSetting.PRICE_PER_DURABILITY_POINT) {
return updatePriceSpecialCase(settings, blacksmithSetting, args[1], newPrice, sender);
} else if (blacksmithSetting == BlacksmithSetting.ENCHANTMENT_COST) {
//Update enchantment cost for an item
Enchantment enchantment = InputParsingHelper.matchEnchantment(args[1]);
if (enchantment == null) {
@@ -248,7 +155,7 @@ public class BlackSmithConfigCommand implements CommandExecutor {
String itemChanged = enchantment.getKey().getKey();
settings.setEnchantmentCost(enchantment, newPrice);
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
BlacksmithTranslatableMessage.getItemValueChangedMessage(detectedGlobalBlacksmithSetting.getCommandName(),
BlacksmithTranslatableMessage.getItemValueChangedMessage(blacksmithSetting.getCommandName(),
itemType, itemChanged, newValue));
return true;
} else {
@@ -259,15 +166,15 @@ public class BlackSmithConfigCommand implements CommandExecutor {
/**
* Updates a special case price configuration value if a special case is encountered
*
* @param settings <p>The settings to modify</p>
* @param detectedGlobalBlacksmithSetting <p>The global setting specified</p>
* @param materialName <p>The material name to update the price for</p>
* @param newPrice <p>The new price to update to</p>
* @param sender <p>The command sender to respond to</p>
* @param settings <p>The settings to modify</p>
* @param blacksmithSetting <p>The setting specified</p>
* @param materialName <p>The material name to update the price for</p>
* @param newPrice <p>The new price to update to</p>
* @param sender <p>The command sender to respond to</p>
* @return <p>True if the input was valid, and the item(s) was/were updated</p>
*/
private boolean updatePriceSpecialCase(@NotNull GlobalBlacksmithSettings settings,
@NotNull GlobalBlacksmithSetting detectedGlobalBlacksmithSetting,
@NotNull BlacksmithSetting blacksmithSetting,
@NotNull String materialName, double newPrice,
@NotNull CommandSender sender) {
ItemType itemType = ItemType.MATERIAL;
@@ -275,14 +182,14 @@ public class BlackSmithConfigCommand implements CommandExecutor {
//Update base price or price per durability point for an item
if (materialName.contains("*")) {
itemChanged = materialName;
updateAllMatchedPrices(settings, detectedGlobalBlacksmithSetting, materialName, newPrice);
updateAllMatchedPrices(settings, blacksmithSetting, materialName, newPrice);
} else {
Material material = InputParsingHelper.matchMaterial(materialName);
if (material == null) {
return false;
}
itemChanged = material.name();
if (detectedGlobalBlacksmithSetting == GlobalBlacksmithSetting.BASE_PRICE) {
if (blacksmithSetting == BlacksmithSetting.BASE_PRICE) {
settings.setBasePrice(material, newPrice);
} else {
settings.setPricePerDurabilityPoint(material, newPrice);
@@ -290,7 +197,7 @@ public class BlackSmithConfigCommand implements CommandExecutor {
}
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
BlacksmithTranslatableMessage.getItemValueChangedMessage(detectedGlobalBlacksmithSetting.getCommandName(),
BlacksmithTranslatableMessage.getItemValueChangedMessage(blacksmithSetting.getCommandName(),
itemType, itemChanged, String.valueOf(newPrice)));
return true;
}
@@ -298,13 +205,13 @@ public class BlackSmithConfigCommand implements CommandExecutor {
/**
* Updates all materials matching the material name wildcard
*
* @param settings <p>The settings to modify</p>
* @param detectedGlobalBlacksmithSetting <p>The global setting specified</p>
* @param materialName <p>The wildcard material name to update cost for</p>
* @param newPrice <p>The new cost for the matched items</p>
* @param settings <p>The settings to modify</p>
* @param blacksmithSetting <p>The setting specified</p>
* @param materialName <p>The wildcard material name to update cost for</p>
* @param newPrice <p>The new cost for the matched items</p>
*/
private void updateAllMatchedPrices(@NotNull GlobalBlacksmithSettings settings,
@NotNull GlobalBlacksmithSetting detectedGlobalBlacksmithSetting,
@NotNull BlacksmithSetting blacksmithSetting,
@NotNull String materialName, double newPrice) {
String search = InputParsingHelper.regExIfy(materialName);
for (Material material : ItemHelper.getAllReforgeAbleMaterials()) {
@@ -312,7 +219,7 @@ public class BlackSmithConfigCommand implements CommandExecutor {
continue;
}
if (detectedGlobalBlacksmithSetting == GlobalBlacksmithSetting.BASE_PRICE) {
if (blacksmithSetting == BlacksmithSetting.BASE_PRICE) {
settings.setBasePrice(material, newPrice);
} else {
settings.setPricePerDurabilityPoint(material, newPrice);

View File

@@ -1,8 +1,7 @@
package net.knarcraft.blacksmith.command.blacksmith;
import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSetting;
import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSetting;
import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@@ -17,7 +16,7 @@ import static net.knarcraft.blacksmith.util.TabCompleteValuesHelper.getTabComple
import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains;
/**
* The tab completer for the command used for changing global configuration options
* The tab completer for the command used for changing global blacksmith configuration options
*/
public class BlackSmithConfigTabCompleter implements TabCompleter {
@@ -32,72 +31,61 @@ public class BlackSmithConfigTabCompleter implements TabCompleter {
//Arguments: <setting> [new value/material or enchantment] []
//Prevent tab-completion when typing messages with spaces
if (skipCompletionForSpacedMessage(args) != null) {
if (args.length > 2 && skipCompletionForSpacedMessage(args[0]) != null) {
return new ArrayList<>();
}
if (args.length == 1) {
List<String> availableCommands = new ArrayList<>();
availableCommands.add("reload");
for (BlacksmithNPCSetting setting : BlacksmithNPCSetting.values()) {
for (BlacksmithSetting setting : BlacksmithSetting.values()) {
availableCommands.add(setting.getCommandName());
}
for (GlobalBlacksmithSetting globalBlacksmithSetting : GlobalBlacksmithSetting.values()) {
availableCommands.add(globalBlacksmithSetting.getCommandName());
}
return filterMatchingContains(availableCommands, args[0]);
} else if (args.length == 2) {
return tabCompleteCommandValues(args[0], args[1]);
} else if (args.length == 3) {
//Get per-material tab completions, or return nothing if an invalid setting was specified
for (GlobalBlacksmithSetting globalBlacksmithSetting : GlobalBlacksmithSetting.values()) {
if (globalBlacksmithSetting.getCommandName().equalsIgnoreCase(args[0])) {
return getPerTypeTabCompletions(globalBlacksmithSetting, args);
}
}
return new ArrayList<>();
}
return null;
}
/**
* Checks whether to tab-complete nothing because a message containing spaces is being written
*
* @param args <p>The arguments given by the user</p>
* @return <p>Null if not writing a spaced message</p>
*/
private List<String> skipCompletionForSpacedMessage(@NotNull String[] args) {
if (args.length > 2) {
BlacksmithNPCSetting blacksmithNpcSetting = null;
for (BlacksmithNPCSetting setting : BlacksmithNPCSetting.values()) {
if (setting.getCommandName().equalsIgnoreCase(args[0])) {
blacksmithNpcSetting = setting;
break;
}
}
if (blacksmithNpcSetting != null && blacksmithNpcSetting.getPath().startsWith("defaults.messages")) {
BlacksmithSetting blacksmithSetting = BlacksmithSetting.getSetting(args[0]);
if (blacksmithSetting != null) {
return getPerTypeTabCompletions(blacksmithSetting, args);
} else {
return new ArrayList<>();
}
}
return null;
}
/**
* Checks whether to tab-complete nothing because a message containing spaces is being written
*
* @param command <p>The command specified by the user</p>
* @return <p>Null if not writing a spaced message</p>
*/
private List<String> skipCompletionForSpacedMessage(@NotNull String command) {
BlacksmithSetting blacksmithSetting = BlacksmithSetting.getSetting(command);
if (blacksmithSetting != null && blacksmithSetting.isMessage()) {
return new ArrayList<>();
}
return null;
}
/**
* Gets tab-completions for a selected material or enchantment
*
* @param globalBlacksmithSetting <p>The global setting to get tab-completions for</p>
* @param args <p>The arguments given by the user</p>
* @param blacksmithSetting <p>The global setting to get tab-completions for</p>
* @param args <p>The arguments given by the user</p>
* @return <p>The tab-completions to show to the user</p>
*/
private List<String> getPerTypeTabCompletions(@NotNull GlobalBlacksmithSetting globalBlacksmithSetting,
private List<String> getPerTypeTabCompletions(@NotNull BlacksmithSetting blacksmithSetting,
@NotNull String[] args) {
//Display possible tab-completions only if a valid enchantment or material is provided
if (((globalBlacksmithSetting == GlobalBlacksmithSetting.BASE_PRICE ||
globalBlacksmithSetting == GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT) &&
if (((blacksmithSetting == BlacksmithSetting.BASE_PRICE ||
blacksmithSetting == BlacksmithSetting.PRICE_PER_DURABILITY_POINT) &&
InputParsingHelper.matchMaterial(args[1]) != null) ||
(globalBlacksmithSetting == GlobalBlacksmithSetting.ENCHANTMENT_COST &&
(blacksmithSetting == BlacksmithSetting.ENCHANTMENT_COST &&
InputParsingHelper.matchEnchantment(args[1]) != null)) {
return filterMatchingContains(getTabCompletions(globalBlacksmithSetting.getValueType()), args[2]);
return filterMatchingContains(getTabCompletions(blacksmithSetting.getValueType()), args[2]);
} else {
return new ArrayList<>();
}
@@ -114,35 +102,29 @@ public class BlackSmithConfigTabCompleter implements TabCompleter {
if (commandName.equalsIgnoreCase("reload")) {
return new ArrayList<>();
}
for (GlobalBlacksmithSetting globalBlacksmithSetting : GlobalBlacksmithSetting.values()) {
if (globalBlacksmithSetting.getCommandName().equalsIgnoreCase(commandName)) {
return getCompletions(globalBlacksmithSetting, commandValue);
}
BlacksmithSetting setting = BlacksmithSetting.getSetting(commandName);
if (setting != null) {
return getCompletions(setting, commandValue);
} else {
return null;
}
for (BlacksmithNPCSetting blacksmithNpcSetting : BlacksmithNPCSetting.values()) {
if (blacksmithNpcSetting.getCommandName().equalsIgnoreCase(commandName)) {
return filterMatchingContains(getTabCompletions(
blacksmithNpcSetting.getValueType()), commandValue);
}
}
return null;
}
/**
* Gets tab-completions for the given global setting and filters on the command value
*
* @param globalBlacksmithSetting <p>The global setting to get tab-completions for</p>
* @param commandValue <p>The command value used to filter between available tab-completions</p>
* @param blacksmithSetting <p>The global setting to get tab-completions for</p>
* @param commandValue <p>The command value used to filter between available tab-completions</p>
* @return <p>The available tab-completions</p>
*/
private List<String> getCompletions(@NotNull GlobalBlacksmithSetting globalBlacksmithSetting,
private List<String> getCompletions(@NotNull BlacksmithSetting blacksmithSetting,
@NotNull String commandValue) {
List<String> returnValues = filterMatchingContains(
getTabCompletions(globalBlacksmithSetting.getValueType()), commandValue);
if (globalBlacksmithSetting == GlobalBlacksmithSetting.BASE_PRICE ||
globalBlacksmithSetting == GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT) {
getTabCompletions(blacksmithSetting.getValueType()), commandValue);
if (blacksmithSetting == BlacksmithSetting.BASE_PRICE ||
blacksmithSetting == BlacksmithSetting.PRICE_PER_DURABILITY_POINT) {
returnValues.addAll(filterMatchingContains(getTabCompletions(SettingValueType.MATERIAL), commandValue));
} else if (globalBlacksmithSetting == GlobalBlacksmithSetting.ENCHANTMENT_COST) {
} else if (blacksmithSetting == BlacksmithSetting.ENCHANTMENT_COST) {
returnValues.addAll(filterMatchingContains(getTabCompletions(SettingValueType.ENCHANTMENT), commandValue));
}
return returnValues;

View File

@@ -4,7 +4,7 @@ import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSetting;
import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.blacksmith.util.InputParsingHelper;
@@ -42,43 +42,42 @@ public class BlackSmithEditCommand implements CommandExecutor {
BlacksmithTrait blacksmithTrait = npc.getTraitNullable(BlacksmithTrait.class);
for (BlacksmithNPCSetting blacksmithNpcSetting : BlacksmithNPCSetting.values()) {
String commandName = blacksmithNpcSetting.getCommandName();
if (commandName.equalsIgnoreCase(args[0])) {
String newValue = args.length < 2 ? null : args[1];
//This makes sure all arguments are treated as a sentence
if (blacksmithNpcSetting.getValueType() == SettingValueType.STRING && args.length > 2) {
newValue = String.join(" ", Arrays.asList(args).subList(1, args.length));
}
return displayOrChangeNPCSetting(blacksmithTrait, blacksmithNpcSetting, newValue, sender);
BlacksmithSetting setting = BlacksmithSetting.getSetting(args[0]);
if (setting != null) {
String newValue = args.length < 2 ? null : args[1];
//This makes sure all arguments are treated as a sentence
if (setting.getValueType() == SettingValueType.STRING && args.length > 2) {
newValue = String.join(" ", Arrays.asList(args).subList(1, args.length));
}
return displayOrChangeNPCSetting(blacksmithTrait, setting, newValue, sender);
} else {
return false;
}
return false;
}
/**
* Changes the given NPC setting, or displays the current value if a new value isn't specified
*
* @param blacksmithTrait <p>The blacksmith trait belonging to the selected NPC</p>
* @param blacksmithNpcSetting <p>The NPC setting to change</p>
* @param newValue <p>The value to change the setting to</p>
* @param sender <p>The command sender to notify about results</p>
* @param blacksmithTrait <p>The blacksmith trait belonging to the selected NPC</p>
* @param blacksmithSetting <p>The NPC setting to change</p>
* @param newValue <p>The value to change the setting to</p>
* @param sender <p>The command sender to notify about results</p>
* @return <p>True if everything went successfully</p>
*/
private boolean displayOrChangeNPCSetting(@NotNull BlacksmithTrait blacksmithTrait,
@NotNull BlacksmithNPCSetting blacksmithNpcSetting,
@NotNull BlacksmithSetting blacksmithSetting,
@Nullable String newValue,
@NotNull CommandSender sender) {
if (newValue == null) {
//Display the current value of the setting
displayNPCSetting(blacksmithTrait, blacksmithNpcSetting, sender);
displayNPCSetting(blacksmithTrait, blacksmithSetting, sender);
} else {
//If an empty value or null, clear the value instead of changing it
if (InputParsingHelper.isEmpty(newValue)) {
newValue = null;
} else {
//Abort if an invalid value is given
boolean isValidType = TypeValidationHelper.isValid(blacksmithNpcSetting.getValueType(), newValue, sender);
boolean isValidType = TypeValidationHelper.isValid(blacksmithSetting.getValueType(), newValue, sender);
if (!isValidType) {
return false;
}
@@ -86,9 +85,9 @@ public class BlackSmithEditCommand implements CommandExecutor {
}
//Change the setting
blacksmithTrait.getSettings().changeSetting(blacksmithNpcSetting, newValue);
blacksmithTrait.getSettings().changeValue(blacksmithSetting, newValue);
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
getValueChangedMessage(blacksmithNpcSetting.getCommandName(), String.valueOf(newValue)));
getValueChangedMessage(blacksmithSetting.getCommandName(), String.valueOf(newValue)));
//Save the changes immediately to prevent data loss on server crash
CitizensAPI.getNPCRegistry().saveToStore();
}
@@ -98,27 +97,27 @@ public class BlackSmithEditCommand implements CommandExecutor {
/**
* Displays the current value of the given NPC setting
*
* @param blacksmithTrait <p>The blacksmith trait of the NPC to get the value from</p>
* @param blacksmithNpcSetting <p>The NPC setting to see the value of</p>
* @param sender <p>The command sender to display the value to</p>
* @param blacksmithTrait <p>The blacksmith trait of the NPC to get the value from</p>
* @param blacksmithSetting <p>The NPC setting to see the value of</p>
* @param sender <p>The command sender to display the value to</p>
*/
private void displayNPCSetting(@NotNull BlacksmithTrait blacksmithTrait,
@NotNull BlacksmithNPCSetting blacksmithNpcSetting, @NotNull CommandSender sender) {
String rawValue = String.valueOf(blacksmithTrait.getSettings().getRawValue(blacksmithNpcSetting));
@NotNull BlacksmithSetting blacksmithSetting, @NotNull CommandSender sender) {
String rawValue = String.valueOf(blacksmithTrait.getSettings().getRawValue(blacksmithSetting));
if (InputParsingHelper.isEmpty(rawValue)) {
//Display the default value, if no custom value has been specified
rawValue = String.valueOf(BlacksmithPlugin.getInstance().getGlobalBlacksmithSettings().getRawValue(
blacksmithNpcSetting));
blacksmithSetting));
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
getCurrentValueMessage(blacksmithNpcSetting.getCommandName(), rawValue));
getCurrentValueMessage(blacksmithSetting.getCommandName(), rawValue));
} else {
//Add a marker if the value has been customized
String marker = BlacksmithPlugin.getTranslator().getTranslatedMessage(
BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER);
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
getCurrentValueMessage(blacksmithNpcSetting.getCommandName(), rawValue) + marker);
getCurrentValueMessage(blacksmithSetting.getCommandName(), rawValue) + marker);
}
if (blacksmithNpcSetting.getPath().startsWith("defaults.messages")) {
if (blacksmithSetting.getPath().startsWith("defaults.messages")) {
sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage(
rawValue.replace(ChatColor.COLOR_CHAR, '&')));
}

View File

@@ -1,6 +1,6 @@
package net.knarcraft.blacksmith.command.blacksmith;
import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSetting;
import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting;
import net.knarcraft.blacksmith.util.TabCompleteValuesHelper;
import net.knarcraft.knarlib.util.TabCompletionHelper;
import org.bukkit.command.Command;
@@ -25,8 +25,10 @@ public class BlackSmithEditTabCompleter implements TabCompleter {
}
List<String> npcSettings = new ArrayList<>();
for (BlacksmithNPCSetting setting : BlacksmithNPCSetting.values()) {
npcSettings.add(setting.getCommandName());
for (BlacksmithSetting setting : BlacksmithSetting.values()) {
if (setting.isPerNPC()) {
npcSettings.add(setting.getCommandName());
}
}
if (args.length == 1) {
@@ -48,13 +50,13 @@ public class BlackSmithEditTabCompleter implements TabCompleter {
* @return <p>Some valid options for the command's argument</p>
*/
private @Nullable List<String> tabCompleteCommandValues(@NotNull String commandName, @NotNull String commandValue) {
for (BlacksmithNPCSetting blacksmithNpcSetting : BlacksmithNPCSetting.values()) {
if (blacksmithNpcSetting.getCommandName().equalsIgnoreCase(commandName)) {
return TabCompletionHelper.filterMatchingContains(TabCompleteValuesHelper.getTabCompletions(
blacksmithNpcSetting.getValueType()), commandValue);
}
BlacksmithSetting setting = BlacksmithSetting.getSetting(commandName);
if (setting != null) {
return TabCompletionHelper.filterMatchingContains(TabCompleteValuesHelper.getTabCompletions(
setting.getValueType()), commandValue);
} else {
return null;
}
return null;
}
}

View File

@@ -1,17 +1,53 @@
package net.knarcraft.blacksmith.command.scrapper;
import org.apache.commons.lang.NotImplementedException;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.command.ReloadCommand;
import net.knarcraft.blacksmith.config.scrapper.GlobalScrapperSettings;
import net.knarcraft.blacksmith.config.scrapper.ScrapperSetting;
import net.knarcraft.blacksmith.util.ConfigCommandHelper;
import net.knarcraft.blacksmith.util.TypeValidationHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
/**
* The command used for changing global scrapper configuration options
*/
public class ScrapperConfigCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] strings) {
throw new NotImplementedException();
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
if (args.length == 0) {
return false;
}
String commandName = args[0];
if (commandName.equalsIgnoreCase("reload")) {
return new ReloadCommand().onCommand(sender, command, label, args);
}
GlobalScrapperSettings settings = BlacksmithPlugin.getInstance().getGlobalScrapperSettings();
//Find which setting the user has specified, if any
ScrapperSetting detectedSetting = ScrapperSetting.getSetting(commandName);
if (detectedSetting == null) {
return false;
}
//Display the current value of a setting
if (args.length == 1) {
ConfigCommandHelper.displayCurrentValue(detectedSetting, settings, sender);
return true;
}
//Change the value of the specified setting
if (TypeValidationHelper.isValid(detectedSetting.getValueType(), args[1], sender)) {
ConfigCommandHelper.changeValue(args, detectedSetting, settings, sender);
return true;
} else {
return false;
}
}
}

View File

@@ -1,20 +1,79 @@
package net.knarcraft.blacksmith.command.scrapper;
import org.apache.commons.lang.NotImplementedException;
import net.knarcraft.blacksmith.config.Setting;
import net.knarcraft.blacksmith.config.scrapper.ScrapperSetting;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import static net.knarcraft.blacksmith.util.TabCompleteValuesHelper.getTabCompletions;
import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains;
/**
* The tab completer for the command used for changing global scrapper configuration options
*/
public class ScrapperConfigTabCompleter implements TabCompleter {
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command,
@NotNull String s, @NotNull String[] strings) {
throw new NotImplementedException();
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command,
@NotNull String s, @NotNull String[] args) {
if (!sender.hasPermission("blacksmith.admin")) {
return new ArrayList<>();
}
//Arguments: <setting> [new value/material or enchantment] []
//Prevent tab-completion when typing messages with spaces
if (args.length > 2 && skipCompletionForSpacedMessage(args[0])) {
return new ArrayList<>();
}
if (args.length == 1) {
List<String> availableCommands = new ArrayList<>();
availableCommands.add("reload");
for (Setting setting : ScrapperSetting.values()) {
availableCommands.add(setting.getCommandName());
}
return filterMatchingContains(availableCommands, args[0]);
} else if (args.length == 2) {
return tabCompleteCommandValues(args[0], args[1]);
}
return null;
}
/**
* Checks whether to tab-complete nothing because a message containing spaces is being written
*
* @param command <p>The command specified by the user</p>
* @return <p>Null if not writing a spaced message</p>
*/
private boolean skipCompletionForSpacedMessage(@NotNull String command) {
Setting scrapperSetting = ScrapperSetting.getSetting(command);
return scrapperSetting != null && scrapperSetting.isMessage();
}
/**
* Tab completes the values available for the given command
*
* @param commandName <p>The name of the used command</p>
* @param commandValue <p>The command value used to filter tab-completions</p>
* @return <p>Some valid options for the command's argument</p>
*/
private List<String> tabCompleteCommandValues(@NotNull String commandName, @NotNull String commandValue) {
if (commandName.equalsIgnoreCase("reload")) {
return new ArrayList<>();
}
Setting scrapperSetting = ScrapperSetting.getSetting(commandName);
if (scrapperSetting != null) {
return filterMatchingContains(getTabCompletions(scrapperSetting.getValueType()), commandValue);
} else {
return null;
}
}
}