2019-10-17 16:04:43 -06:00
|
|
|
package dev.esophose.playerparticles.command;
|
2016-06-10 21:44:08 -06:00
|
|
|
|
2017-04-04 20:31:48 -06:00
|
|
|
import java.util.ArrayList;
|
2017-01-01 19:28:37 -07:00
|
|
|
import java.util.Arrays;
|
2017-03-03 18:40:26 -07:00
|
|
|
import java.util.List;
|
2017-01-01 19:28:37 -07:00
|
|
|
|
2019-10-17 16:04:43 -06:00
|
|
|
import dev.esophose.playerparticles.particles.OtherPPlayer;
|
2016-06-10 21:44:08 -06:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandExecutor;
|
|
|
|
import org.bukkit.command.CommandSender;
|
2018-09-26 02:07:46 -06:00
|
|
|
import org.bukkit.command.TabCompleter;
|
2016-06-10 21:44:08 -06:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2019-10-17 16:04:43 -06:00
|
|
|
import dev.esophose.playerparticles.manager.DataManager;
|
|
|
|
import dev.esophose.playerparticles.manager.LangManager;
|
|
|
|
import dev.esophose.playerparticles.manager.LangManager.Lang;
|
|
|
|
import dev.esophose.playerparticles.manager.PermissionManager;
|
|
|
|
import dev.esophose.playerparticles.particles.PPlayer;
|
2016-06-10 21:44:08 -06:00
|
|
|
|
2018-12-15 18:48:07 -07:00
|
|
|
import net.md_5.bungee.api.ChatColor;
|
|
|
|
|
2018-09-26 02:07:46 -06:00
|
|
|
public class ParticleCommandHandler implements CommandExecutor, TabCompleter {
|
2018-10-06 13:53:31 -06:00
|
|
|
|
2018-03-02 02:47:24 -07:00
|
|
|
/**
|
2018-09-26 02:07:46 -06:00
|
|
|
* A list of all commands
|
2018-03-02 02:47:24 -07:00
|
|
|
*/
|
2018-09-26 02:07:46 -06:00
|
|
|
private static List<CommandModule> commands;
|
2018-12-15 18:48:07 -07:00
|
|
|
private static CommandModuleSecondary ppoCommand;
|
2018-10-06 13:53:31 -06:00
|
|
|
|
2018-03-02 02:47:24 -07:00
|
|
|
static {
|
2019-04-28 00:17:08 -06:00
|
|
|
commands = new ArrayList<>();
|
2018-10-06 13:53:31 -06:00
|
|
|
|
2018-09-26 02:07:46 -06:00
|
|
|
commands.add(new AddCommandModule());
|
|
|
|
commands.add(new DataCommandModule());
|
|
|
|
commands.add(new DefaultCommandModule());
|
|
|
|
commands.add(new EditCommandModule());
|
|
|
|
commands.add(new EffectsCommandModule());
|
|
|
|
commands.add(new FixedCommandModule());
|
|
|
|
commands.add(new GroupCommandModule());
|
|
|
|
commands.add(new GUICommandModule());
|
|
|
|
commands.add(new HelpCommandModule());
|
|
|
|
commands.add(new ListCommandModule());
|
2018-10-28 04:18:34 -06:00
|
|
|
commands.add(new ReloadCommandModule());
|
2018-09-26 02:07:46 -06:00
|
|
|
commands.add(new RemoveCommandModule());
|
|
|
|
commands.add(new ResetCommandModule());
|
|
|
|
commands.add(new StylesCommandModule());
|
2018-11-15 04:13:33 -07:00
|
|
|
commands.add(new ToggleCommandModule());
|
2018-09-26 02:07:46 -06:00
|
|
|
commands.add(new VersionCommandModule());
|
|
|
|
commands.add(new WorldsCommandModule());
|
2018-12-15 18:48:07 -07:00
|
|
|
|
|
|
|
ppoCommand = new OtherCommandModule();
|
2018-09-26 02:07:46 -06:00
|
|
|
}
|
2018-10-06 13:53:31 -06:00
|
|
|
|
2018-09-26 23:31:00 -06:00
|
|
|
/**
|
|
|
|
* Finds a matching CommandModule by its name
|
|
|
|
*
|
|
|
|
* @param commandName The command name
|
|
|
|
* @return The found CommandModule, otherwise null
|
|
|
|
*/
|
2018-09-26 02:07:46 -06:00
|
|
|
public static CommandModule findMatchingCommand(String commandName) {
|
2018-10-06 13:53:31 -06:00
|
|
|
for (CommandModule commandModule : commands)
|
|
|
|
if (commandModule.getName().equalsIgnoreCase(commandName))
|
|
|
|
return commandModule;
|
|
|
|
return null;
|
2018-03-02 02:47:24 -07:00
|
|
|
}
|
2018-10-06 13:53:31 -06:00
|
|
|
|
2018-10-05 21:01:28 -06:00
|
|
|
/**
|
|
|
|
* Get a list of all available commands
|
|
|
|
*
|
|
|
|
* @return A List of all CommandModules registered
|
|
|
|
*/
|
|
|
|
public static List<CommandModule> getCommands() {
|
|
|
|
return commands;
|
|
|
|
}
|
2018-10-06 13:53:31 -06:00
|
|
|
|
2018-10-05 21:01:28 -06:00
|
|
|
/**
|
|
|
|
* Get all available command names
|
|
|
|
*
|
|
|
|
* @return All available command names
|
|
|
|
*/
|
|
|
|
public static List<String> getCommandNames() {
|
2019-04-28 00:17:08 -06:00
|
|
|
List<String> commandNames = new ArrayList<>();
|
2018-10-06 13:53:31 -06:00
|
|
|
for (CommandModule cmd : commands)
|
2018-10-05 21:01:28 -06:00
|
|
|
commandNames.add(cmd.getName());
|
|
|
|
return commandNames;
|
|
|
|
}
|
2016-06-10 21:44:08 -06:00
|
|
|
|
2018-02-25 21:24:22 -07:00
|
|
|
/**
|
2018-12-15 03:14:37 -07:00
|
|
|
* Called when a player executes a PlayerParticles command
|
|
|
|
* Checks what PlayerParticles command it is and calls the corresponding module
|
2018-02-25 21:24:22 -07:00
|
|
|
*
|
|
|
|
* @param sender Who executed the command
|
|
|
|
* @param cmd The command
|
|
|
|
* @param label The command label
|
|
|
|
* @param args The arguments following the command
|
2018-09-26 02:07:46 -06:00
|
|
|
* @return true
|
2018-02-25 21:24:22 -07:00
|
|
|
*/
|
|
|
|
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
2018-12-15 03:14:37 -07:00
|
|
|
if (cmd.getName().equalsIgnoreCase("pp")) {
|
2019-07-16 01:32:45 -06:00
|
|
|
String commandName = args.length > 0 ? args[0] : "";
|
|
|
|
CommandModule commandModule = findMatchingCommand(commandName);
|
|
|
|
if (commandModule == null) {
|
|
|
|
sender.sendMessage(LangManager.getText(Lang.COMMAND_ERROR_UNKNOWN));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
String[] cmdArgs = args.length > 1 ? Arrays.copyOfRange(args, 1, args.length) : new String[0];
|
|
|
|
|
|
|
|
if (!commandModule.canConsoleExecute()) {
|
|
|
|
if (!(sender instanceof Player)) {
|
|
|
|
sender.sendMessage(ChatColor.RED + "Error: This command can only be executed by a player.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
commandModule.onCommandExecute(new OtherPPlayer(sender), cmdArgs);
|
2018-12-15 03:14:37 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Player p = (Player) sender;
|
2018-10-06 13:53:31 -06:00
|
|
|
|
2018-12-15 03:14:37 -07:00
|
|
|
DataManager.getPPlayer(p.getUniqueId(), (pplayer) -> {
|
2019-07-16 01:32:45 -06:00
|
|
|
if (commandModule.requiresEffects() && PermissionManager.getEffectNamesUserHasPermissionFor(p).isEmpty()) {
|
|
|
|
LangManager.sendMessage(pplayer, Lang.COMMAND_ERROR_NO_EFFECTS);
|
2018-10-06 13:53:31 -06:00
|
|
|
} else {
|
2019-07-16 01:32:45 -06:00
|
|
|
commandModule.onCommandExecute(pplayer, cmdArgs);
|
2018-10-06 13:53:31 -06:00
|
|
|
}
|
2018-12-15 03:14:37 -07:00
|
|
|
});
|
|
|
|
} else if (cmd.getName().equalsIgnoreCase("ppo")) {
|
2018-12-15 18:48:07 -07:00
|
|
|
ppoCommand.onCommandExecute(sender, args);
|
2018-12-15 03:14:37 -07:00
|
|
|
}
|
|
|
|
|
2018-03-02 02:47:24 -07:00
|
|
|
return true;
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 02:07:46 -06:00
|
|
|
/**
|
|
|
|
* Activated when a user pushes tab in chat prefixed with /pp
|
|
|
|
*
|
|
|
|
* @param sender The sender that hit tab, should always be a player
|
|
|
|
* @param cmd The command the player is executing
|
|
|
|
* @param alias The possible alias for the command
|
|
|
|
* @param args All arguments following the command
|
|
|
|
* @return A list of commands available to the sender
|
|
|
|
*/
|
|
|
|
public List<String> onTabComplete(CommandSender sender, Command cmd, String alias, String[] args) {
|
|
|
|
if (cmd.getName().equalsIgnoreCase("pp")) {
|
2019-04-28 00:17:08 -06:00
|
|
|
if (!(sender instanceof Player)) return new ArrayList<>();
|
2018-12-15 03:14:37 -07:00
|
|
|
|
2018-12-01 19:34:01 -07:00
|
|
|
PPlayer pplayer = DataManager.getPPlayer(((Player) sender).getUniqueId());
|
2019-04-28 00:17:08 -06:00
|
|
|
if (pplayer == null) return new ArrayList<>();
|
2018-12-01 19:34:01 -07:00
|
|
|
|
2018-10-05 21:01:28 -06:00
|
|
|
if (args.length <= 1) {
|
|
|
|
CommandModule commandModule = findMatchingCommand(""); // Get the default command module
|
2018-12-01 19:34:01 -07:00
|
|
|
return commandModule.onTabComplete(pplayer, args);
|
2019-04-28 00:17:08 -06:00
|
|
|
} else {
|
2018-10-06 13:53:31 -06:00
|
|
|
CommandModule commandModule = findMatchingCommand(args[0]);
|
|
|
|
if (commandModule != null) {
|
2018-12-15 03:14:37 -07:00
|
|
|
String[] cmdArgs = Arrays.copyOfRange(args, 1, args.length);
|
2018-12-01 19:34:01 -07:00
|
|
|
return commandModule.onTabComplete(pplayer, cmdArgs);
|
2018-10-06 13:53:31 -06:00
|
|
|
}
|
|
|
|
}
|
2018-12-15 03:14:37 -07:00
|
|
|
} else if (cmd.getName().equalsIgnoreCase("ppo")) {
|
2018-12-15 18:48:07 -07:00
|
|
|
return ppoCommand.onTabComplete(sender, args);
|
2018-02-25 21:24:22 -07:00
|
|
|
}
|
2018-12-15 03:14:37 -07:00
|
|
|
|
2019-04-28 00:17:08 -06:00
|
|
|
return new ArrayList<>();
|
2018-09-26 02:07:46 -06:00
|
|
|
}
|
2018-02-25 21:24:22 -07:00
|
|
|
|
2016-06-10 21:44:08 -06:00
|
|
|
}
|