Skip to content

Commit

Permalink
Add support for old versions that don't support compass meta class
Browse files Browse the repository at this point in the history
  • Loading branch information
cavallium committed Sep 22, 2020
1 parent 3a36bda commit e83926b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.warp</groupId>
<artifactId>CoordinatesObfuscator</artifactId>
<version>1.3.4</version>
<version>1.3.5</version>
<name>CoordinatesObfuscator</name>
<build>
<resources>
Expand Down Expand Up @@ -52,12 +52,12 @@
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>4.5.0</version>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.16.2-R0.1-SNAPSHOT</version>
<version>1.16.3-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.CompassMeta;
import org.bukkit.inventory.meta.ItemMeta;

public class TranslatorClientbound {

private static final String SERVER_VERSION;
public static final Class<?> COMPASSMETACLASS;
public static final Class<?> SECTIONPOSITIONCLASS;
public static final Method SectionPositionCreateMethod;
public static final Method SectionPositionGetChunkXMethod;
Expand All @@ -53,6 +53,13 @@ public class TranslatorClientbound {
} catch (ClassNotFoundException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
Class<?> compassMetaClass;
try {
compassMetaClass = Class.forName("org.bukkit.inventory.meta.CompassMeta");
} catch (ClassNotFoundException | NoClassDefFoundError ex) {
compassMetaClass = null;
}
COMPASSMETACLASS = compassMetaClass;

try {
Field modifiersField = Field.class.getDeclaredField("modifiers");
Expand Down Expand Up @@ -286,13 +293,16 @@ private static ItemStack transformItemStack(Logger logger, ItemStack itemStack,
if (itemStack.hasItemMeta()) {
ItemMeta itemMeta = itemStack.getItemMeta();

if (itemMeta instanceof CompassMeta) {
CompassMeta compassMeta = (CompassMeta) itemMeta;
Location lodestoneLocation = compassMeta.getLodestone();
if (lodestoneLocation != null) {
compassMeta.setLodestone(lodestoneLocation.subtract(offset.getXInt(), 0, offset.getZInt()));
if (!itemStack.setItemMeta(compassMeta)) {
logger.severe("Can't apply meta");
// Before 1.16.1
if (COMPASSMETACLASS != null) {
if (itemMeta instanceof org.bukkit.inventory.meta.CompassMeta) {
org.bukkit.inventory.meta.CompassMeta compassMeta = (org.bukkit.inventory.meta.CompassMeta) itemMeta;
Location lodestoneLocation = compassMeta.getLodestone();
if (lodestoneLocation != null) {
compassMeta.setLodestone(lodestoneLocation.subtract(offset.getXInt(), 0, offset.getZInt()));
if (!itemStack.setItemMeta(compassMeta)) {
logger.severe("Can't apply meta");
}
}
}
}
Expand Down

0 comments on commit e83926b

Please sign in to comment.