-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathDefaultFunctions.java
More file actions
1036 lines (955 loc) · 44.4 KB
/
DefaultFunctions.java
File metadata and controls
1036 lines (955 loc) · 44.4 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
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
package ch.njol.skript.classes.data;
import ch.njol.skript.Skript;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.KeyedValue;
import ch.njol.skript.lang.function.Functions;
import ch.njol.skript.lang.function.Parameter;
import ch.njol.skript.lang.function.SimpleJavaFunction;
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.registrations.DefaultClasses;
import ch.njol.skript.util.Contract;
import ch.njol.skript.util.Date;
import ch.njol.skript.util.Utils;
import ch.njol.util.Math2;
import ch.njol.util.StringUtils;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.Nullable;
import org.joml.AxisAngle4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.skriptlang.skript.addon.SkriptAddon;
import org.skriptlang.skript.common.function.DefaultFunction;
import org.skriptlang.skript.common.function.Parameter.Modifier;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
public class DefaultFunctions {
private static String str(double n) {
return StringUtils.toString(n, 4);
}
private static final DecimalFormat DEFAULT_INTEGER_FORMAT = new DecimalFormat("###,###");
private static final DecimalFormat DEFAULT_DECIMAL_FORMAT = new DecimalFormat("###,###.##");
static {
SkriptAddon skript = Skript.instance();
Parameter<?>[] numberParam = new Parameter[] {new Parameter<>("n", DefaultClasses.NUMBER, true, null)};
Parameter<?>[] numbersParam = new Parameter[] {new Parameter<>("ns", DefaultClasses.NUMBER, false, null)};
// basic math functions
Functions.register(DefaultFunction.builder(skript, "floor", Long.class)
.description("Rounds a number down, i.e. returns the closest integer smaller than or equal to the argument.")
.examples("floor(2.34) = 2", "floor(2) = 2", "floor(2.99) = 2")
.since("2.2")
.parameter("n", Number.class)
.build(args -> {
Number value = args.get("n");
if (value instanceof Long l)
return l;
return Math2.floor(value.doubleValue());
}));
Functions.registerFunction(new SimpleJavaFunction<Number>("round", new Parameter[] {new Parameter<>("n", DefaultClasses.NUMBER, true, null), new Parameter<>("d", DefaultClasses.NUMBER, true, new SimpleLiteral<Number>(0, false))}, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
if (params[0][0] instanceof Long longValue)
return new Long[] {longValue};
double value = ((Number) params[0][0]).doubleValue();
if (!Double.isFinite(value))
return new Double[] {value};
double placementDouble = ((Number) params[1][0]).doubleValue();
if (!Double.isFinite(placementDouble) || placementDouble >= Integer.MAX_VALUE || placementDouble <= Integer.MIN_VALUE)
return new Double[] {Double.NaN};
int placement = (int) placementDouble;
if (placement == 0)
return new Long[] {Math2.round(value)};
if (placement >= 0) {
BigDecimal decimal = new BigDecimal(Double.toString(value));
decimal = decimal.setScale(placement, RoundingMode.HALF_UP);
return new Double[] {decimal.doubleValue()};
}
long rounded = Math2.round(value);
return new Double[] {(int) Math2.round(rounded * Math.pow(10.0, placement)) / Math.pow(10.0, placement)};
}
}.description("Rounds a number, i.e. returns the closest integer to the argument. Place a second argument to define the decimal placement.")
.examples("round(2.34) = 2", "round(2) = 2", "round(2.99) = 3", "round(2.5) = 3")
.since("2.2, 2.7 (decimal placement)"));
Functions.registerFunction(new SimpleJavaFunction<Long>("ceil", numberParam, DefaultClasses.LONG, true) {
@Override
public Long[] executeSimple(Object[][] params) {
if (params[0][0] instanceof Long)
return new Long[] {(Long) params[0][0]};
return new Long[] {Math2.ceil(((Number) params[0][0]).doubleValue())};
}
}.description("Rounds a number up, i.e. returns the closest integer larger than or equal to the argument.")
.examples("ceil(2.34) = 3", "ceil(2) = 2", "ceil(2.99) = 3")
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Long>("ceiling", numberParam, DefaultClasses.LONG, true) {
@Override
public Long[] executeSimple(Object[][] params) {
if (params[0][0] instanceof Long)
return new Long[] {(Long) params[0][0]};
return new Long[] {Math2.ceil(((Number) params[0][0]).doubleValue())};
}
}.description("Alias of <a href='#ceil'>ceil</a>.")
.examples("ceiling(2.34) = 3", "ceiling(2) = 2", "ceiling(2.99) = 3")
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("abs", numberParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
Number n = (Number) params[0][0];
if (n instanceof Byte || n instanceof Short || n instanceof Integer || n instanceof Long)
return new Long[] {Math.abs(n.longValue())};
return new Double[] {Math.abs(n.doubleValue())};
}
}.description("Returns the absolute value of the argument, i.e. makes the argument positive.")
.examples("abs(3) = 3", "abs(-2) = 2")
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("mod", new Parameter[] {new Parameter<>("d", DefaultClasses.NUMBER, true, null), new Parameter<>("m", DefaultClasses.NUMBER, true, null)}, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
Number d = (Number) params[0][0];
Number m = (Number) params[1][0];
double mm = m.doubleValue();
if (mm == 0)
return new Double[] {Double.NaN};
return new Double[] {Math2.mod(d.doubleValue(), mm)};
}
}.description("Returns the modulo of the given arguments, i.e. the remainder of the division <code>d/m</code>, where d and m are the arguments of this function.",
"The returned value is always positive. Returns NaN (not a number) if the second argument is zero.")
.examples("mod(3, 2) = 1", "mod(256436, 100) = 36", "mod(-1, 10) = 9")
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("exp", numberParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
return new Double[] {Math.exp(((Number) params[0][0]).doubleValue())};
}
}.description("The exponential function. You probably don't need this if you don't know what this is.")
.examples("exp(0) = 1", "exp(1) = " + str(Math.exp(1)))
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("ln", numberParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
return new Double[] {Math.log(((Number) params[0][0]).doubleValue())};
}
}.description("The natural logarithm. You probably don't need this if you don't know what this is.",
"Returns NaN (not a number) if the argument is negative.")
.examples("ln(1) = 0", "ln(exp(5)) = 5", "ln(2) = " + StringUtils.toString(Math.log(2), 4))
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("log", new Parameter[] {new Parameter<>("n", DefaultClasses.NUMBER, true, null), new Parameter<>("base", DefaultClasses.NUMBER, true, new SimpleLiteral<Number>(10, false))}, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
return new Double[] {Math.log10(((Number) params[0][0]).doubleValue()) / Math.log10(((Number) params[1][0]).doubleValue())};
}
}.description("A logarithm, with base 10 if none is specified. This is the inverse operation to exponentiation (for positive bases only), i.e. <code>log(base ^ exponent, base) = exponent</code> for any positive number 'base' and any number 'exponent'.",
"Another useful equation is <code>base ^ log(a, base) = a</code> for any numbers 'base' and 'a'.",
"Please note that due to how numbers are represented in computers, these equations do not hold for all numbers, as the computed values may slightly differ from the correct value.",
"Returns NaN (not a number) if any of the arguments are negative.")
.examples("log(100) = 2 # 10^2 = 100", "log(16, 2) = 4 # 2^4 = 16")
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("sqrt", numberParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
return new Double[] {Math.sqrt(((Number) params[0][0]).doubleValue())};
}
}.description("The square root, which is the inverse operation to squaring a number (for positive numbers only). This is the same as <code>(argument) ^ (1/2)</code> – other roots can be calculated via <code>number ^ (1/root)</code>, e.g. <code>set {_l} to {_volume}^(1/3)</code>.",
"Returns NaN (not a number) if the argument is negative.")
.examples("sqrt(4) = 2", "sqrt(2) = " + str(Math.sqrt(2)), "sqrt(-1) = " + str(Math.sqrt(-1)))
.since("2.2"));
// trigonometry
Functions.registerFunction(new SimpleJavaFunction<Number>("sin", numberParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
return new Double[] {Math.sin(Math.toRadians(((Number) params[0][0]).doubleValue()))};
}
}.description("The sine function. It starts at 0° with a value of 0, goes to 1 at 90°, back to 0 at 180°, to -1 at 270° and then repeats every 360°. Uses degrees, not radians.")
.examples("sin(90) = 1", "sin(60) = " + str(Math.sin(Math.toRadians(60))))
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("cos", numberParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
return new Double[] {Math.cos(Math.toRadians(((Number) params[0][0]).doubleValue()))};
}
}.description("The cosine function. This is basically the <a href='#sin'>sine</a> shifted by 90°, i.e. <code>cos(a) = sin(a + 90°)</code>, for any number a. Uses degrees, not radians.")
.examples("cos(0) = 1", "cos(90) = 0")
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("tan", numberParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
return new Double[] {Math.tan(Math.toRadians(((Number) params[0][0]).doubleValue()))};
}
}.description("The tangent function. This is basically <code><a href='#sin'>sin</a>(arg)/<a href='#cos'>cos</a>(arg)</code>. Uses degrees, not radians.")
.examples("tan(0) = 0", "tan(45) = 1", "tan(89.99) = " + str(Math.tan(Math.toRadians(89.99))))
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("asin", numberParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
return new Double[] {Math.toDegrees(Math.asin(((Number) params[0][0]).doubleValue()))};
}
}.description("The inverse of the <a href='#sin'>sine</a>, also called arcsin. Returns result in degrees, not radians. Only returns values from -90 to 90.")
.examples("asin(0) = 0", "asin(1) = 90", "asin(0.5) = " + str(Math.toDegrees(Math.asin(0.5))))
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("acos", numberParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
return new Double[] {Math.toDegrees(Math.acos(((Number) params[0][0]).doubleValue()))};
}
}.description("The inverse of the <a href='#cos'>cosine</a>, also called arccos. Returns result in degrees, not radians. Only returns values from 0 to 180.")
.examples("acos(0) = 90", "acos(1) = 0", "acos(0.5) = " + str(Math.toDegrees(Math.asin(0.5))))
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("atan", numberParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
return new Double[] {Math.toDegrees(Math.atan(((Number) params[0][0]).doubleValue()))};
}
}.description("The inverse of the <a href='#tan'>tangent</a>, also called arctan. Returns result in degrees, not radians. Only returns values from -90 to 90.")
.examples("atan(0) = 0", "atan(1) = 45", "atan(10000) = " + str(Math.toDegrees(Math.atan(10000))))
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("atan2", new Parameter[] {
new Parameter<>("x", DefaultClasses.NUMBER, true, null),
new Parameter<>("y", DefaultClasses.NUMBER, true, null)
}, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
return new Double[] {Math.toDegrees(Math.atan2(((Number) params[1][0]).doubleValue(), ((Number) params[0][0]).doubleValue()))};
}
}.description("Similar to <a href='#atan'>atan</a>, but requires two coordinates and returns values from -180 to 180.",
"The returned angle is measured counterclockwise in a standard mathematical coordinate system (x to the right, y to the top).")
.examples("atan2(0, 1) = 0", "atan2(10, 0) = 90", "atan2(-10, 5) = " + str(Math.toDegrees(Math.atan2(-10, 5))))
.since("2.2"));
// more stuff
Functions.registerFunction(new SimpleJavaFunction<Number>("sum", numbersParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
Object[] ns = params[0];
double sum = ((Number) ns[0]).doubleValue();
for (int i = 1; i < ns.length; i++)
sum += ((Number) ns[i]).doubleValue();
return new Double[] {sum};
}
}.description("Sums a list of numbers.")
.examples("sum(1) = 1", "sum(2, 3, 4) = 9", "sum({some list variable::*})", "sum(2, {_v::*}, and the player's y-coordinate)")
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("product", numbersParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
Object[] ns = params[0];
double product = ((Number) ns[0]).doubleValue();
for (int i = 1; i < ns.length; i++)
product *= ((Number) ns[i]).doubleValue();
return new Double[] {product};
}
}.description("Calculates the product of a list of numbers.")
.examples("product(1) = 1", "product(2, 3, 4) = 24", "product({some list variable::*})", "product(2, {_v::*}, and the player's y-coordinate)")
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("max", numbersParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
Object[] ns = params[0];
double max = ((Number) ns[0]).doubleValue();
for (int i = 1; i < ns.length; i++) {
double d = ((Number) ns[i]).doubleValue();
if (d > max || Double.isNaN(max))
max = d;
}
return new Double[] {max};
}
}.description("Returns the maximum number from a list of numbers.")
.examples("max(1) = 1", "max(1, 2, 3, 4) = 4", "max({some list variable::*})")
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("min", numbersParam, DefaultClasses.NUMBER, true) {
@Override
public Number[] executeSimple(Object[][] params) {
Object[] ns = params[0];
double min = ((Number) ns[0]).doubleValue();
for (int i = 1; i < ns.length; i++) {
double d = ((Number) ns[i]).doubleValue();
if (d < min || Double.isNaN(min))
min = d;
}
return new Double[] {min};
}
}.description("Returns the minimum number from a list of numbers.")
.examples("min(1) = 1", "min(1, 2, 3, 4) = 1", "min({some list variable::*})")
.since("2.2"));
Functions.registerFunction(new SimpleJavaFunction<Number>("clamp", new Parameter[] {
new Parameter<>("values", DefaultClasses.NUMBER, false, null, true),
new Parameter<>("min", DefaultClasses.NUMBER, true, null),
new Parameter<>("max", DefaultClasses.NUMBER, true, null)
}, DefaultClasses.NUMBER, false, new Contract() {
@Override
public boolean isSingle(Expression<?>... arguments) {
return arguments[0].isSingle();
}
@Override
public Class<?> getReturnType(Expression<?>... arguments) {
return Number.class;
}
}) {
@Override
public @Nullable Number[] executeSimple(Object[][] params) {
//noinspection unchecked
KeyedValue<Number>[] values = (KeyedValue<Number>[]) params[0];
Double[] clampedValues = new Double[values.length];
double min = ((Number) params[1][0]).doubleValue();
double max = ((Number) params[2][0]).doubleValue();
// we'll be nice and swap them if they're in the wrong order
double trueMin = Math.min(min, max);
double trueMax = Math.max(min, max);
for (int i = 0; i < values.length; i++) {
double value = values[i].value().doubleValue();
clampedValues[i] = Math.max(Math.min(value, trueMax), trueMin);
}
setReturnedKeys(KeyedValue.unzip(values).keys().toArray(new String[0]));
return clampedValues;
}
}).description("Clamps one or more values between two numbers.", "This function retains indices")
.examples(
"clamp(5, 0, 10) = 5",
"clamp(5.5, 0, 5) = 5",
"clamp(0.25, 0, 0.5) = 0.25",
"clamp(5, 7, 10) = 7",
"clamp((5, 0, 10, 9, 13), 7, 10) = (7, 7, 10, 9, 10)",
"set {_clamped::*} to clamp({_values::*}, 0, 10)")
.since("2.8.0");
Functions.register(DefaultFunction.builder(skript, "toBase", String[].class)
.description("""
Turns a number into a string using a specific base (decimal, hexadecimal, octal).
For example, converting 32 to hexadecimal (base 16) would be 'toBase(32, 16)', which would return "20".
You can use any base between 2 and 36.
""")
.examples(
"send \"Decode this binary number for a prize! %toBase({_guess}, 2)%\""
)
.since("2.14")
.parameter("n", Long[].class)
.parameter("base", Long.class, Modifier.ranged(2, 36))
.contract(new Contract() {
@Override
public boolean isSingle(Expression<?>... arguments) {
return arguments[0].isSingle();
}
@Override
public Class<?> getReturnType(Expression<?>... arguments) {
return String.class;
}
})
.build(args -> {
Long[] n = args.get("n");
Long base = args.get("base");
String[] results = new String[n.length];
for (int i = 0; i < n.length; i++) {
results[i] = Long.toString(n[i], base.intValue());
}
return results;
}));
Functions.register(DefaultFunction.builder(skript, "fromBase", Long[].class)
.description("""
Turns a text version of a number in a specific base (decimal, hexadecimal, octal) into an actual number.
For example, converting "20" in hexadecimal (base 16) would be 'fromBase("20", 16)', which would return 32.
You can use any base between 2 and 36.
""")
.examples("""
# /binaryText 01110011 01101011 01110010 01101001 01110000 01110100 00100001
# sends "skript!"
command binaryText <text>:
trigger:
set {_characters::*} to argument split at " " without trailing empty string
transform {_characters::*} with fromBase(input, 2) # convert to codepoints
transform {_characters::*} with character from codepoint input # convert to characters
send join {_characters::*}
""")
.since("2.14")
.parameter("string value", String[].class)
.parameter("base", Long.class, Modifier.ranged(2, 36))
.contract(new Contract() {
@Override
public boolean isSingle(Expression<?>... arguments) {
return arguments[0].isSingle();
}
@Override
public Class<?> getReturnType(Expression<?>... arguments) {
return Long.class;
}
})
.build(args -> {
String[] n = args.get("string value");
Long base = args.get("base");
Long[] results = new Long[n.length];
try {
for (int i = 0; i < n.length; i++) {
results[i] = Long.parseLong(n[i], base.intValue());
}
} catch (NumberFormatException e) {
return null;
}
return results;
}));
// misc
Functions.registerFunction(new SimpleJavaFunction<World>("world", new Parameter[] {
new Parameter<>("name", DefaultClasses.STRING, true, null)
}, DefaultClasses.WORLD, true) {
@Override
public World[] executeSimple(Object[][] params) {
World w = Bukkit.getWorld((String) params[0][0]);
return w == null ? new World[0] : new World[] {w};
}
}).description("Gets a world from its name.")
.examples("set {_nether} to world(\"%{_world}%_nether\")")
.since("2.2");
Functions.register(DefaultFunction.builder(skript, "location", Location.class)
.description(
"Creates a location from a world and 3 coordinates, with an optional yaw and pitch.",
"If for whatever reason the world is not found, it will fallback to the server's main world."
)
.examples("""
# TELEPORTING
teleport player to location(1,1,1, world "world")
teleport player to location(1,1,1, world "world", 100, 0)
teleport player to location(1,1,1, world "world", yaw of player, pitch of player)
teleport player to location(1,1,1, world of player)
teleport player to location(1,1,1, world("world"))
teleport player to location({_x}, {_y}, {_z}, {_w}, {_yaw}, {_pitch})
# SETTING BLOCKS
set block at location(1,1,1, world "world") to stone
set block at location(1,1,1, world "world", 100, 0) to stone
set block at location(1,1,1, world of player) to stone
set block at location(1,1,1, world("world")) to stone
set block at location({_x}, {_y}, {_z}, {_w}) to stone
# USING VARIABLES
set {_l1} to location(1,1,1)
set {_l2} to location(10,10,10)
set blocks within {_l1} and {_l2} to stone
if player is within {_l1} and {_l2}:
# OTHER
kill all entities in radius 50 around location(1,65,1, world "world")
delete all entities in radius 25 around location(50,50,50, world "world_nether")
ignite all entities in radius 25 around location(1,1,1, world of player)
"""
)
.since("2.2")
.parameter("x", Number.class)
.parameter("y", Number.class)
.parameter("z", Number.class)
.parameter("world", World.class, Modifier.OPTIONAL)
.parameter("yaw", Float.class, Modifier.OPTIONAL)
.parameter("pitch", Float.class, Modifier.OPTIONAL)
.build(args -> {
World world = args.getOrDefault("world", Bukkit.getWorlds().get(0));
return new Location(world,
args.<Number>get("x").doubleValue(), args.<Number>get("y").doubleValue(), args.<Number>get("z").doubleValue(),
args.getOrDefault("yaw", 0f), args.getOrDefault("pitch", 0f));
}));
Functions.registerFunction(new SimpleJavaFunction<Date>("date", new Parameter[] {
new Parameter<>("year", DefaultClasses.NUMBER, true, null),
new Parameter<>("month", DefaultClasses.NUMBER, true, null),
new Parameter<>("day", DefaultClasses.NUMBER, true, null),
new Parameter<>("hour", DefaultClasses.NUMBER, true, new SimpleLiteral<Number>(0, true)),
new Parameter<>("minute", DefaultClasses.NUMBER, true, new SimpleLiteral<Number>(0, true)),
new Parameter<>("second", DefaultClasses.NUMBER, true, new SimpleLiteral<Number>(0, true)),
new Parameter<>("millisecond", DefaultClasses.NUMBER, true, new SimpleLiteral<Number>(0, true)),
new Parameter<>("zone_offset", DefaultClasses.NUMBER, true, new SimpleLiteral<Number>(Double.NaN, true)),
new Parameter<>("dst_offset", DefaultClasses.NUMBER, true, new SimpleLiteral<Number>(Double.NaN, true))
}, DefaultClasses.DATE, true) {
private final int[] fields = {
Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH,
Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND,
Calendar.ZONE_OFFSET, Calendar.DST_OFFSET
};
private final int[] offsets = {
0, -1, 0,
0, 0, 0, 0,
0, 0
};
private final double[] scale = {
1, 1, 1,
1, 1, 1, 1,
1000 * 60, 1000 * 60
};
private final double[] relations = {
1. / 12, 1. / 30,
1. / 24, 1. / 60, 1. / 60, 1. / 1000,
0, 0,
0
};
{
int length = getSignature().getMaxParameters();
assert fields.length == length
&& offsets.length == length
&& scale.length == length
&& relations.length == length;
}
@Override
public Date[] executeSimple(Object[][] params) {
Calendar c = Calendar.getInstance();
c.setLenient(true);
double carry = 0;
for (int i = 0; i < fields.length; i++) {
int field = fields[i];
Number n = (Number) params[i][0];
double value = n.doubleValue() * scale[i] + offsets[i] + carry;
int v = (int) Math2.floor(value);
carry = (value - v) * relations[i];
//noinspection MagicConstant
c.set(field, v);
}
return new Date[] {new Date(c.getTimeInMillis(), c.getTimeZone())};
}
}.description("Creates a date from a year, month, and day, and optionally also from hour, minute, second and millisecond.",
"A time zone and DST offset can be specified as well (in minutes), if they are left out the server's time zone and DST offset are used (the created date will not retain this information).")
.examples("date(2014, 10, 1) # 0:00, 1st October 2014", "date(1990, 3, 5, 14, 30) # 14:30, 5th May 1990", "date(1999, 12, 31, 23, 59, 59, 999, -3*60, 0) # almost year 2000 in parts of Brazil (-3 hours offset, no DST)")
.since("2.2"));
Functions.register(DefaultFunction.builder(skript, "vector", Vector.class)
.description("Creates a new vector, which can be used with various expressions, effects and functions.")
.examples("vector(0, 0, 0)")
.since("2.2-dev23")
.parameter("x", Number.class)
.parameter("y", Number.class)
.parameter("z", Number.class)
.build(args -> new Vector(
args.<Number>get("x").doubleValue(),
args.<Number>get("y").doubleValue(),
args.<Number>get("z").doubleValue()
)));
Functions.registerFunction(new SimpleJavaFunction<Long>("calcExperience", new Parameter[] {
new Parameter<>("level", DefaultClasses.LONG, true, null)
}, DefaultClasses.LONG, true) {
@Override
public Long[] executeSimple(Object[][] params) {
long level = (long) params[0][0];
long exp;
if (level <= 0) {
exp = 0;
} else if (level >= 1 && level <= 15) {
exp = level * level + 6 * level;
} else if (level >= 16 && level <= 30) { // Truncating decimal parts probably works
exp = (int) (2.5 * level * level - 40.5 * level + 360);
} else { // Half experience points do not exist, anyway
exp = (int) (4.5 * level * level - 162.5 * level + 2220);
}
return new Long[] {exp};
}
}.description("Calculates the total amount of experience needed to achieve given level from scratch in Minecraft.")
.since("2.2-dev32"));
Functions.register(DefaultFunction.builder(skript, "player", Player.class)
.description(
"Returns an online player from their name or UUID, if player is offline function will return nothing.",
"Setting 'getExactPlayer' parameter to true will return the player whose name is exactly equal to the provided name instead of returning a player that their name starts with the provided name."
)
.examples(
"set {_p} to player(\"Notch\") # will return an online player whose name is or starts with 'Notch'",
"set {_p} to player(\"Notch\", true) # will return the only online player whose name is 'Notch'",
"set {_p} to player(\"069a79f4-44e9-4726-a5be-fca90e38aaf5\") # <none> if player is offline"
)
.since("2.8.0")
.parameter("nameOrUUID", String.class)
.parameter("getExactPlayer", Boolean.class, Modifier.OPTIONAL)
.build(args -> {
String name = args.get("nameOrUUID");
boolean isExact = args.getOrDefault("getExactPlayer", false);
UUID uuid = null;
if (name.length() > 16 || name.contains("-")) {
if (Utils.isValidUUID(name))
uuid = UUID.fromString(name);
}
return uuid != null ? Bukkit.getPlayer(uuid) : (isExact ? Bukkit.getPlayerExact(name) : Bukkit.getPlayer(name));
}));
{ // offline player function
// TODO - remove this when Spigot support is dropped
boolean hasIfCached = Skript.methodExists(Bukkit.class, "getOfflinePlayerIfCached", String.class);
List<Parameter<?>> params = new ArrayList<>();
params.add(new Parameter<>("nameOrUUID", DefaultClasses.STRING, true, null));
if (hasIfCached)
params.add(new Parameter<>("allowLookups", DefaultClasses.BOOLEAN, true, new SimpleLiteral<>(true, true)));
Functions.registerFunction(new SimpleJavaFunction<OfflinePlayer>("offlineplayer", params.toArray(new Parameter[0]),
DefaultClasses.OFFLINE_PLAYER, true) {
@Override
public OfflinePlayer[] executeSimple(Object[][] params) {
String name = (String) params[0][0];
UUID uuid = null;
if (name.length() > 16 || name.contains("-")) { // shortcut
if (Utils.isValidUUID(name))
uuid = UUID.fromString(name);
}
OfflinePlayer result;
if (uuid != null) {
result = Bukkit.getOfflinePlayer(uuid); // doesn't do lookups
} else if (hasIfCached && !((Boolean) params[1][0])) {
result = Bukkit.getOfflinePlayerIfCached(name);
if (result == null)
return new OfflinePlayer[0];
} else {
result = Bukkit.getOfflinePlayer(name);
}
return CollectionUtils.array(result);
}
}).description(
"Returns a offline player from their name or UUID. This function will still return the player if they're online. " +
"If Paper 1.16.5+ is used, the 'allowLookup' parameter can be set to false to prevent this function from doing a " +
"web lookup for players who have not joined before. Lookups can cause lag spikes of up to multiple seconds, so " +
"use offline players with caution."
)
.examples(
"set {_p} to offlineplayer(\"Notch\")",
"set {_p} to offlineplayer(\"069a79f4-44e9-4726-a5be-fca90e38aaf5\")",
"set {_p} to offlineplayer(\"Notch\", false)"
)
.since("2.8.0, 2.9.0 (prevent lookups)");
} // end offline player function
Functions.registerFunction(new SimpleJavaFunction<Boolean>("isNaN", numberParam, DefaultClasses.BOOLEAN, true) {
@Override
public Boolean[] executeSimple(Object[][] params) {
return new Boolean[] {Double.isNaN(((Number) params[0][0]).doubleValue())};
}
}).description("Returns true if the input is NaN (not a number).")
.examples("isNaN(0) # false", "isNaN(0/0) # true", "isNaN(sqrt(-1)) # true")
.since("2.8.0");
Functions.register(DefaultFunction.builder(skript, "concat", String.class)
.description("Joins the provided texts (and other things) into a single text.")
.examples(
"concat(\"hello \", \"there\") # hello there",
"concat(\"foo \", 100, \" bar\") # foo 100 bar"
)
.since("2.9.0")
.parameter("texts", Object[].class)
.build(args -> {
StringBuilder builder = new StringBuilder();
Object[] objects = args.get("texts");
for (Object object : objects) {
builder.append(Classes.toString(object));
}
return builder.toString();
}));
// joml functions - for display entities
{
if (Skript.classExists("org.joml.Quaternionf")) {
Functions.registerFunction(new SimpleJavaFunction<>("quaternion", new Parameter[]{
new Parameter<>("w", DefaultClasses.NUMBER, true, null),
new Parameter<>("x", DefaultClasses.NUMBER, true, null),
new Parameter<>("y", DefaultClasses.NUMBER, true, null),
new Parameter<>("z", DefaultClasses.NUMBER, true, null)
}, Classes.getExactClassInfo(Quaternionf.class), true) {
@Override
public Quaternionf[] executeSimple(Object[][] params) {
double w = ((Number) params[0][0]).doubleValue();
double x = ((Number) params[1][0]).doubleValue();
double y = ((Number) params[2][0]).doubleValue();
double z = ((Number) params[3][0]).doubleValue();
return CollectionUtils.array(new Quaternionf(x, y, z, w));
}
})
.description("Returns a quaternion from the given W, X, Y and Z parameters. ")
.examples("quaternion(1, 5.6, 45.21, 10)")
.since("2.10");
}
if (Skript.classExists("org.joml.AxisAngle4f")) {
Functions.registerFunction(new SimpleJavaFunction<>("axisAngle", new Parameter[]{
new Parameter<>("angle", DefaultClasses.NUMBER, true, null),
new Parameter<>("axis", DefaultClasses.VECTOR, true, null)
}, Classes.getExactClassInfo(Quaternionf.class), true) {
@Override
public Quaternionf[] executeSimple(Object[][] params) {
float angle = (float) (((Number) params[0][0]).floatValue() / 180 * Math.PI);
Vector v = ((Vector) params[1][0]);
if (v.isZero() || !Double.isFinite(v.getX()) || !Double.isFinite(v.getY()) || !Double.isFinite(v.getZ()))
return new Quaternionf[0];
Vector3f axis = ((Vector) params[1][0]).toVector3f();
return CollectionUtils.array(new Quaternionf(new AxisAngle4f(angle, axis)));
}
})
.description("Returns a quaternion from the given angle (in degrees) and axis (as a vector). This represents a rotation around the given axis by the given angle.")
.examples("axisangle(90, (vector from player's facing))")
.since("2.10");
}
} // end joml functions
Functions.registerFunction(new SimpleJavaFunction<>("formatNumber", new Parameter[]{
new Parameter<>("number", DefaultClasses.NUMBER, true, null),
new Parameter<>("format", DefaultClasses.STRING, true, new SimpleLiteral<>("", true))
}, DefaultClasses.STRING, true) {
@Override
public String[] executeSimple(Object[][] params) {
Number number = (Number) params[0][0];
String format = (String) params[1][0];
if (format.isEmpty()) {
if (number instanceof Double || number instanceof Float) {
return new String[]{DEFAULT_DECIMAL_FORMAT.format(number)};
} else {
return new String[]{DEFAULT_INTEGER_FORMAT.format(number)};
}
}
try {
return new String[]{new DecimalFormat(format).format(number)};
} catch (IllegalArgumentException e) {
return null; // invalid format
}
}
})
.description(
"Converts numbers to human-readable format. By default, '###,###' (e.g. '123,456,789') " +
"will be used for whole numbers and '###,###.##' (e.g. '123,456,789.00) will be used for decimal numbers. " +
"A hashtag '#' represents a digit, a comma ',' is used to separate numbers, and a period '.' is used for decimals. ",
"Will return none if the format is invalid.",
"For further reference, see this <a href=\"https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html\" target=\"_blank\">article</a>.")
.examples(
"command /balance:",
"\taliases: bal",
"\texecutable by: players",
"\ttrigger:",
"\t\tset {_money} to formatNumber({money::%sender's uuid%})",
"\t\tsend \"Your balance: %{_money}%\" to sender")
.since("2.10");
Functions.registerFunction(new SimpleJavaFunction<>("uuid", new Parameter[]{
new Parameter<>("uuid", DefaultClasses.STRING, true, null)
}, Classes.getExactClassInfo(UUID.class), true) {
@Override
public UUID[] executeSimple(Object[][] params) {
String uuid = (String) params[0][0];
if (Utils.isValidUUID(uuid))
return CollectionUtils.array(UUID.fromString(uuid));
return new UUID[0];
}
}
.description("Returns a UUID from the given string. The string must be in the format of a UUID.")
.examples("uuid(\"069a79f4-44e9-4726-a5be-fca90e38aaf5\")")
.since("2.11")
);
Functions.registerFunction(new SimpleJavaFunction<Number>("mean", new Parameter[]{
new Parameter<>("numbers", DefaultClasses.NUMBER, false, null)
}, DefaultClasses.NUMBER, true) {
@Override
public Number @Nullable [] executeSimple(Object[][] params) {
Double total = 0d;
int length = params[0].length;
for (int i = 0; i < length; i++) {
Number number = (Number) params[0][i];
if (Double.isInfinite(number.doubleValue()) || Double.isNaN(number.doubleValue()))
return null;
if (total.isInfinite() || total.isNaN())
return null;
total += number.doubleValue() / length;
}
return new Number[]{total};
}
})
.description(
"Get the mean (average) of a list of numbers.",
"You cannot get the mean of a set of numbers that includes infinity or NaN."
)
.examples(
"mean(1, 2, 3) = 2",
"mean(0, 5, 10) = 5",
"mean(13, 97, 376, 709) = 298.75"
)
.since("2.11");
Functions.registerFunction(new SimpleJavaFunction<Number>("median", new Parameter[]{
new Parameter<>("numbers", DefaultClasses.NUMBER, false, null)
}, DefaultClasses.NUMBER, true) {
@Override
public Number @Nullable [] executeSimple(Object[][] params) {
AtomicBoolean invalid = new AtomicBoolean(false);
// median requires the numbers to be sorted from lowest to biggest
Number[] sorted = Arrays.stream(params[0])
.filter(object -> {
if (!(object instanceof Number number))
return false;
if (Double.isNaN(number.doubleValue())) {
invalid.set(true);
return false;
}
return true;
})
.map(object -> (Number) object)
.sorted(((o1, o2) -> {
Double n1 = o1.doubleValue();
Double n2 = o2.doubleValue();
return n1.compareTo(n2);
}))
.toArray(Number[]::new);
if (invalid.get())
return null;
int size = sorted.length;
// If the size of numbers provided is odd, we can just grab the middle number
if (size % 2 == 1)
return new Number[]{sorted[Math2.ceil(size /2)]};
// If not, we grab the rounded up and rounded down numbers, then get the average of those
int half = size / 2;
double first = (sorted[half - 1]).doubleValue();
double second = (sorted[half]).doubleValue();
double median = (first+second)/2;
return new Number[]{median};
}
})
.description(
"Get the middle value of a sorted list of numbers. "
+ "If the list has an even number of values, the median is the average of the two middle numbers.",
"You cannot get the median of a set of numbers that includes NaN."
)
.examples(
"median(1, 2, 3, 4, 5) = 3",
"median(1, 2, 3, 4, 5, 6) = 3.5",
"median(0, 123, 456, 789) = 289.5"
)
.since("2.11");
Functions.registerFunction(new SimpleJavaFunction<>("factorial", new Parameter[]{
new Parameter<>("number", DefaultClasses.NUMBER, true, null)
}, DefaultClasses.NUMBER, true) {
@Override
public Number @Nullable [] executeSimple(Object[][] params) {
Double number = ((Number) params[0][0]).doubleValue();
if (number < 0) {
return null;
} else if (number <= 1) { // 0 and 1
return new Number[]{1};
} else if (number > 170) {
return new Number[]{Double.POSITIVE_INFINITY};
}
Double result = 1d;
for (double i = number; i > 1; i--) {
if (result.isInfinite() || result.isNaN())
break;
result *= i;
}
return new Number[]{result};
}
})
.description(
"Get the factorial of a number.",
"Getting the factorial of any number above 21 will return an approximation, not an exact value.",
"Any number after 170 will always return Infinity.",
"Should not be used to calculate permutations or combinations manually."
)
.examples(
"factorial(0) = 1",
"factorial(3) = 3*2*1 = 6",
"factorial(5) = 5*4*3*2*1 = 120",
"factorial(171) = Infinity"
)
.since("2.11");
Functions.registerFunction(new SimpleJavaFunction<Number>("root", new Parameter[]{
new Parameter<>("n", DefaultClasses.NUMBER, true, null),
new Parameter<>("number", DefaultClasses.NUMBER, true, null)
}, DefaultClasses.NUMBER, true) {
@Override
public Number @Nullable [] executeSimple(Object[][] params) {
Double n = ((Number) params[0][0]).doubleValue();
Double number = ((Number) params[1][0]).doubleValue();
if (n == 0) {
return null;
} else if (n == 1) {
return new Number[]{number};
} else if (n == 2) {
return new Number[]{Math.sqrt(number)};
}
return new Number[]{Math.pow(number, (1 / n))};
}
})
.description("Calculates the <i>n</i>th root of a number.")
.examples(
"root(2, 4) = 2 # same as sqrt(4)",
"root(4, 16) = 2",
"root(-4, 16) = 0.5 # same as 16^(-1/4)"
)
.since("2.11");
Functions.registerFunction(new SimpleJavaFunction<Number>("permutations", new Parameter[]{
new Parameter<>("options", DefaultClasses.NUMBER, true, null),
new Parameter<>("selected", DefaultClasses.NUMBER, true, null)
}, DefaultClasses.NUMBER, true) {
@Override
public Number @Nullable [] executeSimple(Object[][] params) {
Double options = ((Number) params[0][0]).doubleValue();
Double selected = ((Number) params[1][0]).doubleValue();
if (selected > options || selected < 0) { // Illegal argument
return null;
} else if (selected.equals(0d)) { // Will always be 1
return new Number[]{1};
} else if (selected.equals(1d)) { // Will always be the number from 'options'
return new Number[]{options};
}
// We can simplify this as there will always be a factorial that can cancel out
// Example: options = 10, selected = 2; 10!/(10-2)! = 10!/8!
// We can deduce that 10! = (10)(9)(8!) ; allowing the '8!' factorial to cancel out, leaving us with: (10)(9)
// Which allows us to start from 10 and go down to 10-2, but will never reach 8 as 'i' needs to be higher
Double result = 1d;
for (double i = options; i > options - selected; i--) {
if (result.isInfinite() || result.isNaN())
break;
result *= i;
}
return new Number[]{result};
}
})
.description(
"Get the number of possible ordered arrangements from 1 to 'options' with each arrangement having a size equal to 'selected'",
"For example, permutations with 3 options and an arrangement size of 1, returns 3: (1), (2), (3)",
"Permutations with 3 options and an arrangement size of 2 returns 6: (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)",
"Note that the bigger the 'options' and lower the 'selected' may result in approximations or even infinity values.",
"Permutations differ from combinations in that permutations account for the arrangement of elements within the set, "
+ "whereas combinations focus on unique sets and ignore the order of elements.",
"Example: (1, 2) and (2, 1) are two distinct permutations because the positions of '1' and '2' are different, "
+ "but they represent a single combination since order doesn't matter in combinations."
)
.examples(
"permutations(10, 2) = 90",
"permutations(10, 4) = 5040",
"permutations(size of {some list::*}, 2)"
)
.since("2.11");
Functions.registerFunction(new SimpleJavaFunction<Number>("combinations", new Parameter[]{
new Parameter<>("options", DefaultClasses.NUMBER, true, null),
new Parameter<>("selected", DefaultClasses.NUMBER, true, null)
}, DefaultClasses.NUMBER, true) {
@Override
public Number @Nullable [] executeSimple(Object[][] params) {
Double options = ((Number) params[0][0]).doubleValue();
Double selected = ((Number) params[1][0]).doubleValue();
if (selected > options || selected < 0) { // Illegal arguments
return null;
} else if (selected.equals(0d)) { // Will always return 1
return new Number[]{1};
} else if (selected.equals(1d)) { // Will always be the number from 'options'
return new Number[]{options};
}
// By the same reasoning from 'permutations' there will always be a factorial that can cancel out
// Example: options = 10, selected = 2 ; 10!/(10-2)!(2!) = 10!/(8!)(2!)
// 10! = (10)(9)(8!) ; the 8! cancel out, leaving us with: (10)(9)/2!
// 'top' will calculate the leftovers in the numerator: (10)(9)
Double top = 1d;