Skip to content

Commit

Permalink
Merge pull request #9 from JohnnyJayJay/fix/legacy-method
Browse files Browse the repository at this point in the history
Fix legacy method issues and ImageTools bug
  • Loading branch information
JohnnyJayJay authored Jul 31, 2020
2 parents 478bdd8 + 096405c commit acbd665
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 20 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ Then add `example-plugin-1.0-TEST.jar` (found in `build/libs`) to your plugins f
```xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.github.johnnyjayjay</groupId>
<artifactId>spigot-maps</artifactId>
<version>2.0</version>
<version>2.1</version>
</dependency>
</dependencies>
```
Expand All @@ -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")
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'com.github.johnnyjayjay'
version '2.0'
version '2.1'

sourceCompatibility = 1.8

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -30,7 +31,7 @@ private InitializationListener(MapStorage storage) {
@EventHandler
public void onMapInitialize(MapInitializeEvent event) {
MapView map = event.getMap();
List<MapRenderer> renderers = storage.provide(map.getId());
List<MapRenderer> renderers = storage.provide(Compatibility.getId(map));
if (renderers != null) {
map.getRenderers().forEach(map::removeRenderer);
renderers.forEach(map::addRenderer);
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/github/johnnyjayjay/spigotmaps/RenderedMap.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
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;
import org.bukkit.inventory.meta.MapMeta;
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;

Expand All @@ -21,11 +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;
view.getRenderers().forEach((renderer) -> storage.store(view.getId(), renderer));
this.mapViewId = Compatibility.getId(view);
view.getRenderers().forEach((renderer) -> storage.store(mapViewId, renderer));
}

/**
Expand Down Expand Up @@ -106,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -91,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}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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;
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 {
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[0] == 1 && version[1] > 13)
|| (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 int getId(MapView map) {
try {
return ((Number) getId.invoke(map)).intValue();
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -121,7 +119,7 @@ public static List<GifImage> 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];
Expand All @@ -133,6 +131,10 @@ public static List<GifImage> 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.
Expand All @@ -155,9 +157,9 @@ private static BufferedImage[] divideIntoParts(BufferedImage image) {
Dimension partSize = MINECRAFT_MAP_SIZE;
int linearParts = image.getWidth() / partSize.width;
List<BufferedImage> 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]);
Expand Down

0 comments on commit acbd665

Please sign in to comment.