summaryrefslogtreecommitdiffstats
path: root/EssentialsExtra/src/net/ess3/extra/EssentialsExtra.java
blob: 41ca3ed57d0f934604092322673ef6d794c05b4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package net.ess3.extra;

import java.io.File;
import java.io.FileOutputStream;
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;
import java.util.logging.Level;
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;


public class EssentialsExtra extends JavaPlugin
{
	private CommandMap commandMap;

	@Override
	public void onEnable()
	{
		final IEssentials ess = ((BukkitPlugin)getServer().getPluginManager().getPlugin("Essentials-3")).getEssentials();
		File commandDir = new File(ess.getPlugin().getDataFolder(), "extras");
		commandDir.mkdir();

		URL[] urls = null;
		try
		{
			PluginManager pm = Bukkit.getServer().getPluginManager();
			Field f = SimplePluginManager.class.getDeclaredField("commandMap");
			f.setAccessible(true);
			commandMap = (CommandMap)f.get(pm);

			JarFile jar = new JarFile(getFile());
			Enumeration<JarEntry> entries = jar.entries();
			while (entries.hasMoreElements())
			{
				JarEntry entry = entries.nextElement();
				String name = entry.getName();
				if (name.startsWith("Command") && name.endsWith(".class"))
				{
					File outFile = new File(commandDir, name);
					if (!outFile.exists())
					{
						InputStream is = jar.getInputStream(entry);
						OutputStream os = new FileOutputStream(outFile);
						byte[] buffer = new byte[4096];
						int length;
						while ((length = is.read(buffer)) > 0)
						{
							os.write(buffer, 0, length);
						}
						os.close();
						is.close();
					}
				}
			}
			urls = new URL[]
			{
				commandDir.toURI().toURL()
			};
		}
		catch (Exception ex)
		{
			getLogger().log(Level.SEVERE, "Enable " + getName(), ex);
			getServer().getPluginManager().disablePlugin(this);
		}

		ClassLoader loader = new URLClassLoader(urls);
		final ICommandHandler handler = new EssentialsCommandHandler(loader, "Command", "essentials.", ess);
		for (File file : commandDir.listFiles())
		{
			String fileName = file.getName();
			if (fileName.startsWith("Command") && fileName.endsWith(".class"))
			{
				String commandName = fileName.substring(7, fileName.length() - 7);
				try
				{
					AnnotatedCommand anot = Class.forName(fileName, true, loader).getAnnotation(AnnotatedCommand.class);
					if (anot == null)
					{
						throw new IllegalArgumentException("Command class is not annotated with AnnotatedCommand.class");
					}
					commandMap.register("Essentials", new Command(commandName, 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);
						}
					});
					getLogger().info("Loaded command " + commandName);
				}
				catch (Exception ex)
				{
					getLogger().log(Level.SEVERE, "Could not register " + fileName, ex);
				}
			}
		}
	}
}