-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
ExperienceCommand.java
164 lines (139 loc) · 5.38 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.entity.Player;
public class ExperienceCommand extends AbstractCommand {
// <--[command]
// @Name Experience
// @Syntax experience [{set}/give/take] (level) [<#>]
// @Required 2
// @Short Gives or takes experience points to the player.
// @Group player
//
// @Description
// This command allows modification of a players experience points.
// Experience can be modified in terms of XP points, or by levels.
// Note that the "set" command does not affect levels, but xp bar fullness.
// (E.g. setting experience to 0 will not change a players level, but will
// set the players experience bar to 0)
//
// @Tags
// <p@player.xp>
// <p@player.xp.to_next_level>
// <p@player.xp.total>
// <p@player.xp.level>
//
// @Usage
// Use to set a player's experience bar to 0.
// - experience set 0
//
// @Usage
// Use give give a player 1 level.
// - experience give level 1
//
// @Usage
// Use to take 1 level from a player.
//
// - experience take level 1
// @Usage
// Use to give a player with the name Morphan1 10 experience points.
// - experience give 10 player:p@Morphan1
// -->
private enum Type {SET, GIVE, TAKE}
public static void setTotalExperience(Player player, int exp) {
player.setTotalExperience(exp);
}
public static void setLevel(Player player, int level) {
player.setLevel(level);
}
public static void giveExperience(Player player, int exp) {
player.giveExp(exp);
}
public static void takeExperience(Player player, int toTake) {
int pastLevelStart = (int) (player.getExp() * player.getExpToLevel());
while (toTake >= pastLevelStart) {
toTake -= pastLevelStart;
player.setExp(0);
if (player.getLevel() == 0) {
return;
}
player.setLevel(player.getLevel() - 1);
pastLevelStart = player.getExpToLevel();
}
int newAmount = pastLevelStart - toTake;
player.setExp(newAmount / (float) player.getExpToLevel());
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
int amount = 0;
Type type = Type.SET;
boolean level = false;
boolean silent = false;
for (Argument arg : ArgumentHelper.interpretArguments(scriptEntry.aHArgs)) {
if (arg.matchesPrimitive(ArgumentHelper.PrimitiveType.Integer)) {
amount = arg.asElement().asInt();
}
else if (arg.matches("SET", "GIVE", "TAKE")) {
type = Type.valueOf(arg.asElement().asString().toUpperCase());
}
else if (arg.matches("LEVEL")) {
level = true;
}
else if (arg.matches("SILENT")) {
silent = true;
}
else {
arg.reportUnhandled();
}
}
scriptEntry.addObject("quantity", amount)
.addObject("type", type)
.addObject("level", level)
.addObject("silent", silent);
}
@Override
public void execute(ScriptEntry scriptEntry) {
Type type = (Type) scriptEntry.getObject("type");
int quantity = (int) scriptEntry.getObject("quantity");
Boolean level = (Boolean) scriptEntry.getObject("level");
//Boolean silent = (Boolean) scriptEntry.getObject("silent");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, name, ArgumentHelper.debugObj("Type", type.toString())
+ ArgumentHelper.debugObj("Quantity", level ? quantity + " levels" : quantity)
+ ArgumentHelper.debugObj("Player", Utilities.getEntryPlayer(scriptEntry).getName()));
}
Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
switch (type) {
case SET:
if (level) {
Utilities.getEntryPlayer(scriptEntry).setLevel(quantity);
}
else {
setTotalExperience(player, quantity);
}
break;
case GIVE:
if (level) {
player.setLevel(player.getLevel() + quantity);
}
else {
player.giveExp(quantity);
}
break;
case TAKE:
if (level) {
int value = player.getLevel() - quantity;
player.setLevel(value <= 0 ? 0 : value);
}
else {
takeExperience(player, quantity);
}
break;
}
}
}