forked from CryptoMorin/XSeries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleDisplay.java
More file actions
380 lines (354 loc) · 13.2 KB
/
ParticleDisplay.java
File metadata and controls
380 lines (354 loc) · 13.2 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
/*
* 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 org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.util.Vector;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Objects;
/**
* By default the particle xyz offsets and speed aren't 0, but
* everything will be 0 by default in this class.
* Particles are spawned to a location. So all the nearby players can see it.
* <p>
* The fields of this class are publicly accessible for ease of use.
* All the fields can be null but they can cause trouble when
* spawning them.
*
* @author Crypto Morin
* @version 2.1.0
* @see XParticle
*/
public class ParticleDisplay {
private static boolean ISFLAT = XParticle.getParticle("FOOTSTEP") == null;
public Particle particle;
public Location location;
public int count;
public double offsetx, offsety, offsetz;
public double extra;
public Vector rotation;
public Object data;
/**
* Make a new instance of particle display.
* The position of each particle will be randomized positively and negatively by the offset parameters on each axis.
*
* @param particle the particle to spawn.
* @param location the location to spawn the particle at.
* @param count the count of particles to spawn.
* @param offsetx the x offset.
* @param offsety the y offset.
* @param offsetz the z offset.
* @param extra in most cases extra is the speed of the particles.
*/
public ParticleDisplay(Particle particle, @Nullable Location location, int count, double offsetx, double offsety, double offsetz, double extra) {
this.particle = particle;
this.location = location;
this.count = count;
this.offsetx = offsetx;
this.offsety = offsety;
this.offsetz = offsetz;
this.extra = extra;
}
public ParticleDisplay(Particle particle, Location location, int count, double offsetx, double offsety, double offsetz) {
this(particle, location, count, offsetx, offsety, offsetz, 0);
}
public ParticleDisplay(Particle particle, Location location, int count) {
this(particle, location, count, 0, 0, 0);
}
public ParticleDisplay(Particle particle, Location location) {
this(particle, location, 0);
}
/**
* Builds a simple ParticleDisplay object with cross-version
* compatible {@link org.bukkit.Particle.DustOptions} properties.
*
* @param location the location of the display.
* @param r the red color RGB.
* @param g the green color RGB.
* @param b the blue color RGB.
* @param size the size of the dust.
* @return a redstone colored dust.
* @see #simple(Location, Particle)
* @since 1.0.0
*/
@Nonnull
public static ParticleDisplay paintDust(@Nullable Location location, int r, int g, int b, float size) {
ParticleDisplay dust = new ParticleDisplay(Particle.REDSTONE, location, 1, 0, 0, 0, 0);
dust.data = new float[]{r, g, b, size};
return dust;
}
/**
* Builds a simple ParticleDisplay object.
* An invocation of this method yields exactly the same result as the expression:
* <p>
* <blockquote>
* new ParticleDisplay(particle, location, 1, 0, 0, 0, 0);
* </blockquote>
*
* @param location the location of the display.
* @param particle the particle of the display.
* @return a simple ParticleDisplay.
* @since 1.0.0
*/
@Nonnull
public static ParticleDisplay simple(@Nullable Location location, @Nonnull Particle particle) {
Objects.requireNonNull(particle, "Cannot build ParticleDisplay with null particle");
return new ParticleDisplay(particle, location, 1, 0, 0, 0, 0);
}
/**
* Builds particle settings from a configuration section.
*
* @param location the location for this particle settings.
* @param config the config section for the settings.
* @return a parsed ParticleDisplay.
* @since 1.0.0
*/
@Nonnull
public static ParticleDisplay fromConfig(@Nullable Location location, @Nonnull ConfigurationSection config) {
Objects.requireNonNull(config, "Cannot parse ParticleDisplay from a null config section");
Particle particle = XParticle.getParticle(config.getString("particle"));
if (particle == null) particle = Particle.FLAME;
int count = config.getInt("count");
double extra = config.getDouble("extra");
double offsetx = 0, offsety = 0, offsetz = 0;
String offset = config.getString("offset");
if (offset != null) {
String[] offsets = StringUtils.split(offset, ',');
if (offsets.length > 0) {
offsetx = NumberUtils.toDouble(offsets[0]);
if (offsets.length > 1) {
offsety = NumberUtils.toDouble(offsets[1]);
if (offsets.length > 2) {
offsetz = NumberUtils.toDouble(offsets[2]);
}
}
}
}
double x = 0, y = 0, z = 0;
String rotation = config.getString("offset");
if (offset != null) {
String[] rotations = StringUtils.split(rotation, ',');
if (rotations.length > 0) {
x = NumberUtils.toDouble(rotations[0]);
if (rotations.length > 1) {
y = NumberUtils.toDouble(rotations[1]);
if (rotations.length > 2) {
z = NumberUtils.toDouble(rotations[2]);
}
}
}
}
Vector rotate = new Vector(x, y, z);
ParticleDisplay display = new ParticleDisplay(particle, location, count, offsetx, offsety, offsetz, extra);
display.rotation = rotate;
return display;
}
/**
* Adjusts the rotation settings to face the entitys direction.
* Only some of the shapes support this method.
*
* @param entity the entity to face.
* @return the same particle display.
* @see #rotate(Vector)
* @since 3.0.0
*/
@Nonnull
public ParticleDisplay faceEntity(@Nonnull Entity entity) {
Objects.requireNonNull(entity, "Cannot face null entity");
Location loc = entity.getLocation();
this.rotation = new Vector(Math.toRadians(loc.getPitch() + 90), Math.toRadians(-loc.getYaw()), 0);
return this;
}
/**
* Clones the location of this particle display and adds xyz.
*
* @param x the x to add to the location.
* @param y the y to add to the location.
* @param z the z to add to the location.
* @return the cloned location.
* @see #clone()
* @since 1.0.0
*/
@Nullable
public Location cloneLocation(double x, double y, double z) {
if (location == null) return null;
return location.clone().add(x, y, z);
}
/**
* Clones this particle settings and adds xyz to its location.
*
* @param x the x to add.
* @param y the y to add.
* @param z the z to add.
* @return the cloned ParticleDisplay.
* @see #clone()
* @since 1.0.0
*/
@Nonnull
public ParticleDisplay cloneWithLocation(double x, double y, double z) {
ParticleDisplay display = clone();
if (location == null) return display;
display.location.add(x, y, z);
return display;
}
/**
* Clones this particle settings.
*
* @return the cloned ParticleDisplay.
* @see #cloneWithLocation(double, double, double)
* @see #cloneLocation(double, double, double)
*/
@SuppressWarnings("MethodDoesntCallSuperMethod")
@Override
public ParticleDisplay clone() {
ParticleDisplay display = new ParticleDisplay(particle, (location == null ? null : location.clone()), count, offsetx, offsety, offsetz, extra);
if (rotation != null) display.rotation = rotation.clone();
display.data = data;
return display;
}
/**
* Rotates the particle position based on this vector.
*
* @param vector the vector to rotate from. The xyz values of this vectors must be radians.
* @see #rotate(double, double, double)
* @since 1.0.0
*/
public ParticleDisplay rotate(@Nonnull Vector vector) {
Objects.requireNonNull(vector, "Cannot rotate ParticleDisplay with null vector");
if (rotation == null) rotation = vector;
else rotation.add(vector);
return this;
}
/**
* Rotates the particle position based on the xyz radians.
* Rotations are only supported for some shapes in {@link XParticle}.
* Rotating some of them can result in weird shapes.
*
* @see #rotate(Vector)
* @since 3.0.0
*/
public ParticleDisplay rotate(double x, double y, double z) {
rotate(new Vector(x, y, z));
return this;
}
/**
* Adds xyz of the given vector to the cloned location before
* spawning particles.
*
* @param vector the vector to add.
* @since 1.0.0
*/
public void spawn(@Nonnull Vector vector) {
Objects.requireNonNull(vector, "Cannot add xyz of null vector to ParticleDisplay");
spawn(vector.getX(), vector.getY(), vector.getZ());
}
/**
* Set the xyz offset of the particle settings.
*
* @since 1.1.0
*/
public ParticleDisplay offset(double x, double y, double z) {
offsetx = x;
offsety = y;
offsetz = z;
return this;
}
/**
* When a particle is set to be directional it'll only
* spawn one particle and the xyz offset values are used for
* the direction of the particle.
* <p>
* Colored particles in 1.12 and below don't support this.
*
* @see #isDirectional()
* @since 1.1.0
*/
public ParticleDisplay directional() {
count = 0;
return this;
}
/**
* Check if this particle setting is a directional particle.
*
* @return true if the particle is directional, otherwise false.
* @see #directional()
* @since 2.1.0
*/
public boolean isDirectional() {
return count == 0;
}
/**
* Spawns the particle at the current location.
*
* @see #spawn(Location)
* @since 2.0.1
*/
public void spawn() {
spawn(this.location);
}
/**
* Adds xyz to the cloned loaction before spawning particle.
*
* @since 1.0.0
*/
public Location spawn(double x, double y, double z) {
Location loc;
if (rotation != null) {
Vector rotate = new Vector(x, y, z);
if (rotation.getX() != 0) XParticle.rotateAroundX(rotate, rotation.getX());
if (rotation.getY() != 0) XParticle.rotateAroundY(rotate, rotation.getY());
if (rotation.getZ() != 0) XParticle.rotateAroundZ(rotate, rotation.getZ());
loc = location.clone().add(rotate);
} else loc = location.clone().add(x, y, z);
spawn(loc);
return loc;
}
/**
* Displays the particle in the specified location.
* This method does not support rotations if used directly.
*
* @param loc the location to display the particle at.
* @see #spawn(double, double, double)
* @since 3.0.0
*/
public void spawn(Location loc) {
if (data != null) {
if (data instanceof float[]) {
float[] datas = (float[]) data;
if (ISFLAT) {
Particle.DustOptions dust = new Particle.DustOptions(Color.fromRGB((int) datas[0], (int) datas[1], (int) datas[2]), datas[3]);
loc.getWorld().spawnParticle(particle, loc, count, offsetx, offsety, offsetz, extra, dust);
} else {
loc.getWorld().spawnParticle(particle, loc, count, (int) datas[0], (int) datas[1], (int) datas[2], datas[3]);
}
}
} else {
loc.getWorld().spawnParticle(particle, loc, count, offsetx, offsety, offsetz, extra);
}
}
}