Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Copyright 2015 Alexander Sagen <alexmsagen@gmail.com> and Thomas Strømmen <thomasstrommen@hotmail.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.pzyko.sh.command;
- import java.lang.reflect.Field;
- import java.util.ArrayList;
- import java.util.List;
- import org.bukkit.Bukkit;
- import org.bukkit.command.CommandMap;
- import org.bukkit.command.CommandSender;
- import org.bukkit.command.TabExecutor;
- import org.bukkit.entity.Player;
- import org.bukkit.permissions.Permission;
- public abstract class Command implements TabExecutor {
- private class ReflectCommand extends org.bukkit.command.Command {
- private TabExecutor exe = null;
- protected ReflectCommand(String command) {
- super(command);
- }
- @Override
- public boolean execute(CommandSender sender, String commandLabel, String[] args) {
- if (exe != null) {
- exe.onCommand(sender, this, commandLabel, args);
- }
- return false;
- }
- public void setExecutor(TabExecutor exe) {
- this.exe = exe;
- }
- @Override
- public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
- if (exe != null)
- return exe.onTabComplete(sender, this, alias, args);
- return null;
- }
- }
- protected String command;
- protected String description;
- protected List<String> alias;
- protected String usage;
- protected String permMessage;
- protected static CommandMap cmap;
- public Command(String command) {
- this(command, null, null, null, null);
- }
- public Command(String command, String usage) {
- this(command, usage, null, null, null);
- }
- public Command(String command, String usage, String description) {
- this(command, usage, description, null, null);
- }
- public Command(String command, String usage, String description, List<String> aliases) {
- this(command, usage, description, null, aliases);
- }
- public Command(String command, String usage, String description, String permissionMessage) {
- this(command, usage, description, permissionMessage, null);
- }
- public Command(String command, String usage, String description, String permissionMessage, List<String> aliases) {
- this.command = command.toLowerCase();
- this.usage = usage;
- this.description = description;
- this.permMessage = permissionMessage;
- this.alias = aliases;
- register();
- }
- CommandMap getCommandMap() {
- if (cmap == null) {
- try {
- Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
- f.setAccessible(true);
- cmap = (CommandMap) f.get(Bukkit.getServer());
- return getCommandMap();
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else if (cmap != null) {
- return cmap;
- }
- return getCommandMap();
- }
- public boolean isAuthorized(CommandSender sender,
- Permission perm) {
- return sender.hasPermission(perm);
- }
- public boolean isAuthorized(CommandSender sender,
- String permission) {
- return sender.hasPermission(permission);
- }
- public boolean isAuthorized(Player player, Permission perm) {
- return player.hasPermission(perm);
- }
- public boolean isAuthorized(Player player, String permission) {
- return player.hasPermission(permission);
- }
- public boolean isPlayer(CommandSender sender) {
- return (sender instanceof Player);
- }
- @Override
- public abstract boolean onCommand(CommandSender sender, org.bukkit.command.Command command, String label, String[] args);
- @Override
- public List<String> onTabComplete(CommandSender sender, org.bukkit.command.Command command, String label, String[] args) {
- ArrayList<String> players = new ArrayList<String>();
- for (Player o : Bukkit.getOnlinePlayers()) {
- if (o.getName().toUpperCase().startsWith(args[args.length - 1].toUpperCase())) {
- players.add(o.getName());
- }
- }
- return players;
- }
- public void register() {
- ReflectCommand cmd = new ReflectCommand(this.command);
- if (this.alias != null) {
- cmd.setAliases(this.alias);
- }
- if (this.description != null) {
- cmd.setDescription(this.description);
- }
- if (this.usage != null) {
- cmd.setUsage(this.usage);
- }
- if (this.permMessage != null) {
- cmd.setPermissionMessage(this.permMessage);
- }
- String fallback = "";
- if(alias != null && !alias.isEmpty()) fallback = alias.get(0);
- getCommandMap().register(fallback, cmd);
- cmd.setExecutor(this);
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement