Splits the blacksmith command into two commands, and much more

This commit is contained in:
2022-08-19 19:08:54 +02:00
parent 755db8c497
commit e1191dad7d
21 changed files with 1077 additions and 235 deletions

View File

@@ -1,36 +0,0 @@
package net.knarcraft.blacksmith.command;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
/**
* The main command used for everything blacksmith related
*/
public class BlackSmithCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
if (!sender.hasPermission("blacksmith.admin")) {
sender.sendMessage(ChatColor.DARK_RED + "You don't have the necessary permission for using this command.");
return true;
}
//TODO: This command should have one config sub-command which changes the default config values, and all
// setting names which can be changed for each NPC.
if (args.length > 0) {
if (args[0].equalsIgnoreCase("reload")) {
return new ReloadCommand().onCommand(sender, command, label, args);
} else if (args[0].equalsIgnoreCase("config")) {
//TODO: Allow changing any global/default setting + reloading here
} else {
return new NPCSettingCommand().onCommand(sender, command, label, args);
}
}
return false;
}
}

View File

@@ -0,0 +1,85 @@
package net.knarcraft.blacksmith.command;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.GlobalSetting;
import net.knarcraft.blacksmith.config.GlobalSettings;
import net.knarcraft.blacksmith.config.NPCSetting;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.jetbrains.annotations.NotNull;
/**
* The command used for changing global configuration options
*/
public class BlackSmithConfigCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
if (args.length > 0) {
String commandName = args[0];
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
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;
}
if (isSpecialCase(settings, commandName, args)) {
return true;
}
for (GlobalSetting globalSetting : GlobalSetting.values()) {
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]);
}
}
}
return false;
}
/**
* Gets whether the command could be processed as one of the three special cases
*
* @param settings <p>The settings to modify</p>
* @param commandName <p>The sub-command the player specified</p>
* @param args <p>All arguments given</p>
* @return <p>True if already handled as a special case</p>
*/
private boolean isSpecialCase(GlobalSettings settings, String commandName, String[] args) {
if (commandName.equalsIgnoreCase(GlobalSetting.BASE_PRICE.getCommandName())) {
settings.setBasePrice(Material.matchMaterial(args[1]), Double.parseDouble(args[2]));
return true;
}
if (commandName.equalsIgnoreCase(GlobalSetting.PRICE_PER_DURABILITY_POINT.getCommandName())) {
settings.setPricePerDurabilityPoint(Material.matchMaterial(args[1]), Double.parseDouble(args[2]));
return true;
}
if (commandName.equalsIgnoreCase(GlobalSetting.ENCHANTMENT_COST.getCommandName())) {
settings.setEnchantmentCost(Enchantment.getByKey(NamespacedKey.minecraft(args[1])), Double.parseDouble(args[2]));
return true;
}
return false;
}
}

View File

@@ -0,0 +1,64 @@
package net.knarcraft.blacksmith.command;
import net.knarcraft.blacksmith.config.GlobalSetting;
import net.knarcraft.blacksmith.config.NPCSetting;
import net.knarcraft.blacksmith.util.TabCompleteValuesHelper;
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;
/**
* The tab completer for the command used for changing global configuration options
*/
public class BlackSmithConfigTabCompleter implements TabCompleter {
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
if (args.length == 1) {
if (!sender.hasPermission("blacksmith.admin")) {
return new ArrayList<>();
}
List<String> availableCommands = new ArrayList<>();
availableCommands.add("reload");
for (NPCSetting setting : NPCSetting.values()) {
availableCommands.add(setting.getCommandName());
}
for (GlobalSetting globalSetting : GlobalSetting.values()) {
availableCommands.add(globalSetting.getCommandName());
}
return availableCommands;
} else if (args.length == 2) {
return tabCompleteCommandValues(args[0]);
}
return null;
}
/**
* Tab completes the values available for the given command
*
* @param commandName <p>The name of the used command</p>
* @return <p>Some valid options for the command's argument</p>
*/
private List<String> tabCompleteCommandValues(String commandName) {
for (GlobalSetting globalSetting : GlobalSetting.values()) {
if (globalSetting.getCommandName().equalsIgnoreCase(commandName)) {
return TabCompleteValuesHelper.getTabCompletions(globalSetting.getValueType());
}
}
for (NPCSetting npcSetting : NPCSetting.values()) {
if (npcSetting.getCommandName().equalsIgnoreCase(commandName)) {
return TabCompleteValuesHelper.getTabCompletions(npcSetting.getValueType());
}
}
return null;
}
}

View File

@@ -0,0 +1,70 @@
package net.knarcraft.blacksmith.command;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.blacksmith.config.NPCSetting;
import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.blacksmith.util.TypeValidationHelper;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
/**
* The main command used for blacksmith editing
*/
public class BlackSmithEditCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender);
if (npc == null || !npc.hasTrait(BlacksmithTrait.class)) {
sender.sendMessage(ChatColor.DARK_RED + "You must select an NPC before running this command");
return true;
}
BlacksmithTrait blacksmithTrait = npc.getTrait(BlacksmithTrait.class);
for (NPCSetting npcSetting : NPCSetting.values()) {
String commandName = npcSetting.getCommandName();
if (commandName.equalsIgnoreCase(args[0])) {
String newValue = args.length < 2 ? null : args[1];
return changeNPCSetting(blacksmithTrait, npcSetting, newValue, sender);
}
}
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 npcSetting <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 changeNPCSetting(BlacksmithTrait blacksmithTrait, NPCSetting npcSetting, String newValue,
CommandSender sender) {
if (newValue == null) {
//Display the current value of the setting
sender.sendMessage(ChatColor.GREEN + "Current value of " + npcSetting.getCommandName() + ": " +
ChatColor.GOLD + blacksmithTrait.getSettings().getRawValue(npcSetting));
return true;
} else {
boolean isValidType = TypeValidationHelper.isValid(npcSetting.getValueType(), newValue, sender);
if (isValidType) {
//Change the setting
blacksmithTrait.getSettings().changeSetting(npcSetting,
ChatColor.translateAlternateColorCodes('&', newValue));
sender.sendMessage(ChatColor.GREEN + npcSetting.getNodeName() + " set to " + ChatColor.GOLD + newValue);
return true;
} else {
return false;
}
}
}
}

View File

@@ -0,0 +1,57 @@
package net.knarcraft.blacksmith.command;
import net.knarcraft.blacksmith.config.NPCSetting;
import net.knarcraft.blacksmith.util.TabCompleteValuesHelper;
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;
/**
* The tab completer for the blacksmith editing command
*/
public class BlackSmithEditTabCompleter implements TabCompleter {
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
List<String> npcSettings = new ArrayList<>();
for (NPCSetting setting : NPCSetting.values()) {
npcSettings.add(setting.getCommandName());
}
if (args.length == 1) {
if (!sender.hasPermission("blacksmith.edit")) {
return new ArrayList<>();
}
return npcSettings;
} else {
if (npcSettings.contains(args[0]) && args.length <= 2) {
return tabCompleteCommandValues(args[0]);
} else {
return new ArrayList<>();
}
}
}
/**
* Tab completes the values available for the given command
*
* @param commandName <p>The name of the used command</p>
* @return <p>Some valid options for the command's argument</p>
*/
private List<String> tabCompleteCommandValues(String commandName) {
for (NPCSetting npcSetting : NPCSetting.values()) {
if (npcSetting.getCommandName().equalsIgnoreCase(commandName)) {
return TabCompleteValuesHelper.getTabCompletions(npcSetting.getValueType());
}
}
return null;
}
}

View File

@@ -1,44 +0,0 @@
package net.knarcraft.blacksmith.command;
import net.knarcraft.blacksmith.config.NPCSetting;
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;
/**
* The tab completer for the main blacksmith command
*/
public class BlackSmithTabCompleter implements TabCompleter {
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
List<String> npcSettings = new ArrayList<>();
for (NPCSetting setting : NPCSetting.values()) {
npcSettings.add(setting.getCommandName());
}
if (args.length == 1) {
if (!sender.hasPermission("blacksmith.admin")) {
return new ArrayList<>();
}
List<String> availableCommands = new ArrayList<>(npcSettings);
availableCommands.add("reload");
return availableCommands;
} else {
if (npcSettings.contains(args[0])) {
return new NPCSettingTabCompleter().onTabComplete(sender, command, label, args);
} else {
return new ArrayList<>();
}
}
}
}

View File

@@ -1,44 +0,0 @@
package net.knarcraft.blacksmith.command;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.blacksmith.config.NPCSetting;
import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
/**
* A class to perform everything related to changing a blacksmith's options
*/
public class NPCSettingCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender);
if (npc == null || !npc.hasTrait(BlacksmithTrait.class)) {
sender.sendMessage(ChatColor.DARK_RED + "You must select an NPC before running this command");
return true;
}
BlacksmithTrait blacksmithTrait = npc.getTrait(BlacksmithTrait.class);
for (NPCSetting npcSetting : NPCSetting.values()) {
String commandName = npcSetting.getCommandName();
if (commandName.equalsIgnoreCase(args[0])) {
if (args.length < 2) {
sender.sendMessage(ChatColor.GREEN + "Current value of " + commandName + ": " +
ChatColor.GOLD + blacksmithTrait.getSettings().getRawValue(npcSetting));
} else {
blacksmithTrait.getSettings().changeSetting(npcSetting,
ChatColor.translateAlternateColorCodes('&', args[1]));
sender.sendMessage(ChatColor.GREEN + npcSetting.getNodeName() + " set to " + ChatColor.GOLD + args[1]);
}
return true;
}
}
return false;
}
}

View File

@@ -1,24 +0,0 @@
package net.knarcraft.blacksmith.command;
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.List;
/**
* The tab completer for the NPC setting command
*/
public class NPCSettingTabCompleter implements TabCompleter {
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
//TODO: Add tab completions based on each setting's type
return null;
}
}