/
ExprScoreboardTags.java
142 lines (128 loc) · 4.4 KB
/
ExprScoreboardTags.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
/**
* 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 java.util.Set;
import java.util.stream.Stream;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import ch.njol.skript.Skript;
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.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
@Name("Scoreboard Tags")
@Description({"Scoreboard tags are simple list of texts stored directly in the data of an <a href='classes.html#entity'>entity</a>.",
"So this is a Minecraft related thing, not Bukkit, so the tags will not get removed when the server stops. " +
"You can visit <a href='https://minecraft.wiki/w/Scoreboard#Tags'>visit Minecraft Wiki</a> for more info.",
"This is changeable and valid for any type of entity. " +
"Also you can use use the <a href='conditions.html#CondHasScoreboardTag'>Has Scoreboard Tag</a> condition to check whether an entity has the given tags.",
"",
"Requires Minecraft 1.11+ (actually added in 1.9 to the game, but added in 1.11 to Spigot)."})
@Examples({"on spawn of a monster:",
"\tif the spawn reason is mob spawner:",
"\t\tadd \"spawned by a spawner\" to the scoreboard tags of event-entity",
"",
"on death of a monster:",
"\tif the attacker is a player:",
"\t\tif the victim doesn't have the scoreboard tag \"spawned by a spawner\":",
"\t\t\tadd 1$ to attacker's balance"})
@Since("2.3")
public class ExprScoreboardTags extends SimpleExpression<String> {
static {
Skript.registerExpression(ExprScoreboardTags.class, String.class, ExpressionType.PROPERTY,
"[(all [[of] the]|the)] scoreboard tags of %entities%",
"%entities%'[s] scoreboard tags");
}
@SuppressWarnings("null")
private Expression<Entity> entities;
@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
entities = (Expression<Entity>) exprs[0];
return true;
}
@Override
@Nullable
public String[] get(Event e) {
return Stream.of(entities.getArray(e))
.map(Entity::getScoreboardTags)
.flatMap(Set::stream)
.toArray(String[]::new);
}
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
switch (mode) {
case SET:
case ADD:
case REMOVE:
case DELETE:
case RESET:
return CollectionUtils.array(String[].class);
default:
return null;
}
}
@Override
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
for (Entity entity : entities.getArray(e)) {
switch (mode) {
case SET:
assert delta != null;
entity.getScoreboardTags().clear();
for (Object tag : delta)
entity.addScoreboardTag((String) tag);
break;
case ADD:
assert delta != null;
for (Object tag : delta)
entity.addScoreboardTag((String) tag);
break;
case REMOVE:
assert delta != null;
for (Object tag : delta)
entity.removeScoreboardTag((String) tag);
break;
case DELETE:
case RESET:
entity.getScoreboardTags().clear();
}
}
}
@Override
public boolean isSingle() {
return false;
}
@Override
public Class<? extends String> getReturnType() {
return String.class;
}
@Override
public String toString(@Nullable Event e, boolean debug) {
return "the scoreboard tags of " + entities.toString(e, debug);
}
}