Access legacy method with reflection to allow compilation

This commit is contained in:
Sataniel98 2018-03-25 22:27:28 +02:00
parent ddf14bd3b9
commit 27ecf2e196
3 changed files with 280 additions and 237 deletions

View File

@ -1,9 +1,24 @@
package com.dre.brewery;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
public class LegacyUtil {
private static Method GET_MATERIAL;
private static Method GET_BLOCK_TYPE_ID_AT;
static {
try {
GET_MATERIAL = Material.class.getDeclaredMethod("getMaterial", int.class);
GET_BLOCK_TYPE_ID_AT = World.class.getDeclaredMethod("getBlockTypeIdAt", Location.class);
} catch (NoSuchMethodException | SecurityException ex) {
}
}
public static final Material FLOWING_LAVA = get("FLOWING_LAVA", "LAVA");
public static final Material LAVA = get("LAVA", "STATIONARY_LAVA");
public static final Material CLOCK = get("CLOCK", "WATCH");
@ -47,4 +62,20 @@ public class LegacyUtil {
return type.name().equals("SIGN_POST") || type == Material.SIGN || type == Material.WALL_SIGN;
}
public static Material getMaterial(int id) {
try {
return GET_MATERIAL != null ? (Material) GET_MATERIAL.invoke(null, id) : null;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException exception) {
return null;
}
}
public static int getBlockTypeIdAt(Location location) {
try {
return GET_BLOCK_TYPE_ID_AT != null ? (int) GET_BLOCK_TYPE_ID_AT.invoke(location.getWorld(), location) : 0;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException exception) {
return 0;
}
}
}

View File

@ -1,5 +1,6 @@
package com.dre.brewery.filedata;
import com.dre.brewery.LegacyUtil;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
@ -54,7 +55,7 @@ public class DataUpdater {
Map<String, Integer> ingredients = new HashMap<>();
for (String ingredient : matSection.getKeys(false)) {
// convert to Material
Material mat = Material.getMaterial(P.p.parseInt(ingredient));
Material mat = LegacyUtil.getMaterial(P.p.parseInt(ingredient));
if (mat != null) {
ingredients.put(mat.name(), matSection.getInt(ingredient));
}
@ -84,7 +85,7 @@ public class DataUpdater {
Map<String, Integer> ingredients = new HashMap<>();
for (String ingredient : ingredientSection.getKeys(false)) {
// convert to Material
Material mat = Material.getMaterial(P.p.parseInt(ingredient));
Material mat = LegacyUtil.getMaterial(P.p.parseInt(ingredient));
if (mat != null) {
ingredients.put(mat.name(), ingredientSection.getInt(ingredient));
}

View File

@ -1,5 +1,8 @@
package com.dre.brewery.integration;
import com.dre.brewery.LegacyUtil;
import com.dre.brewery.P;
import java.util.ArrayList;
import java.util.List;
@ -37,7 +40,11 @@ public class LogBlockBarrel {
}
final ItemStack[] diff = compareInventories(items, after);
for (final ItemStack item : diff) {
consumer.queueChestAccess(player.getName(), loc, loc.getWorld().getBlockTypeIdAt(loc), (short) item.getTypeId(), (short) item.getAmount(), rawData(item));
if (P.use1_13) {
consumer.queueChestAccess(player.getName(), loc, LegacyUtil.getBlockTypeIdAt(loc), (short) item.getType().getId(), (short) item.getAmount(), rawData(item));
} else {
// TODO: New method for LogBlock for 1.13+
}
}
}
@ -71,7 +78,11 @@ public class LogBlockBarrel {
if (!isLogging(spigotLoc.getWorld(), Logging.CHESTACCESS)) return;
final ItemStack[] items = compressInventory(contents);
for (final ItemStack item : items) {
consumer.queueChestAccess(playerName, spigotLoc, spigotLoc.getWorld().getBlockTypeIdAt(spigotLoc), (short) item.getTypeId(), (short) (item.getAmount() * -1), rawData(item));
if (P.use1_13) {
consumer.queueChestAccess(playerName, spigotLoc, LegacyUtil.getBlockTypeIdAt(spigotLoc), (short) item.getType().getId(), (short) (item.getAmount() * -1), rawData(item));
} else {
// TODO: New method for LogBlock for 1.13+
}
}
}