From d3844ae12c2b16d89c8ccf33edd700dcbd4d3802 Mon Sep 17 00:00:00 2001 From: Aria Date: Fri, 13 Sep 2019 01:35:44 +0200 Subject: [PATCH] Made commands customizable --- .../java/net/Indyuce/mmocore/MMOCore.java | 37 +++++++++++++------ .../mmocore/command/AttributesCommand.java | 15 ++++++-- .../Indyuce/mmocore/command/ClassCommand.java | 15 ++++++-- .../mmocore/command/DepositCommand.java | 15 ++++++-- .../mmocore/command/FriendsCommand.java | 15 ++++++-- .../Indyuce/mmocore/command/PartyCommand.java | 15 ++++++-- .../mmocore/command/PlayerStatsCommand.java | 15 ++++++-- .../mmocore/command/QuestsCommand.java | 15 ++++++-- .../mmocore/command/SkillsCommand.java | 15 ++++++-- .../mmocore/command/WaypointsCommand.java | 15 ++++++-- .../mmocore/command/WithdrawCommand.java | 15 ++++++-- .../mmocore/manager/ConfigManager.java | 1 + src/main/resources/default/commands.yml | 30 +++++++++++++++ src/main/resources/plugin.yml | 23 ------------ 14 files changed, 167 insertions(+), 74 deletions(-) create mode 100644 src/main/resources/default/commands.yml diff --git a/src/main/java/net/Indyuce/mmocore/MMOCore.java b/src/main/java/net/Indyuce/mmocore/MMOCore.java index a6f1cbd7..7271c273 100644 --- a/src/main/java/net/Indyuce/mmocore/MMOCore.java +++ b/src/main/java/net/Indyuce/mmocore/MMOCore.java @@ -1,9 +1,12 @@ package net.Indyuce.mmocore; import java.io.File; +import java.lang.reflect.Field; import java.util.logging.Level; import org.bukkit.Bukkit; +import org.bukkit.command.CommandMap; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; @@ -268,18 +271,30 @@ public class MMOCore extends JavaPlugin { Bukkit.getOnlinePlayers().forEach(player -> PlayerData.setup(player).setPlayer(player)); // commands - getCommand("player").setExecutor(new PlayerStatsCommand()); - getCommand("attributes").setExecutor(new AttributesCommand()); - getCommand("class").setExecutor(new ClassCommand()); - getCommand("waypoints").setExecutor(new WaypointsCommand()); - getCommand("quests").setExecutor(new QuestsCommand()); - getCommand("skills").setExecutor(new SkillsCommand()); - getCommand("friends").setExecutor(new FriendsCommand()); - getCommand("party").setExecutor(new PartyCommand()); + try { + final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap"); + + bukkitCommandMap.setAccessible(true); + CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer()); + + FileConfiguration config = new ConfigFile("commands").getConfig(); + + commandMap.register(config.getString("player"), new PlayerStatsCommand(config.getConfigurationSection("player"))); + commandMap.register(config.getString("attributes"), new AttributesCommand(config.getConfigurationSection("attributes"))); + commandMap.register(config.getString("class"), new ClassCommand(config.getConfigurationSection("class"))); + commandMap.register(config.getString("waypoints"), new WaypointsCommand(config.getConfigurationSection("waypoints"))); + commandMap.register(config.getString("quests"), new QuestsCommand(config.getConfigurationSection("quests"))); + commandMap.register(config.getString("skills"), new SkillsCommand(config.getConfigurationSection("skills"))); + commandMap.register(config.getString("friends"), new FriendsCommand(config.getConfigurationSection("friends"))); + commandMap.register(config.getString("party"), new PartyCommand(config.getConfigurationSection("party"))); - if (hasEconomy() && economy.isValid()) { - getCommand("withdraw").setExecutor(new WithdrawCommand()); - getCommand("deposit").setExecutor(new DepositCommand()); + if (hasEconomy() && economy.isValid()) { + commandMap.register(config.getString("withdraw"), new WithdrawCommand(config.getConfigurationSection("withdraw"))); + commandMap.register(config.getString("deposit"), new DepositCommand(config.getConfigurationSection("deposit"))); + } + } + catch(NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) { + ex.printStackTrace(); } MMOCoreCommand mmoCoreCommand = new MMOCoreCommand(); diff --git a/src/main/java/net/Indyuce/mmocore/command/AttributesCommand.java b/src/main/java/net/Indyuce/mmocore/command/AttributesCommand.java index a517d89b..2799e166 100644 --- a/src/main/java/net/Indyuce/mmocore/command/AttributesCommand.java +++ b/src/main/java/net/Indyuce/mmocore/command/AttributesCommand.java @@ -1,17 +1,24 @@ package net.Indyuce.mmocore.command; import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.manager.InventoryManager; -public class AttributesCommand implements CommandExecutor { +public class AttributesCommand extends BukkitCommand { + public AttributesCommand(ConfigurationSection config) { + super(config.getString("main")); + + setAliases(config.getStringList("aliases")); + setDescription("Opens the attribute menu."); + } + @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + public boolean execute(CommandSender sender, String label, String[] args) { if (!(sender instanceof Player)) { sender.sendMessage(ChatColor.RED + "This command is for players only."); return true; diff --git a/src/main/java/net/Indyuce/mmocore/command/ClassCommand.java b/src/main/java/net/Indyuce/mmocore/command/ClassCommand.java index af3daf8d..ec788c16 100644 --- a/src/main/java/net/Indyuce/mmocore/command/ClassCommand.java +++ b/src/main/java/net/Indyuce/mmocore/command/ClassCommand.java @@ -2,17 +2,24 @@ package net.Indyuce.mmocore.command; import org.bukkit.Bukkit; import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.manager.InventoryManager; -public class ClassCommand implements CommandExecutor { +public class ClassCommand extends BukkitCommand { + public ClassCommand(ConfigurationSection config) { + super(config.getString("main")); + + setAliases(config.getStringList("aliases")); + setDescription("Select a new class."); + } + @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + public boolean execute(CommandSender sender, String label, String[] args) { if (!sender.hasPermission("mmocore.class-select")) return false; diff --git a/src/main/java/net/Indyuce/mmocore/command/DepositCommand.java b/src/main/java/net/Indyuce/mmocore/command/DepositCommand.java index dc70d84d..214334e4 100644 --- a/src/main/java/net/Indyuce/mmocore/command/DepositCommand.java +++ b/src/main/java/net/Indyuce/mmocore/command/DepositCommand.java @@ -2,16 +2,23 @@ package net.Indyuce.mmocore.command; import org.bukkit.Bukkit; import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import net.Indyuce.mmocore.gui.eco.DepositMenu; -public class DepositCommand implements CommandExecutor { +public class DepositCommand extends BukkitCommand { + public DepositCommand(ConfigurationSection config) { + super(config.getString("main")); + + setAliases(config.getStringList("aliases")); + setDescription("Opens the currency deposit menu."); + } + @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + public boolean execute(CommandSender sender, String label, String[] args) { if (!sender.hasPermission("mmocore.currency")) return false; diff --git a/src/main/java/net/Indyuce/mmocore/command/FriendsCommand.java b/src/main/java/net/Indyuce/mmocore/command/FriendsCommand.java index 04808e37..ab988ffb 100644 --- a/src/main/java/net/Indyuce/mmocore/command/FriendsCommand.java +++ b/src/main/java/net/Indyuce/mmocore/command/FriendsCommand.java @@ -3,9 +3,9 @@ package net.Indyuce.mmocore.command; import java.util.UUID; import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import net.Indyuce.mmocore.MMOCore; @@ -15,9 +15,16 @@ import net.Indyuce.mmocore.api.player.social.FriendRequest; import net.Indyuce.mmocore.api.player.social.Request; import net.Indyuce.mmocore.manager.InventoryManager; -public class FriendsCommand implements CommandExecutor { +public class FriendsCommand extends BukkitCommand { + public FriendsCommand(ConfigurationSection config) { + super(config.getString("main")); + + setAliases(config.getStringList("aliases")); + setDescription("Opens the friends menu."); + } + @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + public boolean execute(CommandSender sender, String label, String[] args) { if (!(sender instanceof Player)) { sender.sendMessage(ChatColor.RED + "This command is for players only."); return true; diff --git a/src/main/java/net/Indyuce/mmocore/command/PartyCommand.java b/src/main/java/net/Indyuce/mmocore/command/PartyCommand.java index 6eec1c88..c2d8e9e1 100644 --- a/src/main/java/net/Indyuce/mmocore/command/PartyCommand.java +++ b/src/main/java/net/Indyuce/mmocore/command/PartyCommand.java @@ -4,9 +4,9 @@ import java.util.UUID; import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import net.Indyuce.mmocore.MMOCore; @@ -15,10 +15,17 @@ import net.Indyuce.mmocore.api.player.social.PartyInvite; import net.Indyuce.mmocore.api.player.social.Request; import net.Indyuce.mmocore.manager.InventoryManager; -public class PartyCommand implements CommandExecutor { +public class PartyCommand extends BukkitCommand { + + public PartyCommand(ConfigurationSection config) { + super(config.getString("main")); + + setAliases(config.getStringList("aliases")); + setDescription("Opens the party menu."); + } @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + public boolean execute(CommandSender sender, String label, String[] args) { if (!(sender instanceof Player)) { sender.sendMessage(ChatColor.RED + "This command is for players only."); return true; diff --git a/src/main/java/net/Indyuce/mmocore/command/PlayerStatsCommand.java b/src/main/java/net/Indyuce/mmocore/command/PlayerStatsCommand.java index 4b525c96..b5bca585 100644 --- a/src/main/java/net/Indyuce/mmocore/command/PlayerStatsCommand.java +++ b/src/main/java/net/Indyuce/mmocore/command/PlayerStatsCommand.java @@ -1,17 +1,24 @@ package net.Indyuce.mmocore.command; import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.manager.InventoryManager; -public class PlayerStatsCommand implements CommandExecutor { +public class PlayerStatsCommand extends BukkitCommand { + public PlayerStatsCommand(ConfigurationSection config) { + super(config.getString("main")); + + setAliases(config.getStringList("aliases")); + setDescription("Show player stats."); + } + @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + public boolean execute(CommandSender sender, String label, String[] args) { if (!(sender instanceof Player)) { sender.sendMessage(ChatColor.RED + "This command is for players only."); return true; diff --git a/src/main/java/net/Indyuce/mmocore/command/QuestsCommand.java b/src/main/java/net/Indyuce/mmocore/command/QuestsCommand.java index 6cf0de36..fdd3db7b 100644 --- a/src/main/java/net/Indyuce/mmocore/command/QuestsCommand.java +++ b/src/main/java/net/Indyuce/mmocore/command/QuestsCommand.java @@ -1,16 +1,23 @@ package net.Indyuce.mmocore.command; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.manager.InventoryManager; -public class QuestsCommand implements CommandExecutor { +public class QuestsCommand extends BukkitCommand { + public QuestsCommand(ConfigurationSection config) { + super(config.getString("main")); + + setAliases(config.getStringList("aliases")); + setDescription("Opens the quests menu."); + } + @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + public boolean execute(CommandSender sender, String label, String[] args) { if (sender instanceof Player) InventoryManager.QUEST_LIST.newInventory(PlayerData.get((Player) sender)).open(); return true; diff --git a/src/main/java/net/Indyuce/mmocore/command/SkillsCommand.java b/src/main/java/net/Indyuce/mmocore/command/SkillsCommand.java index 17ceac57..aa1f3430 100644 --- a/src/main/java/net/Indyuce/mmocore/command/SkillsCommand.java +++ b/src/main/java/net/Indyuce/mmocore/command/SkillsCommand.java @@ -1,17 +1,24 @@ package net.Indyuce.mmocore.command; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.manager.InventoryManager; -public class SkillsCommand implements CommandExecutor { +public class SkillsCommand extends BukkitCommand { + public SkillsCommand(ConfigurationSection config) { + super(config.getString("main")); + + setAliases(config.getStringList("aliases")); + setDescription("Opens the skills menu."); + } + @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + public boolean execute(CommandSender sender, String label, String[] args) { if (sender instanceof Player) { PlayerData data = PlayerData.get((Player) sender); if (data.getProfess().getSkills().size() < 1) { diff --git a/src/main/java/net/Indyuce/mmocore/command/WaypointsCommand.java b/src/main/java/net/Indyuce/mmocore/command/WaypointsCommand.java index c55d8630..e826cdbc 100644 --- a/src/main/java/net/Indyuce/mmocore/command/WaypointsCommand.java +++ b/src/main/java/net/Indyuce/mmocore/command/WaypointsCommand.java @@ -1,16 +1,23 @@ package net.Indyuce.mmocore.command; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.manager.InventoryManager; -public class WaypointsCommand implements CommandExecutor { +public class WaypointsCommand extends BukkitCommand { + public WaypointsCommand(ConfigurationSection config) { + super(config.getString("main")); + + setAliases(config.getStringList("aliases")); + setDescription("Open the waypoints menu."); + } + @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + public boolean execute(CommandSender sender, String label, String[] args) { if (sender instanceof Player && sender.hasPermission("mmocore.waypoints")) InventoryManager.WAYPOINTS.newInventory(PlayerData.get((Player) sender)).open(); return true; diff --git a/src/main/java/net/Indyuce/mmocore/command/WithdrawCommand.java b/src/main/java/net/Indyuce/mmocore/command/WithdrawCommand.java index 77e3f66c..26d9764d 100644 --- a/src/main/java/net/Indyuce/mmocore/command/WithdrawCommand.java +++ b/src/main/java/net/Indyuce/mmocore/command/WithdrawCommand.java @@ -4,17 +4,24 @@ import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Sound; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.api.eco.Withdraw; -public class WithdrawCommand implements CommandExecutor { +public class WithdrawCommand extends BukkitCommand { + public WithdrawCommand(ConfigurationSection config) { + super(config.getString("main")); + + setAliases(config.getStringList("aliases")); + setDescription("Creates a withdraw request."); + } + @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + public boolean execute(CommandSender sender, String label, String[] args) { if (!sender.hasPermission("mmocore.currency")) return false; diff --git a/src/main/java/net/Indyuce/mmocore/manager/ConfigManager.java b/src/main/java/net/Indyuce/mmocore/manager/ConfigManager.java index bb15dd39..85ffa4cc 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/ConfigManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/ConfigManager.java @@ -86,6 +86,7 @@ public class ConfigManager { loadDefaultFile("waypoints.yml"); loadDefaultFile("restrictions.yml"); loadDefaultFile("chests.yml"); + loadDefaultFile("commands.yml"); loadOptions(); } diff --git a/src/main/resources/default/commands.yml b/src/main/resources/default/commands.yml new file mode 100644 index 00000000..8f34b4a3 --- /dev/null +++ b/src/main/resources/default/commands.yml @@ -0,0 +1,30 @@ +player: + main: "player" + aliases: ["p", "profile"] +attributes: + main: "attributes" + aliases: ["att", "stats"] +class: + main: "class" + aliases: ["c"] +waypoints: + main: "waypoints" + aliases: ["wp"] +quests: + main: "quests" + aliases: ["q", "journal"] +skills: + main: "skills" + aliases: ["s"] +friends: + main: "friends" + aliases: ["f"] +party: + main: "party" + aliases: [] +withdraw: + main: "withdraw" + aliases: ["w"] +deposit: + main: "deposit" + aliases: ["d"] \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index dc960f5e..612a8afd 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -7,32 +7,9 @@ loadbefore: [MMOItems] softdepend: [Vault,MythicMobs,PlaceholderAPI] api-version: 1.13 commands: - player: - description: Show player stats. - aliases: [p] - class: - description: Choose a new class. - attributes: - description: Opens the attribute menu. - waypoints: - description: Open the waypoints menu. mmocore: description: Main command. aliases: [rpg] - deposit: - description: Opens the currency deposit menu. - withdraw: - description: Creates a withdraw request. - friends: - description: Opens the friends menu. - aliases: [f] - party: - description: Opens the party menu. - aliases: [f] - quests: - description: Opens the quests menu. - skills: - description: Opens the skills menu. permissions: mmocore.admin: description: Access to /rpg