Commits

Parker Hawke authored and md_5 committed d43a1e722f5
SPIGOT-2450: Improve scoreboard criteria API, add missing DisplaySlots
No tags

src/main/java/org/bukkit/scoreboard/Criteria.java

Added
1 +package org.bukkit.scoreboard;
2 +
3 +import com.google.common.base.Preconditions;
4 +import org.bukkit.Bukkit;
5 +import org.bukkit.Material;
6 +import org.bukkit.Statistic;
7 +import org.bukkit.Statistic.Type;
8 +import org.bukkit.entity.EntityType;
9 +import org.jetbrains.annotations.NotNull;
10 +
11 +/**
12 + * Represents a scoreboard criteria, either custom or built-in to the Minecraft server, used to
13 + * keep track of and manually or automatically change scores on a scoreboard.
14 + * <p>
15 + * While this class outlines constants for standard criteria, see {@link #statistic(Statistic)}
16 + * (and its overloads) to create instances for statistically-backed criteria.
17 + */
18 +public interface Criteria {
19 +
20 + /**
21 + * The dummy criteria. Not changed by the server.
22 + */
23 + public static final Criteria DUMMY = Bukkit.getScoreboardCriteria("dummy");
24 + /**
25 + * The trigger criteria. Changed when a player runs the /trigger command for an objective.
26 + */
27 + public static final Criteria TRIGGER = Bukkit.getScoreboardCriteria("trigger");
28 + /**
29 + * Increments automatically when a player dies.
30 + */
31 + public static final Criteria DEATH_COUNT = Bukkit.getScoreboardCriteria("deathCount");
32 + /**
33 + * Increments automatically when a player kills another player.
34 + */
35 + public static final Criteria PLAYER_KILL_COUNT = Bukkit.getScoreboardCriteria("playerKillCount");
36 + /**
37 + * Increments automatically when a player kills another living entity.
38 + */
39 + public static final Criteria TOTAL_KILL_COUNT = Bukkit.getScoreboardCriteria("totalKillCount");
40 + /**
41 + * Mirrors the player's health points (0 for no health, 20 for maximum default health).
42 + */
43 + public static final Criteria HEALTH = Bukkit.getScoreboardCriteria("health");
44 + /**
45 + * Mirrors the player's food points (0 for no food, 20 for maximum food).
46 + */
47 + public static final Criteria FOOD = Bukkit.getScoreboardCriteria("food");
48 + /**
49 + * Mirrors the player's air supply (0 for no air, 300 for maximum air).
50 + */
51 + public static final Criteria AIR = Bukkit.getScoreboardCriteria("air");
52 + /**
53 + * Mirrors the player's armor points (0 for no armor, 20 for maximum armor).
54 + */
55 + public static final Criteria ARMOR = Bukkit.getScoreboardCriteria("armor");
56 + /**
57 + * Mirrors the player's experience points.
58 + */
59 + public static final Criteria XP = Bukkit.getScoreboardCriteria("xp");
60 + /**
61 + * Mirrors the player's experience level.
62 + */
63 + public static final Criteria LEVEL = Bukkit.getScoreboardCriteria("level");
64 +
65 + /**
66 + * Increments automatically when a player kills another player on the black team.
67 + */
68 + public static final Criteria TEAM_KILL_BLACK = Bukkit.getScoreboardCriteria("teamkill.black");
69 + /**
70 + * Increments automatically when a player kills another player on the dark blue team.
71 + */
72 + public static final Criteria TEAM_KILL_DARK_BLUE = Bukkit.getScoreboardCriteria("teamkill.dark_blue");
73 + /**
74 + * Increments automatically when a player kills another player on the dark green team.
75 + */
76 + public static final Criteria TEAM_KILL_DARK_GREEN = Bukkit.getScoreboardCriteria("teamkill.dark_green");
77 + /**
78 + * Increments automatically when a player kills another player on the dark aqua team.
79 + */
80 + public static final Criteria TEAM_KILL_DARK_AQUA = Bukkit.getScoreboardCriteria("teamkill.dark_aqua");
81 + /**
82 + * Increments automatically when a player kills another player on the dark red team.
83 + */
84 + public static final Criteria TEAM_KILL_DARK_RED = Bukkit.getScoreboardCriteria("teamkill.dark_red");
85 + /**
86 + * Increments automatically when a player kills another player on the dark purple team.
87 + */
88 + public static final Criteria TEAM_KILL_DARK_PURPLE = Bukkit.getScoreboardCriteria("teamkill.dark_purple");
89 + /**
90 + * Increments automatically when a player kills another player on the gold team.
91 + */
92 + public static final Criteria TEAM_KILL_GOLD = Bukkit.getScoreboardCriteria("teamkill.gold");
93 + /**
94 + * Increments automatically when a player kills another player on the gray team.
95 + */
96 + public static final Criteria TEAM_KILL_GRAY = Bukkit.getScoreboardCriteria("teamkill.gray");
97 + /**
98 + * Increments automatically when a player kills another player on the dark gray team.
99 + */
100 + public static final Criteria TEAM_KILL_DARK_GRAY = Bukkit.getScoreboardCriteria("teamkill.dark_gray");
101 + /**
102 + * Increments automatically when a player kills another player on the blue team.
103 + */
104 + public static final Criteria TEAM_KILL_BLUE = Bukkit.getScoreboardCriteria("teamkill.blue");
105 + /**
106 + * Increments automatically when a player kills another player on the green team.
107 + */
108 + public static final Criteria TEAM_KILL_GREEN = Bukkit.getScoreboardCriteria("teamkill.green");
109 + /**
110 + * Increments automatically when a player kills another player on the aqua team.
111 + */
112 + public static final Criteria TEAM_KILL_AQUA = Bukkit.getScoreboardCriteria("teamkill.aqua");
113 + /**
114 + * Increments automatically when a player kills another player on the red team.
115 + */
116 + public static final Criteria TEAM_KILL_RED = Bukkit.getScoreboardCriteria("teamkill.red");
117 + /**
118 + * Increments automatically when a player kills another player on the light purple team.
119 + */
120 + public static final Criteria TEAM_KILL_LIGHT_PURPLE = Bukkit.getScoreboardCriteria("teamkill.light_purple");
121 + /**
122 + * Increments automatically when a player kills another player on the yellow team.
123 + */
124 + public static final Criteria TEAM_KILL_YELLOW = Bukkit.getScoreboardCriteria("teamkill.yellow");
125 + /**
126 + * Increments automatically when a player kills another player on the white team.
127 + */
128 + public static final Criteria TEAM_KILL_WHITE = Bukkit.getScoreboardCriteria("teamkill.white");
129 +
130 + /**
131 + * Increments automatically when a player is killed by a player on the black team.
132 + */
133 + public static final Criteria KILLED_BY_TEAM_BLACK = Bukkit.getScoreboardCriteria("killedByTeam.black");
134 + /**
135 + * Increments automatically when a player is killed by a player on the dark blue team.
136 + */
137 + public static final Criteria KILLED_BY_TEAM_DARK_BLUE = Bukkit.getScoreboardCriteria("killedByTeam.dark_blue");
138 + /**
139 + * Increments automatically when a player is killed by a player on the dark green team.
140 + */
141 + public static final Criteria KILLED_BY_TEAM_DARK_GREEN = Bukkit.getScoreboardCriteria("killedByTeam.dark_green");
142 + /**
143 + * Increments automatically when a player is killed by a player on the dark aqua team.
144 + */
145 + public static final Criteria KILLED_BY_TEAM_DARK_AQUA = Bukkit.getScoreboardCriteria("killedByTeam.dark_aqua");
146 + /**
147 + * Increments automatically when a player is killed by a player on the dark red team.
148 + */
149 + public static final Criteria KILLED_BY_TEAM_DARK_RED = Bukkit.getScoreboardCriteria("killedByTeam.dark_red");
150 + /**
151 + * Increments automatically when a player is killed by a player on the dark purple team.
152 + */
153 + public static final Criteria KILLED_BY_TEAM_DARK_PURPLE = Bukkit.getScoreboardCriteria("killedByTeam.dark_purple");
154 + /**
155 + * Increments automatically when a player is killed by a player on the gold team.
156 + */
157 + public static final Criteria KILLED_BY_TEAM_GOLD = Bukkit.getScoreboardCriteria("killedByTeam.gold");
158 + /**
159 + * Increments automatically when a player is killed by a player on the gray team.
160 + */
161 + public static final Criteria KILLED_BY_TEAM_GRAY = Bukkit.getScoreboardCriteria("killedByTeam.gray");
162 + /**
163 + * Increments automatically when a player is killed by a player on the dark gray team.
164 + */
165 + public static final Criteria KILLED_BY_TEAM_DARK_GRAY = Bukkit.getScoreboardCriteria("killedByTeam.dark_gray");
166 + /**
167 + * Increments automatically when a player is killed by a player on the blue team.
168 + */
169 + public static final Criteria KILLED_BY_TEAM_BLUE = Bukkit.getScoreboardCriteria("killedByTeam.blue");
170 + /**
171 + * Increments automatically when a player is killed by a player on the green team.
172 + */
173 + public static final Criteria KILLED_BY_TEAM_GREEN = Bukkit.getScoreboardCriteria("killedByTeam.green");
174 + /**
175 + * Increments automatically when a player is killed by a player on the aqua team.
176 + */
177 + public static final Criteria KILLED_BY_TEAM_AQUA = Bukkit.getScoreboardCriteria("killedByTeam.aqua");
178 + /**
179 + * Increments automatically when a player is killed by a player on the red team.
180 + */
181 + public static final Criteria KILLED_BY_TEAM_RED = Bukkit.getScoreboardCriteria("killedByTeam.red");
182 + /**
183 + * Increments automatically when a player is killed by a player on the light purple team.
184 + */
185 + public static final Criteria KILLED_BY_TEAM_LIGHT_PURPLE = Bukkit.getScoreboardCriteria("killedByTeam.light_purple");
186 + /**
187 + * Increments automatically when a player is killed by a player on the yellow team.
188 + */
189 + public static final Criteria KILLED_BY_TEAM_YELLOW = Bukkit.getScoreboardCriteria("killedByTeam.yellow");
190 + /**
191 + * Increments automatically when a player is killed by a player on the white team.
192 + */
193 + public static final Criteria KILLED_BY_TEAM_WHITE = Bukkit.getScoreboardCriteria("killedByTeam.white");
194 +
195 + /**
196 + * Get the name of this criteria (its unique id).
197 + *
198 + * @return the name
199 + */
200 + @NotNull
201 + public String getName();
202 +
203 + /**
204 + * Get whether or not this criteria is read only. If read only, scoreboards with this criteria
205 + * cannot have their scores changed.
206 + *
207 + * @return true if read only, false otherwise
208 + */
209 + public boolean isReadOnly();
210 +
211 + /**
212 + * Get the {@link RenderType} used by default for this criteria.
213 + *
214 + * @return the default render type
215 + */
216 + @NotNull
217 + public RenderType getDefaultRenderType();
218 +
219 + /**
220 + * Get a {@link Criteria} for the specified statistic pertaining to blocks or items.
221 + * <p>
222 + * This method expects a {@link Statistic} of {@link Type#BLOCK} or {@link Type#ITEM} and the
223 + * {@link Material} matching said type (e.g. BLOCK statistics require materials where
224 + * {@link Material#isBlock()} is true). This acts as a convenience to create more complex
225 + * compound criteria such as those that increment on block breaks, or item uses. An example
226 + * would be {@code Criteria.statistic(Statistic.CRAFT_ITEM, Material.STICK)}, returning a
227 + * Criteria representing "minecraft.crafted:minecraft.stick" which will increment when the
228 + * player crafts a stick.
229 + * <p>
230 + * If the provided statistic does not require additional data, {@link #statistic(Statistic)}
231 + * is called and returned instead.
232 + * <p>
233 + * This method provides no guarantee that any given criteria exists on the vanilla server.
234 + *
235 + * @param statistic the statistic for which to get a criteria
236 + * @param material the relevant material
237 + *
238 + * @return the criteria
239 + *
240 + * @throws IllegalArgumentException if {@link Statistic#getType()} is anything other than
241 + * {@link Type#BLOCK} or {@link Type#ITEM}
242 + * @throws IllegalArgumentException if {@link Statistic#getType()} is {@link Type#BLOCK}, but
243 + * {@link Material#isBlock()} is false
244 + * @throws IllegalArgumentException if {@link Statistic#getType()} is {@link Type#ITEM}, but
245 + * {@link Material#isItem()} is false
246 + */
247 + @NotNull
248 + public static Criteria statistic(@NotNull Statistic statistic, @NotNull Material material) {
249 + Preconditions.checkArgument(statistic != null, "statistic must not be null");
250 + Preconditions.checkArgument(material != null, "material must not be null");
251 +
252 + Type type = statistic.getType();
253 + Preconditions.checkArgument(type == Type.BLOCK || type == Type.ITEM, "statistic type must be either BLOCK or ITEM, given %s", type);
254 + Preconditions.checkArgument(type != Type.BLOCK || material.isBlock(), "statistic type is BLOCK but got non-block Material, %s", material);
255 + Preconditions.checkArgument(type != Type.ITEM || material.isItem(), "statistic type is ITEM but got non-item Material, %s", material);
256 +
257 + // Good use case for a switch expression
258 + if (type == Type.BLOCK) {
259 + switch (statistic) {
260 + case MINE_BLOCK:
261 + return Bukkit.getScoreboardCriteria("minecraft.mined:minecraft." + material.getKey().getKey());
262 + default:
263 + break;
264 + }
265 + } else if (type == Type.ITEM) {
266 + switch (statistic) {
267 + case BREAK_ITEM:
268 + return Bukkit.getScoreboardCriteria("minecraft.broken:minecraft." + material.getKey().getKey());
269 + case CRAFT_ITEM:
270 + return Bukkit.getScoreboardCriteria("minecraft.crafted:minecraft." + material.getKey().getKey());
271 + case USE_ITEM:
272 + return Bukkit.getScoreboardCriteria("minecraft.used:minecraft." + material.getKey().getKey());
273 + case PICKUP:
274 + return Bukkit.getScoreboardCriteria("minecraft.picked_up:minecraft." + material.getKey().getKey());
275 + case DROP:
276 + return Bukkit.getScoreboardCriteria("minecraft.dropped:minecraft." + material.getKey().getKey());
277 + default:
278 + break;
279 + }
280 + }
281 +
282 + return statistic(statistic); // Fallback to a regular statistic
283 + }
284 +
285 + /**
286 + * Get a {@link Criteria} for the specified statistic pertaining to an entity type.
287 + * <p>
288 + * This method expects a {@link Statistic} of {@link Type#ENTITY}. This acts as a convenience
289 + * to create more complex compound criteria such as being killed by a specific entity type.
290 + * An example would be {@code Criteria.statistic(Statistic.KILL_ENTITY, EntityType.CREEPER)},
291 + * returning a Criteria representing "minecraft.killed:minecraft.creeper" which will increment
292 + * when the player kills a creepers.
293 + * <p>
294 + * If the provided statistic does not require additional data, {@link #statistic(Statistic)}
295 + * is called and returned instead.
296 + * <p>
297 + * This method provides no guarantee that any given criteria exists on the vanilla server.
298 + *
299 + * @param statistic the statistic for which to get a criteria
300 + * @param entityType the relevant entity type
301 + *
302 + * @return the criteria
303 + *
304 + * @throws IllegalArgumentException if {@link Statistic#getType()} is not {@link Type#ENTITY}
305 + */
306 + @NotNull
307 + public static Criteria statistic(@NotNull Statistic statistic, @NotNull EntityType entityType) {
308 + Preconditions.checkArgument(statistic != null, "statistic must not be null");
309 + Preconditions.checkArgument(entityType != null, "entityType must not be null");
310 + Preconditions.checkArgument(statistic.getType() == Type.ENTITY, "statistic type must be ENTITY, given %s", statistic.getType());
311 +
312 + switch (statistic) {
313 + case KILL_ENTITY:
314 + return Bukkit.getScoreboardCriteria("minecraft.killed:minecraft." + entityType.getKey().getKey());
315 + case ENTITY_KILLED_BY:
316 + return Bukkit.getScoreboardCriteria("minecraft.killed_by:minecraft." + entityType.getKey().getKey());
317 + default:
318 + break;
319 + }
320 +
321 + return statistic(statistic); // Fallback to a regular statistic
322 + }
323 +
324 + /**
325 + * Get a {@link Criteria} for the specified statistic.
326 + * <p>
327 + * An example would be {@code Criteria.statistic(Statistic.FLY_ONE_CM)}, returning a Criteria
328 + * representing "minecraft.custom:minecraft.aviate_one_cm" which will increment when the player
329 + * flies with an elytra.
330 + * <p>
331 + * This method provides no guarantee that any given criteria exists on the vanilla server. All
332 + * statistics are accepted, however some may not operate as expected if additional data is
333 + * required. For block/item-related statistics, see {@link #statistic(Statistic, Material)},
334 + * and for entity-related statistics, see {@link #statistic(Statistic, EntityType)}
335 + *
336 + * @param statistic the statistic for which to get a criteria
337 + *
338 + * @return the criteria
339 + */
340 + @NotNull
341 + public static Criteria statistic(@NotNull Statistic statistic) {
342 + Preconditions.checkArgument(statistic != null, "statistic must not be null");
343 + return Bukkit.getScoreboardCriteria("minecraft.custom:minecraft." + statistic.getKey().getKey());
344 + }
345 +
346 + /**
347 + * Get (or create) a new {@link Criteria} by its name.
348 + *
349 + * @param name the criteria name
350 + *
351 + * @return the created criteria
352 + */
353 + @NotNull
354 + public static Criteria create(@NotNull String name) {
355 + return Bukkit.getScoreboardCriteria(name);
356 + }
357 +
358 +}

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut