mirror of
https://github.com/essentials/Essentials.git
synced 2025-08-13 18:14:38 +02:00
Use annotations to register commands
This commit is contained in:
@@ -2,11 +2,13 @@
|
||||
import java.util.Random;
|
||||
import net.ess3.api.IUser;
|
||||
import net.ess3.commands.EssentialsCommand;
|
||||
import net.ess3.extra.AnnotatedCommand;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Ocelot;
|
||||
|
||||
|
||||
@AnnotatedCommand(description = "Throw an exploding kitten at your opponent", usage = "/<command>")
|
||||
public class Commandkittycannon extends EssentialsCommand
|
||||
{
|
||||
private static Random random = new Random();
|
||||
|
@@ -2,11 +2,12 @@ package net.ess3.extra;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
@@ -15,8 +16,12 @@ import net.ess3.api.ICommandHandler;
|
||||
import net.ess3.api.IEssentials;
|
||||
import net.ess3.bukkit.BukkitPlugin;
|
||||
import net.ess3.commands.EssentialsCommandHandler;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.SimplePluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
|
||||
@@ -24,6 +29,8 @@ public class EssentialsExtra extends JavaPlugin
|
||||
{
|
||||
private IEssentials ess;
|
||||
private ICommandHandler handler;
|
||||
private CommandMap commandMap;
|
||||
private ClassLoader loader;
|
||||
|
||||
@Override
|
||||
public void onEnable()
|
||||
@@ -35,6 +42,11 @@ public class EssentialsExtra extends JavaPlugin
|
||||
URL[] urls = null;
|
||||
try
|
||||
{
|
||||
PluginManager pm = Bukkit.getServer().getPluginManager();
|
||||
Field f = SimplePluginManager.class.getDeclaredField("commandMap");
|
||||
f.setAccessible(true);
|
||||
CommandMap map = (CommandMap)f.get(pm);
|
||||
|
||||
JarFile jar = new JarFile(getFile());
|
||||
Enumeration<JarEntry> entries = jar.entries();
|
||||
while (entries.hasMoreElements())
|
||||
@@ -64,28 +76,49 @@ public class EssentialsExtra extends JavaPlugin
|
||||
commandDir.toURI().toURL()
|
||||
};
|
||||
}
|
||||
catch (IOException ex)
|
||||
catch (Exception ex)
|
||||
{
|
||||
getLogger().log(Level.SEVERE, "Could not get extra command dir", ex);
|
||||
getLogger().log(Level.SEVERE, "Enable " + getName(), ex);
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
}
|
||||
|
||||
loader = new URLClassLoader(urls, getClassLoader());
|
||||
|
||||
for (File file : commandDir.listFiles())
|
||||
{
|
||||
String name = file.getName();
|
||||
if (name.startsWith("Command") && name.endsWith(".class"))
|
||||
{
|
||||
getLogger().info("Loaded command " + name.substring(0, name.length() - 7));
|
||||
try
|
||||
{
|
||||
registerCommand(name);
|
||||
getLogger().info("Loaded command " + name.substring(0, name.length() - 7));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
getLogger().log(Level.SEVERE, "Could not register " + name, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ClassLoader loader = new URLClassLoader(urls, getClassLoader());
|
||||
handler = new EssentialsCommandHandler(loader, "Command", "essentials.", ess);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
|
||||
private void registerCommand(String name) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException
|
||||
{
|
||||
return handler.handleCommand(sender, command, label, args);
|
||||
|
||||
AnnotatedCommand anot = Class.forName(name).getAnnotation(AnnotatedCommand.class);
|
||||
if (anot == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Command class is not annotated with AnnotatedCommand.class");
|
||||
}
|
||||
commandMap.register("Essentials", new Command(name.substring(0, name.length() - 7), anot.description(), anot.usage(), Arrays.asList(anot.aliases()))
|
||||
{
|
||||
@Override
|
||||
public boolean execute(CommandSender cs, String label, String[] args)
|
||||
{
|
||||
return handler.handleCommand(cs, this, label, args);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -7,7 +7,3 @@ website: http://tiny.cc/EssentialsWiki
|
||||
description: Provides extra commands for Essentials
|
||||
authors: [md_5]
|
||||
depend: [Essentials-3]
|
||||
commands:
|
||||
kittycannon:
|
||||
description: Throw an exploding kitten at your opponent
|
||||
usage: /<command>
|
||||
|
Reference in New Issue
Block a user