forked from CryptoMorin/XSeries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXParticle.java
More file actions
2049 lines (1832 loc) · 87.1 KB
/
XParticle.java
File metadata and controls
2049 lines (1832 loc) · 87.1 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
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Crypto Morin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.cryptomorin.xseries;
import com.google.common.base.Enums;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
/**
* <b>XParticle</b> - Different particle animations, text and image renderer.<br>
* This utility uses {@link ParticleDisplay} for cleaner code. This class adds the ability
* to define the optional values for spawning particles.
* <p>
* While this class provides many methods with options to spawn unique shapes,
* it's recommended to make your own shapes by copying the code from these methods.
* Note that some of the values for some methods are extremely sensitive and can change
* the shape significantly by adding small numbers such as 0.5<br>
* Most of the method parameters have a recommended value set to start with.
* Note that these values are there to show how the intended normal shape
* looks like before you start changing the values.
* <p>
* It's recommended to use low particle counts.
* In most cases, decreasing the rate is better than increasing the particle count.
* Most of the methods provide an option called "rate" that you can get more particles
* by decreasing the distance between each point the particle spawns.<br>
* Most of the {@link ParticleDisplay} used in this class are intended to
* have 1 particle count and 0 xyz offset and speed.
* <p>
* Particles are rendered as front-facing 2D sprites, meaning they always face the player.
* Minecraft clients will automatically clear previous particles if you reach the limit.
* Particle range is 32 blocks. Particle count limit is 16,384.
* Particles are not entities.
* <p>
* All the methods and operations used in this class are thread-safe.
* Most of the methods do not run asynchronous by default.
* If you're doing a resource intensive operation it's recommended
* to either use {@link CompletableFuture#runAsync(Runnable)} or
* {@link BukkitRunnable#runTaskTimerAsynchronously(Plugin, long, long)} for
* smoothly animated shapes.
* For huge animations you can use splittable tasks.
* https://www.spigotmc.org/threads/409003/
* By "huge", the algorithm used to generate locations is considered. You should not spawn
* a lot of particles at once. This will cause FPS drops for most of
* the clients, unless they have a good PC.
* <p>
* You can test your 2D shapes at <a href="https://www.desmos.com/calculator">Desmos</a>
* Stuff you can do with with
* <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html">Java {@link Math}</a>
* Getting started with <a href="https://www.spigotmc.org/wiki/vector-programming-for-beginners/">Vectors</a>.
* Particles: https://minecraft.gamepedia.com/Particles
*
* @author Crypto Morin
* @version 3.1.0
* @see ParticleDisplay
* @see Particle
* @see Location
* @see Vector
*/
public final class XParticle {
/**
* A full circle has two PIs.
* Don't know what the fuck is a PI? You can
* watch this <a href="https://www.youtube.com/watch?v=pMpQK7Y8CiM">YouTube video</a>
* <p>
* PI is a radian number itself. So you can obtain other radians by simply
* dividing PI.
* Some simple ones:
* <p>
* <b>Important Radians:</b>
* <pre>
* PI / 2 = 90 degrees
* PI / 3 = 60 degrees
* PI / 4 = 45 degrees
* PI / 6 = 30 degrees
* </pre>
* Any degree can be converted simply be using {@code PI/180 * degree}
*
* @see Math#toRadians(double)
* @see Math#toDegrees(double)
* @since 1.0.0
*/
public static final double PII = 2 * Math.PI;
/**
* RGB list of all the 7 rainbow colors in order.
*
* @since 2.0.0
*/
public static final List<int[]> RAINBOW = new ArrayList<>();
static {
RAINBOW.add(new int[]{128, 0, 128}); // Violet
RAINBOW.add(new int[]{75, 0, 130}); // Indigo
RAINBOW.add(new int[]{0, 0, 255}); // Blue
RAINBOW.add(new int[]{0, 255, 0}); // Green
RAINBOW.add(new int[]{255, 255, 0}); // Yellow
RAINBOW.add(new int[]{255, 140, 0}); // Orange
RAINBOW.add(new int[]{255, 0, 0}); // Red
}
/**
* An optimized and stable way of getting particles for cross-version support.
*
* @param particle the particle name.
* @return a particle that matches the specified name.
* @since 1.0.0
*/
public static Particle getParticle(String particle) {
return Enums.getIfPresent(Particle.class, particle).orNull();
}
/**
* Get a random particle from a list of particle names.
*
* @param particles the particles name.
* @return a random particle from the list.
* @since 1.0.0
*/
public static Particle randomParticle(String... particles) {
int rand = randInt(0, particles.length - 1);
return getParticle(particles[rand]);
}
/**
* A thread safe way to get a random double in a range.
*
* @param min the minimum number.
* @param max the maximum number.
* @return a random number.
* @see #randInt(int, int)
* @since 1.0.0
*/
public static double random(double min, double max) {
return ThreadLocalRandom.current().nextDouble(min, max);
}
/**
* A thread safe way to get a random integer in a range.
*
* @param min the minimum number.
* @param max the maximum number.
* @return a random number.
* @see #random(double, double)
* @since 1.0.0
*/
public static int randInt(int min, int max) {
return ThreadLocalRandom.current().nextInt(min, max + 1);
}
/**
* Generate a random RGB color for particles.
*
* @return a random color.
* @since 1.0.0
*/
public static Color randomColor() {
ThreadLocalRandom gen = ThreadLocalRandom.current();
int randR = gen.nextInt(0, 256);
int randG = gen.nextInt(0, 256);
int randB = gen.nextInt(0, 256);
return Color.fromRGB(randR, randG, randB);
}
/**
* Generate a random colorized dust with a random size.
*
* @return a REDSTONE colored dust.
* @since 1.0.0
*/
public static Particle.DustOptions randomDust() {
float size = randInt(5, 10) / 0.1f;
return new Particle.DustOptions(randomColor(), size);
}
/**
* Creates a blacksun-like increasing circles.
*
* @param radius the radius of the biggest circle.
* @param radiusRate the radius rate change of circles.
* @param rate the rate of the biggest cirlce points.
* @param rateChange the rate change of circle points.
* @see #circle(double, double, ParticleDisplay)
* @since 1.0.0
*/
public static void blackSun(double radius, double radiusRate, double rate, double rateChange, ParticleDisplay display) {
double j = 0;
for (double i = 10; i > 0; i -= radiusRate) {
j += rateChange;
circle(radius + i, rate - j, display);
}
}
/**
* Spawn a circle.
* Tutorial: https://www.spigotmc.org/threads/111238/
* Uses its own unique directional pattern.
*
* @param radius the circle radius.
* @param rate the rate of cirlce points/particles.
* @see #sphere(double, double, ParticleDisplay)
* @since 1.0.0
*/
public static void circle(double radius, double rate, ParticleDisplay display) {
// 180 degrees = PI
// We need a full circle, 360 so we need two pies!
// https://www.spigotmc.org/threads/176792/
// cos and sin methods only accept radians.
// Converting degrees to radians is not resource intensive. It's a really simple operation.
// However we can skip the conversion by using radians in the first place.
double rateDiv = Math.PI / rate;
for (double theta = 0; theta <= PII; theta += rateDiv) {
// In order to curve our straight line in the loop, we need to
// use cos and sin. It doesn't matter, you can get x as sin and z as cos.
// But you'll get weird results if you use si+n or cos for both or using tan or cot.
double x = radius * Math.cos(theta);
double z = radius * Math.sin(theta);
if (display.isDirectional()) {
// We're going to get the angle in these two coordinates.
// Then we can spread each particle in the right angle.
double phi = Math.atan2(z, x);
double directionX = Math.cos(phi);
double directionZ = Math.sin(phi);
display.offset(directionX, display.offsety, directionZ);
}
display.spawn(x, 0, z);
}
}
/**
* Spawns connected 3D ellipses.
*
* @param plugin the timer handler.
* @param maxRadius the maximum radius for the ellipses.
* @param rate the rate of the 3D ellipses circle points.
* @param radiusRate the rate of the circle radius change.
* @param extend the extension for each ellipse.
* @return the animation handler.
* @see #magicCircles(JavaPlugin, double, double, double, double, ParticleDisplay)
* @since 3.0.0
*/
public static BukkitTask circularBeam(JavaPlugin plugin, double maxRadius, double rate, double radiusRate, double extend, ParticleDisplay display) {
return new BukkitRunnable() {
double rateDiv = Math.PI / rate;
double radiusDiv = Math.PI / radiusRate;
double dynamicRadius = 0;
Vector dir = display.location.getDirection().normalize().multiply(extend);
@Override
public void run() {
// If we wanted to use actual numbers as the radius then the curve for
// each loop wouldn't be smooth.
double radius = maxRadius * Math.sin(dynamicRadius);
// Spawn normal circles.
for (double theta = 0; theta < PII; theta += rateDiv) {
double x = radius * Math.sin(theta);
double z = radius * Math.cos(theta);
display.spawn(x, 0, z);
}
dynamicRadius += radiusDiv;
if (dynamicRadius > Math.PI) dynamicRadius = 0;
// Next circle center location.
display.location.add(dir);
}
}.runTaskTimerAsynchronously(plugin, 0L, 1L);
}
/**
* Spawns circles increasing their radius.
*
* @param plugin the timer handler.
* @param radius the radius for the first circle.
* @param rate the rate of circle points.
* @param radiusRate the circle radius change rate.
* @param distance the distance between each circle.
* @return the animation handler.
* @see #circularBeam(JavaPlugin, double, double, double, double, ParticleDisplay)
* @since 3.0.0
*/
public static BukkitTask magicCircles(JavaPlugin plugin, double radius, double rate, double radiusRate, double distance, ParticleDisplay display) {
return new BukkitRunnable() {
double radiusDiv = Math.PI / radiusRate;
double dynamicRadius = radius;
Vector dir = display.location.getDirection().normalize().multiply(distance);
@Override
public void run() {
double rateDiv = Math.PI / (rate * dynamicRadius);
for (double theta = 0; theta < PII; theta += rateDiv) {
double x = dynamicRadius * Math.sin(theta);
double z = dynamicRadius * Math.cos(theta);
display.spawn(x, 0, z);
}
// We're going to use normal numbers since the circle radius will be always changing
// in one axis.
dynamicRadius += radiusDiv;
display.location.add(dir);
}
}.runTaskTimerAsynchronously(plugin, 0L, 1L);
}
/**
* Spawn a 3D infinity sign.
*
* @param radius the radius of the infinity circles.
* @param rate the rate of the sign points.
* @since 3.0.0
*/
public static void infinity(double radius, double rate, ParticleDisplay display) {
double rateDiv = Math.PI / rate;
for (double i = 0; i < PII; i += rateDiv) {
double x = Math.sin(i);
double smooth = Math.pow(x, 2) + 1;
double curve = radius * Math.cos(i);
double z = curve / smooth;
double y = (curve * x) / smooth;
// If you remove x the infinity symbol will be 2D
circle(1, rate, display.cloneWithLocation(x, y, z));
}
}
/**
* Spawn a cone.
*
* @param height the height of the cone.
* @param radius the radius of the cone circle.
* @param rate the rate of the cone circles.
* @param circleRate the rate of the cone circle points.
* @since 1.0.0
*/
public static void cone(double height, double radius, double rate, double circleRate, ParticleDisplay display) {
// Our biggest radius / amount of loop times = the amount to subtract from the biggest radius so it wouldn't be negative.
double radiusDiv = radius / (height / rate);
// We're going spawn circles with different radiuses and rates to make a cone.
for (double i = 0; i < height; i += rate) {
radius -= radiusDiv;
// The remainder of radiusDiv division might be not 0
// This will happen to the last loop only.
if (radius < 0) radius = 0;
circle(radius, circleRate - i, display.cloneWithLocation(0, i, 0));
}
}
/**
* Spawn an ellipse.
*
* @param radius the radius of the ellipse.
* @param otherRadius the curve of the ellipse.
* @param rate the rate of ellipse points.
* @see #circle(double, double, ParticleDisplay)
* @since 2.0.0
*/
public static void ellipse(double radius, double otherRadius, double rate, ParticleDisplay display) {
double rateDiv = Math.PI / rate;
// The only difference between circles and ellipses are that
// ellipses use a different radius for one of their axis.
for (double theta = 0; theta <= PII; theta += rateDiv) {
double x = radius * Math.cos(theta);
double y = otherRadius * Math.sin(theta);
display.spawn(x, y, 0);
}
}
/**
* Spawn a blackhole.
*
* @param plugin the timer handler.
* @param points the points of the blackhole pulls.
* @param radius the radius of the blackhole circle.
* @param rate the rate of the blackhole circle points.
* @param mode blackhole mode. There are 5 modes.
* @since 3.0.0
*/
public static void blackhole(JavaPlugin plugin, int points, double radius, double rate, int mode, ParticleDisplay display) {
display.directional();
display.extra = 0.1;
double rateDiv = Math.PI / rate;
new BukkitRunnable() {
double theta = 0;
@Override
public void run() {
for (int i = 0; i < points; i++) {
// Spawn a circle.
double angle = PII * ((double) i / points);
double x = radius * Math.cos(theta + angle);
double z = radius * Math.sin(theta + angle);
// Set the angle of the circle point as its degree.
double phi = Math.atan2(z, x);
double xDirection = -Math.cos(phi);
double zDirection = -Math.sin(phi);
display.offset(xDirection, 0, zDirection);
display.spawn(x, 0, z);
// The modes are done by random math methods that are
// just randomly tested to give a different shape.
if (mode > 1) {
x = radius * Math.cos(-theta + angle);
z = radius * Math.sin(-theta + angle);
// Eye shaped blackhole
if (mode == 2) phi = Math.atan2(z, x);
else if (mode == 3) phi = Math.atan2(x, z);
else if (mode == 4) Math.atan2(Math.log(x), Math.log(z));
xDirection = -Math.cos(phi);
zDirection = -Math.sin(phi);
display.offset(xDirection, 0, zDirection);
display.spawn(x, 0, z);
}
}
theta += rateDiv;
}
}.runTaskTimerAsynchronously(plugin, 0L, 1L);
}
/**
* Spawns a rainbow.
*
* @param radius the radius of the smallest circle.
* @param rate the rate of the rainbow points.
* @param curve the curve the the rainbow circles.
* @param layers the layers of each rainbow color.
* @param compact the distance between each circles.
* @since 2.0.0
*/
public static void rainbow(double radius, double rate, double curve, double layers, double compact, ParticleDisplay display) {
double secondRadius = radius * curve;
// Rainbows have 7 colors.
// Refer to RAINBOW constant for the color order.
for (int i = 0; i < 7; i++) {
// Get the rainbow color in order.
int[] rgb = RAINBOW.get(i);
display = ParticleDisplay.paintDust(display.location, rgb[0], rgb[1], rgb[2], 1F);
// Display the same color multiple times.
for (int layer = 0; layer < layers; layer++) {
double rateDiv = Math.PI / (rate * (i + 2));
// We're going to create our rainbow layer from half circles.
for (double theta = 0; theta <= Math.PI; theta += rateDiv) {
double x = radius * Math.cos(theta);
double y = secondRadius * Math.sin(theta);
display.spawn(x, y, 0);
}
radius += compact;
}
}
}
/**
* Spawns a crescent.
*
* @param radius the radius of crescent's big circle.
* @param rate the rate of the crescent's circle points.
* @see #circle(double, double, ParticleDisplay)
* @since 1.0.0
*/
public static void crescent(double radius, double rate, ParticleDisplay display) {
double rateDiv = Math.PI / rate;
// Crescents are two circles, one with a smaller radius and slightly shifted to the open part of the bigger circle.
// To align the opening of the bigger circle with the +X axis we'll have to adjust our start and end radians.
for (double theta = Math.toRadians(45); theta <= Math.toRadians(325); theta += rateDiv) {
// Our circle at the bottom.
double x = Math.cos(theta);
double z = Math.sin(theta);
display.spawn(radius * x, 0, radius * z);
// Slightly move the smaller circle to connect the openings.
double smallerRadius = radius / 1.3;
display.spawn(smallerRadius * x + 0.8, 0, smallerRadius * z);
}
}
/**
* Something similar to <a href="https://en.wikipedia.org/wiki/Wave_function">Quantum Wave function</a>
*
* @param extend the particle width extension. Recommended value is 3
* @param heightRange the height range of randomized waves. Recommended value is 1
* @param size the size of the terrain. Normal size is 3
* @param rate the rate of waves points. Recommended value is around 30
* @since 2.0.0
*/
public static void waveFunction(double extend, double heightRange, double size, double rate, ParticleDisplay display) {
double height = heightRange / 2;
boolean increase = true;
double increaseRandomizer = random(heightRange / 2, heightRange);
double rateDiv = Math.PI / rate;
// Each wave is like a circle curving up and down.
size *= PII;
// We're going to create randomized circles.
for (double x = 0; x <= size; x += rateDiv) {
double xx = extend * x;
double y1 = Math.sin(x);
// Maximum value of sin is 1, when our sin is 1 it means
// one full circle has been created, so we'll regenerate our random height.
if (y1 == 1) {
increase = !increase;
if (increase) increaseRandomizer = random(heightRange / 2, heightRange);
else increaseRandomizer = random(-heightRange, -heightRange / 2);
}
height += increaseRandomizer;
// We'll generate horizontal cos/sin circles and move forward.
for (double z = 0; z <= size; z += rateDiv) {
double y2 = Math.cos(z);
double yy = height * y1 * y2;
double zz = extend * z;
display.spawn(xx, yy, zz);
}
}
}
/**
* Spawns a galaxy-like vortex.
* Note that the speed of the particle is important.
* Speed 0 will spawn static lines.
*
* @param plugin the timer handler.
* @param points the points of the vortex.
* @param rate the speed of the vortex.
* @return the task handling the animation.
* @since 2.0.0
*/
public static BukkitTask vortex(JavaPlugin plugin, int points, double rate, ParticleDisplay display) {
double rateDiv = Math.PI / rate;
display.directional();
return new BukkitRunnable() {
double theta = 0;
@Override
public void run() {
theta += rateDiv;
for (int i = 0; i < points; i++) {
// Calculate our starting point in a circle radius.
double multiplier = (PII * ((double) i / points));
double x = Math.cos(theta + multiplier);
double z = Math.sin(theta + multiplier);
// Calculate our direction of the spreading particles based on their angle.
double angle = Math.atan2(z, x);
double xDirection = Math.cos(angle);
double zDirection = Math.sin(angle);
display.offset(xDirection, 0, zDirection);
display.spawn(x, 0, z);
}
}
}.runTaskTimerAsynchronously(plugin, 0L, 1L);
}
/**
* Not really a cylinder. It looks more like a cage.
* For an actual cylidner just use {@link #circle(double, double, ParticleDisplay)}
* and use one the xyz axis to build multiple circles.
*
* @param height the height of the cylinder.
* @param radius the radius of the cylinder circles.
* @param rate the rate of cylinder points.
* @since 1.0.0
*/
public static void cylinder(double height, double radius, double rate, ParticleDisplay display) {
double rateDiv = Math.PI / rate;
// We want to connect our circle points so we use 180
for (double theta = 0; theta <= Math.PI; theta += rateDiv) {
// Our circle at the bottom.
double x = radius * Math.cos(theta);
double z = radius * Math.sin(theta);
// Bottom Circle
display.spawn(x, 0, z);
display.spawn(-x, 0, -z);
// Top Circle
display.spawn(x, height, z);
display.spawn(-x, height, -z);
// Connect the circle points from opposite sides to each other.
Location point1 = display.cloneLocation(x, 0, z);
Location point2 = display.cloneLocation(-x, 0, -z);
line(point1, point2, 0.1, display);
Location point21 = display.cloneLocation(x, height, z);
Location point22 = display.cloneLocation(-x, height, -z);
line(point21, point22, 0.1, display);
// Connect the two circles points to each other.
line(point1, point21, 0.1, display);
line(point2, point22, 0.1, display);
}
}
/**
* This will move the shape around in an area randomly while rotating them.
* The position of the shape will be randomized positively and negatively by the offset parameters on each axis.
*
* @param plugin the schedule handler.
* @param update the timer period in ticks.
* @param rate the distance between each location. Recommended value is 5.
* @param runnable the particles to spawn.
* @param displays the display references used to spawn particles in the runnable.
* @return the async task handling the movement.
* @see #rotateAround(JavaPlugin, long, double, double, double, double, Runnable, ParticleDisplay...)
* @see #guard(JavaPlugin, long, double, double, double, double, Runnable, ParticleDisplay...)
* @since 1.0.0
*/
public static BukkitTask moveRotatingAround(JavaPlugin plugin, long update, double rate, double offsetx, double offsety, double offsetz,
Runnable runnable, ParticleDisplay... displays) {
return new BukkitRunnable() {
double rotation = 180;
@Override
public void run() {
rotation += rate;
// Generate random radians.
double x = Math.toRadians(90 + rotation);
double y = Math.toRadians(60 + rotation);
double z = Math.toRadians(30 + rotation);
Vector vector = new Vector(offsetx * Math.PI, offsety * Math.PI, offsetz * Math.PI);
if (offsetx != 0) rotateAroundX(vector, x);
if (offsety != 0) rotateAroundY(vector, y);
if (offsetz != 0) rotateAroundZ(vector, z);
for (ParticleDisplay display : displays) display.location.add(vector);
runnable.run();
for (ParticleDisplay display : displays) display.location.subtract(vector);
}
}.runTaskTimerAsynchronously(plugin, 0L, update);
}
/**
* This will move the particle around in an area randomly.
* The position of the shape will be randomized positively and negatively by the offset parameters on each axis.
*
* @param plugin the schedule handler.
* @param update the timer period in ticks.
* @param rate the distance between each location. Recommended value is 5.
* @param runnable the particles to spawn.
* @param displays the display references used to spawn particles in the runnable.
* @return the async task handling the movement.
* @see #rotateAround(JavaPlugin, long, double, double, double, double, Runnable, ParticleDisplay...)
* @see #guard(JavaPlugin, long, double, double, double, double, Runnable, ParticleDisplay...)
* @since 1.0.0
*/
public static BukkitTask moveAround(JavaPlugin plugin, long update, double rate, double endRate, double offsetx, double offsety, double offsetz,
Runnable runnable, ParticleDisplay... displays) {
return new BukkitRunnable() {
double multiplier = 0;
boolean opposite = false;
@Override
public void run() {
if (opposite) multiplier -= rate;
else multiplier += rate;
double x = multiplier * offsetx;
double y = multiplier * offsety;
double z = multiplier * offsetz;
for (ParticleDisplay display : displays) display.location.add(x, y, z);
runnable.run();
for (ParticleDisplay display : displays) display.location.subtract(x, y, z);
if (opposite) {
if (multiplier <= 0) opposite = !opposite;
} else {
if (multiplier >= endRate) opposite = !opposite;
}
}
}.runTaskTimerAsynchronously(plugin, 0L, update);
}
/**
* A simple test method to spawn a shape repeatedly for diagnosis.
*
* @param plugin the timer handler.
* @param runnable the shape(s) to display.
* @return the timer task handling the displays.
* @since 1.0.0
*/
public static BukkitTask testDisplay(JavaPlugin plugin, Runnable runnable) {
return Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, 0L, 1L);
}
/**
* This will rotate the shape around in an area randomly.
* The position of the shape will be randomized positively and negatively by the offset parameters on each axis.
*
* @param plugin the schedule handler.
* @param update the timer period in ticks.
* @param rate the distance between each location. Recommended value is 5.
* @param runnable the particles to spawn.
* @param displays the displays references used to spawn particles in the runnable.
* @return the async task handling the movement.
* @see #moveRotatingAround(JavaPlugin, long, double, double, double, double, Runnable, ParticleDisplay...)
* @see #guard(JavaPlugin, long, double, double, double, double, Runnable, ParticleDisplay...)
* @since 1.0.0
*/
public static BukkitTask rotateAround(JavaPlugin plugin, long update, double rate, double offsetx, double offsety, double offsetz,
Runnable runnable, ParticleDisplay... displays) {
return new BukkitRunnable() {
double rotation = 180;
@Override
public void run() {
rotation += rate;
double x = Math.toRadians((90 + rotation) * offsetx);
double y = Math.toRadians((60 + rotation) * offsety);
double z = Math.toRadians((30 + rotation) * offsetz);
Vector vector = new Vector(x, y, z);
for (ParticleDisplay display : displays) display.rotate(vector);
runnable.run();
}
}.runTaskTimerAsynchronously(plugin, 0L, update);
}
/**
* This will move the particle around in an area randomly.
* The position of the shape will be randomized positively and negatively by the offset parameters on each axis.
* Note that the ParticleDisplays used in runnable and displays options must be from the same reference.
* <p>
* <b>Example</b>
* <pre>
* ParticleDisplays display = new ParticleDisplay(...);
* {@code WRONG: moveAround(plugin, 1, 5, 1.5, 1.5, 1.5, () -> circle(1, 10, new ParticleDisplay(...)), display);}
* {@code CORRECT: moveAround(plugin, 1, 5, 1.5, 1.5, 1.5, () -> circle(1, 10, display), display);}
* </pre>
*
* @param plugin the schedule handler.
* @param update the timer period in ticks.
* @param rate the distance between each location. Recommended value is 5.
* @param runnable the particles to spawn.
* @param displays the displays references used to spawn particles in the runnable.
* @return the async task handling the movement.
* @see #rotateAround(JavaPlugin, long, double, double, double, double, Runnable, ParticleDisplay...)
* @see #moveRotatingAround(JavaPlugin, long, double, double, double, double, Runnable, ParticleDisplay...)
* @since 1.0.0
*/
public static BukkitTask guard(JavaPlugin plugin, long update, double rate, double offsetx, double offsety, double offsetz,
Runnable runnable, ParticleDisplay... displays) {
return new BukkitRunnable() {
double rotation = 180;
@Override
public void run() {
rotation += rate;
double x = Math.toRadians((90 + rotation) * offsetx);
double y = Math.toRadians((60 + rotation) * offsety);
double z = Math.toRadians((30 + rotation) * offsetz);
Vector vector = new Vector(offsetx * Math.PI, offsety * Math.PI, offsetz * Math.PI);
rotateAroundX(vector, x);
rotateAroundY(vector, y);
rotateAroundZ(vector, z);
for (ParticleDisplay display : displays) {
display.rotation = new Vector(x, y, z);
display.location.add(vector);
}
runnable.run();
for (ParticleDisplay display : displays) display.location.subtract(vector);
}
}.runTaskTimerAsynchronously(plugin, 0L, update);
}
/**
* Spawn a sphere.
* Tutorial: https://www.spigotmc.org/threads/146338/
* Also uses its own unique directional pattern.
*
* @param radius the circle radius.
* @param rate the rate of cirlce points/particles.
* @see #circle(double, double, ParticleDisplay)
* @since 1.0.0
*/
public static void sphere(double radius, double rate, ParticleDisplay display) {
// Cache
double rateDiv = Math.PI / rate;
// To make a sphere we're going to generate multiple circles
// next to each other.
for (double phi = 0; phi <= Math.PI; phi += rateDiv) {
// Cache
double y1 = radius * Math.cos(phi);
double y2 = radius * Math.sin(phi);
for (double theta = 0; theta <= PII; theta += rateDiv) {
double x = Math.cos(theta) * y2;
double z = Math.sin(theta) * y2;
if (display.isDirectional()) {
// We're going to do the same thing from spreading circle.
// Since this is a 3D shape we'll need to get the y value as well.
// I'm not sure if this is the right way to do it.
double omega = Math.atan2(z, x);
double directionX = Math.cos(omega);
double directionY = Math.sin(Math.atan2(y2, y1));
double directionZ = Math.sin(omega);
display.offset(directionX, directionY, directionZ);
}
display.spawn(x, y1, z);
}
}
}
/**
* Spawns a sphere with spikes coming out from the center.
* The sphere points will not be visible.
*
* @param radius the radius of the sphere.
* @param rate the rate of sphere spike points.
* @param chance the chance to grow a spike randomly.
* @param minRandomDistance he minimum distance of spikes from sphere.
* @param maxRandomDistance the maximum distance of spikes from sphere.
* @see #sphere(double, double, ParticleDisplay)
* @since 1.0.0
*/
public static void spikeSphere(double radius, double rate, int chance, double minRandomDistance, double maxRandomDistance, ParticleDisplay display) {
double rateDiv = Math.PI / rate;
// Generate normal circle points.
for (double phi = 0; phi <= Math.PI; phi += rateDiv) {
double y = radius * Math.cos(phi);
double sinPhi = radius * Math.sin(phi);
for (double theta = 0; theta <= PII; theta += rateDiv) {
double x = Math.cos(theta) * sinPhi;
double z = Math.sin(theta) * sinPhi;
if (chance == 0 || randInt(0, chance) == 1) {
Location start = display.cloneLocation(x, y, z);
// We want to get the direction of our center location and the circle point
// so we cant spawn spikes on the opposite direction.
Vector endVect = start.clone().subtract(display.location).toVector().multiply(random(minRandomDistance, maxRandomDistance));
Location end = start.clone().add(endVect);
line(start, end, 0.1, display);
}
}
}
}
/**
* Spawns a donut-shaped ring.
* When the tube radius is greater than the main radius, the hole radius in the middle of the circle
* will increase as the circles come closer to the mid-point.
*
* @param rate the number of circles used to form the ring (tunnel circles)
* @param radius the radius of the ring.
* @param tubeRadius the radius of the circles used to form the ring (tunnel circles)
* @param tubeRate the rate of circle points.
* @see #circle(double, double, ParticleDisplay)
* @since 1.0.0
*/
public static void ring(double rate, double tubeRate, double radius, double tubeRadius, ParticleDisplay display) {
double rateDiv = Math.PI / rate;
double tubeDiv = Math.PI / tubeRadius;
// Use circles to build the ring.
for (double theta = 0; theta <= PII; theta += rateDiv) {
double cos = Math.cos(theta);
double sin = Math.sin(theta);
for (double phi = 0; phi <= PII; phi += tubeDiv) {
double finalRadius = radius + (tubeRadius * Math.cos(phi));
double x = finalRadius * cos;
double y = finalRadius * sin;
double z = tubeRadius * Math.sin(phi);
display.spawn(x, y, z);
}
}
}
/**
* Spawns animated spikes randomly spreading at the end location.
*
* @param plugin the timer handler.
* @param amount the amount of spikes to spawn.
* @param rate rate of spike line points.
* @param start start location of spikes.
* @param originEnd end location of spikes.
* @since 1.0.0
*/
public static void spread(JavaPlugin plugin, int amount, int rate, Location start, Location originEnd,
double offsetx, double offsety, double offsetz, ParticleDisplay display) {
new BukkitRunnable() {
int count = amount;
@Override
public void run() {
count--;
int frame = rate;
while (frame != 0) {
double x = random(-offsetx, offsetx);
double y = random(-offsety, offsety);
double z = random(-offsetz, offsetz);
Location end = originEnd.clone().add(x, y, z);
line(start, end, 0.1, display);
frame--;
}
if (count == 0) cancel();
}
}.runTaskTimerAsynchronously(plugin, 0L, 1L);
}
/**
* Spawns a circle with heart shaped circles sticking out.
* This method can be used to create many other shapes other than heart.
*
* @param cut defines the count of two oval pairs. For heart use 2
* @param cutAngle defines the compression of two oval pairs. For heart use 4
* @param depth the depth of heart's inner spike.
* @param compressHeight compress the heart along the y axis.
* @param rate the rate of the heart points. Will be converted to radians.
* @since 1.0.0
*/
public static void heart(double cut, double cutAngle, double depth, double compressHeight, double rate, ParticleDisplay display) {
for (double theta = 0; theta <= PII; theta += Math.PI / rate) {
double phi = theta / cut;
double cos = Math.cos(phi);
double sin = Math.sin(phi);
double omega = Math.pow(Math.abs(Math.sin(2 * cutAngle * phi)) + depth * Math.abs(Math.sin(cutAngle * phi)), 1 / compressHeight);
double y = omega * (sin + cos);
double z = omega * (cos - sin);
display.spawn(0, y, z);
}