mirror of
https://github.com/essentials/Essentials.git
synced 2025-08-14 02:24:16 +02:00
Use annotations to register commands
This commit is contained in:
@@ -2,11 +2,13 @@
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import net.ess3.api.IUser;
|
import net.ess3.api.IUser;
|
||||||
import net.ess3.commands.EssentialsCommand;
|
import net.ess3.commands.EssentialsCommand;
|
||||||
|
import net.ess3.extra.AnnotatedCommand;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.entity.EntityType;
|
import org.bukkit.entity.EntityType;
|
||||||
import org.bukkit.entity.Ocelot;
|
import org.bukkit.entity.Ocelot;
|
||||||
|
|
||||||
|
|
||||||
|
@AnnotatedCommand(description = "Throw an exploding kitten at your opponent", usage = "/<command>")
|
||||||
public class Commandkittycannon extends EssentialsCommand
|
public class Commandkittycannon extends EssentialsCommand
|
||||||
{
|
{
|
||||||
private static Random random = new Random();
|
private static Random random = new Random();
|
||||||
|
@@ -2,11 +2,12 @@ package net.ess3.extra;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.jar.JarEntry;
|
import java.util.jar.JarEntry;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
@@ -15,8 +16,12 @@ import net.ess3.api.ICommandHandler;
|
|||||||
import net.ess3.api.IEssentials;
|
import net.ess3.api.IEssentials;
|
||||||
import net.ess3.bukkit.BukkitPlugin;
|
import net.ess3.bukkit.BukkitPlugin;
|
||||||
import net.ess3.commands.EssentialsCommandHandler;
|
import net.ess3.commands.EssentialsCommandHandler;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
|
import org.bukkit.command.CommandMap;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.plugin.PluginManager;
|
||||||
|
import org.bukkit.plugin.SimplePluginManager;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
|
||||||
@@ -24,6 +29,8 @@ public class EssentialsExtra extends JavaPlugin
|
|||||||
{
|
{
|
||||||
private IEssentials ess;
|
private IEssentials ess;
|
||||||
private ICommandHandler handler;
|
private ICommandHandler handler;
|
||||||
|
private CommandMap commandMap;
|
||||||
|
private ClassLoader loader;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable()
|
public void onEnable()
|
||||||
@@ -35,6 +42,11 @@ public class EssentialsExtra extends JavaPlugin
|
|||||||
URL[] urls = null;
|
URL[] urls = null;
|
||||||
try
|
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());
|
JarFile jar = new JarFile(getFile());
|
||||||
Enumeration<JarEntry> entries = jar.entries();
|
Enumeration<JarEntry> entries = jar.entries();
|
||||||
while (entries.hasMoreElements())
|
while (entries.hasMoreElements())
|
||||||
@@ -64,28 +76,49 @@ public class EssentialsExtra extends JavaPlugin
|
|||||||
commandDir.toURI().toURL()
|
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);
|
getServer().getPluginManager().disablePlugin(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loader = new URLClassLoader(urls, getClassLoader());
|
||||||
|
|
||||||
for (File file : commandDir.listFiles())
|
for (File file : commandDir.listFiles())
|
||||||
{
|
{
|
||||||
String name = file.getName();
|
String name = file.getName();
|
||||||
if (name.startsWith("Command") && name.endsWith(".class"))
|
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);
|
handler = new EssentialsCommandHandler(loader, "Command", "essentials.", ess);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void registerCommand(String name) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
|
|
||||||
{
|
{
|
||||||
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
|
description: Provides extra commands for Essentials
|
||||||
authors: [md_5]
|
authors: [md_5]
|
||||||
depend: [Essentials-3]
|
depend: [Essentials-3]
|
||||||
commands:
|
|
||||||
kittycannon:
|
|
||||||
description: Throw an exploding kitten at your opponent
|
|
||||||
usage: /<command>
|
|
||||||
|
2
pom.xml
2
pom.xml
@@ -76,7 +76,7 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<bukkit.version>1.3.2-R0.1</bukkit.version>
|
<bukkit.version>1.3.2-R1.1-SNAPSHOT</bukkit.version>
|
||||||
<build.number>Unknown</build.number>
|
<build.number>Unknown</build.number>
|
||||||
<org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>true</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>
|
<org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>true</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>
|
||||||
<org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>
|
<org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>2</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>
|
||||||
|
Reference in New Issue
Block a user