From e20920b56a92c54dcc19ef02490fb04dd8042393 Mon Sep 17 00:00:00 2001 From: JohnnyJayJay Date: Tue, 21 Jul 2020 16:26:15 +0200 Subject: [PATCH 01/11] Add getImage() method for GifRenderer --- .../johnnyjayjay/spigotmaps/rendering/GifRenderer.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/com/github/johnnyjayjay/spigotmaps/rendering/GifRenderer.java b/src/main/java/com/github/johnnyjayjay/spigotmaps/rendering/GifRenderer.java index 500ef40..8ace501 100644 --- a/src/main/java/com/github/johnnyjayjay/spigotmaps/rendering/GifRenderer.java +++ b/src/main/java/com/github/johnnyjayjay/spigotmaps/rendering/GifRenderer.java @@ -66,6 +66,15 @@ protected void render(RenderContext context) { ticksToWait = msToTicks(frame.getMsDelay()); } + /** + * Returns the {@link GifImage} used by this renderer. + * + * @return the image + */ + public GifImage getImage() { + return image; + } + /** * Returns how often the gif will still repeat itself or {@link #REPEAT_FOREVER} if it repeats indefinitely. */ From 8687b6bc7f0ff3fae2fede3614f26a1afbe353cb Mon Sep 17 00:00:00 2001 From: JohnnyJayJay Date: Tue, 21 Jul 2020 16:54:10 +0200 Subject: [PATCH 02/11] Use MethodHandle instead of calling MapView#getId() directly --- .../spigotmaps/InitializationListener.java | 3 +- .../johnnyjayjay/spigotmaps/RenderedMap.java | 8 +++- .../spigotmaps/rendering/RenderContext.java | 7 +++- .../spigotmaps/util/Compatibility.java | 42 +++++++++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java diff --git a/src/main/java/com/github/johnnyjayjay/spigotmaps/InitializationListener.java b/src/main/java/com/github/johnnyjayjay/spigotmaps/InitializationListener.java index 6954772..2b11679 100644 --- a/src/main/java/com/github/johnnyjayjay/spigotmaps/InitializationListener.java +++ b/src/main/java/com/github/johnnyjayjay/spigotmaps/InitializationListener.java @@ -1,5 +1,6 @@ package com.github.johnnyjayjay.spigotmaps; +import com.github.johnnyjayjay.spigotmaps.util.Compatibility; import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -30,7 +31,7 @@ private InitializationListener(MapStorage storage) { @EventHandler public void onMapInitialize(MapInitializeEvent event) { MapView map = event.getMap(); - List renderers = storage.provide(map.getId()); + List renderers = storage.provide(Compatibility.getId(map)); if (renderers != null) { map.getRenderers().forEach(map::removeRenderer); renderers.forEach(map::addRenderer); diff --git a/src/main/java/com/github/johnnyjayjay/spigotmaps/RenderedMap.java b/src/main/java/com/github/johnnyjayjay/spigotmaps/RenderedMap.java index 92b5720..eb03b9b 100644 --- a/src/main/java/com/github/johnnyjayjay/spigotmaps/RenderedMap.java +++ b/src/main/java/com/github/johnnyjayjay/spigotmaps/RenderedMap.java @@ -1,5 +1,7 @@ package com.github.johnnyjayjay.spigotmaps; +import com.github.johnnyjayjay.spigotmaps.util.Compatibility; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -7,6 +9,9 @@ import org.bukkit.map.MapRenderer; import org.bukkit.map.MapView; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; import java.util.Arrays; import java.util.List; @@ -25,7 +30,8 @@ public class RenderedMap { private RenderedMap(MapView view, MapStorage storage) { this.view = view; this.storage = storage; - view.getRenderers().forEach((renderer) -> storage.store(view.getId(), renderer)); + int id = Compatibility.getId(view); + view.getRenderers().forEach((renderer) -> storage.store(id, renderer)); } /** diff --git a/src/main/java/com/github/johnnyjayjay/spigotmaps/rendering/RenderContext.java b/src/main/java/com/github/johnnyjayjay/spigotmaps/rendering/RenderContext.java index d1d58a9..28b2e33 100644 --- a/src/main/java/com/github/johnnyjayjay/spigotmaps/rendering/RenderContext.java +++ b/src/main/java/com/github/johnnyjayjay/spigotmaps/rendering/RenderContext.java @@ -1,6 +1,7 @@ package com.github.johnnyjayjay.spigotmaps.rendering; import com.github.johnnyjayjay.spigotmaps.util.Checks; +import com.github.johnnyjayjay.spigotmaps.util.Compatibility; import org.bukkit.entity.Player; import org.bukkit.map.MapCanvas; import org.bukkit.map.MapView; @@ -19,11 +20,13 @@ public class RenderContext { private final MapView mapView; private final MapCanvas mapCanvas; private final Player player; + private final int mapViewId; private RenderContext(MapView mapView, MapCanvas mapCanvas, Player player) { this.mapView = mapView; this.mapCanvas = mapCanvas; this.player = player; + this.mapViewId = Compatibility.getId(mapView); } /** @@ -68,12 +71,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; RenderContext that = (RenderContext) o; - return mapView.getId() == that.mapView.getId() + return mapViewId == that.mapViewId && Objects.equals(player, that.player); } @Override public int hashCode() { - return Objects.hash(mapView.getId(), player); + return Objects.hash(mapViewId, player); } } diff --git a/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java new file mode 100644 index 0000000..b88a829 --- /dev/null +++ b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java @@ -0,0 +1,42 @@ +package com.github.johnnyjayjay.spigotmaps.util; + +import org.bukkit.Bukkit; +import org.bukkit.map.MapView; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import java.util.Arrays; + +/** + * @author Johnny_JayJay (https://www.github.com/JohnnyJayJay) + */ +public final class Compatibility { + + private static final MethodHandle getId; + + static { + MethodType methodType = MethodType.methodType(isLegacy() ? short.class : int.class); + try { + getId = MethodHandles.publicLookup().findVirtual(MapView.class, "getId", methodType); + } catch (NoSuchMethodException | IllegalAccessException e) { + throw new AssertionError("MapView#getId() could not be found. This should never happen."); + } + } + + public static boolean isLegacy() { + int[] version = Arrays.stream(Bukkit.getVersion().split("\\.")) + .mapToInt(Integer::parseInt) + .toArray(); + return version[0] < 1 || version[1] < 13 || version[2] < 2; + } + + public static int getId(MapView map) { + try { + return ((Number) getId.invoke(map)).intValue(); + } catch (Throwable throwable) { + throw new RuntimeException(throwable); + } + } + +} From 0e226b5aaea1b433f8dc7971741ac54d6047bf10 Mon Sep 17 00:00:00 2001 From: JohnnyJayJay Date: Tue, 21 Jul 2020 17:17:24 +0200 Subject: [PATCH 03/11] Fix version string matching --- .../spigotmaps/util/Compatibility.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java index b88a829..0ecdce6 100644 --- a/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java +++ b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java @@ -7,16 +7,28 @@ import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * @author Johnny_JayJay (https://www.github.com/JohnnyJayJay) */ public final class Compatibility { + private static final boolean legacy; private static final MethodHandle getId; static { - MethodType methodType = MethodType.methodType(isLegacy() ? short.class : int.class); + Matcher versionFinder = Pattern.compile("(?<=\\(MC: )\\d+\\.\\d+\\.\\d+(?=\\))").matcher(Bukkit.getVersion()); + if (!versionFinder.find()) { + throw new AssertionError("Could not find MC version in Bukkit.getVersion()"); + } + int[] version = Arrays.stream(Bukkit.getVersion().split("\\.")) + .mapToInt(Integer::parseInt) + .toArray(); + legacy = version[0] < 1 || version[1] < 13 || version[2] < 2; + + MethodType methodType = MethodType.methodType(legacy ? short.class : int.class); try { getId = MethodHandles.publicLookup().findVirtual(MapView.class, "getId", methodType); } catch (NoSuchMethodException | IllegalAccessException e) { @@ -25,10 +37,7 @@ public final class Compatibility { } public static boolean isLegacy() { - int[] version = Arrays.stream(Bukkit.getVersion().split("\\.")) - .mapToInt(Integer::parseInt) - .toArray(); - return version[0] < 1 || version[1] < 13 || version[2] < 2; + return legacy; } public static int getId(MapView map) { From 82272a092ddcaceadd6e6f6d29e5be2907ee4de8 Mon Sep 17 00:00:00 2001 From: JohnnyJayJay Date: Tue, 21 Jul 2020 17:34:27 +0200 Subject: [PATCH 04/11] Fix further compatibility issue with setting map views --- .../github/johnnyjayjay/spigotmaps/RenderedMap.java | 11 ++++++++--- .../johnnyjayjay/spigotmaps/util/Compatibility.java | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/johnnyjayjay/spigotmaps/RenderedMap.java b/src/main/java/com/github/johnnyjayjay/spigotmaps/RenderedMap.java index eb03b9b..45d3b34 100644 --- a/src/main/java/com/github/johnnyjayjay/spigotmaps/RenderedMap.java +++ b/src/main/java/com/github/johnnyjayjay/spigotmaps/RenderedMap.java @@ -26,12 +26,13 @@ public class RenderedMap { private final MapView view; private final MapStorage storage; + private final int mapViewId; private RenderedMap(MapView view, MapStorage storage) { this.view = view; this.storage = storage; - int id = Compatibility.getId(view); - view.getRenderers().forEach((renderer) -> storage.store(id, renderer)); + this.mapViewId = Compatibility.getId(view); + view.getRenderers().forEach((renderer) -> storage.store(mapViewId, renderer)); } /** @@ -112,7 +113,11 @@ public ItemStack createItemStack() { public ItemStack createItemStack(String displayName, String... lore) { ItemStack itemStack = new ItemStack(Material.MAP); MapMeta mapMeta = (MapMeta) itemStack.getItemMeta(); - mapMeta.setMapView(view); + if (Compatibility.isLegacy()) { + itemStack.setDurability((short) mapViewId); + } else { + mapMeta.setMapView(view); + } mapMeta.setDisplayName(displayName); mapMeta.setLore(lore.length == 0 ? null : Arrays.asList(lore)); itemStack.setItemMeta(mapMeta); diff --git a/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java index 0ecdce6..7633e13 100644 --- a/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java +++ b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java @@ -23,7 +23,7 @@ public final class Compatibility { if (!versionFinder.find()) { throw new AssertionError("Could not find MC version in Bukkit.getVersion()"); } - int[] version = Arrays.stream(Bukkit.getVersion().split("\\.")) + int[] version = Arrays.stream(versionFinder.group().split("\\.")) .mapToInt(Integer::parseInt) .toArray(); legacy = version[0] < 1 || version[1] < 13 || version[2] < 2; From 4f80348a967754cc98256383d7a3953be83ff44a Mon Sep 17 00:00:00 2001 From: JohnnyJayJay Date: Tue, 21 Jul 2020 17:37:12 +0200 Subject: [PATCH 05/11] Indent Compatibility class with 4 spaces --- .../spigotmaps/util/Compatibility.java | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java index 7633e13..2168fe3 100644 --- a/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java +++ b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java @@ -15,37 +15,37 @@ */ public final class Compatibility { - private static final boolean legacy; - private static final MethodHandle getId; - - static { - Matcher versionFinder = Pattern.compile("(?<=\\(MC: )\\d+\\.\\d+\\.\\d+(?=\\))").matcher(Bukkit.getVersion()); - if (!versionFinder.find()) { - throw new AssertionError("Could not find MC version in Bukkit.getVersion()"); - } - int[] version = Arrays.stream(versionFinder.group().split("\\.")) - .mapToInt(Integer::parseInt) - .toArray(); - legacy = version[0] < 1 || version[1] < 13 || version[2] < 2; - - MethodType methodType = MethodType.methodType(legacy ? short.class : int.class); - try { - getId = MethodHandles.publicLookup().findVirtual(MapView.class, "getId", methodType); - } catch (NoSuchMethodException | IllegalAccessException e) { - throw new AssertionError("MapView#getId() could not be found. This should never happen."); + private static final boolean legacy; + private static final MethodHandle getId; + + static { + Matcher versionFinder = Pattern.compile("(?<=\\(MC: )\\d+\\.\\d+\\.\\d+(?=\\))").matcher(Bukkit.getVersion()); + if (!versionFinder.find()) { + throw new AssertionError("Could not find MC version in Bukkit.getVersion()"); + } + int[] version = Arrays.stream(versionFinder.group().split("\\.")) + .mapToInt(Integer::parseInt) + .toArray(); + legacy = version[0] < 1 || version[1] < 13 || version[2] < 2; + + MethodType methodType = MethodType.methodType(legacy ? short.class : int.class); + try { + getId = MethodHandles.publicLookup().findVirtual(MapView.class, "getId", methodType); + } catch (NoSuchMethodException | IllegalAccessException e) { + throw new AssertionError("MapView#getId() could not be found. This should never happen."); + } } - } - public static boolean isLegacy() { - return legacy; - } + public static boolean isLegacy() { + return legacy; + } - public static int getId(MapView map) { - try { - return ((Number) getId.invoke(map)).intValue(); - } catch (Throwable throwable) { - throw new RuntimeException(throwable); + public static int getId(MapView map) { + try { + return ((Number) getId.invoke(map)).intValue(); + } catch (Throwable throwable) { + throw new RuntimeException(throwable); + } } - } } From 782e892302d0a3c1535c0a956791adb39b943729 Mon Sep 17 00:00:00 2001 From: JohnnyJayJay Date: Tue, 21 Jul 2020 18:02:04 +0200 Subject: [PATCH 06/11] Fix legacy version check --- .../github/johnnyjayjay/spigotmaps/util/Compatibility.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java index 2168fe3..fe75a31 100644 --- a/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java +++ b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java @@ -26,7 +26,9 @@ public final class Compatibility { int[] version = Arrays.stream(versionFinder.group().split("\\.")) .mapToInt(Integer::parseInt) .toArray(); - legacy = version[0] < 1 || version[1] < 13 || version[2] < 2; + legacy = !(version[0] > 1 + || (version[0] == 1 && version[1] > 13) + || (version[0] == 1 && version[1] == 13 && version[2] >= 2)); MethodType methodType = MethodType.methodType(legacy ? short.class : int.class); try { From 98366947c5eaed3a92efc6bcea86003e050fedf9 Mon Sep 17 00:00:00 2001 From: JohnnyJayJay Date: Tue, 21 Jul 2020 18:31:59 +0200 Subject: [PATCH 07/11] Fix bug in ImageTools division --- .../com/github/johnnyjayjay/spigotmaps/util/ImageTools.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/johnnyjayjay/spigotmaps/util/ImageTools.java b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/ImageTools.java index b2ef7fd..48213ad 100644 --- a/src/main/java/com/github/johnnyjayjay/spigotmaps/util/ImageTools.java +++ b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/ImageTools.java @@ -155,9 +155,9 @@ private static BufferedImage[] divideIntoParts(BufferedImage image) { Dimension partSize = MINECRAFT_MAP_SIZE; int linearParts = image.getWidth() / partSize.width; List result = new ArrayList<>(linearParts * linearParts); - for (int i = 0; i < linearParts; i++) { - for (int j = 0; j < linearParts; j++) { - result.add(image.getSubimage(partSize.width * i, partSize.height * i, partSize.width, partSize.height)); + for (int x = 0; x < linearParts; x++) { + for (int y = 0; y < linearParts; y++) { + result.add(image.getSubimage(partSize.width * x, partSize.height * y, partSize.width, partSize.height)); } } return result.toArray(new BufferedImage[0]); From cf32fbb2663dffb3ee2b35e5c25b54d8e859aca5 Mon Sep 17 00:00:00 2001 From: JohnnyJayJay Date: Tue, 21 Jul 2020 18:41:50 +0200 Subject: [PATCH 08/11] Bump version to 2.1 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 2b2d086..4dac783 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group 'com.github.johnnyjayjay' -version '2.0' +version '2.1' sourceCompatibility = 1.8 From 8db9fcb95ab5097de12e328a9b5c54c5bc355427 Mon Sep 17 00:00:00 2001 From: JohnnyJayJay Date: Fri, 31 Jul 2020 10:57:24 +0200 Subject: [PATCH 09/11] Add static create method to GifRenderer --- .../spigotmaps/rendering/GifRenderer.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/com/github/johnnyjayjay/spigotmaps/rendering/GifRenderer.java b/src/main/java/com/github/johnnyjayjay/spigotmaps/rendering/GifRenderer.java index 8ace501..e79fc05 100644 --- a/src/main/java/com/github/johnnyjayjay/spigotmaps/rendering/GifRenderer.java +++ b/src/main/java/com/github/johnnyjayjay/spigotmaps/rendering/GifRenderer.java @@ -100,6 +100,18 @@ public void setFrame(int frame) { this.currentFrame = frame; } + /** + * Creates a new {@link GifRenderer} that renders a specific gif for the specified players + * or everyone if none are specified. + * + * @param image the gif to render. + * @param players the players to render for. Must not be {@code null}. + * @return a never-null instance of {@link GifRenderer}. + */ + public static GifRenderer create(GifImage image, Player... players) { + return builder().gif(image).addPlayers(players).build(); + } + /** * Creates and returns a new instance of this class' {@link Builder}. */ From c3a6c713eb58654952681d21968ae961c2df179f Mon Sep 17 00:00:00 2001 From: JohnnyJayJay Date: Fri, 31 Jul 2020 11:31:03 +0200 Subject: [PATCH 10/11] Fix ArrayIndexOutOfBoundsException when dividing gifs --- .../johnnyjayjay/spigotmaps/util/ImageTools.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/johnnyjayjay/spigotmaps/util/ImageTools.java b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/ImageTools.java index 48213ad..e63ac26 100644 --- a/src/main/java/com/github/johnnyjayjay/spigotmaps/util/ImageTools.java +++ b/src/main/java/com/github/johnnyjayjay/spigotmaps/util/ImageTools.java @@ -4,9 +4,7 @@ import com.github.johnnyjayjay.spigotmaps.rendering.SimpleTextRenderer; import javax.imageio.ImageIO; -import java.awt.Color; -import java.awt.Dimension; -import java.awt.Graphics2D; +import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; @@ -121,7 +119,7 @@ public static List divideIntoMapSizedParts(GifImage gif, boolean crop) ); } - int parts = newFrames[0].getImage().getWidth() / MINECRAFT_MAP_SIZE.width; + int parts = square(newFrames[0].getImage().getWidth() / MINECRAFT_MAP_SIZE.width); GifImage.Frame[][] dividedParts = new GifImage.Frame[parts][newFrames.length]; for (int i = 0; i < newFrames.length; i++) { GifImage.Frame frame = newFrames[i]; @@ -133,6 +131,10 @@ public static List divideIntoMapSizedParts(GifImage gif, boolean crop) return Arrays.stream(dividedParts).map(Arrays::asList).map(GifImage::create).collect(Collectors.toList()); } + private static int square(int x) { + return x * x; + } + /** * Takes an image and resizes it in a way that the parts returned by this method can be put together * to form the whole image. From 096405c7d8943b1abb4a23788167e8c33030c867 Mon Sep 17 00:00:00 2001 From: JohnnyJayJay Date: Fri, 31 Jul 2020 11:40:37 +0200 Subject: [PATCH 11/11] Update readme --- README.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d60194b..de1d10a 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,8 @@ Then add `example-plugin-1.0-TEST.jar` (found in `build/libs`) to your plugins f ```xml - jitpack.io - https://jitpack.io + jcenter + https://jcenter.bintray.com/ @@ -40,7 +40,7 @@ Then add `example-plugin-1.0-TEST.jar` (found in `build/libs`) to your plugins f com.github.johnnyjayjay spigot-maps - 2.0 + 2.1 ``` @@ -49,13 +49,11 @@ Then add `example-plugin-1.0-TEST.jar` (found in `build/libs`) to your plugins f ```groovy repositories { - maven { - url "https://jitpack.io" - } + jcenter() } dependencies { - implementation "com.github.johnnyjayjay:spigot-maps:2.0" + implementation("com.github.johnnyjayjay:spigot-maps:2.1") } ```