|
| 1 | +package de.buddelbubi.commands; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | + |
| 5 | +import cn.nukkit.Player; |
| 6 | +import cn.nukkit.Server; |
| 7 | +import cn.nukkit.command.Command; |
| 8 | +import cn.nukkit.command.CommandSender; |
| 9 | +import cn.nukkit.plugin.Plugin; |
| 10 | +import de.buddelbubi.PluginManagerInstance; |
| 11 | +import de.buddelbubi.utils.TextFormat; |
| 12 | +import de.buddelbubi.utils.WindowFactory; |
| 13 | +import de.buddelbubi.utils.WorkArounds; |
| 14 | + |
| 15 | +public class PluginManagerCommand extends Command{ |
| 16 | + |
| 17 | + public PluginManagerCommand(String name) { |
| 18 | + super(name); |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + @Override |
| 23 | + public boolean execute(CommandSender arg0, String arg1, String[] args) { |
| 24 | + |
| 25 | + |
| 26 | + String prefix = PluginManagerInstance.prefix; |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + if(args.length >= 1) { |
| 32 | + |
| 33 | + if(!arg0.hasPermission("pluginmanager." + args[0])) { |
| 34 | + |
| 35 | + arg0.sendMessage(prefix + "§cYou are lacking the permission 'pluginmanager." + args[0] + "'."); |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + switch (args[0]) { |
| 40 | + case "load": |
| 41 | + |
| 42 | + if(args.length == 2) { |
| 43 | + |
| 44 | + File file = new File(Server.getInstance().getPluginPath(), (args[1].contains(".jar") ? args[1] : args[1] + ".jar")); |
| 45 | + if(file.exists()) { |
| 46 | + |
| 47 | + Plugin plugin = Server.getInstance().getPluginManager().loadPlugin(file); |
| 48 | + |
| 49 | + if(plugin != null) { |
| 50 | + |
| 51 | + for(String depend : plugin.getDescription().getDepend()) { |
| 52 | + |
| 53 | + if(Server.getInstance().getPluginManager().getPlugin(depend) == null) { |
| 54 | + arg0.sendMessage(prefix + "§cCould not enable " + TextFormat.getFormatedPluginName(plugin) + " due a missing dependency: §4" + depend + ".jar"); |
| 55 | + return false; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + Server.getInstance().getPluginManager().enablePlugin(plugin); |
| 60 | + arg0.sendMessage(prefix + TextFormat.getFormatedPluginName(plugin) + " loaded successully."); |
| 61 | + |
| 62 | + } else arg0.sendMessage(prefix + "§cSomething went wrong while loading the plugin."); |
| 63 | + |
| 64 | + } else arg0.sendMessage(prefix + "There is no file called " + file.getName() + "."); |
| 65 | + |
| 66 | + } else arg0.sendMessage(prefix + "§cDo /pluginmanager load [filename]"); |
| 67 | + |
| 68 | + break; |
| 69 | + |
| 70 | + case "unload": |
| 71 | + |
| 72 | + if(args.length == 2) { |
| 73 | + |
| 74 | + |
| 75 | + Plugin plugin = Server.getInstance().getPluginManager().getPlugin(args[1]); |
| 76 | + |
| 77 | + if(plugin == PluginManagerInstance.plugin) { |
| 78 | + arg0.sendMessage(prefix + "§cYou can not disable PluginManager."); |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + if(plugin != null) { |
| 83 | + |
| 84 | + |
| 85 | + WorkArounds.unregisterPlugin(plugin); |
| 86 | + Server.getInstance().getPluginManager().disablePlugin(plugin); |
| 87 | + |
| 88 | + arg0.sendMessage(prefix + TextFormat.getFormatedPluginName(plugin) + " unloaded successully."); |
| 89 | + |
| 90 | + } else arg0.sendMessage(prefix + "§cThere is no plugin called " + args[1] + "."); |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + } else arg0.sendMessage(prefix + "§cDo /pluginmanager unload [filename]"); |
| 95 | + |
| 96 | + break; |
| 97 | + |
| 98 | + case "enable": |
| 99 | + |
| 100 | + if(args.length == 2) { |
| 101 | + |
| 102 | + Plugin plugin = Server.getInstance().getPluginManager().getPlugin(args[1]); |
| 103 | + |
| 104 | + if(plugin != null) { |
| 105 | + |
| 106 | + if(plugin.isEnabled()) { |
| 107 | + arg0.sendMessage(prefix + "§c" + TextFormat.getFormatedPluginName(plugin) + " is already enabled."); |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + for(String depend : plugin.getDescription().getDepend()) { |
| 112 | + |
| 113 | + if(Server.getInstance().getPluginManager().getPlugin(depend) == null) { |
| 114 | + arg0.sendMessage(prefix + "§cCould not enable " + TextFormat.getFormatedPluginName(plugin) + " due a missing dependency: §4" + depend + ".jar"); |
| 115 | + return false; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + Server.getInstance().getPluginManager().enablePlugin(plugin); |
| 120 | + arg0.sendMessage(prefix + TextFormat.getFormatedPluginName(plugin) + " enabled successully."); |
| 121 | + |
| 122 | + } else arg0.sendMessage(prefix + "§cThere is no Plugin called " + args[1] + "."); |
| 123 | + |
| 124 | + } else arg0.sendMessage(prefix + "§cDo /pluginmanager enable [Plugin]"); |
| 125 | + |
| 126 | + break; |
| 127 | + |
| 128 | + case "disable": |
| 129 | + |
| 130 | + if(args.length == 2) { |
| 131 | + |
| 132 | + Plugin plugin = Server.getInstance().getPluginManager().getPlugin(args[1]); |
| 133 | + |
| 134 | + if(plugin == PluginManagerInstance.plugin) { |
| 135 | + arg0.sendMessage(prefix + "§cYou can not disable PluginManager."); |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + if(plugin != null) { |
| 140 | + |
| 141 | + if(!plugin.isEnabled()) { |
| 142 | + arg0.sendMessage(prefix + "§c" + TextFormat.getFormatedPluginName(plugin) + " is already disabled."); |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + Server.getInstance().getPluginManager().disablePlugin(plugin); |
| 147 | + |
| 148 | + |
| 149 | + arg0.sendMessage(prefix + TextFormat.getFormatedPluginName(plugin) + " disabled successully."); |
| 150 | + |
| 151 | + } else arg0.sendMessage(prefix + "§cThere is no Plugin called " + args[1] + "."); |
| 152 | + |
| 153 | + } else arg0.sendMessage(prefix + "§cDo /pluginmanager disable [Plugin]"); |
| 154 | + |
| 155 | + break; |
| 156 | + |
| 157 | + case "reload": |
| 158 | + |
| 159 | + if(args.length == 2) { |
| 160 | + |
| 161 | + Plugin plugin = Server.getInstance().getPluginManager().getPlugin(args[1]); |
| 162 | + |
| 163 | + if(plugin == PluginManagerInstance.plugin) { |
| 164 | + arg0.sendMessage(prefix + "§cYou can not disable PluginManager."); |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | + if(plugin != null) { |
| 169 | + |
| 170 | + File file = new File(plugin.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()); |
| 171 | + |
| 172 | + long millis = System.currentTimeMillis(); |
| 173 | + |
| 174 | + WorkArounds.unregisterPlugin(plugin); |
| 175 | + |
| 176 | + for(String depend : plugin.getDescription().getDepend()) { |
| 177 | + |
| 178 | + if(Server.getInstance().getPluginManager().getPlugin(depend) == null) { |
| 179 | + arg0.sendMessage(prefix + "§cCould not enable " + TextFormat.getFormatedPluginName(plugin) + " due a missing dependency: §4" + depend + ".jar"); |
| 180 | + return false; |
| 181 | + } |
| 182 | + } |
| 183 | + Server.getInstance().getPluginManager().disablePlugin(plugin); |
| 184 | + try { |
| 185 | + Server.getInstance().enablePlugin(Server.getInstance().getPluginManager().loadPlugin(file)); |
| 186 | + } catch (Exception e) { |
| 187 | + arg0.sendMessage(prefix + "§cCouldn't load Plugin " + plugin.getName() + ". File renamed or deleted?"); |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | + arg0.sendMessage(prefix + TextFormat.getFormatedPluginName(plugin) + " reloaded successully. (" + (System.currentTimeMillis() - millis) + " milliseconds)"); |
| 192 | + |
| 193 | + } else arg0.sendMessage(prefix + "§cThere is no Plugin called " + args[1] + "."); |
| 194 | + |
| 195 | + } else if(args.length == 1) { |
| 196 | + |
| 197 | + |
| 198 | + new Thread(new Runnable() { |
| 199 | + |
| 200 | + @Override |
| 201 | + public void run() { |
| 202 | + long millis = System.currentTimeMillis(); |
| 203 | + Server.getInstance().broadcastMessage(PluginManagerInstance.prefix + "§eReloading the Server..."); |
| 204 | + Server.getInstance().reload(); |
| 205 | + Server.getInstance().broadcastMessage(PluginManagerInstance.prefix + "§eServer reloaded in " + (System.currentTimeMillis() - millis) + " milliseconds."); |
| 206 | + |
| 207 | + } |
| 208 | + }).start(); |
| 209 | + |
| 210 | + } else arg0.sendMessage(prefix + "§cDo /pluginmanager reload [Plugin]"); |
| 211 | + break; |
| 212 | + |
| 213 | + case "help": |
| 214 | + |
| 215 | + arg0.sendMessage(prefix + "Help Page\n" |
| 216 | + + "§e/pluginmanager §7opens an UI to manage all plugins ingame.\n" |
| 217 | + + "§e/pluginmanager load [file] §7loads a plugin from a .jar file.\n" |
| 218 | + + "§e/pluginmanager unload [plugin] §7unloads a plugin from your server.\n" |
| 219 | + + "§e/pluginmanager reload [plugin] §7reloads a plugin without reloading the whole server.\n" |
| 220 | + + "§e/pluginmanager enable [plugin] §7enables a disabled plugin.\n" |
| 221 | + + "§e/pluginmanager disable [plugin] §7disables a plugin.\n" |
| 222 | + + "§e/reload [plugin] §7reloads a single plugin."); |
| 223 | + |
| 224 | + |
| 225 | + break; |
| 226 | + |
| 227 | + default: |
| 228 | + arg0.sendMessage(prefix + "§cDo /pluginmanager help"); |
| 229 | + break; |
| 230 | + } |
| 231 | + |
| 232 | + } else { |
| 233 | + if(arg0 instanceof Player) { |
| 234 | + |
| 235 | + Player p = (Player) arg0; |
| 236 | + |
| 237 | + if(p.hasPermission("pluginmanager.ui")) |
| 238 | + |
| 239 | + WindowFactory.openPluginListWindow(p); |
| 240 | + |
| 241 | + else p.sendMessage(prefix + "§cYou are lacking the permission 'pluginmanager.ui'"); |
| 242 | + |
| 243 | + } else arg0.sendMessage(prefix + "§cDo /pluginmanager help"); |
| 244 | + } |
| 245 | + |
| 246 | + |
| 247 | + return false; |
| 248 | + } |
| 249 | + |
| 250 | +} |
0 commit comments