-
-
Notifications
You must be signed in to change notification settings - Fork 362
/
BukkitClasses.java
1553 lines (1389 loc) · 55.9 KB
/
BukkitClasses.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
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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.classes.data;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Difficulty;
import org.bukkit.FireworkEffect;
import org.bukkit.GameMode;
import org.bukkit.GameRule;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.SoundCategory;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.DoubleChest;
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.Cat;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Panda.Gene;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason;
import org.bukkit.event.entity.EntityTransformEvent.TransformReason;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerQuitEvent.QuitReason;
import org.bukkit.event.player.PlayerResourcePackStatusEvent.Status;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.inventory.BlockInventoryHolder;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.Metadatable;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.CachedServerIcon;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.Nullable;
import ch.njol.skript.Skript;
import ch.njol.skript.SkriptConfig;
import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.bukkitutil.EnchantmentUtils;
import ch.njol.skript.bukkitutil.ItemUtils;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.ConfigurationSerializer;
import ch.njol.skript.classes.EnumClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.entity.EntityData;
import ch.njol.skript.expressions.ExprDamageCause;
import ch.njol.skript.expressions.base.EventValueExpression;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.skript.localization.Language;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.util.BlockUtils;
import ch.njol.skript.util.EnchantmentType;
import ch.njol.skript.util.PotionEffectUtils;
import ch.njol.skript.util.StringMode;
import ch.njol.util.StringUtils;
import ch.njol.yggdrasil.Fields;
import io.papermc.paper.world.MoonPhase;
/**
* @author Peter Güttinger
*/
public class BukkitClasses {
public BukkitClasses() {}
public static final Pattern UUID_PATTERN = Pattern.compile("(?i)[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}");
static {
final boolean GET_ENTITY_METHOD_EXISTS = Skript.methodExists(Bukkit.class, "getEntity", UUID.class);
Classes.registerClass(new ClassInfo<>(Entity.class, "entity")
.user("entit(y|ies)")
.name("Entity")
.description("An entity is something in a <a href='#world'>world</a> that's not a <a href='#block'>block</a>, " +
"e.g. a <a href='#player'>player</a>, a skeleton, or a zombie, but also " +
"<a href='#projectile'>projectiles</a> like arrows, fireballs or thrown potions, " +
"or special entities like dropped items, falling blocks or paintings.")
.usage("player, op, wolf, tamed ocelot, powered creeper, zombie, unsaddled pig, fireball, arrow, dropped item, item frame, etc.")
.examples("entity is a zombie or creeper",
"player is an op",
"projectile is an arrow",
"shoot a fireball from the player")
.since("1.0")
.defaultExpression(new EventValueExpression<>(Entity.class))
.parser(new Parser<Entity>() {
@Override
@Nullable
public Entity parse(final String s, final ParseContext context) {
UUID uuid;
try {
uuid = UUID.fromString(s);
} catch (IllegalArgumentException iae) {
return null;
}
if (GET_ENTITY_METHOD_EXISTS) {
return Bukkit.getEntity(uuid);
} else {
for (World world : Bukkit.getWorlds()) {
for (Entity entity : world.getEntities()) {
if (entity.getUniqueId().equals(uuid)) {
return entity;
}
}
}
}
return null;
}
@Override
public boolean canParse(final ParseContext context) {
return context == ParseContext.COMMAND || context == ParseContext.PARSE;
}
@Override
public String toVariableNameString(final Entity e) {
return "entity:" + e.getUniqueId().toString().toLowerCase(Locale.ENGLISH);
}
@Override
public String toString(final Entity e, final int flags) {
return EntityData.toString(e, flags);
}
})
.changer(DefaultChangers.entityChanger));
Classes.registerClass(new ClassInfo<>(LivingEntity.class, "livingentity")
.user("living ?entit(y|ies)")
.name("Living Entity")
.description("A living <a href='#entity'>entity</a>, i.e. a mob or <a href='#player'>player</a>, " +
"not inanimate entities like <a href='#projectile'>projectiles</a> or dropped items.")
.usage("see <a href='#entity'>entity</a>, but ignore inanimate objects")
.examples("spawn 5 powered creepers",
"shoot a zombie from the creeper")
.since("1.0")
.defaultExpression(new EventValueExpression<>(LivingEntity.class))
.changer(DefaultChangers.entityChanger));
Classes.registerClass(new ClassInfo<>(Projectile.class, "projectile")
.user("projectiles?")
.name("Projectile")
.description("A projectile, e.g. an arrow, snowball or thrown potion.")
.usage("arrow, fireball, snowball, thrown potion, etc.")
.examples("projectile is a snowball",
"shoot an arrow at speed 5 from the player")
.since("1.0")
.defaultExpression(new EventValueExpression<>(Projectile.class))
.changer(DefaultChangers.nonLivingEntityChanger));
Classes.registerClass(new ClassInfo<>(Block.class, "block")
.user("blocks?")
.name("Block")
.description("A block in a <a href='#world'>world</a>. It has a <a href='#location'>location</a> and a <a href='#itemstack'>type</a>, " +
"and can also have a <a href='#direction'>direction</a> (mostly a <a href='expressions.html#ExprFacing'>facing</a>), an <a href='#inventory'>inventory</a>, or other special properties.")
.usage("")
.examples("")
.since("1.0")
.defaultExpression(new EventValueExpression<>(Block.class))
.parser(new Parser<Block>() {
@Override
@Nullable
public Block parse(final String s, final ParseContext context) {
return null;
}
@Override
public boolean canParse(final ParseContext context) {
return false;
}
@Override
public String toString(final Block b, final int flags) {
return BlockUtils.blockToString(b, flags);
}
@Override
public String toVariableNameString(final Block b) {
return b.getWorld().getName() + ":" + b.getX() + "," + b.getY() + "," + b.getZ();
}
@Override
public String getDebugMessage(final Block b) {
return toString(b, 0) + " block (" + b.getWorld().getName() + ":" + b.getX() + "," + b.getY() + "," + b.getZ() + ")";
}
})
.changer(DefaultChangers.blockChanger)
.serializer(new Serializer<Block>() {
@Override
public Fields serialize(final Block b) {
final Fields f = new Fields();
f.putObject("world", b.getWorld());
f.putPrimitive("x", b.getX());
f.putPrimitive("y", b.getY());
f.putPrimitive("z", b.getZ());
return f;
}
@Override
public void deserialize(final Block o, final Fields f) {
assert false;
}
@Override
protected Block deserialize(final Fields fields) throws StreamCorruptedException {
final World w = fields.getObject("world", World.class);
final int x = fields.getPrimitive("x", int.class), y = fields.getPrimitive("y", int.class), z = fields.getPrimitive("z", int.class);
if (w == null)
throw new StreamCorruptedException();
return w.getBlockAt(x, y, z);
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
@Override
public boolean canBeInstantiated() {
return false;
}
// return b.getWorld().getName() + ":" + b.getX() + "," + b.getY() + "," + b.getZ();
@Override
@Nullable
public Block deserialize(final String s) {
final String[] split = s.split("[:,]");
if (split.length != 4)
return null;
final World w = Bukkit.getWorld(split[0]);
if (w == null)
return null;
try {
final int[] l = new int[3];
for (int i = 0; i < 3; i++)
l[i] = Integer.parseInt(split[i + 1]);
return w.getBlockAt(l[0], l[1], l[2]);
} catch (final NumberFormatException e) {
return null;
}
}
}));
Classes.registerClass(new ClassInfo<>(BlockData.class, "blockdata")
.user("block ?datas?")
.name("Block Data")
.description("Block data is the detailed information about a block, referred to in Minecraft as BlockStates, " +
"allowing for the manipulation of different aspects of the block, including shape, waterlogging, direction the block is facing, " +
"and so much more. Information regarding each block's optional data can be found on Minecraft's Wiki. Find the block you're " +
"looking for and scroll down to 'Block States'. Different states must be separated by a semicolon (see examples). " +
"The 'minecraft:' namespace is optional, as well as are underscores.")
.examples("set block at player to campfire[lit=false]",
"set target block of player to oak stairs[facing=north;waterlogged=true]",
"set block at player to grass_block[snowy=true]",
"set loop-block to minecraft:chest[facing=north]",
"set block above player to oak_log[axis=y]",
"set target block of player to minecraft:oak_leaves[distance=2;persistent=false]")
.after("itemtype")
.since("2.5")
.parser(new Parser<BlockData>() {
@Nullable
@Override
public BlockData parse(String input, ParseContext context) {
return BlockUtils.createBlockData(input);
}
@Override
public String toString(BlockData o, int flags) {
return o.getAsString().replace(",", ";");
}
@Override
public String toVariableNameString(BlockData o) {
return "blockdata:" + o.getAsString();
}
})
.serializer(new Serializer<BlockData>() {
@Override
public Fields serialize(BlockData o) {
Fields f = new Fields();
f.putObject("blockdata", o.getAsString());
return f;
}
@Override
public void deserialize(BlockData o, Fields f) {
assert false;
}
@Override
protected BlockData deserialize(Fields f) throws StreamCorruptedException {
String data = f.getObject("blockdata", String.class);
assert data != null;
try {
return Bukkit.createBlockData(data);
} catch (IllegalArgumentException ex) {
throw new StreamCorruptedException("Invalid block data: " + data);
}
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
}));
Classes.registerClass(new ClassInfo<>(Location.class, "location")
.user("locations?")
.name("Location")
.description("A location in a <a href='#world'>world</a>. Locations are world-specific and even store a <a href='#direction'>direction</a>, " +
"e.g. if you save a location and later teleport to it you will face the exact same direction you did when you saved the location.")
.usage("")
.examples("")
.since("1.0")
.defaultExpression(new EventValueExpression<>(Location.class))
.parser(new Parser<Location>() {
@Override
@Nullable
public Location parse(final String s, final ParseContext context) {
return null;
}
@Override
public boolean canParse(final ParseContext context) {
return false;
}
@Override
public String toString(final Location l, final int flags) {
String worldPart = l.getWorld() == null ? "" : " in '" + l.getWorld().getName() + "'"; // Safety: getWorld is marked as Nullable by spigot
return "x: " + Skript.toString(l.getX()) + ", y: " + Skript.toString(l.getY()) + ", z: " + Skript.toString(l.getZ()) + ", yaw: " + Skript.toString(l.getYaw()) + ", pitch: " + Skript.toString(l.getPitch()) + worldPart;
}
@Override
public String toVariableNameString(final Location l) {
return l.getWorld().getName() + ":" + l.getX() + "," + l.getY() + "," + l.getZ();
}
@Override
public String getDebugMessage(final Location l) {
return "(" + l.getWorld().getName() + ":" + l.getX() + "," + l.getY() + "," + l.getZ() + "|yaw=" + l.getYaw() + "/pitch=" + l.getPitch() + ")";
}
}).serializer(new Serializer<Location>() {
@Override
public Fields serialize(Location location) {
Fields fields = new Fields();
World world = null;
try {
world = location.getWorld();
} catch (IllegalArgumentException exception) {
Skript.warning("A location failed to serialize with its defined world, as the world was unloaded.");
}
fields.putObject("world", world);
fields.putPrimitive("x", location.getX());
fields.putPrimitive("y", location.getY());
fields.putPrimitive("z", location.getZ());
fields.putPrimitive("yaw", location.getYaw());
fields.putPrimitive("pitch", location.getPitch());
return fields;
}
@Override
public void deserialize(final Location o, final Fields f) {
assert false;
}
@Override
public Location deserialize(final Fields f) throws StreamCorruptedException {
return new Location(f.getObject("world", World.class),
f.getPrimitive("x", double.class), f.getPrimitive("y", double.class), f.getPrimitive("z", double.class),
f.getPrimitive("yaw", float.class), f.getPrimitive("pitch", float.class));
}
@Override
public boolean canBeInstantiated() {
return false; // no nullary constructor - also, saving the location manually prevents errors should Location ever be changed
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
// return l.getWorld().getName() + ":" + l.getX() + "," + l.getY() + "," + l.getZ() + "|" + l.getYaw() + "/" + l.getPitch();
@Override
@Nullable
public Location deserialize(final String s) {
final String[] split = s.split("[:,|/]");
if (split.length != 6)
return null;
final World w = Bukkit.getWorld(split[0]);
if (w == null)
return null;
try {
final double[] l = new double[5];
for (int i = 0; i < 5; i++)
l[i] = Double.parseDouble(split[i + 1]);
return new Location(w, l[0], l[1], l[2], (float) l[3], (float) l[4]);
} catch (final NumberFormatException e) {
return null;
}
}
})
.cloner(Location::clone));
Classes.registerClass(new ClassInfo<>(Vector.class, "vector")
.user("vectors?")
.name("Vector")
.description("Vector is a collection of numbers. In Minecraft, 3D vectors are used to express velocities of entities.")
.usage("vector(x, y, z)")
.examples("")
.since("2.2-dev23")
.defaultExpression(new EventValueExpression<>(Vector.class))
.parser(new Parser<Vector>() {
@Override
@Nullable
public Vector parse(final String s, final ParseContext context) {
return null;
}
@Override
public boolean canParse(final ParseContext context) {
return false;
}
@Override
public String toString(final Vector vec, final int flags) {
return "x: " + Skript.toString(vec.getX()) + ", y: " + Skript.toString(vec.getY()) + ", z: " + Skript.toString(vec.getZ());
}
@Override
public String toVariableNameString(final Vector vec) {
return "vector:" + vec.getX() + "," + vec.getY() + "," + vec.getZ();
}
@Override
public String getDebugMessage(final Vector vec) {
return "(" + vec.getX() + "," + vec.getY() + "," + vec.getZ() + ")";
}
})
.serializer(new Serializer<Vector>() {
@Override
public Fields serialize(Vector o) {
Fields f = new Fields();
f.putPrimitive("x", o.getX());
f.putPrimitive("y", o.getY());
f.putPrimitive("z", o.getZ());
return f;
}
@Override
public void deserialize(Vector o, Fields f) {
assert false;
}
@Override
public Vector deserialize(final Fields f) throws StreamCorruptedException {
return new Vector(f.getPrimitive("x", double.class), f.getPrimitive("y", double.class), f.getPrimitive("z", double.class));
}
@Override
public boolean mustSyncDeserialization() {
return false;
}
@Override
protected boolean canBeInstantiated() {
return false;
}
})
.cloner(Vector::clone));
Classes.registerClass(new ClassInfo<>(World.class, "world")
.user("worlds?")
.name("World")
.description("One of the server's worlds. Worlds can be put into scripts by surrounding their name with double quotes, e.g. \"world_nether\", " +
"but this might not work reliably as <a href='#string'>text</a> uses the same syntax.")
.usage("<code>\"world_name\"</code>, e.g. \"world\"")
.examples("broadcast \"Hello!\" to the world \"world_nether\"")
.since("1.0, 2.2 (alternate syntax)")
.after("string")
.defaultExpression(new EventValueExpression<>(World.class))
.parser(new Parser<World>() {
@SuppressWarnings("null")
private final Pattern parsePattern = Pattern.compile("(?:(?:the )?world )?\"(.+)\"", Pattern.CASE_INSENSITIVE);
@Override
@Nullable
public World parse(final String s, final ParseContext context) {
// REMIND allow shortcuts '[over]world', 'nether' and '[the_]end' (server.properties: 'level-name=world') // inconsistent with 'world is "..."'
if (context == ParseContext.COMMAND || context == ParseContext.PARSE || context == ParseContext.CONFIG)
return Bukkit.getWorld(s);
final Matcher m = parsePattern.matcher(s);
if (m.matches())
return Bukkit.getWorld(m.group(1));
return null;
}
@Override
public String toString(final World w, final int flags) {
return "" + w.getName();
}
@Override
public String toVariableNameString(final World w) {
return "" + w.getName();
}
}).serializer(new Serializer<World>() {
@Override
public Fields serialize(final World w) {
final Fields f = new Fields();
f.putObject("name", w.getName());
return f;
}
@Override
public void deserialize(final World o, final Fields f) {
assert false;
}
@Override
public boolean canBeInstantiated() {
return false;
}
@Override
protected World deserialize(final Fields fields) throws StreamCorruptedException {
final String name = fields.getObject("name", String.class);
assert name != null;
final World w = Bukkit.getWorld(name);
if (w == null)
throw new StreamCorruptedException("Missing world " + name);
return w;
}
// return w.getName();
@Override
@Nullable
public World deserialize(final String s) {
return Bukkit.getWorld(s);
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
Classes.registerClass(new ClassInfo<>(Inventory.class, "inventory")
.user("inventor(y|ies)")
.name("Inventory")
.description("An inventory of a <a href='#player'>player</a> or <a href='#block'>block</a>. " +
"Inventories have many effects and conditions regarding the items contained.",
"An inventory has a fixed amount of <a href='#slot'>slots</a> which represent a specific place in the inventory, " +
"e.g. the <a href='expressions.html#ExprArmorSlot'>helmet slot</a> for players " +
"(Please note that slot support is still very limited but will be improved eventually).")
.usage("")
.examples("")
.since("1.0")
.defaultExpression(new EventValueExpression<>(Inventory.class))
.parser(new Parser<Inventory>() {
@Override
@Nullable
public Inventory parse(final String s, final ParseContext context) {
return null;
}
@Override
public boolean canParse(final ParseContext context) {
return false;
}
@Override
public String toString(final Inventory i, final int flags) {
return "inventory of " + Classes.toString(i.getHolder());
}
@Override
public String getDebugMessage(final Inventory i) {
return "inventory of " + Classes.getDebugMessage(i.getHolder());
}
@Override
public String toVariableNameString(final Inventory i) {
return "inventory of " + Classes.toString(i.getHolder(), StringMode.VARIABLE_NAME);
}
}).changer(DefaultChangers.inventoryChanger));
Classes.registerClass(new EnumClassInfo<>(InventoryAction.class, "inventoryaction", "inventory actions")
.user("inventory ?actions?")
.name("Inventory Action")
.description("What player just did in inventory event. Note that when in creative game mode, most actions do not work correctly.")
.examples("")
.since("2.2-dev16"));
Classes.registerClass(new EnumClassInfo<>(ClickType.class, "clicktype", "click types")
.user("click ?types?")
.name("Click Type")
.description("Click type, mostly for inventory events. Tells exactly which keys/buttons player pressed, " +
"assuming that default keybindings are used in client side.")
.examples("")
.since("2.2-dev16b, 2.2-dev35 (renamed to click type)"));
Classes.registerClass(new EnumClassInfo<>(InventoryType.class, "inventorytype", "inventory types")
.user("inventory ?types?")
.name("Inventory Type")
.description("Minecraft has several different inventory types with their own use cases.")
.examples("")
.since("2.2-dev32"));
Classes.registerClass(new ClassInfo<>(Player.class, "player")
.user("players?")
.name("Player")
.description(
"A player. Depending on whether a player is online or offline several actions can be performed with them, " +
"though you won't get any errors when using effects that only work if the player is online (e.g. changing their inventory) on an offline player.",
"You have two possibilities to use players as command arguments: <player> and <offline player>. " +
"The first requires that the player is online and also accepts only part of the name, " +
"while the latter doesn't require that the player is online, but the player's name has to be entered exactly."
).usage(
"Parsing an offline player as a player (online) will return nothing (none), for that case you would need to parse as " +
"offlineplayer which only returns nothing (none) if player doesn't exist in Minecraft databases (name not taken) otherwise it will return the player regardless of their online status."
).examples(
"set {_p} to \"Notch\" parsed as a player # returns <none> unless Notch is actually online or starts with Notch like Notchan",
"set {_p} to \"N\" parsed as a player # returns Notch if Notch is online because their name starts with 'N' (case insensitive) however, it would return nothing if no player whose name starts with 'N' is online."
).since("1.0")
.defaultExpression(new EventValueExpression<>(Player.class))
.after("string", "world")
.parser(new Parser<Player>() {
@Override
@Nullable
public Player parse(String string, ParseContext context) {
if (context == ParseContext.COMMAND || context == ParseContext.PARSE) {
if (string.isEmpty())
return null;
if (UUID_PATTERN.matcher(string).matches())
return Bukkit.getPlayer(UUID.fromString(string));
String name = string.toLowerCase(Locale.ENGLISH);
int nameLength = name.length(); // caching
List<Player> players = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.getName().toLowerCase(Locale.ENGLISH).startsWith(name)) {
if (player.getName().length() == nameLength) // a little better in performance than String#equals()
return player;
players.add(player);
}
}
if (players.size() == 1)
return players.get(0);
if (players.size() == 0)
Skript.error(String.format(Language.get("commands.no player starts with"), string));
else
Skript.error(String.format(Language.get("commands.multiple players start with"), string));
return null;
}
assert false;
return null;
}
@Override
public boolean canParse(final ParseContext context) {
return context == ParseContext.COMMAND || context == ParseContext.PARSE;
}
@Override
public String toString(final Player p, final int flags) {
return "" + p.getName();
}
@Override
public String toVariableNameString(final Player p) {
if (SkriptConfig.usePlayerUUIDsInVariableNames.value())
return "" + p.getUniqueId();
else
return "" + p.getName();
}
@Override
public String getDebugMessage(final Player p) {
return p.getName() + " " + Classes.getDebugMessage(p.getLocation());
}
})
.changer(DefaultChangers.playerChanger)
.serializeAs(OfflinePlayer.class));
Classes.registerClass(new ClassInfo<>(OfflinePlayer.class, "offlineplayer")
.user("offline ?players?")
.name("Offline Player")
.description(
"A player that is possibly offline. See <a href='#player'>player</a> for more information. " +
"Please note that while all effects and conditions that require a player can be used with an " +
"offline player as well, they will not work if the player is not actually online."
).usage(
"Parsing an offline player as a player (online) will return nothing (none), for that case you would need to parse as " +
"offlineplayer which only returns nothing (none) if player doesn't exist in Minecraft databases (name not taken) otherwise it will return the player regardless of their online status."
).examples("set {_p} to \"Notch\" parsed as an offlineplayer # returns Notch even if they're offline")
.since("2.0 beta 8")
.defaultExpression(new EventValueExpression<>(OfflinePlayer.class))
.after("string", "world")
.parser(new Parser<OfflinePlayer>() {
@Override
@Nullable
public OfflinePlayer parse(final String s, final ParseContext context) {
if (context == ParseContext.COMMAND || context == ParseContext.PARSE) {
if (UUID_PATTERN.matcher(s).matches())
return Bukkit.getOfflinePlayer(UUID.fromString(s));
else if (!SkriptConfig.playerNameRegexPattern.value().matcher(s).matches())
return null;
return Bukkit.getOfflinePlayer(s);
}
assert false;
return null;
}
@Override
public boolean canParse(ParseContext context) {
return context == ParseContext.COMMAND || context == ParseContext.PARSE;
}
@Override
public String toString(OfflinePlayer p, int flags) {
return p.getName() == null ? p.getUniqueId().toString() : p.getName();
}
@Override
public String toVariableNameString(OfflinePlayer p) {
if (SkriptConfig.usePlayerUUIDsInVariableNames.value() || p.getName() == null)
return "" + p.getUniqueId();
else
return "" + p.getName();
}
@Override
public String getDebugMessage(OfflinePlayer p) {
if (p.isOnline())
return Classes.getDebugMessage(p.getPlayer());
return toString(p, 0);
}
}).serializer(new Serializer<OfflinePlayer>() {
@Override
public Fields serialize(final OfflinePlayer p) {
final Fields f = new Fields();
f.putObject("uuid", p.getUniqueId());
return f;
}
@Override
public void deserialize(final OfflinePlayer o, final Fields f) {
assert false;
}
@Override
public boolean canBeInstantiated() {
return false;
}
@SuppressWarnings("deprecation")
@Override
protected OfflinePlayer deserialize(final Fields fields) throws StreamCorruptedException {
if (fields.contains("uuid")) {
final UUID uuid = fields.getObject("uuid", UUID.class);
if (uuid == null)
throw new StreamCorruptedException();
return Bukkit.getOfflinePlayer(uuid);
} else {
final String name = fields.getObject("name", String.class);
if (name == null)
throw new StreamCorruptedException();
return Bukkit.getOfflinePlayer(name);
}
}
// return p.getName();
@SuppressWarnings("deprecation")
@Override
@Nullable
public OfflinePlayer deserialize(final String s) {
return Bukkit.getOfflinePlayer(s);
}
@Override
public boolean mustSyncDeserialization() {
return true;
}
}));
Classes.registerClass(new ClassInfo<>(CommandSender.class, "commandsender")
.user("((commands?)? ?)?(sender|executor)s?")
.name("Command Sender")
.description("A player or the console.")
.usage("use <a href='expressions.html#LitConsole'>the console</a> for the console",
"see <a href='#player'>player</a> for players.")
.examples("command /push [<player>]:",
"\ttrigger:",
"\t\tif arg-1 is not set:",
"\t\t\tif command sender is console:",
"\t\t\t\tsend \"You can't push yourself as a console :\\\" to sender",
"\t\t\t\tstop",
"\t\t\tpush sender upwards with force 2",
"\t\t\tsend \"Yay!\"",
"\t\telse:",
"\t\t\tpush arg-1 upwards with force 2",
"\t\t\tsend \"Yay!\" to sender and arg-1")
.since("1.0")
.defaultExpression(new EventValueExpression<>(CommandSender.class))
.parser(new Parser<CommandSender>() {
@Override
@Nullable
public CommandSender parse(final String s, final ParseContext context) {
return null;
}
@Override
public boolean canParse(final ParseContext context) {
return false;
}
@Override
public String toString(final CommandSender s, final int flags) {
return "" + s.getName();
}
@Override
public String toVariableNameString(final CommandSender s) {
return "" + s.getName();
}
}));
Classes.registerClass(new ClassInfo<>(InventoryHolder.class, "inventoryholder")
.name(ClassInfo.NO_DOC)
.defaultExpression(new EventValueExpression<>(InventoryHolder.class))
.after("entity", "block")
.parser(new Parser<InventoryHolder>() {
@Override
public boolean canParse(ParseContext context) {
return false;
}
@Override
public String toString(InventoryHolder holder, int flags) {
if (holder instanceof BlockState) {
return Classes.toString(((BlockState) holder).getBlock());
} else if (holder instanceof DoubleChest) {
return Classes.toString(holder.getInventory().getLocation().getBlock());
} else if (holder instanceof BlockInventoryHolder) {
return Classes.toString(((BlockInventoryHolder) holder).getBlock());
} else if (Classes.getSuperClassInfo(holder.getClass()).getC() == InventoryHolder.class) {
return holder.getClass().getSimpleName(); // an inventory holder and only that
} else {
return Classes.toString(holder);
}
}
@Override
public String toVariableNameString(InventoryHolder holder) {
return toString(holder, 0);
}
}));
Classes.registerClass(new EnumClassInfo<>(GameMode.class, "gamemode", "game modes", new SimpleLiteral<>(GameMode.SURVIVAL, true))
.user("game ?modes?")
.name("Game Mode")
.description("The game modes survival, creative, adventure and spectator.")
.examples("player's gamemode is survival",
"set the player argument's game mode to creative")
.since("1.0"));
Classes.registerClass(new ClassInfo<>(ItemStack.class, "itemstack")
.user("items?")
.name("Item")
.description("An item, e.g. a stack of torches, a furnace, or a wooden sword of sharpness 2. " +
"Unlike <a href='#itemtype'>item type</a> an item can only represent exactly one item (e.g. an upside-down cobblestone stair facing west), " +
"while an item type can represent a whole range of items (e.g. any cobble stone stairs regardless of direction).",
"You don't usually need this type except when you want to make a command that only accepts an exact item.",
"Please note that currently 'material' is exactly the same as 'item', i.e. can have an amount & enchantments.")
.usage("<code>[<number> [of]] <alias> [of <enchantment> <level>]</code>, Where <alias> must be an alias that represents exactly one item " +
"(i.e cannot be a general alias like 'sword' or 'plant')")
.examples("set {_item} to type of the targeted block",
"{_item} is a torch")
.since("1.0")
.after("number")
.supplier(() -> Arrays.stream(Material.values())
.map(ItemStack::new)
.iterator())
.parser(new Parser<ItemStack>() {
@Override
@Nullable
public ItemStack parse(final String s, final ParseContext context) {
ItemType t = Aliases.parseItemType(s);
if (t == null)
return null;
t = t.getItem();
if (t.numTypes() != 1) {
Skript.error("'" + s + "' represents multiple materials");
return null;
}
final ItemStack i = t.getRandom();
assert i != null;
return i;
}
@Override
public String toString(final ItemStack i, final int flags) {
return ItemType.toString(i, flags);
}
@Override
public String toVariableNameString(final ItemStack i) {
final StringBuilder b = new StringBuilder("item:");
b.append(i.getType().name());
b.append(":" + ItemUtils.getDamage(i));
b.append("*" + i.getAmount());
for (Entry<Enchantment, Integer> entry : i.getEnchantments().entrySet())
b.append("#" + EnchantmentUtils.getKey(entry.getKey()))
.append(":" + entry.getValue());
return b.toString();
}
})
.cloner(ItemStack::clone)
.serializer(new ConfigurationSerializer<>()));
Classes.registerClass(new ClassInfo<>(Item.class, "itementity")
.name(ClassInfo.NO_DOC)
.since("2.0")
.changer(DefaultChangers.itemChanger));
Classes.registerClass(new EnumClassInfo<>(Biome.class, "biome", "biomes")
.user("biomes?")
.name("Biome")
.description("All possible biomes Minecraft uses to generate a world.")
.examples("biome at the player is desert")
.since("1.4.4")
.after("damagecause"));
Classes.registerClass(new ClassInfo<>(PotionEffect.class, "potioneffect")
.user("potion ?effects?")
.name("Potion Effect")
.description("A potion effect, including the potion effect type, tier and duration.")
.usage("speed of tier 1 for 10 seconds")
.since("2.5.2")
.parser(new Parser<PotionEffect>() {
@Override
public boolean canParse(ParseContext context) {
return false;
}