Advertisement
Guest User

Untitled

a guest
Jun 29th, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. package it.tristana.commons.command;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import org.bukkit.command.Command;
  7. import org.bukkit.command.CommandExecutor;
  8. import org.bukkit.command.CommandSender;
  9. import org.bukkit.entity.Player;
  10.  
  11. import it.tristana.commons.helper.CommonsHelper;
  12.  
  13. public class MainCommand implements CommandExecutor {
  14.  
  15.     private String command;
  16.     private Map<String, SubCommand> commands;
  17.  
  18.     public MainCommand(String command) {
  19.         this.command = command;
  20.         commands = new HashMap<String, SubCommand>();
  21.         registerSubCommand(CommandHelp.COMMAND, new CommandHelp(this));
  22.     }
  23.  
  24.     @Override
  25.     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  26.         if (args.length > 0) {
  27.             SubCommand subCommand = commands.get(args[0].toLowerCase());
  28.             if (subCommand != null && canExecute(sender, subCommand)) {
  29.                 subCommand.execute(sender, args);
  30.             }
  31.             else {
  32.                 help(sender);
  33.             }
  34.         }
  35.         else {
  36.             help(sender);
  37.         }
  38.         return true;
  39.     }
  40.  
  41.     static boolean canExecute(CommandSender sender, SubCommand command) {
  42.         boolean result = false;
  43.         String permission = command.getPermission();
  44.         if (permission == null || sender.hasPermission(permission)) {
  45.             if (command.isOnlyPlayers()) {
  46.                 if (sender instanceof Player) {
  47.                     result = true;
  48.                 }
  49.             }
  50.             else {
  51.                 result = true;
  52.             }
  53.         }
  54.         return result;
  55.     }
  56.    
  57.     private void help(CommandSender sender) {
  58.         CommonsHelper.info(sender, String.format("Type '/%s %s' for a list of commands", command, CommandHelp.COMMAND));
  59.     }
  60.    
  61.     public void registerSubCommand(String name, SubCommand command) {
  62.         commands.put(name.toLowerCase(), command);
  63.     }
  64.    
  65.     public String getCommand() {
  66.         return command;
  67.     }
  68.    
  69.     public Map<String, SubCommand> getCommands() {
  70.         return commands;
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement