Clearing Default Commands

Discussion in 'Resources' started by ResultStatic, Sep 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ResultStatic

    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;
        }
     
    d3v1n302418 and Skionz like this.
  2. Offline

    RainoBoy97

    If you use a simple for loop you don't need the second one :)
     
  3. Offline

    teej107

    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.
     
  4. Offline

    RainoBoy97

    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)
     
  5. Offline

    xTrollxDudex

    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;
        }
     
    ferrybig likes this.
  6. Offline

    teej107

    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.
     
  7. Offline

    werter318

    Yeah, you sorta stole my code/idea and made it worse.

    Me > EcMiner > PvPDojo > You.
     
  8. Offline

    Cirno

    Er... I'm not intending to be rude or anything, but do you have any proof to your claim?
     
  9. Offline

    ResultStatic

    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.
     
    Last edited by a moderator: Jun 14, 2016
  10. Offline

    werter318

    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.
     
  11. Offline

    ResultStatic

    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
     
  12. Offline

    d3v1n302418

    ResultStatic Besides the redundant for loops (could be replaced by a Iterator), this is great! Thanks for this ;)
     
Thread Status:
Not open for further replies.

Share This Page