Skip to content

Commit df84f14

Browse files
Absolutionismerenkarakal
authored andcommitted
Add ExprTestPlayer and Update Comparators (SkriptLang#7840)
1 parent 4352973 commit df84f14

File tree

7 files changed

+340
-29
lines changed

7 files changed

+340
-29
lines changed

src/main/java/ch/njol/skript/classes/data/DefaultComparators.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,7 @@
1010
import ch.njol.skript.entity.BoatData;
1111
import ch.njol.skript.entity.EntityData;
1212
import ch.njol.skript.entity.RabbitData;
13-
import ch.njol.skript.util.BlockUtils;
14-
import ch.njol.skript.util.Color;
15-
import ch.njol.skript.util.Date;
16-
import ch.njol.skript.util.EnchantmentType;
17-
import ch.njol.skript.util.Experience;
18-
import ch.njol.skript.util.GameruleValue;
19-
import ch.njol.skript.util.StructureType;
20-
import ch.njol.skript.util.Time;
21-
import ch.njol.skript.util.Timeperiod;
22-
import ch.njol.skript.util.Timespan;
23-
import ch.njol.skript.util.WeatherType;
13+
import ch.njol.skript.util.*;
2414
import ch.njol.skript.util.slot.EquipmentSlot;
2515
import ch.njol.skript.util.slot.Slot;
2616
import ch.njol.skript.util.slot.SlotWithIndex;
@@ -350,7 +340,7 @@ public boolean supportsOrdering() {
350340
Comparators.registerComparator(OfflinePlayer.class, OfflinePlayer.class, new Comparator<OfflinePlayer, OfflinePlayer>() {
351341
@Override
352342
public Relation compare(OfflinePlayer p1, OfflinePlayer p2) {
353-
return Relation.get(Objects.equals(p1.getName(), p2.getName()));
343+
return Relation.get(Objects.equals(p1.getUniqueId(), p2.getUniqueId()));
354344
}
355345

356346
@Override
@@ -362,16 +352,23 @@ public boolean supportsOrdering() {
362352
// OfflinePlayer - String
363353
Comparators.registerComparator(OfflinePlayer.class, String.class, new Comparator<OfflinePlayer, String>() {
364354
@Override
365-
public Relation compare(OfflinePlayer p, String name) {
366-
String offlineName = p.getName();
367-
return offlineName == null ? Relation.NOT_EQUAL : Relation.get(offlineName.equalsIgnoreCase(name));
355+
public Relation compare(OfflinePlayer player, String name) {
356+
if (Utils.isValidUUID(name)) {
357+
UUID uuid = UUID.fromString(name);
358+
return Relation.get(player.getUniqueId().equals(uuid));
359+
}
360+
String playerName = player.getName();
361+
return playerName == null ? Relation.NOT_EQUAL : Relation.get(playerName.equalsIgnoreCase(name));
368362
}
369363

370364
@Override
371365
public boolean supportsOrdering() {
372366
return false;
373367
}
374368
});
369+
370+
// OfflinePlayer - UUID
371+
Comparators.registerComparator(OfflinePlayer.class, UUID.class, (player, uuid) -> Relation.get(player.getUniqueId().equals(uuid)));
375372

376373
// World - String
377374
Comparators.registerComparator(World.class, String.class, new Comparator<World, String>() {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package ch.njol.skript.test.runner;
2+
3+
import ch.njol.skript.Skript;
4+
import ch.njol.skript.doc.NoDoc;
5+
import ch.njol.skript.lang.Expression;
6+
import ch.njol.skript.lang.ExpressionType;
7+
import ch.njol.skript.lang.SkriptParser.ParseResult;
8+
import ch.njol.skript.lang.util.SimpleExpression;
9+
import ch.njol.skript.test.utils.TestOfflinePlayer;
10+
import ch.njol.util.Kleenean;
11+
import org.bukkit.OfflinePlayer;
12+
import org.bukkit.event.Event;
13+
import org.jetbrains.annotations.Nullable;
14+
15+
@NoDoc
16+
public class ExprTestOfflinePlayer extends SimpleExpression<OfflinePlayer> {
17+
18+
private static final OfflinePlayer PLAYER;
19+
20+
static {
21+
if (TestMode.ENABLED) {
22+
Skript.registerExpression(ExprTestOfflinePlayer.class, OfflinePlayer.class, ExpressionType.SIMPLE,
23+
"[the] test(-| )offline[-| ]player");
24+
PLAYER = TestOfflinePlayer.getInstance();
25+
} else {
26+
PLAYER = null;
27+
}
28+
}
29+
30+
@Override
31+
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
32+
return true;
33+
}
34+
35+
@Override
36+
protected OfflinePlayer @Nullable [] get(Event event) {
37+
return new OfflinePlayer[] {PLAYER};
38+
}
39+
40+
@Override
41+
public boolean isSingle() {
42+
return true;
43+
}
44+
45+
@Override
46+
public Class<? extends OfflinePlayer> getReturnType() {
47+
return OfflinePlayer.class;
48+
}
49+
50+
@Override
51+
public String toString(@Nullable Event event, boolean debug) {
52+
return "the test offline player";
53+
}
54+
55+
}
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
package ch.njol.skript.test.utils;
2+
3+
import ch.njol.skript.test.runner.TestMode;
4+
import com.destroystokyo.paper.profile.PlayerProfile;
5+
import com.destroystokyo.paper.profile.ProfileProperty;
6+
import io.papermc.paper.persistence.PersistentDataContainerView;
7+
import org.bukkit.*;
8+
import org.bukkit.entity.EntityType;
9+
import org.bukkit.entity.Player;
10+
import org.jetbrains.annotations.NotNull;
11+
import org.jspecify.annotations.Nullable;
12+
13+
import java.time.Duration;
14+
import java.time.Instant;
15+
import java.util.Date;
16+
import java.util.Map;
17+
import java.util.UUID;
18+
19+
/**
20+
* A wrapper for an {@link OfflinePlayer} and a custom {@link PlayerProfile}.
21+
* Allows having a valid {@link OfflinePlayer} object without the need to do any lookups, especially if Mojang authorization is down.
22+
*/
23+
public class TestOfflinePlayer implements OfflinePlayer {
24+
25+
private static final String PLAYER_NAME = "SkriptLang";
26+
private static final UUID PLAYER_UUID = UUID.randomUUID();
27+
private static final OfflinePlayer PLAYER = Bukkit.getOfflinePlayer(PLAYER_UUID);
28+
private static final PlayerProfile PLAYER_PROFILE = PLAYER.getPlayerProfile();
29+
private static final @Nullable TestOfflinePlayer instance;
30+
31+
static {
32+
if (TestMode.ENABLED) {
33+
instance = new TestOfflinePlayer();
34+
PLAYER_PROFILE.setProperty(new ProfileProperty(
35+
"textures",
36+
"ewogICJ0aW1lc3RhbXAiIDogMTc0NzQyOTg2MTQwOCwKICAicHJvZmlsZUlkIiA6ICI2OWUzNzAyNjJjN2Q0MjU1YWM3NjliMTNhNWZlOGY3NCIsCiAgInByb2ZpbGVOYW1lIiA6ICJTYWh2ZGUiLAogICJzaWduYXR1cmVSZXF1aXJlZCIgOiB0cnVlLAogICJ0ZXh0dXJlcyIgOiB7CiAgICAiU0tJTiIgOiB7CiAgICAgICJ1cmwiIDogImh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNTE2MGFiZWVhNDI1YzZmODMyYjc0NmE0NTQ0YzVmYjlhOTgxYjAyZTFiZDg1ZmVhNWM3ZWY4MzFiZGM4NzRmMyIKICAgIH0KICB9Cn0="
37+
));
38+
} else {
39+
instance = null;
40+
}
41+
}
42+
43+
private TestOfflinePlayer() {}
44+
45+
public static @Nullable TestOfflinePlayer getInstance() {
46+
return instance;
47+
}
48+
49+
@Override
50+
public @Nullable String getName() {
51+
return PLAYER_NAME;
52+
}
53+
54+
@Override
55+
public UUID getUniqueId() {
56+
return PLAYER_UUID;
57+
}
58+
59+
@Override
60+
public boolean isOnline() {
61+
return PLAYER.isOnline();
62+
}
63+
64+
@Override
65+
public boolean isConnected() {
66+
return PLAYER.isConnected();
67+
}
68+
69+
@Override
70+
public PlayerProfile getPlayerProfile() {
71+
return PLAYER_PROFILE;
72+
}
73+
74+
@Override
75+
public boolean isBanned() {
76+
return PLAYER.isBanned();
77+
}
78+
79+
@Override
80+
public <E extends BanEntry<? super PlayerProfile>> @Nullable E ban(@Nullable String reason, @Nullable Date expires, @Nullable String source) {
81+
return PLAYER.ban(reason, expires, source);
82+
}
83+
84+
@Override
85+
public <E extends BanEntry<? super PlayerProfile>> @Nullable E ban(@Nullable String reason, @Nullable Instant expires, @Nullable String source) {
86+
return PLAYER.ban(reason, expires, source);
87+
}
88+
89+
@Override
90+
public <E extends BanEntry<? super PlayerProfile>> @Nullable E ban(@Nullable String reason, @Nullable Duration duration, @Nullable String source) {
91+
return PLAYER.ban(reason, duration, source);
92+
}
93+
94+
@Override
95+
public boolean isWhitelisted() {
96+
return PLAYER.isWhitelisted();
97+
}
98+
99+
@Override
100+
public void setWhitelisted(boolean value) {
101+
PLAYER.setWhitelisted(value);
102+
}
103+
104+
@Override
105+
public @Nullable Player getPlayer() {
106+
return PLAYER.getPlayer();
107+
}
108+
109+
@Override
110+
public long getFirstPlayed() {
111+
return PLAYER.getFirstPlayed();
112+
}
113+
114+
@Override
115+
public long getLastPlayed() {
116+
return PLAYER.getLastPlayed();
117+
}
118+
119+
@Override
120+
public boolean hasPlayedBefore() {
121+
return PLAYER.hasPlayedBefore();
122+
}
123+
124+
@Override
125+
public long getLastLogin() {
126+
return PLAYER.getLastLogin();
127+
}
128+
129+
@Override
130+
public long getLastSeen() {
131+
return PLAYER.getLastSeen();
132+
}
133+
134+
@Override
135+
public @Nullable Location getRespawnLocation(boolean loadLocationAndValidate) {
136+
return PLAYER.getRespawnLocation(loadLocationAndValidate);
137+
}
138+
139+
@Override
140+
public void incrementStatistic(Statistic statistic) throws IllegalArgumentException {
141+
PLAYER.incrementStatistic(statistic);
142+
}
143+
144+
@Override
145+
public void decrementStatistic(Statistic statistic) throws IllegalArgumentException {
146+
PLAYER.decrementStatistic(statistic);
147+
}
148+
149+
@Override
150+
public void incrementStatistic(Statistic statistic, int amount) throws IllegalArgumentException {
151+
PLAYER.incrementStatistic(statistic, amount);
152+
}
153+
154+
@Override
155+
public void decrementStatistic(Statistic statistic, int amount) throws IllegalArgumentException {
156+
PLAYER.decrementStatistic(statistic, amount);
157+
}
158+
159+
@Override
160+
public void setStatistic(Statistic statistic, int newValue) throws IllegalArgumentException {
161+
PLAYER.setStatistic(statistic, newValue);
162+
}
163+
164+
@Override
165+
public int getStatistic(Statistic statistic) throws IllegalArgumentException {
166+
return PLAYER.getStatistic(statistic);
167+
}
168+
169+
@Override
170+
public void incrementStatistic(Statistic statistic, Material material) throws IllegalArgumentException {
171+
PLAYER.incrementStatistic(statistic, material);
172+
}
173+
174+
@Override
175+
public void decrementStatistic(Statistic statistic, Material material) throws IllegalArgumentException {
176+
PLAYER.decrementStatistic(statistic, material);
177+
}
178+
179+
@Override
180+
public int getStatistic(Statistic statistic, Material material) throws IllegalArgumentException {
181+
return PLAYER.getStatistic(statistic, material);
182+
}
183+
184+
@Override
185+
public void incrementStatistic(Statistic statistic, Material material, int amount) throws IllegalArgumentException {
186+
PLAYER.incrementStatistic(statistic, material, amount);
187+
}
188+
189+
@Override
190+
public void decrementStatistic(Statistic statistic, Material material, int amount) throws IllegalArgumentException {
191+
PLAYER.decrementStatistic(statistic, material, amount);
192+
}
193+
194+
@Override
195+
public void setStatistic(Statistic statistic, Material material, int newValue) throws IllegalArgumentException {
196+
PLAYER.setStatistic(statistic, material, newValue);
197+
}
198+
199+
@Override
200+
public void incrementStatistic(Statistic statistic, EntityType entityType) throws IllegalArgumentException {
201+
PLAYER.incrementStatistic(statistic, entityType);
202+
}
203+
204+
@Override
205+
public void decrementStatistic(Statistic statistic, EntityType entityType) throws IllegalArgumentException {
206+
PLAYER.decrementStatistic(statistic, entityType);
207+
}
208+
209+
@Override
210+
public int getStatistic(Statistic statistic, EntityType entityType) throws IllegalArgumentException {
211+
return PLAYER.getStatistic(statistic, entityType);
212+
}
213+
214+
@Override
215+
public void incrementStatistic(Statistic statistic, EntityType entityType, int amount) throws IllegalArgumentException {
216+
PLAYER.incrementStatistic(statistic, entityType, amount);
217+
}
218+
219+
@Override
220+
public void decrementStatistic(Statistic statistic, EntityType entityType, int amount) {
221+
PLAYER.decrementStatistic(statistic, entityType, amount);
222+
}
223+
224+
@Override
225+
public void setStatistic(Statistic statistic, EntityType entityType, int newValue) {
226+
PLAYER.setStatistic(statistic, entityType, newValue);
227+
}
228+
229+
@Override
230+
public @Nullable Location getLastDeathLocation() {
231+
return PLAYER.getLastDeathLocation();
232+
}
233+
234+
@Override
235+
public @Nullable Location getLocation() {
236+
return PLAYER.getLocation();
237+
}
238+
239+
@Override
240+
public PersistentDataContainerView getPersistentDataContainer() {
241+
return PLAYER.getPersistentDataContainer();
242+
}
243+
244+
@Override
245+
public @NotNull Map<String, Object> serialize() {
246+
return PLAYER.serialize();
247+
}
248+
249+
@Override
250+
public boolean isOp() {
251+
return PLAYER.isOp();
252+
}
253+
254+
@Override
255+
public void setOp(boolean value) {
256+
PLAYER.setOp(value);
257+
}
258+
259+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
test "entity owner of tameable":
22
spawn a wolf at test-location:
33
assert event-entity is tameable with "entity is tameable condition not passing, last spawned wolf is not tameable"
4-
set owner of event-entity to "Notch" parsed as offline player
5-
assert "%owner of event-entity%" = "Notch" with "Owner of last spawned wolf was not set"
4+
set owner of event-entity to test-offline-player
5+
assert "%owner of event-entity%" is test-offline-player with "Owner of last spawned wolf was not set"
66
delete event-entity

src/test/skript/tests/syntaxes/expressions/ExprSkullOwner.sk

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ test "skull owner - block":
33
set {_old} to block at {_loc}
44
set block at {_loc} to player head
55
assert skull owner of (block at {_loc}) is not set with "Block Skull Owner should not be set"
6-
set {_player} to "Sovde" parsed as offline player
7-
set skull owner of (block at {_loc}) to {_player}
8-
assert skull owner of (block at {_loc}) is {_player} with "Block Skull Owner did not change"
6+
set skull owner of (block at {_loc}) to test-offline-player
7+
assert skull owner of (block at {_loc}) is test-offline-player with "Block Skull Owner did not change"
98
set block at {_loc} to {_old}
109

1110
test "skull owner - item":
1211
set {_skull} to a player head
1312
assert skull owner of {_skull} is not set with "Item Skull Owner should not be set"
14-
set {_player} to "Sovde" parsed as offline player
15-
set skull owner of {_skull} to {_player}
16-
assert skull owner of {_skull} is {_player} with "Item Skull Owner did not change"
13+
set skull owner of {_skull} to test-offline-player
14+
assert skull owner of {_skull} is test-offline-player with "Item Skull Owner did not change"

0 commit comments

Comments
 (0)