mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-01-26 10:01:46 +01:00
Remove deprecated Command API and getHandle method
This commit is contained in:
parent
5b6f55ec50
commit
0fb3757c8d
@ -25,13 +25,10 @@ import net.citizensnpcs.npc.ai.NPCHolder;
|
||||
import net.citizensnpcs.trait.CurrentLocation;
|
||||
import net.citizensnpcs.util.Messages;
|
||||
import net.citizensnpcs.util.NMS;
|
||||
import net.minecraft.server.v1_4_R1.EntityPlayer;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.craftbukkit.v1_4_R1.CraftServer;
|
||||
import org.bukkit.craftbukkit.v1_4_R1.entity.CraftPlayer;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@ -62,13 +59,6 @@ public class EventListen implements Listener {
|
||||
private final NPCRegistry npcRegistry = CitizensAPI.getNPCRegistry();
|
||||
private final ListMultimap<ChunkCoord, Integer> toRespawn = ArrayListMultimap.create();
|
||||
|
||||
public EventListen() {
|
||||
instance = this; // TODO: remove singleton
|
||||
}
|
||||
|
||||
/*
|
||||
* Chunk events
|
||||
*/
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onChunkLoad(ChunkLoadEvent event) {
|
||||
ChunkCoord coord = toCoord(event.getChunk());
|
||||
@ -175,10 +165,9 @@ public class EventListen implements Listener {
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
|
||||
EntityPlayer handle = ((CraftPlayer) event.getPlayer()).getHandle();
|
||||
if (!(handle instanceof NPCHolder))
|
||||
if (!(event.getPlayer() instanceof NPCHolder))
|
||||
return;
|
||||
((CraftServer) Bukkit.getServer()).getHandle().players.remove(handle);
|
||||
NMS.removeFromServerPlayerList(event.getPlayer());
|
||||
// on teleport, player NPCs are added to the server player list. this is
|
||||
// undesirable as player NPCs are not real players and confuse plugins.
|
||||
}
|
||||
@ -209,10 +198,6 @@ public class EventListen implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Player events
|
||||
*/
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
|
||||
NPC npc = npcRegistry.getNPC(event.getRightClicked());
|
||||
@ -221,7 +206,6 @@ public class EventListen implements Listener {
|
||||
|
||||
Player player = event.getPlayer();
|
||||
|
||||
// Call right-click event
|
||||
NPCRightClickEvent rightClickEvent = new NPCRightClickEvent(npc, player);
|
||||
Bukkit.getPluginManager().callEvent(rightClickEvent);
|
||||
}
|
||||
@ -231,9 +215,6 @@ public class EventListen implements Listener {
|
||||
Editor.leave(event.getPlayer());
|
||||
}
|
||||
|
||||
/*
|
||||
* World events
|
||||
*/
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onWorldLoad(WorldLoadEvent event) {
|
||||
for (ChunkCoord chunk : toRespawn.keySet()) {
|
||||
@ -349,11 +330,8 @@ public class EventListen implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
private static EventListen instance;
|
||||
|
||||
public static void addForRespawn(Location loc, int id) {
|
||||
if (instance == null)
|
||||
return;
|
||||
instance.toRespawn.put(instance.toCoord(loc), id);
|
||||
@EventHandler
|
||||
public void onNeedsRespawn(NPCNeedsRespawnEvent event) {
|
||||
toRespawn.put(toCoord(event.getSpawnLocation()), event.getNPC().getId());
|
||||
}
|
||||
}
|
31
src/main/java/net/citizensnpcs/NPCNeedsRespawnEvent.java
Normal file
31
src/main/java/net/citizensnpcs/NPCNeedsRespawnEvent.java
Normal file
@ -0,0 +1,31 @@
|
||||
package net.citizensnpcs;
|
||||
|
||||
import net.citizensnpcs.api.event.NPCEvent;
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
public class NPCNeedsRespawnEvent extends NPCEvent {
|
||||
private final Location spawn;
|
||||
|
||||
public NPCNeedsRespawnEvent(NPC npc, Location at) {
|
||||
super(npc);
|
||||
this.spawn = at;
|
||||
}
|
||||
|
||||
public Location getSpawnLocation() {
|
||||
return spawn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
package net.citizensnpcs.command;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Deprecated
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Command {
|
||||
/**
|
||||
* A list of root-level command aliases that will be accepted for this
|
||||
* command. For example: <code>{"npc", "npc2"}</code> would match both /npc
|
||||
* and /npc2.
|
||||
*/
|
||||
String[] aliases();
|
||||
|
||||
/**
|
||||
* A short description of the command that will be displayed with the
|
||||
* command usage and help. Translatable.
|
||||
*/
|
||||
String desc();
|
||||
|
||||
/**
|
||||
* Defines the flags available for this command. A flag is a single
|
||||
* character such as <code>-f</code> that will alter the behaviour of the
|
||||
* command. Each character in this string will be counted as a valid flag:
|
||||
* extra flags will be discarded. Accepts * as a catch all.
|
||||
*/
|
||||
String flags() default "";
|
||||
|
||||
/**
|
||||
* A longer description of the command and any flags it uses which will be
|
||||
* displayed in addition to {@link desc} in help commands. Translatable.
|
||||
*/
|
||||
String help() default "";
|
||||
|
||||
/**
|
||||
* The maximum number of arguments that the command will accept. Default is
|
||||
* <code>-1</code>, or an <b>unlimited</b> number of arguments.
|
||||
*/
|
||||
int max() default -1;
|
||||
|
||||
/**
|
||||
* Minimum number of arguments that are accepted by the command.
|
||||
*/
|
||||
int min() default 0;
|
||||
|
||||
/**
|
||||
* The argument modifiers accepted by the command. Also accepts
|
||||
* <code>'*'</code> as a catch all.
|
||||
*/
|
||||
String[] modifiers() default "";
|
||||
|
||||
/**
|
||||
* The permission of the command. The comamnd sender will get an error if
|
||||
* this is not met.
|
||||
*/
|
||||
String permission() default "";
|
||||
|
||||
/**
|
||||
* Command usage string that is displayed when an error occurs with the
|
||||
* command processing.
|
||||
*/
|
||||
String usage() default "";
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package net.citizensnpcs.command;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
import net.citizensnpcs.command.exception.CommandException;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@Deprecated
|
||||
public interface CommandAnnotationProcessor {
|
||||
/**
|
||||
* @return The {@link Annotation} class that this processor will accept.
|
||||
*/
|
||||
Class<? extends Annotation> getAnnotationClass();
|
||||
|
||||
/**
|
||||
* @param sender
|
||||
* The command sender
|
||||
* @param context
|
||||
* The context of the command, including arguments
|
||||
* @param instance
|
||||
* The {@link Annotation} instance
|
||||
* @param args
|
||||
* The method arguments
|
||||
* @throws CommandException
|
||||
* If an exception occurs
|
||||
*/
|
||||
void process(CommandSender sender, CommandContext context, Annotation instance, Object[] args)
|
||||
throws CommandException;
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package net.citizensnpcs.command;
|
||||
|
||||
|
||||
@Deprecated
|
||||
public interface CommandConfigurable {
|
||||
void configure(CommandContext args);
|
||||
}
|
@ -1,253 +0,0 @@
|
||||
// $Id$
|
||||
/*
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.citizensnpcs.command;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.BlockCommandSender;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
@Deprecated
|
||||
public class CommandContext {
|
||||
protected String[] args;
|
||||
protected final Set<Character> flags = new HashSet<Character>();
|
||||
private Location location = null;
|
||||
private final CommandSender sender;
|
||||
protected final Map<String, String> valueFlags = Maps.newHashMap();
|
||||
|
||||
public CommandContext(CommandSender sender, String[] args) {
|
||||
this.sender = sender;
|
||||
int i = 1;
|
||||
for (; i < args.length; i++) {
|
||||
// initial pass for quotes
|
||||
args[i] = args[i].trim();
|
||||
if (args[i].length() == 0) {
|
||||
// Ignore this
|
||||
continue;
|
||||
} else if (args[i].charAt(0) == '\'' || args[i].charAt(0) == '"') {
|
||||
char quote = args[i].charAt(0);
|
||||
String quoted = args[i].substring(1); // remove initial quote
|
||||
for (int inner = i + 1; inner < args.length; inner++) {
|
||||
if (args[inner].isEmpty())
|
||||
continue;
|
||||
String test = args[inner].trim();
|
||||
quoted += " " + test;
|
||||
if (test.charAt(test.length() - 1) == quote) {
|
||||
args[i] = quoted.substring(0, quoted.length() - 1);
|
||||
for (int j = i + 1; j != inner; ++j)
|
||||
args[j] = "";
|
||||
// remove ending quote
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i = 1; i < args.length; ++i) {
|
||||
// second pass for flags
|
||||
if (args[i].length() == 0)
|
||||
continue;
|
||||
if (i + 1 < args.length && args[i].length() > 2 && args[i].matches("^--[a-zA-Z]+$")) {
|
||||
int inner = i + 1;
|
||||
while (args[inner].length() == 0) {
|
||||
// later args may have been quoted
|
||||
++inner;
|
||||
if (inner >= args.length) {
|
||||
inner = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (inner != -1) {
|
||||
valueFlags.put(args[i].toLowerCase().replaceFirst("--", ""), args[inner]);
|
||||
args[i] = "";
|
||||
args[inner] = "";
|
||||
}
|
||||
} else if (args[i].charAt(0) == '-' && args[i].matches("^-[a-zA-Z]+$")) {
|
||||
for (int k = 1; k < args[i].length(); k++)
|
||||
flags.add(args[i].charAt(k));
|
||||
args[i] = "";
|
||||
}
|
||||
}
|
||||
List<String> copied = Lists.newArrayList();
|
||||
for (String arg : args) {
|
||||
arg = arg.trim();
|
||||
if (arg == null || arg.isEmpty())
|
||||
continue;
|
||||
copied.add(arg.trim());
|
||||
}
|
||||
this.args = copied.toArray(new String[copied.size()]);
|
||||
}
|
||||
|
||||
public CommandContext(String[] args) {
|
||||
this(null, args);
|
||||
}
|
||||
|
||||
public int argsLength() {
|
||||
return args.length - 1;
|
||||
}
|
||||
|
||||
public String getCommand() {
|
||||
return args[0];
|
||||
}
|
||||
|
||||
public double getDouble(int index) throws NumberFormatException {
|
||||
return Double.parseDouble(args[index + 1]);
|
||||
}
|
||||
|
||||
public double getDouble(int index, double def) throws NumberFormatException {
|
||||
return index + 1 < args.length ? Double.parseDouble(args[index + 1]) : def;
|
||||
}
|
||||
|
||||
public String getFlag(String ch) {
|
||||
return valueFlags.get(ch);
|
||||
}
|
||||
|
||||
public String getFlag(String ch, String def) {
|
||||
final String value = valueFlags.get(ch);
|
||||
if (value == null) {
|
||||
return def;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public double getFlagDouble(String ch) throws NumberFormatException {
|
||||
return Double.parseDouble(valueFlags.get(ch));
|
||||
}
|
||||
|
||||
public double getFlagDouble(String ch, double def) throws NumberFormatException {
|
||||
final String value = valueFlags.get(ch);
|
||||
if (value == null) {
|
||||
return def;
|
||||
}
|
||||
|
||||
return Double.parseDouble(value);
|
||||
}
|
||||
|
||||
public int getFlagInteger(String ch) throws NumberFormatException {
|
||||
return Integer.parseInt(valueFlags.get(ch));
|
||||
}
|
||||
|
||||
public int getFlagInteger(String ch, int def) throws NumberFormatException {
|
||||
final String value = valueFlags.get(ch);
|
||||
if (value == null) {
|
||||
return def;
|
||||
}
|
||||
|
||||
return Integer.parseInt(value);
|
||||
}
|
||||
|
||||
public Set<Character> getFlags() {
|
||||
return flags;
|
||||
}
|
||||
|
||||
public int getInteger(int index) throws NumberFormatException {
|
||||
return Integer.parseInt(args[index + 1]);
|
||||
}
|
||||
|
||||
public int getInteger(int index, int def) throws NumberFormatException {
|
||||
if (index + 1 < args.length) {
|
||||
try {
|
||||
return Integer.parseInt(args[index + 1]);
|
||||
} catch (NumberFormatException ex) {
|
||||
}
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
public String getJoinedStrings(int initialIndex) {
|
||||
return getJoinedStrings(initialIndex, ' ');
|
||||
}
|
||||
|
||||
public String getJoinedStrings(int initialIndex, char delimiter) {
|
||||
initialIndex = initialIndex + 1;
|
||||
StringBuilder buffer = new StringBuilder(args[initialIndex]);
|
||||
for (int i = initialIndex + 1; i < args.length; i++)
|
||||
buffer.append(delimiter).append(args[i]);
|
||||
return buffer.toString().trim();
|
||||
}
|
||||
|
||||
public String[] getPaddedSlice(int index, int padding) {
|
||||
String[] slice = new String[args.length - index + padding];
|
||||
System.arraycopy(args, index, slice, padding, args.length - index);
|
||||
return slice;
|
||||
}
|
||||
|
||||
public Location getSenderLocation() {
|
||||
if (location != null || sender == null)
|
||||
return location;
|
||||
if (sender instanceof Player)
|
||||
location = ((Player) sender).getLocation();
|
||||
else if (sender instanceof BlockCommandSender)
|
||||
location = ((BlockCommandSender) sender).getBlock().getLocation();
|
||||
return location;
|
||||
}
|
||||
|
||||
public Location getSenderTargetBlockLocation() {
|
||||
if (sender == null)
|
||||
return location;
|
||||
if (sender instanceof Player)
|
||||
location = ((Player) sender).getTargetBlock(null, 50).getLocation();
|
||||
else if (sender instanceof BlockCommandSender)
|
||||
location = ((BlockCommandSender) sender).getBlock().getLocation();
|
||||
return location;
|
||||
}
|
||||
|
||||
public String[] getSlice(int index) {
|
||||
String[] slice = new String[args.length - index];
|
||||
System.arraycopy(args, index, slice, 0, args.length - index);
|
||||
return slice;
|
||||
}
|
||||
|
||||
public String getString(int index) {
|
||||
return args[index + 1];
|
||||
}
|
||||
|
||||
public String getString(int index, String def) {
|
||||
return index + 1 < args.length ? args[index + 1] : def;
|
||||
}
|
||||
|
||||
public Map<String, String> getValueFlags() {
|
||||
return valueFlags;
|
||||
}
|
||||
|
||||
public boolean hasFlag(char ch) {
|
||||
return flags.contains(ch);
|
||||
}
|
||||
|
||||
public boolean hasValueFlag(String ch) {
|
||||
return valueFlags.containsKey(ch);
|
||||
}
|
||||
|
||||
public int length() {
|
||||
return args.length;
|
||||
}
|
||||
|
||||
public boolean matches(String command) {
|
||||
return args[0].equalsIgnoreCase(command);
|
||||
}
|
||||
}
|
@ -1,434 +0,0 @@
|
||||
package net.citizensnpcs.command;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.citizensnpcs.api.util.Messaging;
|
||||
import net.citizensnpcs.command.exception.CommandException;
|
||||
import net.citizensnpcs.command.exception.CommandUsageException;
|
||||
import net.citizensnpcs.command.exception.NoPermissionsException;
|
||||
import net.citizensnpcs.command.exception.ServerCommandException;
|
||||
import net.citizensnpcs.command.exception.UnhandledCommandException;
|
||||
import net.citizensnpcs.command.exception.WrappedCommandException;
|
||||
import net.citizensnpcs.util.Messages;
|
||||
import net.citizensnpcs.util.StringHelper;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.ListMultimap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
@Deprecated
|
||||
public class CommandManager {
|
||||
private final Map<Class<? extends Annotation>, CommandAnnotationProcessor> annotationProcessors = Maps.newHashMap();
|
||||
|
||||
/*
|
||||
* Mapping of commands (including aliases) with a description. Root commands
|
||||
* are stored under a key of null, whereas child commands are cached under
|
||||
* their respective Method. The child map has the key of the command name
|
||||
* (one for each alias) with the method.
|
||||
*/
|
||||
private final Map<String, Method> commands = new HashMap<String, Method>();
|
||||
|
||||
// Stores the injector used to getInstance.
|
||||
private Injector injector;
|
||||
// Used to store the instances associated with a method.
|
||||
private final Map<Method, Object> instances = new HashMap<Method, Object>();
|
||||
private final ListMultimap<Method, Annotation> registeredAnnotations = ArrayListMultimap.create();
|
||||
|
||||
private final Set<Method> serverCommands = new HashSet<Method>();
|
||||
|
||||
/**
|
||||
*
|
||||
* Attempt to execute a command using the root {@link Command} given. A list
|
||||
* of method arguments may be used when calling the command handler method.
|
||||
*
|
||||
* A command handler method should follow the form
|
||||
* <code>command(CommandContext args, CommandSender sender)</code> where
|
||||
* {@link CommandSender} can be replaced with {@link Player} to only accept
|
||||
* players. The method parameters must include the method args given, if
|
||||
* any.
|
||||
*
|
||||
* @param command
|
||||
* The command to execute
|
||||
* @param args
|
||||
* The arguments of the command
|
||||
* @param sender
|
||||
* The sender of the command
|
||||
* @param methodArgs
|
||||
* The method arguments to be used when calling the command
|
||||
* handler
|
||||
* @throws CommandException
|
||||
* Any exceptions caused from execution of the command
|
||||
*/
|
||||
public void execute(org.bukkit.command.Command command, String[] args, CommandSender sender, Object... methodArgs)
|
||||
throws CommandException {
|
||||
// must put command into split.
|
||||
String[] newArgs = new String[args.length + 1];
|
||||
System.arraycopy(args, 0, newArgs, 1, args.length);
|
||||
newArgs[0] = command.getName().toLowerCase();
|
||||
|
||||
Object[] newMethodArgs = new Object[methodArgs.length + 1];
|
||||
System.arraycopy(methodArgs, 0, newMethodArgs, 1, methodArgs.length);
|
||||
executeMethod(newArgs, sender, newMethodArgs);
|
||||
}
|
||||
|
||||
// Attempt to execute a command.
|
||||
private void executeMethod(String[] args, CommandSender sender, Object[] methodArgs) throws CommandException {
|
||||
String cmdName = args[0].toLowerCase();
|
||||
String modifier = args.length > 1 ? args[1] : "";
|
||||
|
||||
Method method = commands.get(cmdName + " " + modifier.toLowerCase());
|
||||
if (method == null)
|
||||
method = commands.get(cmdName + " *");
|
||||
|
||||
if (method == null)
|
||||
throw new UnhandledCommandException();
|
||||
|
||||
if (!serverCommands.contains(method) && sender instanceof ConsoleCommandSender)
|
||||
throw new ServerCommandException();
|
||||
|
||||
if (!hasPermission(method, sender))
|
||||
throw new NoPermissionsException();
|
||||
|
||||
Command cmd = method.getAnnotation(Command.class);
|
||||
CommandContext context = new CommandContext(sender, args);
|
||||
|
||||
if (context.argsLength() < cmd.min())
|
||||
throw new CommandUsageException(Messages.COMMAND_TOO_FEW_ARGUMENTS, getUsage(args, cmd));
|
||||
|
||||
if (cmd.max() != -1 && context.argsLength() > cmd.max())
|
||||
throw new CommandUsageException(Messages.COMMAND_TOO_MANY_ARGUMENTS, getUsage(args, cmd));
|
||||
|
||||
if (!cmd.flags().contains("*")) {
|
||||
for (char flag : context.getFlags())
|
||||
if (cmd.flags().indexOf(String.valueOf(flag)) == -1)
|
||||
throw new CommandUsageException("Unknown flag: " + flag, getUsage(args, cmd));
|
||||
}
|
||||
|
||||
methodArgs[0] = context;
|
||||
|
||||
for (Annotation annotation : registeredAnnotations.get(method)) {
|
||||
CommandAnnotationProcessor processor = annotationProcessors.get(annotation.annotationType());
|
||||
processor.process(sender, context, annotation, methodArgs);
|
||||
}
|
||||
|
||||
Object instance = instances.get(method);
|
||||
try {
|
||||
method.invoke(instance, methodArgs);
|
||||
} catch (IllegalArgumentException e) {
|
||||
logger.log(Level.SEVERE, "Failed to execute command", e);
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.log(Level.SEVERE, "Failed to execute command", e);
|
||||
} catch (InvocationTargetException e) {
|
||||
if (e.getCause() instanceof CommandException)
|
||||
throw (CommandException) e.getCause();
|
||||
throw new WrappedCommandException(e.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A safe version of <code>execute</code> which catches and logs all errors
|
||||
* that occur. Returns whether the command handler should print usage or
|
||||
* not.
|
||||
*
|
||||
* @see #execute(Command, String[], CommandSender, Object...)
|
||||
* @return Whether further usage should be printed
|
||||
*/
|
||||
public boolean executeSafe(org.bukkit.command.Command command, String[] args, CommandSender sender,
|
||||
Object... methodArgs) {
|
||||
try {
|
||||
try {
|
||||
execute(command, args, sender, methodArgs);
|
||||
} catch (ServerCommandException ex) {
|
||||
Messaging.sendTr(sender, Messages.COMMAND_MUST_BE_INGAME);
|
||||
} catch (CommandUsageException ex) {
|
||||
Messaging.sendError(sender, ex.getMessage());
|
||||
Messaging.sendError(sender, ex.getUsage());
|
||||
} catch (UnhandledCommandException ex) {
|
||||
return false;
|
||||
} catch (WrappedCommandException ex) {
|
||||
throw ex.getCause();
|
||||
} catch (CommandException ex) {
|
||||
Messaging.sendError(sender, ex.getMessage());
|
||||
} catch (NumberFormatException ex) {
|
||||
Messaging.sendErrorTr(sender, Messages.COMMAND_INVALID_NUMBER);
|
||||
}
|
||||
} catch (Throwable ex) {
|
||||
ex.printStackTrace();
|
||||
if (sender instanceof Player) {
|
||||
Messaging.sendErrorTr(sender, Messages.COMMAND_REPORT_ERROR);
|
||||
Messaging.sendError(sender, ex.getClass().getName() + ": " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches for the closest modifier using Levenshtein distance to the given
|
||||
* top level command and modifier.
|
||||
*
|
||||
* @param command
|
||||
* The top level command
|
||||
* @param modifier
|
||||
* The modifier to use as the base
|
||||
* @return The closest modifier, or empty
|
||||
*/
|
||||
public String getClosestCommandModifier(String command, String modifier) {
|
||||
int minDist = Integer.MAX_VALUE;
|
||||
command = command.toLowerCase();
|
||||
String closest = "";
|
||||
for (String cmd : commands.keySet()) {
|
||||
String[] split = cmd.split(" ");
|
||||
if (split.length <= 1 || !split[0].equals(command))
|
||||
continue;
|
||||
int distance = StringHelper.getLevenshteinDistance(modifier, split[1]);
|
||||
if (minDist > distance) {
|
||||
minDist = distance;
|
||||
closest = split[1];
|
||||
}
|
||||
}
|
||||
|
||||
return closest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link CommandInfo} for the given top level command and
|
||||
* modifier, or null if not found.
|
||||
*
|
||||
* @param rootCommand
|
||||
* The top level command
|
||||
* @param modifier
|
||||
* The modifier (may be empty)
|
||||
* @return The command info for the command
|
||||
*/
|
||||
public CommandInfo getCommand(String rootCommand, String modifier) {
|
||||
String joined = Joiner.on(' ').join(rootCommand, modifier);
|
||||
for (Entry<String, Method> entry : commands.entrySet()) {
|
||||
if (!entry.getKey().equalsIgnoreCase(joined))
|
||||
continue;
|
||||
Command commandAnnotation = entry.getValue().getAnnotation(Command.class);
|
||||
if (commandAnnotation == null)
|
||||
continue;
|
||||
return new CommandInfo(commandAnnotation);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all modified and root commands from the given root level command.
|
||||
* For example, if <code>/npc look</code> and <code>/npc jump</code> were
|
||||
* defined, calling <code>getCommands("npc")</code> would return
|
||||
* {@link CommandInfo}s for both commands.
|
||||
*
|
||||
* @param command
|
||||
* The root level command
|
||||
* @return The list of {@link CommandInfo}s
|
||||
*/
|
||||
public List<CommandInfo> getCommands(String command) {
|
||||
List<CommandInfo> cmds = Lists.newArrayList();
|
||||
command = command.toLowerCase();
|
||||
for (Entry<String, Method> entry : commands.entrySet()) {
|
||||
if (!entry.getKey().startsWith(command))
|
||||
continue;
|
||||
Command commandAnnotation = entry.getValue().getAnnotation(Command.class);
|
||||
if (commandAnnotation == null)
|
||||
continue;
|
||||
cmds.add(new CommandInfo(commandAnnotation));
|
||||
}
|
||||
return cmds;
|
||||
}
|
||||
|
||||
// Get the usage string for a command.
|
||||
private String getUsage(String[] args, Command cmd) {
|
||||
StringBuilder command = new StringBuilder("/");
|
||||
command.append(args[0] + " ");
|
||||
// removed arbitrary positioning of flags.
|
||||
command.append(cmd.usage());
|
||||
return command.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see whether there is a command handler for the given command at
|
||||
* the root level. This will check aliases as well.
|
||||
*
|
||||
* @param cmd
|
||||
* The command to check
|
||||
* @param modifier
|
||||
* The modifier to check (may be empty)
|
||||
* @return Whether the command is handled
|
||||
*/
|
||||
public boolean hasCommand(org.bukkit.command.Command cmd, String modifier) {
|
||||
String cmdName = cmd.getName().toLowerCase();
|
||||
return commands.containsKey(cmdName + " " + modifier.toLowerCase()) || commands.containsKey(cmdName + " *");
|
||||
}
|
||||
|
||||
// Returns whether a CommandSenders has permission.
|
||||
private boolean hasPermission(CommandSender sender, String perm) {
|
||||
return sender.hasPermission("citizens." + perm);
|
||||
}
|
||||
|
||||
// Returns whether a player has access to a command.
|
||||
private boolean hasPermission(Method method, CommandSender sender) {
|
||||
Command cmd = method.getAnnotation(Command.class);
|
||||
if (cmd.permission().isEmpty() || hasPermission(sender, cmd.permission()) || hasPermission(sender, "admin"))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a class that contains commands (methods annotated with
|
||||
* {@link Command}). If no dependency {@link Injector} is specified, then
|
||||
* only static methods of the class will be registered. Otherwise, new
|
||||
* instances the command class will be created and instance methods will be
|
||||
* called.
|
||||
*
|
||||
* @see #setInjector(Injector)
|
||||
* @param clazz
|
||||
* The class to scan
|
||||
*/
|
||||
public void register(Class<?> clazz) {
|
||||
registerMethods(clazz, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an {@link CommandAnnotationProcessor} that can process
|
||||
* annotations before a command is executed.
|
||||
*
|
||||
* Methods with the {@link Command} annotation will have the rest of their
|
||||
* annotations scanned and stored if there is a matching
|
||||
* {@link CommandAnnotationProcessor}. Annotations that do not have a
|
||||
* processor are discarded. The scanning method uses annotations from the
|
||||
* declaring class as a base before narrowing using the method's
|
||||
* annotations.
|
||||
*
|
||||
* @param processor
|
||||
* The annotation processor
|
||||
*/
|
||||
public void registerAnnotationProcessor(CommandAnnotationProcessor processor) {
|
||||
annotationProcessors.put(processor.getAnnotationClass(), processor);
|
||||
}
|
||||
|
||||
/*
|
||||
* Register the methods of a class. This will automatically construct
|
||||
* instances as necessary.
|
||||
*/
|
||||
private void registerMethods(Class<?> clazz, Method parent) {
|
||||
Object obj = injector != null ? injector.getInstance(clazz) : null;
|
||||
registerMethods(clazz, parent, obj);
|
||||
}
|
||||
|
||||
// Register the methods of a class.
|
||||
private void registerMethods(Class<?> clazz, Method parent, Object obj) {
|
||||
for (Method method : clazz.getMethods()) {
|
||||
if (!method.isAnnotationPresent(Command.class))
|
||||
continue;
|
||||
boolean isStatic = Modifier.isStatic(method.getModifiers());
|
||||
|
||||
Command cmd = method.getAnnotation(Command.class);
|
||||
|
||||
// Cache the aliases too
|
||||
for (String alias : cmd.aliases()) {
|
||||
for (String modifier : cmd.modifiers()) {
|
||||
commands.put(alias + " " + modifier, method);
|
||||
}
|
||||
}
|
||||
|
||||
List<Annotation> annotations = Lists.newArrayList();
|
||||
for (Annotation annotation : method.getDeclaringClass().getAnnotations()) {
|
||||
Class<? extends Annotation> annotationClass = annotation.annotationType();
|
||||
if (annotationProcessors.containsKey(annotationClass))
|
||||
annotations.add(annotation);
|
||||
}
|
||||
for (Annotation annotation : method.getAnnotations()) {
|
||||
Class<? extends Annotation> annotationClass = annotation.annotationType();
|
||||
if (!annotationProcessors.containsKey(annotationClass))
|
||||
continue;
|
||||
Iterator<Annotation> itr = annotations.iterator();
|
||||
while (itr.hasNext()) {
|
||||
Annotation previous = itr.next();
|
||||
if (previous.annotationType() == annotationClass) {
|
||||
itr.remove();
|
||||
}
|
||||
}
|
||||
annotations.add(annotation);
|
||||
}
|
||||
|
||||
if (annotations.size() > 0)
|
||||
registeredAnnotations.putAll(method, annotations);
|
||||
|
||||
Class<?>[] parameterTypes = method.getParameterTypes();
|
||||
if (parameterTypes.length <= 1 || parameterTypes[1] == CommandSender.class)
|
||||
serverCommands.add(method);
|
||||
|
||||
// We want to be able invoke with an instance
|
||||
if (!isStatic) {
|
||||
// Can't register this command if we don't have an instance
|
||||
if (obj == null)
|
||||
continue;
|
||||
|
||||
instances.put(method, obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setInjector(Injector injector) {
|
||||
this.injector = injector;
|
||||
}
|
||||
|
||||
public static class CommandInfo {
|
||||
private final Command commandAnnotation;
|
||||
|
||||
public CommandInfo(Command commandAnnotation) {
|
||||
this.commandAnnotation = commandAnnotation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null || getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
CommandInfo other = (CommandInfo) obj;
|
||||
if (commandAnnotation == null) {
|
||||
if (other.commandAnnotation != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!commandAnnotation.equals(other.commandAnnotation)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public Command getCommandAnnotation() {
|
||||
return commandAnnotation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 31 + ((commandAnnotation == null) ? 0 : commandAnnotation.hashCode());
|
||||
}
|
||||
}
|
||||
|
||||
// Logger for general errors.
|
||||
private static final Logger logger = Logger.getLogger(CommandManager.class.getCanonicalName());
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
package net.citizensnpcs.command;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import net.citizensnpcs.api.util.Messaging;
|
||||
|
||||
@Deprecated
|
||||
public class Injector {
|
||||
private final Class<?>[] argClasses;
|
||||
private final Object[] args;
|
||||
|
||||
public Injector(Object... args) {
|
||||
this.args = args;
|
||||
argClasses = new Class[args.length];
|
||||
for (int i = 0; i < args.length; ++i) {
|
||||
argClasses[i] = args[i].getClass();
|
||||
}
|
||||
}
|
||||
|
||||
public Object getInstance(Class<?> clazz) {
|
||||
try {
|
||||
Constructor<?> ctr = clazz.getConstructor(argClasses);
|
||||
ctr.setAccessible(true);
|
||||
return ctr.newInstance(args);
|
||||
} catch (NoSuchMethodException e) {
|
||||
try {
|
||||
return clazz.newInstance();
|
||||
} catch (Exception ex) {
|
||||
Messaging.log(Level.SEVERE, "Error initializing commands class " + clazz + ": ");
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
} catch (InvocationTargetException e) {
|
||||
Messaging.log(Level.SEVERE, "Error initializing commands class " + clazz + ": ");
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (InstantiationException e) {
|
||||
Messaging.log(Level.SEVERE, "Error initializing commands class " + clazz + ": ");
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (IllegalAccessException e) {
|
||||
Messaging.log(Level.SEVERE, "Error initializing commands class " + clazz + ": ");
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package net.citizensnpcs.command.exception;
|
||||
|
||||
import net.citizensnpcs.api.util.Messaging;
|
||||
|
||||
public class CommandException extends Exception {
|
||||
public CommandException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public CommandException(String message) {
|
||||
super(Messaging.tryTranslate(message));
|
||||
}
|
||||
|
||||
public CommandException(String key, Object... replacements) {
|
||||
super(Messaging.tr(key, replacements));
|
||||
}
|
||||
|
||||
public CommandException(Throwable t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 870638193072101739L;
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package net.citizensnpcs.command.exception;
|
||||
|
||||
|
||||
public class CommandUsageException extends CommandException {
|
||||
protected String usage;
|
||||
|
||||
public CommandUsageException(String message, String usage) {
|
||||
super(message);
|
||||
this.usage = usage;
|
||||
}
|
||||
|
||||
public String getUsage() {
|
||||
return usage;
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -6761418114414516542L;
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package net.citizensnpcs.command.exception;
|
||||
|
||||
import net.citizensnpcs.util.Messages;
|
||||
|
||||
public class NoPermissionsException extends CommandException {
|
||||
public NoPermissionsException() {
|
||||
super(Messages.COMMAND_NO_PERMISSION);
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -602374621030168291L;
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package net.citizensnpcs.command.exception;
|
||||
|
||||
|
||||
public class RequirementMissingException extends CommandException {
|
||||
public RequirementMissingException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -4299721983654504028L;
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package net.citizensnpcs.command.exception;
|
||||
|
||||
|
||||
public class ServerCommandException extends CommandException {
|
||||
private static final long serialVersionUID = 9120268556899197316L;
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package net.citizensnpcs.command.exception;
|
||||
|
||||
|
||||
public class UnhandledCommandException extends CommandException {
|
||||
private static final long serialVersionUID = 3370887306593968091L;
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package net.citizensnpcs.command.exception;
|
||||
|
||||
|
||||
public class WrappedCommandException extends CommandException {
|
||||
public WrappedCommandException(Throwable t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -4075721444847778918L;
|
||||
}
|
@ -4,7 +4,7 @@ import java.util.Set;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.citizensnpcs.EventListen;
|
||||
import net.citizensnpcs.NPCNeedsRespawnEvent;
|
||||
import net.citizensnpcs.Settings.Setting;
|
||||
import net.citizensnpcs.api.CitizensAPI;
|
||||
import net.citizensnpcs.api.ai.Navigator;
|
||||
@ -78,11 +78,6 @@ public class CitizensNPC extends AbstractNPC {
|
||||
return entityController.getBukkitEntity();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public EntityLiving getHandle() {
|
||||
return ((CraftLivingEntity) getBukkitEntity()).getHandle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Navigator getNavigator() {
|
||||
return navigator;
|
||||
@ -178,7 +173,7 @@ public class CitizensNPC extends AbstractNPC {
|
||||
Messaging.debug("Retrying spawn of", getId(), "later due to chunk being unloaded.");
|
||||
// we need to wait for a chunk load before trying to spawn
|
||||
entityController.remove();
|
||||
EventListen.addForRespawn(at, getId());
|
||||
Bukkit.getPluginManager().callEvent(new NPCNeedsRespawnEvent(this, at));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,7 @@ import net.minecraft.server.v1_4_R1.EnchantmentManager;
|
||||
import net.minecraft.server.v1_4_R1.Entity;
|
||||
import net.minecraft.server.v1_4_R1.EntityLiving;
|
||||
import net.minecraft.server.v1_4_R1.EntityMonster;
|
||||
import net.minecraft.server.v1_4_R1.EntityPlayer;
|
||||
import net.minecraft.server.v1_4_R1.EntityTypes;
|
||||
import net.minecraft.server.v1_4_R1.MathHelper;
|
||||
import net.minecraft.server.v1_4_R1.MobEffectList;
|
||||
@ -68,6 +69,7 @@ public class NMS {
|
||||
private static Field SPEED_FIELD;
|
||||
private static Set<Integer> STAIR_MATERIALS = Sets.newHashSet();
|
||||
private static Field THREAD_STOPPER;
|
||||
|
||||
public static void addOrRemoveFromPlayerList(LivingEntity bukkitEntity, boolean remove) {
|
||||
if (bukkitEntity == null)
|
||||
return;
|
||||
@ -80,6 +82,7 @@ public class NMS {
|
||||
handle.world.players.add(handle);
|
||||
}
|
||||
}
|
||||
|
||||
public static void attack(EntityLiving handle, EntityLiving target) {
|
||||
int damage = handle instanceof EntityMonster ? ((EntityMonster) handle).c((Entity) target) : 2;
|
||||
|
||||
@ -378,4 +381,9 @@ public class NMS {
|
||||
STAIR_MATERIALS.add(material.getId());
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeFromServerPlayerList(Player player) {
|
||||
EntityPlayer handle = ((CraftPlayer) player).getHandle();
|
||||
((CraftServer) Bukkit.getServer()).getHandle().players.remove(handle);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user