Re: What do you think of commands like this?
Quote:
Originally Posted by
Handsfree
I like this method mainly because of the automatic command info generation.
It's nice how each command has is own method because netbeans shows you all the commands in a list and you can navigate easily through them. Also it works nicely with the editor because If you need you can collapse any method you don't want to see.
It just makes it easier to find your commands.
I do like the switch though. :)
Just updated my system.
PHP Code:
public class DonorCommands extends Commands {
public static boolean execute(MapleClient c, String[] sub, char heading) {
MapleCharacter chr = c.getPlayer();
Channel cserv = c.getChannelServer();
MapleCharacter victim; // For commands with targets.
ResultSet rs; // For commands with MySQL results.
try {
Command command = Command.valueOf(sub[0]);
switch (command) {
default:
// chr.yellowMessage("Command: " + heading + sub[0] + ": does not exist.");
return false;
case donor:
chr.setHp(30000);
chr.setMaxHp(30000);
chr.setMp(30000);
chr.setMaxMp(30000);
chr.updateSingleStat(MapleStat.HP, 30000);
chr.updateSingleStat(MapleStat.MAXHP, 30000);
chr.updateSingleStat(MapleStat.MP, 30000);
chr.updateSingleStat(MapleStat.MAXMP, 30000);
chr.message("Who's awesome? You're awesome!");
break;
case heal:
chr.setHp(chr.getMaxHp());
chr.setMp(chr.getMaxMp());
chr.updateSingleStat(MapleStat.HP, chr.getMaxHp());
chr.updateSingleStat(MapleStat.MP, chr.getMaxMp());
chr.message("Healed for free. Thanks for your donation!");
break;
case help:
if (sub.length > 1) {
if (sub[1].equalsIgnoreCase("donor")) {
chr.dropMessage(ServerConstants.SERVER_NAME + "'s DonorCommands Help");
for (Command cmd : Command.values()) {
chr.dropMessage(heading + cmd.name() + " - " + cmd.getDescription());
}
break;
} else {
return false;
}
} else {
return false;
}
case itemvac:
List<MapleMapObject> items = chr.getMap().getMapObjectsInRange(chr.getPosition(), Double.POSITIVE_INFINITY, Arrays.asList(MapleMapObjectType.ITEM));
for (MapleMapObject item : items) {
MapleMapItem mapitem = (MapleMapItem) item;
if (!MapleInventoryManipulator.addFromDrop(c, mapitem.getItem(), true)) {
continue;
}
mapitem.setPickedUp(true);
chr.getMap().broadcastMessage(MaplePacketCreator.removeItemFromMap(mapitem.getObjectId(), 2, chr.getId()), mapitem.getPosition());
chr.getMap().removeMapObject(item);
chr.getMap().nullifyObject(item);
}
break;
}
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
private static enum Command {
donor("Rewards you for donating!"),
heal("Heals you, for free!"),
help("Displays this help message."),
itemvac("Vacuums up all the items on the map.");
private final String description;
private Command(String description){
this.description = description;
}
public String getDescription() {
return this.description;
}
}
}
Sexy, no? :)
And, I don't know about Netbeans, but in Eclipse:
http://cl.ly/162w1f3G2X22150N0e09/Sc...46.16%20AM.png
Re: What do you think of commands like this?
Switch is much neater in my opinion, than the thread starter's way.
Re: What do you think of commands like this?
Quote:
Originally Posted by
SuperLol
I create an instance of each command and add them into a map. Then when people use a command, it retrieves it from the map. This method is faster than using if statements every time. It uses slightly more memory (command object is small though). Handlers should actually be done this way too (how it is in odin)... it's more industry standard.
I hope by "add them into a map" you mean a Map<String, Command> and not a MapleMap. :P
Quote:
Originally Posted by
aaronweiss
That shows the items in the enum not the actual command location. ( Yes that's in Netbeans. :P )
I like only having to keep track of my commands in one place rather then an enum and a switch.
Re: What do you think of commands like this?
Quote:
Originally Posted by
Handsfree
That shows the items in the enum not the actual command location. ( Yes that's in Netbeans. :P )
I like only having to keep track of my commands in one place rather then an enum and a switch.
Alphabetical order. :)
It's good practice.
Re: What do you think of commands like this?
It's good practice because it's much easier to find, and relatively neater. :P
Re: What do you think of commands like this?
It's great, because the commands file is always messy and unreadable and it's nice to make it more clean.
But, it's does the same thing so... who cares?
Re: What do you think of commands like this?
Quote:
Originally Posted by
AngryPepe
It's great, because the commands file is always messy and unreadable and it's nice to make it more clean.
But, it's does the same thing so... who cares?
Well, obviously, people who care both having nice, clean, legible code care. If they didn't, there wouldn't even be threads like this.
Re: What do you think of commands like this?
the older way would be much simpler easier and a faster way imo
Re: What do you think of commands like this?
Maybe I'll release what I got so you guys can get a better look.
Re: What do you think of commands like this?
Aren't switch statements waaaaaaaaaaaay slower than any other method? It may be neather to the eye but what's the point if you format your code correctly? Nothing wront with if/else if to me..
Re: What do you think of commands like this?
What do you mean by slower? Slower to write? Maybe by a few fractions of a second. I don't type slowly, so this doesn't bother me. Slower to execute? If I'm not mistaken, they compile to more optimized bytecode, hence why switch statements couldn't be used on Strings until Java 7. Slower to read? Definitely not. I'm not seeing your point here. More legible code is definitely advantageous if the only downside is taking a little longer to write it.
Re: What do you think of commands like this?
I have released what I have here: Click
Re: What do you think of commands like this?
Quote:
Originally Posted by
aaronweiss
What do you mean by slower? Slower to write? Maybe by a few fractions of a second. I don't type slowly, so this doesn't bother me. Slower to execute? If I'm not mistaken, they compile to more optimized bytecode, hence why switch statements couldn't be used on Strings until Java 7. Slower to read? Definitely not. I'm not seeing your point here. More legible code is definitely advantageous if the only downside is taking a little longer to write it.
Alright, I didn't do proper research. I meant it was slower to execute.
And in some cases, it is. You typically want an if/else statement if you use a small amount of options.
Now, imagine you had this long list of commands (and let's be honest. Every repack is stuffed with those..).
You execute this command and it's all down the bottom of the if/else statement. The compiler has to perform each statement starting from the top to see if it's true or false. This obviously takes very long.
Now for the switch statement. If the switch statement is small enough the compiler will handle and execute it just like an if/else if statement. If it's long enough the compiler will perform some magic and well, index all cases before hand. That way it's able to jump straight to the desired case.
Another thing I found is that String Switches take, the longer they are, more time to execute.
Ex:
Code:
Abc: 0.612 seconds.
Abcd: 0.835 seconds.
Abcde: 1.093 seconds.
Lost the code I used for it though. Shouldn't be too hard to make your own.
Conclusion: Switch statements are better than if/else statements in this case. One more thing; I'd check if 'command' is null before going into the switch (or try).
Re: What do you think of commands like this?
You shouldn't use switch statements for Strings. At least, as far as I know. Even though they added the functionality in Java 7, it's still fairly new. It's easier to use enumerations, and it'll likely be faster. I'll be running some tests on which is faster later, but it should be the enumeration.