-
-
Notifications
You must be signed in to change notification settings - Fork 447
Expand file tree
/
Copy pathDefaultComparators.java
More file actions
622 lines (539 loc) · 18.9 KB
/
Copy pathDefaultComparators.java
File metadata and controls
622 lines (539 loc) · 18.9 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package ch.njol.skript.classes.data;
import ch.njol.skript.Skript;
import ch.njol.skript.SkriptConfig;
import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.aliases.ItemData;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.entity.BoatChestData;
import ch.njol.skript.entity.BoatData;
import ch.njol.skript.entity.EntityData;
import ch.njol.skript.entity.RabbitData;
import ch.njol.skript.util.*;
import ch.njol.skript.util.slot.EquipmentSlot;
import ch.njol.skript.util.slot.Slot;
import ch.njol.skript.util.slot.SlotWithIndex;
import ch.njol.util.StringUtils;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentOffer;
import org.bukkit.entity.*;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.skriptlang.skript.lang.comparator.Comparator;
import org.skriptlang.skript.lang.comparator.Comparators;
import org.skriptlang.skript.lang.comparator.Relation;
import java.util.Objects;
import java.util.UUID;
@SuppressWarnings({"rawtypes"})
public class DefaultComparators {
public DefaultComparators() {}
static {
// Number - Number
Comparators.registerComparator(Number.class, Number.class, new Comparator<>() {
@Override
public Relation compare(Number n1, Number n2) {
if (n1 instanceof Long && n2 instanceof Long)
return Relation.get(n1.longValue() - n2.longValue());
double epsilon = Skript.EPSILON;
@SuppressWarnings("WrapperTypeMayBePrimitive") Double d1, d2;
if (n1 instanceof Float || n2 instanceof Float) {
d1 = (double) n1.floatValue();
d2 = (double) n2.floatValue();
epsilon = Math.min(d1, d2) * 1e-6; // dynamic epsilon
} else {
d1 = n1.doubleValue();
d2 = n2.doubleValue();
}
if (d1.isNaN() || d2.isNaN()) {
return Relation.SMALLER;
} else if (d1.isInfinite() || d2.isInfinite()) {
return d1 > d2 ? Relation.GREATER : d1 < d2 ? Relation.SMALLER : Relation.EQUAL;
} else {
double diff = d1 - d2;
if (Math.abs(diff) < epsilon)
return Relation.EQUAL;
return Relation.get(diff);
}
}
@Override
public boolean supportsOrdering() {
return true;
}
});
// Slot - Slot
Comparators.registerComparator(Slot.class, Slot.class, new Comparator<Slot, Slot>() {
@Override
public Relation compare(Slot slot1, Slot slot2) {
if (slot1 instanceof EquipmentSlot != slot2 instanceof EquipmentSlot)
return Relation.NOT_EQUAL;
return Relation.get(slot1.isSameSlot(slot2));
}
});
// Slot - Number
Comparators.registerComparator(Slot.class, Number.class, new Comparator<Slot, Number>() {
@Override
public Relation compare(Slot o1, Number o2) {
if (o1 instanceof SlotWithIndex) {
return Relation.get(((SlotWithIndex) o1).getIndex() - o2.intValue());
}
return Relation.NOT_EQUAL;
}
@Override
public boolean supportsOrdering() {
return true;
}
});
// Slot - ItemType
Comparators.registerComparator(Slot.class, ItemType.class, new Comparator<Slot, ItemType>() {
@Override
public Relation compare(Slot slot, ItemType item) {
ItemStack stack = slot.getItem();
if (stack == null || stack.getAmount() == 0)
return Comparators.compare(new ItemType(Material.AIR), item);
return Comparators.compare(new ItemType(stack), item);
}
@Override
public boolean supportsInversion() {
return false;
}
});
// ItemType - Slot
Comparators.registerComparator(ItemType.class, Slot.class, new Comparator<ItemType, Slot>() {
@Override
public Relation compare(ItemType item, Slot slot) {
ItemStack stack = slot.getItem();
if (stack == null || stack.getAmount() == 0)
return Comparators.compare(item, new ItemType(Material.AIR));
return Comparators.compare(item, new ItemType(stack));
}
@Override
public boolean supportsInversion() {
return false;
}
});
// ItemStack - ItemType
Comparators.registerComparator(ItemStack.class, ItemType.class, new Comparator<ItemStack, ItemType>() {
@Override
public Relation compare(ItemStack is, ItemType it) {
return Comparators.compare(new ItemType(is), it);
}
@Override
public boolean supportsInversion() {
return false;
}
});
// ItemType - ItemStack
Comparators.registerComparator(ItemType.class, ItemStack.class, new Comparator<ItemType, ItemStack>() {
@Override
public Relation compare(ItemType it, ItemStack is) {
return Comparators.compare(it, new ItemType(is));
}
@Override
public boolean supportsInversion() {
return false;
}
});
// Block - ItemType
Comparators.registerComparator(Block.class, ItemType.class, new Comparator<Block, ItemType>() {
@Override
public Relation compare(Block b, ItemType it) {
return Comparators.compare(new ItemType(b), it);
}
@Override
public boolean supportsInversion() {
return false;
}
});
// Block - BlockData
Comparators.registerComparator(Block.class, BlockData.class, new Comparator<Block, BlockData>() {
@Override
public Relation compare(Block block, BlockData data) {
return Relation.get(block.getBlockData().matches(data));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// BlockData - BlockData
Comparators.registerComparator(BlockData.class, BlockData.class, new Comparator<BlockData, BlockData>() {
@Override
public Relation compare(BlockData data1, BlockData data2) {
return Relation.get(data1.matches(data2));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// ItemType - ItemType
Comparators.registerComparator(ItemType.class, ItemType.class, new Comparator<ItemType, ItemType>() {
@Override
public Relation compare(ItemType i1, ItemType i2) {
int otherAmount = i2.getAmount();
if (i1.getAmount() != otherAmount) {
// See https://github.com/SkriptLang/Skript/issues/4278 for reference
if (otherAmount != 1) // Don't ignore stack size if the other ItemType has a stack size other than one, even if it may be an alias
return Relation.NOT_EQUAL;
for (ItemData itemData : i2.getTypes()) {
if (!itemData.isAlias()) // Don't ignore stack size if the other ItemType has non alias data.
return Relation.NOT_EQUAL;
}
}
return Relation.get(i1.isSimilar(i2));
}
@Override
public boolean supportsInversion() {
return false;
}
});
// Block - Block
Comparators.registerComparator(Block.class, Block.class, new Comparator<Block, Block>() {
@Override
public Relation compare(Block b1, Block b2) {
return Relation.get(BlockUtils.extractBlock(b1).equals(BlockUtils.extractBlock(b2)));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// Entity - EntityData
Comparators.registerComparator(Entity.class, EntityData.class, new Comparator<Entity, EntityData>() {
@Override
public Relation compare(Entity e, EntityData t) {
return Relation.get(t.isInstance(e));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// EntityData - EntityData
Comparators.registerComparator(EntityData.class, EntityData.class, new Comparator<EntityData, EntityData>() {
@Override
public Relation compare(EntityData t1, EntityData t2) {
return Relation.get(t2.isSupertypeOf(t1));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
}
// EntityData - ItemType
public final static Comparator<EntityData, ItemType> entityItemComparator = new Comparator<EntityData, ItemType>() {
@Override
public Relation compare(EntityData e, ItemType i) {
// TODO fix broken comparisions - will probably require updating potion API of Skript
if (e instanceof Item)
return Relation.get(i.isOfType(((Item) e).getItemStack()));
// if (Skript.classExists("org.bukkit.entity.WitherSkull") && e instanceof WitherSkull)
// return Relation.get(i.isOfType(Material.SKULL_ITEM.getId(), (short) 1));
if (e instanceof BoatData)
return Relation.get(((BoatData)e).isOfItemType(i));
if (e instanceof BoatChestData)
return Relation.get(((BoatChestData) e).isOfItemType(i));
if (e instanceof RabbitData)
return Relation.get(i.isOfType(Material.RABBIT));
for (ItemData data : i.getTypes()) {
assert data != null;
EntityData<?> entity = Aliases.getRelatedEntity(data);
if (entity != null && entity.getType().isAssignableFrom(e.getType()))
return Relation.EQUAL;
}
return Relation.NOT_EQUAL;
}
@Override
public boolean supportsOrdering() {
return false;
}
};
static {
Comparators.registerComparator(EntityData.class, ItemType.class, entityItemComparator);
// Entity - ItemType
// This skips (entity -> entitydata) == itemtype
// It was not working reliably, because there is a converter chain
// entity -> player -> inventoryholder -> block that sometimes takes a priority
Comparators.registerComparator(Entity.class, ItemType.class, new Comparator<Entity, ItemType>() {
@Override
public Relation compare(Entity entity, ItemType item) {
return entityItemComparator.compare(EntityData.fromEntity(entity), item);
}
@Override
public boolean supportsOrdering() {
return false;
}
});
}
static {
// CommandSender - CommandSender
Comparators.registerComparator(CommandSender.class, CommandSender.class, new Comparator<CommandSender, CommandSender>() {
@Override
public Relation compare(CommandSender s1, CommandSender s2) {
return Relation.get(s1.equals(s2));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// OfflinePlayer - OfflinePlayer
Comparators.registerComparator(OfflinePlayer.class, OfflinePlayer.class, new Comparator<OfflinePlayer, OfflinePlayer>() {
@Override
public Relation compare(OfflinePlayer p1, OfflinePlayer p2) {
return Relation.get(Objects.equals(p1.getUniqueId(), p2.getUniqueId()));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// OfflinePlayer - String
Comparators.registerComparator(OfflinePlayer.class, String.class, new Comparator<OfflinePlayer, String>() {
@Override
public Relation compare(OfflinePlayer player, String name) {
if (Utils.isValidUUID(name)) {
UUID uuid = UUID.fromString(name);
return Relation.get(player.getUniqueId().equals(uuid));
}
String playerName = player.getName();
return playerName == null ? Relation.NOT_EQUAL : Relation.get(playerName.equalsIgnoreCase(name));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// OfflinePlayer - UUID
Comparators.registerComparator(OfflinePlayer.class, UUID.class, (player, uuid) -> Relation.get(player.getUniqueId().equals(uuid)));
// World - String
Comparators.registerComparator(World.class, String.class, new Comparator<World, String>() {
@Override
public Relation compare(World w, String name) {
return Relation.get(w.getName().equalsIgnoreCase(name));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// String - String
Comparators.registerComparator(String.class, String.class, new Comparator<String, String>() {
@Override
public Relation compare(String s1, String s2) {
return Relation.get(StringUtils.equals(s1, s2, SkriptConfig.caseSensitive.value()));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// Date - Date
Comparators.registerComparator(Date.class, Date.class, new Comparator<Date, Date>() {
@Override
public Relation compare(Date d1, Date d2) {
return Relation.get(d1.compareTo(d2));
}
@Override
public boolean supportsOrdering() {
return true;
}
});
// Time - Time
Comparators.registerComparator(Time.class, Time.class, new Comparator<Time, Time>() {
@Override
public Relation compare(Time t1, Time t2) {
return Relation.get(t1.getTime() - t2.getTime());
}
@Override
public boolean supportsOrdering() {
return true;
}
});
// Timespan - Timespan
Comparators.registerComparator(Timespan.class, Timespan.class, new Comparator<Timespan, Timespan>() {
@Override
public Relation compare(Timespan t1, Timespan t2) {
return Relation.get(t1.getAs(Timespan.TimePeriod.MILLISECOND) - t2.getAs(Timespan.TimePeriod.MILLISECOND));
}
@Override
public boolean supportsOrdering() {
return true;
}
});
// Time - Timeperiod
Comparators.registerComparator(Time.class, Timeperiod.class, new Comparator<Time, Timeperiod>() {
@Override
public Relation compare(Time t, Timeperiod p) {
return Relation.get(p.contains(t));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// Object - ClassInfo
Comparators.registerComparator(Object.class, ClassInfo.class, new Comparator<Object, ClassInfo>() {
@Override
public Relation compare(Object o, ClassInfo c) {
return Relation.get(c.getC().isInstance(o) || o instanceof ClassInfo && c.getC().isAssignableFrom(((ClassInfo<?>) o).getC()));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// DamageCause - ItemType
Comparators.registerComparator(DamageCause.class, ItemType.class, new Comparator<DamageCause, ItemType>() {
@Override
public Relation compare(DamageCause dc, ItemType t) {
switch (dc) {
case FIRE:
return Relation.get(t.isOfType(Material.FIRE));
case LAVA:
return Relation.get(t.getMaterial() == Material.LAVA);
case MAGIC:
return Relation.get(t.isOfType(Material.POTION));
case HOT_FLOOR:
return Relation.get(t.isOfType(Material.MAGMA_BLOCK));
}
return Relation.NOT_EQUAL;
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// DamageCause - EntityData
Comparators.registerComparator(DamageCause.class, EntityData.class, new Comparator<DamageCause, EntityData>() {
@Override
public Relation compare(DamageCause dc, EntityData e) {
switch (dc) {
case ENTITY_ATTACK:
return Relation.get(EntityData.fromClass(Entity.class).isSupertypeOf(e));
case PROJECTILE:
return Relation.get(EntityData.fromClass(Projectile.class).isSupertypeOf(e));
case WITHER:
return Relation.get(EntityData.fromClass(Wither.class).isSupertypeOf(e));
case FALLING_BLOCK:
return Relation.get(EntityData.fromClass(FallingBlock.class).isSupertypeOf(e));
//$CASES-OMITTED$
default:
return Relation.NOT_EQUAL;
}
}
@Override
public boolean supportsOrdering() {
return false;
}
});
Comparators.registerComparator(GameruleValue.class, GameruleValue.class, new Comparator<GameruleValue, GameruleValue>() {
@Override
public Relation compare(GameruleValue o1, GameruleValue o2) {
return Relation.get(o1.equals(o2));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
Comparators.registerComparator(GameruleValue.class, Number.class, new Comparator<GameruleValue, Number>() {
@Override
public Relation compare(GameruleValue o1, Number o2) {
if (!(o1.getGameruleValue() instanceof Number)) return Relation.NOT_EQUAL;
Number gameruleValue = (Number) o1.getGameruleValue();
return Comparators.compare(gameruleValue, o2);
}
@Override
public boolean supportsOrdering() {
return true;
}
});
Comparators.registerComparator(GameruleValue.class, Boolean.class, new Comparator<GameruleValue, Boolean>() {
@Override
public Relation compare(GameruleValue o1, Boolean o2) {
if (!(o1.getGameruleValue() instanceof Boolean)) return Relation.NOT_EQUAL;
return Relation.get(o2.equals(o1.getGameruleValue()));
}
@Override
public boolean supportsOrdering() {
return false;
}
});
Comparators.registerComparator(Inventory.class, InventoryType.class, new Comparator<Inventory, InventoryType>() {
@Override
public Relation compare(Inventory inventory, InventoryType inventoryType) {
return Relation.get(inventory.getType() == inventoryType);
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// World - WeatherType
Comparators.registerComparator(World.class, WeatherType.class, new Comparator<World, WeatherType>() {
@Override
public Relation compare(World world, WeatherType weatherType) {
return Relation.get(WeatherType.fromWorld(world) == weatherType);
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// Location - Location
Comparators.registerComparator(Location.class, Location.class, new Comparator<Location, Location>() {
@Override
public Relation compare(Location first, Location second) {
return Relation.get(
// compare worlds
Objects.equals(first.getWorld(), second.getWorld()) &&
// compare xyz coords
first.toVector().equals(second.toVector()) &&
// normalize yaw and pitch to [-180, 180) and [-90, 90] respectively
// before comparing them
Location.normalizeYaw(first.getYaw()) == Location.normalizeYaw(second.getYaw()) &&
Location.normalizePitch(first.getPitch()) == Location.normalizePitch(second.getPitch())
);
}
@Override
public boolean supportsOrdering() {
return false;
}
});
// Color - Color
Comparators.registerComparator(Color.class, Color.class, (one, two) -> Relation.get(one.asBukkitColor().equals(two.asBukkitColor())));
Comparators.registerComparator(Color.class, org.bukkit.Color.class, (one, two) -> Relation.get(one.asBukkitColor().equals(two)));
Comparators.registerComparator(org.bukkit.Color.class, org.bukkit.Color.class, (one, two) -> Relation.get(one.equals(two)));
if (Skript.classExists("org.bukkit.entity.EntitySnapshot")) {
boolean SNAPSHOT_AS_STRING_EXISTS = Skript.methodExists(EntitySnapshot.class, "getAsString");
Comparators.registerComparator(EntitySnapshot.class, EntitySnapshot.class, new Comparator<EntitySnapshot, EntitySnapshot>() {
@Override
public Relation compare(EntitySnapshot snap1, EntitySnapshot snap2) {
if (!snap1.getEntityType().equals(snap1.getEntityType()))
return Relation.NOT_EQUAL;
boolean isEqual;
if (!SNAPSHOT_AS_STRING_EXISTS)
isEqual = snap1.equals(snap2) || snap1.hashCode() == snap2.hashCode();
else
isEqual = snap1.getAsString().equalsIgnoreCase(snap2.getAsString());
return Relation.get(isEqual);
}
});
}
// UUID
Comparators.registerComparator(UUID.class, UUID.class, (one, two) -> Relation.get(one.equals(two)));
Comparators.registerComparator(UUID.class, String.class, (one, two) -> Relation.get(one.toString().equals(two)));
}
}