-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from JohnnyJayJay/fix/legacy-method
Fix legacy method issues and ImageTools bug
- Loading branch information
Showing
8 changed files
with
109 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ plugins { | |
} | ||
|
||
group 'com.github.johnnyjayjay' | ||
version '2.0' | ||
version '2.1' | ||
|
||
sourceCompatibility = 1.8 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/com/github/johnnyjayjay/spigotmaps/util/Compatibility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters