/
FakeImage.java
447 lines (404 loc) · 14.5 KB
/
FakeImage.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
package io.josemmo.bukkit.plugin.renderer;
import io.josemmo.bukkit.plugin.storage.ImageFile;
import io.josemmo.bukkit.plugin.utils.DirectionUtils;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.Rotation;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
public class FakeImage extends FakeEntity {
public static final int MAX_DIMENSION = 30; // In blocks
public static final int MAX_STEPS = 500; // For animated images
public static final int MIN_DELAY = 1; // Minimum step delay in 50ms intervals (50ms / 50ms)
public static final int MAX_DELAY = 50; // Maximum step delay in 50ms intervals (5000ms / 50ms)
public static final UUID UNKNOWN_PLAYER_ID = new UUID(0, 0);
private static boolean animateImages = false;
private final String filename;
private final Location location;
private final BlockFace face;
private final Rotation rotation;
private final int width;
private final int height;
private final Date placedAt;
private final OfflinePlayer placedBy;
private final BiFunction<Integer, Integer, Vector> getLocationVector;
private Runnable onLoadedListener = null;
// Generated values
private FakeItemFrame[] frames = null;
private int delay = 0; // Delay between steps in 50ms intervals, "0" for N/A
private int numOfSteps = -1; // Total number of animation steps
// Animation task attributes
private ScheduledFuture<?> task;
private final Set<Player> animatingPlayers = new HashSet<>();
private int currentStep = -1; // Current animation step
/**
* Enable image animation
*/
public static void enableAnimation() {
animateImages = true;
}
/**
* Is animation enabled
* @return Is animation enabled
*/
public static boolean isAnimationEnabled() {
return animateImages;
}
/**
* Get image rotation from player eyesight
* @param face Image block face
* @param location Player eye location
* @return Image rotation
*/
public static @NotNull Rotation getRotationFromPlayerEyesight(@NotNull BlockFace face, @NotNull Location location) {
// Images placed on N/S/E/W faces never have rotation
if (face != BlockFace.UP && face != BlockFace.DOWN) {
return Rotation.NONE;
}
// Top and down images depend on where player is looking
BlockFace eyeDirection = DirectionUtils.getCardinalDirection(location.getYaw());
switch (eyeDirection) {
case EAST:
return (face == BlockFace.DOWN) ? Rotation.CLOCKWISE_135 : Rotation.CLOCKWISE_45;
case SOUTH:
return Rotation.CLOCKWISE;
case WEST:
return (face == BlockFace.DOWN) ? Rotation.CLOCKWISE_45 : Rotation.CLOCKWISE_135;
default:
return Rotation.NONE;
}
}
/**
* Get proportional height
* @param sizeInPixels Image file dimension in pixels
* @param width Desired width in blocks
* @return Height in blocks (capped at <code>FakeImage.MAX_DIMENSION</code>)
*/
public static int getProportionalHeight(@NotNull Dimension sizeInPixels, int width) {
float imageRatio = (float) sizeInPixels.height / sizeInPixels.width;
int height = Math.round(width * imageRatio);
height = Math.min(height, MAX_DIMENSION);
return height;
}
/**
* Class constructor
* @param filename Image filename
* @param location Top-left corner where image will be placed
* @param face Block face
* @param rotation Image rotation
* @param width Width in blocks
* @param height Height in blocks
* @param placedAt Placed at
* @param placedBy Placed by
*/
public FakeImage(
@NotNull String filename,
@NotNull Location location,
@NotNull BlockFace face,
@NotNull Rotation rotation,
int width,
int height,
@Nullable Date placedAt,
@NotNull OfflinePlayer placedBy
) {
this.filename = filename;
this.location = location;
this.face = face;
this.rotation = rotation;
this.width = width;
this.height = height;
this.placedAt = placedAt;
this.placedBy = placedBy;
// Define function for retrieving item frame positional vector from <row,column> pair
if (face == BlockFace.SOUTH) {
getLocationVector = (col, row) -> new Vector(col, -row, 0);
} else if (face == BlockFace.NORTH) {
getLocationVector = (col, row) -> new Vector(-col, -row, 0);
} else if (face == BlockFace.EAST) {
getLocationVector = (col, row) -> new Vector(0, -row, -col);
} else if (face == BlockFace.WEST) {
getLocationVector = (col, row) -> new Vector(0, -row, col);
} else if (face == BlockFace.UP) {
if (rotation == Rotation.CLOCKWISE_45) {
getLocationVector = (col, row) -> new Vector(-row, 0, col);
} else if (rotation == Rotation.CLOCKWISE_135) {
getLocationVector = (col, row) -> new Vector(row, 0, -col);
} else if (rotation == Rotation.CLOCKWISE) {
getLocationVector = (col, row) -> new Vector(-col, 0, -row);
} else { // Rotation.NONE
getLocationVector = (col, row) -> new Vector(col, 0, row);
}
} else { // BlockFace.DOWN
if (rotation == Rotation.CLOCKWISE_45) {
getLocationVector = (col, row) -> new Vector(-row, 0, -col);
} else if (rotation == Rotation.CLOCKWISE_135) {
getLocationVector = (col, row) -> new Vector(row, 0, col);
} else if (rotation == Rotation.CLOCKWISE) {
getLocationVector = (col, row) -> new Vector(-col, 0, row);
} else { // Rotation.NONE
getLocationVector = (col, row) -> new Vector(col, 0, -row);
}
}
plugin.fine("Created FakeImage#(" + location + "," + face + ") from ImageFile#(" + filename + ")");
}
/**
* Get image filename
* @return Image filename
*/
public @NotNull String getFilename() {
return filename;
}
/**
* Get image file instance
* @return Image file instance or NULL if not found
*/
public @Nullable ImageFile getFile() {
return plugin.getStorage().get(filename);
}
/**
* Get location instance
* @return Location instance
*/
public @NotNull Location getLocation() {
return location;
}
/**
* Get image block face
* @return Image block face
*/
public @NotNull BlockFace getBlockFace() {
return face;
}
/**
* Get image rotation
* @return Image rotation
*/
public @NotNull Rotation getRotation() {
return rotation;
}
/**
* Get image width in blocks
* @return Image width
*/
public int getWidth() {
return width;
}
/**
* Get image height in blocks
* @return Image height
*/
public int getHeight() {
return height;
}
/**
* Get placed at date
* @return Placed at date
*/
public @Nullable Date getPlacedAt() {
return placedAt;
}
/**
* Get placed by player
* @return Placed by player instance
*/
public @NotNull OfflinePlayer getPlacedBy() {
return placedBy;
}
/**
* Get image delay
* @return Image delay in 50ms intervals
*/
public int getDelay() {
return delay;
}
/**
* Get the world area IDs where this image is located
* @return Array of world area IDs
*/
public @NotNull WorldAreaId[] getWorldAreaIds() {
WorldAreaId topLeft = WorldAreaId.fromLocation(location);
WorldAreaId bottomRight = WorldAreaId.fromLocation(
location.clone().add(getLocationVector.apply(width-1, height-1))
);
if (topLeft.equals(bottomRight)) {
return new WorldAreaId[]{topLeft};
}
return new WorldAreaId[]{topLeft, bottomRight};
}
/**
* Verify whether a point is contained in the image plane
* @param location Location instance (only for coordinates)
* @param face Block face
* @return TRUE for contained, FALSE otherwise
*/
public boolean contains(@NotNull Location location, @NotNull BlockFace face) {
// Is point facing the same plane as the image?
if (face != this.face) {
return false;
}
// Get sorted plane edges
Location topLeft = this.location;
Location bottomRight = this.location.clone().add(getLocationVector.apply(width-1, height-1));
int[] x = new int[]{topLeft.getBlockX(), bottomRight.getBlockX()};
int[] y = new int[]{topLeft.getBlockY(), bottomRight.getBlockY()};
int[] z = new int[]{topLeft.getBlockZ(), bottomRight.getBlockZ()};
Arrays.sort(x);
Arrays.sort(y);
Arrays.sort(z);
// Is point located inside the plane limits?
int[] point = new int[] {location.getBlockX(), location.getBlockY(), location.getBlockZ()};
if (point[0] < x[0] || point[0] > x[1]) return false;
if (point[1] < y[0] || point[1] > y[1]) return false;
if (point[2] < z[0] || point[2] > z[1]) return false;
return true;
}
/**
* Set on loaded listener
* <p>
* Defines a single-use listener that gets called when the fake image finishes loading,
* at which point the listener is automatically unregistered.
* @param onLoadedListener Listener
*/
public void setOnLoadedListener(@NotNull Runnable onLoadedListener) {
this.onLoadedListener = onLoadedListener;
}
/**
* Load generated instance attributes
*/
private void load() {
ImageFile file = getFile();
FakeMapsContainer container;
if (file == null) {
container = FakeMap.getErrorMatrix(width, height);
plugin.warning("File \"" + filename + "\" does not exist");
} else {
container = file.getMapsAndSubscribe(this);
}
// Extract data from container
FakeMap[][][] maps = container.getFakeMaps();
numOfSteps = maps[0][0].length;
delay = container.getDelay();
// Generate frames
frames = new FakeItemFrame[width*height];
for (int col=0; col<width; col++) {
for (int row=0; row<height; row++) {
Location frameLocation = location.clone().add(getLocationVector.apply(col, row));
frames[height*col+row] = new FakeItemFrame(frameLocation, face, rotation, maps[col][row]);
}
}
// Notify listener
if (onLoadedListener != null) {
onLoadedListener.run();
onLoadedListener = null;
}
}
/**
* Spawn image for a player
* @param player Player instance
*/
public void spawn(@NotNull Player player) {
tryToRunAsyncTask(() -> {
synchronized (this) {
waitForProtocolLib();
// Load frames and other generated values from disk if not already loaded
if (frames == null) {
load();
}
// Spawn frames in player's client
for (FakeItemFrame frame : frames) {
frame.spawn(player);
frame.render(player, 0);
}
// Add player to animation task
if (animateImages && numOfSteps > 1) {
animatingPlayers.add(player);
if (task == null) {
task = plugin.getScheduler().scheduleAtFixedRate(
this::nextStep,
0,
delay*50L,
TimeUnit.MILLISECONDS
);
plugin.fine("Spawned animation task for FakeImage#(" + location + "," + face + ")");
}
}
}
});
}
/**
* Destroy image for a player
* @param player Player instance
*/
public void destroy(@NotNull Player player) {
final FakeItemFrame[] framesRef = frames; // In case renderer FakeImage#invalidate() gets called
tryToRunAsyncTask(() -> {
synchronized (this) {
// Unregister player from animation task
animatingPlayers.remove(player);
if (animatingPlayers.isEmpty()) {
destroyAnimationTask();
}
// Send packets to destroy item frames
if (framesRef != null) {
for (FakeItemFrame frame : framesRef) {
frame.destroy(player);
}
}
}
});
}
/**
* Invalidate cache
* <p>
* Removes all item frames associated with this image.
*/
public void invalidate() {
tryToRunAsyncTask(() -> {
synchronized (this) {
// Clear animation task (if exists)
destroyAnimationTask();
animatingPlayers.clear();
// Free array of fake item frames
frames = null;
// Notify invalidation to source ImageFile
ImageFile file = getFile();
if (file != null) {
file.unsubscribe(this);
}
plugin.fine("Invalidated FakeImage#(" + location + "," + face + ")");
}
});
}
/**
* Send next animation step to all registered players
*/
private void nextStep() {
currentStep = (currentStep + 1) % numOfSteps;
for (Player player : animatingPlayers) {
for (FakeItemFrame frame : frames) {
frame.render(player, currentStep);
}
}
}
/**
* Destroy animation task
*/
private void destroyAnimationTask() {
if (task == null) {
// No active animation task
return;
}
task.cancel(true);
task = null;
currentStep = -1;
plugin.fine("Destroyed animation task for FakeImage#(" + location + "," + face + ")");
}
}