basically i got tired of the default commands, they are so useless and ugly looking in chat. i have also seen many threads looking for ways to delete / disable the default commands. i tried deleting the default permissions, by they literally made it impossible to delete default permissions. here's my code. you will either need to make your own stop/reload command or you can specify for it to not delete it. also i did try to delete them by check if they werent an instance of PluginCommand, but worldedit extends Command doesnt use PluginCommand. i also have some more code to made it so u can register commands outside of ur plugin.yml if anyone wants it call from onEnable() Code: public void clearDefaultCommands() { try { Field f = this.getCommandMap().getClass().getDeclaredField("knownCommands"); f.setAccessible(true); final Object list = f.get(this.getCommandMap()); new BukkitRunnable() { public void run() { Map<String, Command> commands = (Map<String, Command>) list; ArrayList<Command> deleted = new ArrayList<Command>(); for (Command cmd : commands.values()) { if (cmd.getPermission() != null) { if (cmd.getPermission().startsWith("bukkit.command")) { deleted.add(cmd); } } } for (Command d : deleted) { commands.values().remove(d); } } }.runTaskLater(PvPDojo.getInstance(), 1); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } } public CommandMap getCommandMap() { try { Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap"); f.setAccessible(true); CommandMap map = (CommandMap) f.get(Bukkit.getServer()); return map; } catch (Exception e) { e.printStackTrace(); } return null; }
He did use a simple for-loop. If he used a simple iterator he wouldn't need to create a list and loop through it. You can't remove objects from a collection while looping through them or you'll get a ConcurrentModificationException.
teej107 A simple for loop is Code: for (int i = 0; i < list.size(); i++) and an advanced (enhanced) for loop is Code: for (Object o : list)
Formatting problems, efficiency problems, redundant code, possibly inline not fixed, Code: public void clearDefaultCommands() { try { Field f = CommandMap.class.getDeclaredField("knownCommands"); f.setAccessible(true); final Map<String, Command> commands = (Map<String, Command>) f.get(this.getCommandMap()); new BukkitRunnable() { public void run() { for (Iterator<Command> it = commands.values().iterator(), Command cmd = null; it.hasNext(); cmd != null && cmd.getPermission() != null && cmd.getPermission().startsWith("bukkit.command"); cmd = it.next()) it.remove(); } }.runTaskLater(PvPDojo.getInstance(), 1); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } } public CommandMap getCommandMap() { try { Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap"); f.setAccessible(true); return (CommandMap) f.get(Bukkit.getServer()); } catch (Exception e) { e.printStackTrace(); } return null; }
RainoBoy97 It would be a lot simpler to just use an iterator because you would have to make sure you don't go out of bounds using a "simple" for-loop.
werter318 lol what? Cirno hes talking about the server i code for, probably jealous our server is popular and his isnt. lol EDIT by Moderator: merged posts, please use the edit button instead of double posting.
ResultStatic Wait what, you don't know me from Dojo? I was a coder before you lol... Ask schmockyyy. It didn't last long though because I didn't have the time and I'm not really into MC anymore. So please don't act like you didn't copy that part from the Dojo plugin and just know that it is mine.
werter318 copy what, i dont understand. i made the above code if thats what your talking about. you can even see my previous thread on how to delete permissions because i was trying to remove the default commands. i ended up doing it this way, if i had this code in the first place, i wouldnt have needed to edit the default perms. if your talking about dojo's code, im recoding the entire plugin so none of your code will be used if u actually did any of it. since i think ecminer did everything coding wise on that server
ResultStatic Besides the redundant for loops (could be replaced by a Iterator), this is great! Thanks for this