-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathMapUtils.java
More file actions
497 lines (451 loc) · 21.3 KB
/
MapUtils.java
File metadata and controls
497 lines (451 loc) · 21.3 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
/*
* This file is part of ImageFrame.
*
* Copyright (C) 2025. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2025. Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.loohp.imageframe.utils;
import com.google.common.collect.Iterators;
import com.loohp.imageframe.ImageFrame;
import com.loohp.imageframe.nms.NMS;
import com.loohp.imageframe.objectholders.CombinedMapItemHandler;
import com.loohp.imageframe.objectholders.CombinedMapItemInfo;
import com.loohp.imageframe.objectholders.DitheringType;
import com.loohp.imageframe.objectholders.ImageMap;
import com.loohp.imageframe.objectholders.ImageMapHitTargetResult;
import com.loohp.imageframe.objectholders.IntPosition;
import com.loohp.imageframe.objectholders.MapPacketSentCallback;
import com.loohp.imageframe.objectholders.MutablePair;
import com.loohp.platformscheduler.Scheduler;
import net.kyori.adventure.key.Key;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Rotation;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.map.MapCursor;
import org.bukkit.map.MapCursorCollection;
import org.bukkit.map.MapPalette;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.RayTraceResult;
import org.bukkit.util.Vector;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Future;
public class MapUtils {
@SuppressWarnings("deprecation")
public static final byte PALETTE_TRANSPARENT = MapPalette.TRANSPARENT;
public static final byte[] PALETTE_GRAYSCALE = generateGrayScale();
public static final int MAP_WIDTH = 128;
public static final String GIF_CONTENT_TYPE = "image/gif";
public static final List<BlockFace> CARTESIAN_BLOCK_FACES = Collections.unmodifiableList(Arrays.asList(BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST, BlockFace.UP, BlockFace.DOWN));
@SuppressWarnings("removal")
private static byte[] generateGrayScale() {
Set<Byte> bytes = new TreeSet<>();
for (int i = 0; i < 256; i++) {
bytes.add(MapPalette.matchColor(i, i, i));
}
byte[] result = new byte[bytes.size()];
int i = 0;
for (byte b : bytes) {
result[i++] = b;
}
return result;
}
public static World getMainWorld() {
return Bukkit.getWorlds().get(0);
}
public static void sendImageMap(MapView mapView, Collection<? extends Player> players) {
sendImageMap(mapView.getId(), mapView, -1, players, null);
}
public static void sendImageMap(MapView mapView, Collection<? extends Player> players, boolean now) {
sendImageMap(mapView.getId(), mapView, -1, players, null, now);
}
public static void sendImageMap(MapView mapView, Collection<? extends Player> players, MapPacketSentCallback completionCallback) {
sendImageMap(mapView.getId(), mapView, -1, players);
}
public static void sendImageMap(MapView mapView, Collection<? extends Player> players, MapPacketSentCallback completionCallback, boolean now) {
sendImageMap(mapView.getId(), mapView, -1, players, now);
}
public static void sendImageMap(int mapId, MapView mapView, int currentTick, Collection<? extends Player> players) {
sendImageMap(mapId, mapView, currentTick, players, null);
}
public static void sendImageMap(int mapId, MapView mapView, int currentTick, Collection<? extends Player> players, boolean now) {
sendImageMap(mapId, mapView, currentTick, players, null, now);
}
public static void sendImageMap(int mapId, MapView mapView, int currentTick, Collection<? extends Player> players, MapPacketSentCallback completionCallback) {
sendImageMap(mapId, mapView, currentTick, players, completionCallback, false);
}
public static void sendImageMap(int mapId, MapView mapView, int currentTick, Collection<? extends Player> players, MapPacketSentCallback completionCallback, boolean now) {
List<MapRenderer> renderers = mapView.getRenderers();
if (renderers.isEmpty()) {
throw new IllegalArgumentException("mapView is not from an image map");
}
Optional<MapRenderer> optMapRenderer = renderers.stream().filter(each -> each instanceof ImageMap.ImageMapRenderer).findFirst();
if (!optMapRenderer.isPresent()) {
throw new IllegalArgumentException("mapView is not from an image map");
}
ImageMap.ImageMapRenderer imageMapManager = (ImageMap.ImageMapRenderer) optMapRenderer.get();
for (Player player : players) {
MutablePair<byte[], Collection<MapCursor>> renderData = currentTick < 0 ? imageMapManager.renderPacketData(mapView, player) : imageMapManager.renderPacketData(mapView, currentTick, player);
byte[] colors = renderData.getFirst();
Collection<MapCursor> cursors = renderData.getSecond();
Object packet = NMS.getInstance().createMapPacket(mapId, colors, cursors);
if (now) {
NMS.getInstance().sendPacket(player, packet);
if (completionCallback != null) {
completionCallback.accept(player, mapId, true);
}
} else {
ImageFrame.rateLimitedPacketSendingManager.queue(player, packet, completionCallback == null ? null : (p, r) -> completionCallback.accept(p, mapId, r));
}
}
}
public static byte[] toMapPaletteBytes(BufferedImage image, DitheringType ditheringType) {
return ditheringType == null ? DitheringType.NEAREST_COLOR.applyDithering(image) : ditheringType.applyDithering(image);
}
public static BufferedImage resize(BufferedImage source, int width, int height) {
return resize(source, width, height, MAP_WIDTH);
}
public static BufferedImage resize(BufferedImage source, int width, int height, int mapWidth) {
BufferedImage image = new BufferedImage(width * mapWidth, height * mapWidth, BufferedImage.TYPE_INT_ARGB);
Graphics g = image.createGraphics();
double wRatio = (double) image.getWidth() / (double) source.getWidth();
double hRatio = (double) image.getHeight() / (double) source.getHeight();
if (wRatio < hRatio) {
int h = (int) Math.round(source.getHeight() * wRatio);
g.drawImage(source, 0, (image.getHeight() - h) / 2, image.getWidth(), h, null);
} else {
int w = (int) Math.round(source.getWidth() * hRatio);
g.drawImage(source, (image.getWidth() - w) / 2, 0, w, image.getHeight(), null);
}
g.dispose();
return image;
}
public static BufferedImage getSubImage(BufferedImage source, int x, int y) {
return getSubImage(source, x, y, MAP_WIDTH);
}
@SuppressWarnings("SuspiciousNameCombination")
public static BufferedImage getSubImage(BufferedImage source, int x, int y, int mapWidth) {
int startX = x * mapWidth;
int startY = y * mapWidth;
int width = source.getWidth() - startX;
int height = source.getHeight() - startY;
if (width < mapWidth || height < mapWidth) {
BufferedImage image = new BufferedImage(mapWidth, mapWidth, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.drawImage(source.getSubimage(startX, startY, width, height), 0, 0, null);
g.dispose();
return image;
}
return source.getSubimage(startX, startY, mapWidth, mapWidth);
}
public static boolean areImagesEqual(BufferedImage img1, BufferedImage img2) {
if (img1 == img2) {
return true;
}
if (img1 == null || img2 == null) {
return false;
}
if (img1.getWidth() != img2.getWidth() || img1.getHeight() != img2.getHeight()) {
return false;
}
for (int x = 0; x < img1.getWidth(); x++) {
for (int y = 0; y < img1.getHeight(); y++) {
if (img1.getRGB(x, y) != img2.getRGB(x, y)) {
return false;
}
}
}
return true;
}
public static MapView getItemMapView(ItemStack itemStack) {
if (itemStack == null || itemStack.getType().equals(Material.AIR) || !itemStack.hasItemMeta()) {
return null;
}
ItemMeta meta = itemStack.getItemMeta();
if (!(meta instanceof MapMeta)) {
return null;
}
MapMeta mapMeta = (MapMeta) meta;
MapView mapView;
try {
if (!mapMeta.hasMapView()) {
return null;
}
mapView = mapMeta.getMapView();
} catch (IllegalStateException ignored) {
return null;
}
if (mapView == null) {
return null;
}
if (mapMeta.hasMapId()) {
tryDeleteBlankDataFile(getMainWorld(), mapMeta.getMapId());
}
return mapView;
}
public static MapView getPlayerMapView(Player player) {
return getItemMapView(player.getEquipment().getItemInMainHand());
}
public static int removeEmptyMaps(Player player, int count, boolean checkGameMode) {
if (checkGameMode) {
GameMode gameMode = player.getGameMode();
if (gameMode.equals(GameMode.CREATIVE) || gameMode.equals(GameMode.SPECTATOR)) {
return 0;
}
}
if (player.getInventory().containsAtLeast(new ItemStack(Material.MAP), count)) {
player.getInventory().removeItem(new ItemStack(Material.MAP, count));
return count;
}
return -1;
}
public static void setColors(MapView mapView, byte[] colors) {
NMS.getInstance().setColors(mapView, colors);
}
public static MapCursorCollection toMapCursorCollection(Collection<MapCursor> mapCursors) {
MapCursorCollection mapCursorCollection = new MapCursorCollection();
for (MapCursor mapCursor : mapCursors) {
mapCursorCollection.addCursor(mapCursor);
}
return mapCursorCollection;
}
public static ImageMapHitTargetResult rayTraceTargetImageMap(Player player, double maxDistance) {
return rayTraceTargetImageMap((LivingEntity) player, maxDistance);
}
public static ImageMapHitTargetResult rayTraceTargetImageMap(LivingEntity livingEntity, double maxDistance) {
Location location = livingEntity.getEyeLocation();
return rayTraceTargetImageMap(location, location.getDirection(), maxDistance);
}
public static ImageMapHitTargetResult rayTraceTargetImageMap(Location start, Vector direction, double maxDistance) {
RayTraceResult rayTraceResult = rayTraceItemFrame(start, direction, maxDistance);
if (rayTraceResult == null) {
return null;
}
ItemFrame itemFrame = (ItemFrame) rayTraceResult.getHitEntity();
if (itemFrame == null) {
return null;
}
Vector hitPosition = rayTraceResult.getHitPosition();
ItemStack itemStack = itemFrame.getItem();
MapView mapView = getItemMapView(itemStack);
if (mapView == null) {
return null;
}
ImageMap imageMap = ImageFrame.imageMapManager.getFromMapView(mapView);
if (imageMap == null || !imageMap.isValid()) {
return null;
}
IntPosition target = getTargetPixelOnItemFrame(itemFrame.getLocation().toVector(), itemFrame.getFacing().getDirection(), hitPosition, itemFrame.getRotation());
IntPosition localTarget = new IntPosition((target.getX() + MAP_WIDTH) / 2, (target.getY() + MAP_WIDTH) / 2);
int mapViewIndex = imageMap.getMapViews().indexOf(mapView);
int mapViewX = mapViewIndex % imageMap.getWidth();
int mapViewY = mapViewIndex / imageMap.getWidth();
IntPosition globalTarget = new IntPosition(localTarget.getX() + (mapViewX * MAP_WIDTH), localTarget.getY() + (mapViewY * MAP_WIDTH));
return new ImageMapHitTargetResult(itemFrame, imageMap, localTarget, globalTarget, hitPosition);
}
public static RayTraceResult rayTraceItemFrame(Location start, Vector direction, double maxDistance) {
if (maxDistance < 0.0) {
return null;
} else {
Vector startPos = start.toVector();
Vector dir = direction.clone().normalize().multiply(maxDistance);
BoundingBox aabb = BoundingBox.of(startPos, startPos).expandDirectional(dir).expand(0.125);
Collection<Entity> entities = start.getWorld().getNearbyEntities(aabb, e -> e instanceof ItemFrame);
Entity nearestHitEntity = null;
RayTraceResult nearestHitResult = null;
double nearestDistanceSq = Double.MAX_VALUE;
for (Entity entity : entities) {
ItemFrame itemFrame = (ItemFrame) entity;
Vector facing = itemFrame.getFacing().getDirection().normalize();
Vector opposite = facing.clone().multiply(-1);
BoundingBox boundingBox = entity.getBoundingBox();
for (BlockFace blockFace : CARTESIAN_BLOCK_FACES) {
Vector expansion = blockFace.getDirection().normalize();
if (!expansion.equals(facing) && !expansion.equals(opposite)) {
boundingBox.expandDirectional(expansion.multiply(0.125));
}
}
RayTraceResult hitResult = boundingBox.rayTrace(startPos, direction, maxDistance);
if (hitResult != null) {
double distanceSq = startPos.distanceSquared(hitResult.getHitPosition());
if (distanceSq < nearestDistanceSq) {
nearestHitEntity = entity;
nearestHitResult = hitResult;
nearestDistanceSq = distanceSq;
}
}
}
return nearestHitEntity == null ? null : new RayTraceResult(nearestHitResult.getHitPosition(), nearestHitEntity, nearestHitResult.getHitBlockFace());
}
}
public static IntPosition getTargetPixelOnItemFrame(Vector center, Vector facing, Vector position, Rotation rotation) {
Vector offset = position.clone().subtract(center);
if (facing.getBlockX() != 0) {
offset.setX(0);
} else if (facing.getBlockY() != 0) {
offset.setY(0);
} else {
offset.setZ(0);
}
offset.rotateAroundAxis(facing, Math.toRadians(rotation.ordinal() * 90));
int x;
int y;
if (facing.getBlockX() != 0) {
x = (int) Math.round(offset.getZ() * 256);
if (facing.getBlockX() > 0) {
x = -x;
}
y = (int) Math.round(offset.getY() * -256);
} else if (facing.getBlockY() != 0) {
x = (int) Math.round(offset.getX() * 256);
y = (int) Math.round(offset.getZ() * 256);
if (facing.getBlockY() < 0) {
y = -y;
}
} else {
x = (int) Math.round(offset.getX() * -256);
if (facing.getBlockZ() > 0) {
x = -x;
}
y = (int) Math.round(offset.getY() * -256);
}
return new IntPosition(Math.min(Math.max(x, Byte.MIN_VALUE), Byte.MAX_VALUE), Math.min(Math.max(y, Byte.MIN_VALUE), Byte.MAX_VALUE));
}
public static Object toNMSMapIcon(MapCursor mapCursor) {
return NMS.getInstance().toNMSMapIcon(mapCursor);
}
public static Object toNMSMapIconType(MapCursor.Type type) {
return NMS.getInstance().toNMSMapIconType(type);
}
public static boolean isRenderOnFrame(MapCursor.Type type) {
return NMS.getInstance().isRenderOnFrame(type);
}
@SuppressWarnings("DataFlowIssue")
public static Future<MapView> createMap(World world) {
return FutureUtils.callSyncMethod(() -> {
int worldNextId = NMS.getInstance().getNextAvailableMapId(world);
int ifNextId = ImageFrame.imageMapManager.getMaps().stream().flatMap(i -> i.getMapIds().stream()).mapToInt(i -> i).max().orElse(-1) + 1;
int worldDataNextId;
File worldDataFolder = new File(world.getWorldFolder(), "data");
if (worldDataFolder.exists() && worldDataFolder.isDirectory()) {
worldDataNextId = Arrays.stream(worldDataFolder.listFiles())
.map(f -> f.getName())
.filter(s -> s.startsWith("map_"))
.map(s -> {
try {
return Integer.parseInt(s.substring("map_".length(), s.indexOf(".")));
} catch (NumberFormatException e) {
return null;
}
})
.filter(s -> s != null)
.mapToInt(i -> i)
.max().orElse(-1) + 1;
} else {
worldDataNextId = 0;
}
int id = Math.max(worldNextId, Math.max(ifNextId, worldDataNextId));
tryDeleteBlankDataFile(world, id);
return NMS.getInstance().getMapOrCreateMissing(world, id);
});
}
public static Future<MapView> getMap(int id) {
return FutureUtils.callSyncMethod(() -> Bukkit.getMap(id));
}
public static Future<MapView> getMapOrCreateMissing(World world, int id) {
return FutureUtils.callSyncMethod(() -> {
tryDeleteBlankDataFile(world, id);
return NMS.getInstance().getMapOrCreateMissing(world, id);
});
}
public static MutablePair<byte[], List<MapCursor>> bukkitRenderMap(MapView mapView, Player player) {
return NMS.getInstance().bukkitRenderMap(mapView, player);
}
public static File getWorldDataFolder(World world) {
File worldFolder = world.getWorldFolder();
World.Environment environment = world.getEnvironment();
if (environment.equals(World.Environment.NORMAL)) {
return new File(worldFolder, "data");
} else if (environment.equals(World.Environment.NETHER)) {
return new File(worldFolder, "DIM-1/data");
} else if (environment.equals(World.Environment.THE_END)) {
return new File(worldFolder, "DIM1/data");
} else if (environment.equals(World.Environment.CUSTOM)) {
Key namespacedKey = NMS.getInstance().getWorldNamespacedKey(world);
return new File(world.getWorldFolder(), namespacedKey.value() + "/data");
} else {
throw new UnsupportedOperationException("Dimension type " + environment + " of world " + world.getName() + " not supported yet!");
}
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static void tryDeleteBlankDataFile(World world, int mapId) {
if (ImageFrame.tryDeleteBlankMapFiles) {
File dataFolder = getWorldDataFolder(world);
if (dataFolder.exists() && dataFolder.isDirectory()) {
File mapFile = new File(dataFolder, "map_" + mapId + ".dat");
if (mapFile.exists() && mapFile.length() <= 0) {
mapFile.delete();
}
}
}
}
public static Future<Boolean> canViewMap(Player player, MapView mapView) {
return Scheduler.callSyncMethod(ImageFrame.plugin, () -> {
if (NMS.getInstance().getViewers(mapView).contains(player)) {
return true;
}
ImageMap imageMap = ImageFrame.imageMapManager.getFromMapView(mapView);
if (imageMap == null) {
return false;
}
InventoryView inventoryView = player.getOpenInventory();
for (Iterator<ItemStack> itr = Iterators.concat(inventoryView.getTopInventory().iterator(), inventoryView.getBottomInventory().iterator()); itr.hasNext();) {
ItemStack itemStack = itr.next();
if (CombinedMapItemHandler.isCombinedMaps(itemStack)) {
CombinedMapItemInfo info = NMS.getInstance().getCombinedMapItemInfo(itemStack);
if (imageMap.getImageIndex() == info.getImageMapIndex()) {
return true;
}
}
}
return false;
});
}
}