forked from CryptoMorin/XSeries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XParticle.java
2500 lines (2252 loc) · 105 KB
/
XParticle.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2021 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.particles;
import com.google.common.base.Enums;
import org.bukkit.Color;
import org.bukkit.*;
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.NumberConversions;
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> - The most unique particle animation, 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.<br>
* There are some shapes such as the magic circles, illuminati and the explosion method
* that mainly focus on using the other methods to create a new shape.
* <p>
* 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 Yes, Chaos theory.<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.<br>
* All the parameters and return types are not null.
* <p>
* It's recommended to use low particle counts.
* In most cases, increasing 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.
* Rates for methods act in two ways. They're either for straight lines like the polygon
* method which lower rate means more points (usually 0.1 is used) and shapes that are curved such as
* the circle method, which higher rate means more points (these types of rates usually start from 30).<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 powerful PC.
* <p>
* You can test your 2D shapes at <a href="https://www.desmos.com/calculator">Desmos</a><br>
* Stuff you can do with with
* <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html">Java {@link Math}</a><br>
* Getting started with <a href="https://www.spigotmc.org/wiki/vector-programming-for-beginners/">Vectors</a><br>
* Extra stuff if you want to read more: https://www.spigotmc.org/threads/418399/<br>
* Particles: https://minecraft.gamepedia.com/Particles<br>
*
* @author Crypto Morin
* @version 5.0.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 = 180 degrees
* 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;
private XParticle() {}
/**
* 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) / 10f;
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.
*
* @param radius the circle radius.
* @param rate the rate of cirlce points/particles.
*
* @see #sphere(double, double, ParticleDisplay)
* @see #circle(double, double, double, double, double, ParticleDisplay)
* @since 1.0.0
*/
public static void circle(double radius, double rate, ParticleDisplay display) {
circle(radius, radius, 1, rate, 0, display);
}
/**
* Spawns a circle.
* Most common shapes that can be built:
* <pre>
* The simplest shape, a circle
* circle(3, 3, 1, 30, 0, display);
*
* An ellipse only has a different radius for one of its waves.
* circle(3, 4, 1, 30, 0, display);
* </pre>
* <p>
* Tutorial: https://www.spigotmc.org/threads/111238/
* Uses its own unique directional pattern.
*
* @param radius the first radius of the circle.
* @param radius2 the second radius of the circle.
* @param extension the extension of the circle waves.
* @param rate the rate of the circle points.
* @param limit the limit of the circle. Usually from 0 to PII.
* If you choose 0, it'll be a full circle {@link #PII}
* If you choose -1, it'll do a full loop based on the extension.
*
* @see #illuminati(double, double, ParticleDisplay)
* @see #eye(double, double, double, double, ParticleDisplay)
*/
public static void circle(double radius, double radius2, double extension, double rate, double limit, 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 / Math.abs(rate);
// If no limit is specified do a full loop.
if (limit == 0) limit = PII;
else if (limit == -1) limit = PII / Math.abs(extension);
// If the extension changes (isn't 1), the wave might not do a full
// loop anymore. So by simply dividing PI from the extension you can get the limit for a full loop.
// By full loop it means: sin(bx) {0 < x < PI} if b (the extension) is equal to 1
// Using period => T = 2PI/|b|
for (double theta = 0; theta <= limit; 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(extension * theta);
double z = radius2 * Math.sin(extension * 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(extension * phi);
double directionZ = Math.sin(extension * phi);
display.offset(directionX, display.getOffset().getY(), directionZ);
}
display.spawn(x, 0, z);
}
}
/**
* Spawns a diamond-shaped rhombus.
*
* @param radiusRate the radius of the diamond. Lower means longer radius.
* @param rate the rate of the diamond points.
* @param height the height of the diamond.
*
* @since 4.0.0
*/
public static void diamond(double radiusRate, double rate, double height, ParticleDisplay display) {
double count = 0;
for (double y = 0; y < height * 2; y += rate) {
// We're going to increase our x particles as we get closer to the center
// and decrease as we move away. If the radius is equal to rate it'll form a rotated square.
if (y < height) count += radiusRate;
else count -= radiusRate;
// Now we can make an arrow or a right triangle if let x be equal to 0
// But we want both sides to have particle.
for (double x = -count; x < count; x += rate) display.spawn(x, y, 0);
}
}
/**
* 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() {
final double rateDiv = Math.PI / rate;
final double radiusDiv = Math.PI / radiusRate;
final Vector dir = display.getLocation().getDirection().normalize().multiply(extend);
double dynamicRadius = 0;
@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.getLocation().add(dir);
}
}.runTaskTimerAsynchronously(plugin, 0L, 1L);
}
/**
* Spawns the given shape(s) in the runnable in a circular form.
* The distance between the shapes are evenly separated.
*
* @param count the count of the shapes.
* @param radius the radius of the circular form.
* @param runnable the shape(s) to display.
*
* @since 4.0.0
*/
public static void flower(int count, double radius, ParticleDisplay display, Runnable runnable) {
for (double theta = 0; theta < PII; theta += PII / count) {
double x = radius * Math.cos(theta);
double z = radius * Math.sin(theta);
display.getLocation().add(x, 0, z);
runnable.run();
display.getLocation().subtract(x, 0, z);
}
}
/**
* Spawns a filled circle using circles.
*
* @param radius the radius of the circle.
* @param rate the rate of the circle points.
* @param radiusRate the radius change of the circle to fill it.
*
* @see #circle(double, double, ParticleDisplay)
* @since 4.0.0
*/
public static void filledCircle(double radius, double rate, double radiusRate, ParticleDisplay display) {
double dynamicRate = 0;
for (double i = 0.1; i < radius; i += radiusRate) {
if (i > radius) i = radius;
dynamicRate += rate / (radius / radiusRate);
circle(i, dynamicRate, display);
}
}
/**
* Spawns a double pendulum with chaotic movement.
* Note that if this runs for too long it'll stop working due to
* the limit of doubles resulting in a {@link Double#NaN}
* <p>
* <a href="https://en.wikipedia.org/wiki/Double_pendulum">Double pendulum</a>
* is a way to show <a href="https://en.wikipedia.org/wiki/Chaos_theory">Chaos motion</a>.
* The particles display are showing the path where the second
* pendulum is going from.
* <p>
* Changing the mass or length to a lower value can make the
* shape stop producing new paths since it reaches the doubles limit.
* Source: https://www.myphysicslab.com/pendulum/double-pendulum-en.html
*
* @param plugin the timer handler.
* @param radius the radius of the pendulum. Yes this doesn't depend on length since the length needs to be a really
* high value and this won't work with Minecraft's xyz.
* @param gravity the gravity of the enviroment. Recommended is -1 positive numbers will mean gravity towards space.
* @param length the length of the first pendulum. Recommended is 200
* @param length2 the length of the second pendulum. Recommended is 200
* @param mass1 the mass of the first pendulum. Recommended is 50
* @param mass2 the mass of the second pendulum. Recommended is 50
* @param dimension3 if it should enter 3D mode.
* @param speed the speed of the animation.
*
* @return the animation handler.
* @since 4.0.0
*/
public static BukkitTask chaoticDoublePendulum(JavaPlugin plugin, double radius, double gravity, double length, double length2,
double mass1, double mass2,
boolean dimension3, int speed, ParticleDisplay display) {
// If you want the particles to stay. But it's gonna lag a lot.
//Map<Vector, Vector> locs = new HashMap<>();
return new BukkitRunnable() {
final Vector rotation = new Vector(Math.PI / 33, Math.PI / 44, Math.PI / 55);
double theta = Math.PI / 2;
double theta2 = Math.PI / 2;
double thetaPrime = 0;
double thetaPrime2 = 0;
@Override
public void run() {
int repeat = speed;
while (repeat-- != 0) {
if (dimension3) display.rotate(rotation);
double totalMass = mass1 + mass2;
double totalMassDouble = 2 * totalMass;
double deltaTheta = theta - theta2;
double lenLunar = (totalMassDouble - mass2 * Math.cos(2 * theta - 2 * theta2));
double deltaCosTheta = Math.cos(deltaTheta);
double deltaSinTheta = Math.sin(deltaTheta);
double phi = thetaPrime * thetaPrime * length;
double phi2 = thetaPrime2 * thetaPrime2 * length2;
// Don't expect me to explain these... Read the website.
double num1 = -gravity * totalMassDouble * Math.sin(theta);
double num2 = -mass2 * gravity * Math.sin(theta - 2 * theta2);
double num3 = -2 * deltaSinTheta * mass2;
double num4 = phi2 + phi * deltaCosTheta;
double len = length * lenLunar;
double thetaDoublePrime = (num1 + num2 + num3 * num4) / len;
num1 = 2 * deltaSinTheta;
num2 = phi * totalMass;
num3 = gravity * totalMass * Math.cos(theta);
num4 = phi2 * mass2 * deltaCosTheta;
len = length2 * lenLunar;
double thetaDoublePrime2 = (num1 * (num2 + num3 + num4)) / len;
thetaPrime += thetaDoublePrime;
thetaPrime2 += thetaDoublePrime2;
theta += thetaPrime;
theta2 += thetaPrime2;
double x = radius * Math.sin(theta);
double y = radius * Math.cos(theta);
double x2 = x + radius * Math.sin(theta2);
double y2 = y + radius * Math.cos(theta2);
display.spawn(x2, y2, 0);
// locs.forEach((v, v2) -> {
// ParticleDisplay dis = display.clone();
// dis.rotation = v2;
// dis.spawn(v.getX(), v.getY(), v.getZ());
// });
// locs.put(new Vector(x2, y2, 0), display.rotation.clone());
}
}
}.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() {
final double radiusDiv = Math.PI / radiusRate;
final Vector dir = display.getLocation().getDirection().normalize().multiply(distance);
double dynamicRadius = radius;
@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.getLocation().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.
*
* @see #circle(double, double, ParticleDisplay)
* @since 2.0.0
*/
public static void ellipse(double start, double end, double rate, double radius, double otherRadius, ParticleDisplay display) {
// The only difference between circles and ellipses are that
// ellipses use a different radius for one of their axis.
for (double theta = start; theta <= end; theta += rate) {
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.
* @param time the amount of ticks to keep the blackhole.
*
* @since 3.0.0
*/
public static BukkitTask blackhole(JavaPlugin plugin, int points, double radius, double rate, int mode, int time, ParticleDisplay display) {
display.directional();
display.extra = 0.1;
return new BukkitRunnable() {
final double rateDiv = Math.PI / rate;
int timer = time;
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;
if (--timer <= 0) cancel();
}
}.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) {
int[][] rainbow = {
{128, 0, 128}, // Violet
{75, 0, 130}, // Indigo
{0, 0, 255}, // Blue
{0, 255, 0}, // Green
{255, 255, 0}, // Yellow
{255, 140, 0}, // Orange
{255, 0, 0} // Red
};
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[i];
display = ParticleDisplay.colored(display.getLocation(), rgb[0], rgb[1], rgb[2], 1);
// 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;
double end = Math.toRadians(325);
// 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 <= end; 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) {
filledCircle(radius, rate, 3, display);
filledCircle(radius, rate, 3, display.cloneWithLocation(0, height, 0));
for (double y = 0; y < height; y += 0.1) {
circle(radius, rate, display.cloneWithLocation(0, y, 0));
}
}
/**
* 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) ParticleDisplay.rotateAround(vector, ParticleDisplay.Axis.X, x);
if (offsety != 0) ParticleDisplay.rotateAround(vector, ParticleDisplay.Axis.Y, y);
if (offsetz != 0) ParticleDisplay.rotateAround(vector, ParticleDisplay.Axis.Z, z);
for (ParticleDisplay display : displays) display.getLocation().add(vector);
runnable.run();
for (ParticleDisplay display : displays) display.getLocation().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.getLocation().add(x, y, z);
runnable.run();
for (ParticleDisplay display : displays) display.getLocation().subtract(x, y, z);
if (opposite) {
if (multiplier <= 0) opposite = false;
} else {
if (multiplier >= endRate) opposite = true;
}
}
}.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) {