-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
ExprTotalExperience.java
138 lines (128 loc) · 3.95 KB
/
ExprTotalExperience.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
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.expressions;
import ch.njol.skript.bukkitutil.PlayerUtils;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
@Name("Total Experience")
@Description({
"The total experience, in points, of players or experience orbs.",
"Adding to a player's experience will trigger Mending, but setting their experience will not."
})
@Examples({
"set total experience of player to 100",
"",
"add 100 to player's experience",
"",
"if player's total experience is greater than 100:",
"\tset player's total experience to 0",
"\tgive player 1 diamond"
})
@Since("2.7")
public class ExprTotalExperience extends SimplePropertyExpression<Entity, Integer> {
static {
register(ExprTotalExperience.class, Integer.class, "[total] experience", "entities");
}
@Override
@Nullable
public Integer convert(Entity entity) {
// experience orbs
if (entity instanceof ExperienceOrb)
return ((ExperienceOrb) entity).getExperience();
// players need special treatment
if (entity instanceof Player)
return PlayerUtils.getTotalXP(((Player) entity).getLevel(), ((Player) entity).getExp());
// invalid entity type
return null;
}
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
switch (mode) {
case ADD:
case REMOVE:
case SET:
case DELETE:
case RESET:
return new Class[]{Number.class};
case REMOVE_ALL:
default:
return null;
}
}
@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
int change = delta == null ? 0 : ((Number) delta[0]).intValue();
switch (mode) {
case RESET:
case DELETE:
// RESET and DELETE will have change = 0, so just fall through to SET
case SET:
if (change < 0)
change = 0;
for (Entity entity : getExpr().getArray(event)) {
if (entity instanceof ExperienceOrb) {
((ExperienceOrb) entity).setExperience(change);
} else if (entity instanceof Player) {
PlayerUtils.setTotalXP((Player) entity, change);
}
}
break;
case REMOVE:
change = -change;
// fall through to ADD
case ADD:
int xp;
for (Entity entity : getExpr().getArray(event)) {
if (entity instanceof ExperienceOrb) {
//ensure we don't go below 0
xp = ((ExperienceOrb) entity).getExperience() + change;
((ExperienceOrb) entity).setExperience(Math.max(xp, 0));
} else if (entity instanceof Player) {
// can only giveExp() positive experience
if (change < 0) {
// ensure we don't go below 0
xp = PlayerUtils.getTotalXP((Player) entity) + change;
PlayerUtils.setTotalXP((Player) entity, (Math.max(xp, 0)));
} else {
((Player) entity).giveExp(change);
}
}
}
break;
}
}
@Override
public Class<? extends Integer> getReturnType() {
return Integer.class;
}
@Override
protected String getPropertyName() {
return "total experience";
}
}