daily pastebin goal
39%
SHARE
TWEET

Command.java

a guest Dec 12th, 2015 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  *   Copyright 2015 Alexander Sagen <alexmsagen@gmail.com> and Thomas Strømmen <thomasstrommen@hotmail.com>
  3.  *
  4.  *   Licensed under the Apache License, Version 2.0 (the "License");
  5.  *   you may not use this file except in compliance with the License.
  6.  *   You may obtain a copy of the License at
  7.  *
  8.  *       http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *   Unless required by applicable law or agreed to in writing, software
  11.  *   distributed under the License is distributed on an "AS IS" BASIS,
  12.  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *   See the License for the specific language governing permissions and
  14.  *   limitations under the License.
  15.  */
  16. package org.pzyko.sh.command;
  17.  
  18. import java.lang.reflect.Field;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21.  
  22. import org.bukkit.Bukkit;
  23. import org.bukkit.command.CommandMap;
  24. import org.bukkit.command.CommandSender;
  25. import org.bukkit.command.TabExecutor;
  26. import org.bukkit.entity.Player;
  27. import org.bukkit.permissions.Permission;
  28.  
  29. public abstract class Command implements TabExecutor {
  30.  
  31.     private class ReflectCommand extends org.bukkit.command.Command {
  32.  
  33.         private TabExecutor exe = null;
  34.  
  35.         protected ReflectCommand(String command) {
  36.             super(command);
  37.         }
  38.  
  39.         @Override
  40.         public boolean execute(CommandSender sender, String commandLabel, String[] args) {
  41.             if (exe != null) {
  42.                 exe.onCommand(sender, this, commandLabel, args);
  43.             }
  44.             return false;
  45.         }
  46.  
  47.         public void setExecutor(TabExecutor exe) {
  48.             this.exe = exe;
  49.         }
  50.  
  51.         @Override
  52.         public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
  53.             if (exe != null)
  54.                 return exe.onTabComplete(sender, this, alias, args);
  55.             return null;
  56.         }
  57.     }
  58.  
  59.     protected String command;
  60.     protected String description;
  61.     protected List<String> alias;
  62.     protected String usage;
  63.     protected String permMessage;
  64.  
  65.     protected static CommandMap cmap;
  66.  
  67.     public Command(String command) {
  68.         this(command, null, null, null, null);
  69.     }
  70.  
  71.     public Command(String command, String usage) {
  72.         this(command, usage, null, null, null);
  73.     }
  74.  
  75.     public Command(String command, String usage, String description) {
  76.         this(command, usage, description, null, null);
  77.     }
  78.  
  79.     public Command(String command, String usage, String description, List<String> aliases) {
  80.         this(command, usage, description, null, aliases);
  81.     }
  82.  
  83.     public Command(String command, String usage, String description, String permissionMessage) {
  84.         this(command, usage, description, permissionMessage, null);
  85.     }
  86.  
  87.     public Command(String command, String usage, String description, String permissionMessage, List<String> aliases) {
  88.         this.command = command.toLowerCase();
  89.         this.usage = usage;
  90.         this.description = description;
  91.         this.permMessage = permissionMessage;
  92.         this.alias = aliases;
  93.         register();
  94.     }
  95.  
  96.     CommandMap getCommandMap() {
  97.         if (cmap == null) {
  98.             try {
  99.                 Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
  100.                 f.setAccessible(true);
  101.                 cmap = (CommandMap) f.get(Bukkit.getServer());
  102.                 return getCommandMap();
  103.             } catch (Exception e) {
  104.                 e.printStackTrace();
  105.             }
  106.         } else if (cmap != null) {
  107.             return cmap;
  108.         }
  109.         return getCommandMap();
  110.     }
  111.  
  112.     public boolean isAuthorized(CommandSender sender,
  113.             Permission perm) {
  114.         return sender.hasPermission(perm);
  115.     }
  116.  
  117.     public boolean isAuthorized(CommandSender sender,
  118.             String permission) {
  119.         return sender.hasPermission(permission);
  120.     }
  121.  
  122.     public boolean isAuthorized(Player player, Permission perm) {
  123.         return player.hasPermission(perm);
  124.     }
  125.  
  126.     public boolean isAuthorized(Player player, String permission) {
  127.         return player.hasPermission(permission);
  128.     }
  129.  
  130.     public boolean isPlayer(CommandSender sender) {
  131.         return (sender instanceof Player);
  132.     }
  133.  
  134.     @Override
  135.     public abstract boolean onCommand(CommandSender sender, org.bukkit.command.Command command, String label, String[] args);
  136.  
  137.     @Override
  138.     public List<String> onTabComplete(CommandSender sender, org.bukkit.command.Command command, String label, String[] args) {
  139.      ArrayList<String> players = new ArrayList<String>();
  140.         for (Player o : Bukkit.getOnlinePlayers()) {
  141.             if (o.getName().toUpperCase().startsWith(args[args.length - 1].toUpperCase())) {
  142.                 players.add(o.getName());
  143.             }
  144.         }
  145.         return players;
  146.     }
  147.    
  148.     public void register() {
  149.         ReflectCommand cmd = new ReflectCommand(this.command);
  150.         if (this.alias != null) {
  151.             cmd.setAliases(this.alias);
  152.         }
  153.         if (this.description != null) {
  154.             cmd.setDescription(this.description);
  155.         }
  156.         if (this.usage != null) {
  157.             cmd.setUsage(this.usage);
  158.         }
  159.         if (this.permMessage != null) {
  160.             cmd.setPermissionMessage(this.permMessage);
  161.         }
  162.         String fallback = "";
  163.         if(alias != null && !alias.isEmpty()) fallback = alias.get(0);
  164.         getCommandMap().register(fallback, cmd);
  165.         cmd.setExecutor(this);
  166.     };
  167.    
  168. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top