-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
ExperienceCommand.java
87 lines (64 loc) · 2.97 KB
/
ExperienceCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package net.aufdemrand.denizen.scripts.commands.player;
import net.aufdemrand.denizen.utilities.ExpUtil;
import org.bukkit.entity.Player;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
public class ExperienceCommand extends AbstractCommand {
private enum Type { SET, GIVE, TAKE }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
int amount = 0;
Type type = Type.SET;
boolean level = false;
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesQuantity(arg) || aH.matchesInteger(arg)) {
amount = aH.getIntegerFrom(arg);
}
else if (aH.matchesArg("SET, GIVE, TAKE", arg))
type = Type.valueOf(arg.toUpperCase());
else if(aH.matchesArg("LEVEL", arg))
level = true;
else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}
scriptEntry.addObject("quantity", amount)
.addObject("type", type)
.addObject("level", level);
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Type type = (Type) scriptEntry.getObject("type");
Integer quantity = (Integer) scriptEntry.getObject("quantity");
Boolean level = (Boolean) scriptEntry.getObject("level");
dB.report(name, aH.debugObj("Type", type.toString())
+ aH.debugObj("Quantity", level ? quantity.toString() + " levels" : quantity.toString())
+ aH.debugObj("Player", scriptEntry.getPlayer().getName()));
Player player = scriptEntry.getPlayer().getPlayerEntity();
switch (type) {
case SET:
if(level)
player.setLevel(quantity);
else
player.setTotalExperience(quantity);
break;
case GIVE:
if(level)
player.setLevel(player.getLevel() + quantity);
else
player.giveExp(player.getTotalExperience() + quantity);
break;
case TAKE:
if(level)
player.setLevel((player.getLevel() - quantity < 0
? 0 : player.getLevel() - quantity));
else
player.setTotalExperience((player.getTotalExperience() - quantity < 0
? 0 : player.getTotalExperience() - quantity));
break;
}
}
}