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
+81 -50
View File
@@ -1,50 +1,81 @@
package com.dre.brewery; package com.dre.brewery;
import org.bukkit.Material; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class LegacyUtil { import org.bukkit.Location;
import org.bukkit.Material;
public static final Material FLOWING_LAVA = get("FLOWING_LAVA", "LAVA"); import org.bukkit.World;
public static final Material LAVA = get("LAVA", "STATIONARY_LAVA");
public static final Material CLOCK = get("CLOCK", "WATCH"); public class LegacyUtil {
public static final Material OAK_STAIRS = get("OAK_STAIRS", "WOOD_STAIRS");
public static final Material SPRUCE_STAIRS = get("SPRUCE_STAIRS", "SPRUCE_WOOD_STAIRS"); private static Method GET_MATERIAL;
public static final Material BIRCH_STAIRS = get("BIRCH_STAIRS", "BIRCH_WOOD_STAIRS"); private static Method GET_BLOCK_TYPE_ID_AT;
public static final Material JUNGLE_STAIRS = get("JUNGLE_STAIRS", "JUNGLE_WOOD_STAIRS");
public static final Material ACACIA_STAIRS = get("ACACIA_STAIRS"); static {
public static final Material DARK_OAK_STAIRS = get("DARK_OAK_STAIRS"); try {
GET_MATERIAL = Material.class.getDeclaredMethod("getMaterial", int.class);
private static Material get(String name) { GET_BLOCK_TYPE_ID_AT = World.class.getDeclaredMethod("getBlockTypeIdAt", Location.class);
try { } catch (NoSuchMethodException | SecurityException ex) {
return Material.valueOf(name); }
} catch (IllegalArgumentException exception) { }
return null;
} 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");
private static Material get(String oldName, String newName) { public static final Material OAK_STAIRS = get("OAK_STAIRS", "WOOD_STAIRS");
try { public static final Material SPRUCE_STAIRS = get("SPRUCE_STAIRS", "SPRUCE_WOOD_STAIRS");
return Material.valueOf(P.use1_13 ? newName : oldName); public static final Material BIRCH_STAIRS = get("BIRCH_STAIRS", "BIRCH_WOOD_STAIRS");
} catch (IllegalArgumentException exception) { public static final Material JUNGLE_STAIRS = get("JUNGLE_STAIRS", "JUNGLE_WOOD_STAIRS");
return null; public static final Material ACACIA_STAIRS = get("ACACIA_STAIRS");
} public static final Material DARK_OAK_STAIRS = get("DARK_OAK_STAIRS");
}
private static Material get(String name) {
public static boolean isWoodPlanks(Material type) { try {
return type.name().contains("PLANKS") || type.name().equals("WOOD"); return Material.valueOf(name);
} } catch (IllegalArgumentException exception) {
return null;
public static boolean isWoodStairs(Material type) { }
return type == OAK_STAIRS || type == SPRUCE_STAIRS || type == BIRCH_STAIRS || type == JUNGLE_STAIRS }
|| (type == ACACIA_STAIRS && ACACIA_STAIRS != null) || (type == DARK_OAK_STAIRS && DARK_OAK_STAIRS != null);
} private static Material get(String oldName, String newName) {
try {
public static boolean isFence(Material material) { return Material.valueOf(P.use1_13 ? newName : oldName);
return material.name().endsWith("FENCE"); } catch (IllegalArgumentException exception) {
} return null;
}
public static boolean isSign(Material type) { }
return type.name().equals("SIGN_POST") || type == Material.SIGN || type == Material.WALL_SIGN;
} public static boolean isWoodPlanks(Material type) {
return type.name().contains("PLANKS") || type.name().equals("WOOD");
} }
public static boolean isWoodStairs(Material type) {
return type == OAK_STAIRS || type == SPRUCE_STAIRS || type == BIRCH_STAIRS || type == JUNGLE_STAIRS
|| (type == ACACIA_STAIRS && ACACIA_STAIRS != null) || (type == DARK_OAK_STAIRS && DARK_OAK_STAIRS != null);
}
public static boolean isFence(Material material) {
return material.name().endsWith("FENCE");
}
public static boolean isSign(Material type) {
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;
}
}
}
+107 -106
View File
@@ -1,106 +1,107 @@
package com.dre.brewery.filedata; package com.dre.brewery.filedata;
import java.io.File; import com.dre.brewery.LegacyUtil;
import java.io.IOException; import java.io.File;
import java.util.HashMap; import java.io.IOException;
import java.util.Map; import java.util.HashMap;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import com.dre.brewery.P;
import com.dre.brewery.P;
public class DataUpdater {
public class DataUpdater {
private FileConfiguration data;
private File file; private FileConfiguration data;
private File file;
public DataUpdater(FileConfiguration data, File file) {
this.data = data; public DataUpdater(FileConfiguration data, File file) {
this.file = file; this.data = data;
} this.file = file;
}
public void update(String fromVersion) {
if (fromVersion.equalsIgnoreCase("1.0")) { public void update(String fromVersion) {
update10(); if (fromVersion.equalsIgnoreCase("1.0")) {
//fromVersion = "1.1"; update10();
} //fromVersion = "1.1";
}
try {
data.save(file); try {
} catch (IOException e) { data.save(file);
e.printStackTrace(); } catch (IOException e) {
} e.printStackTrace();
} }
}
@SuppressWarnings("deprecation")
public void update10() { @SuppressWarnings("deprecation")
public void update10() {
data.set("Version", DataSave.dataVersion);
data.set("Version", DataSave.dataVersion);
ConfigurationSection section = data.getConfigurationSection("Ingredients");
try { ConfigurationSection section = data.getConfigurationSection("Ingredients");
if (section != null) { try {
for (String id : section.getKeys(false)) { if (section != null) {
ConfigurationSection matSection = section.getConfigurationSection(id + ".mats"); for (String id : section.getKeys(false)) {
if (matSection != null) { ConfigurationSection matSection = section.getConfigurationSection(id + ".mats");
// matSection has all the materials + amount as Integers if (matSection != null) {
Map<String, Integer> ingredients = new HashMap<>(); // matSection has all the materials + amount as Integers
for (String ingredient : matSection.getKeys(false)) { Map<String, Integer> ingredients = new HashMap<>();
// convert to Material for (String ingredient : matSection.getKeys(false)) {
Material mat = Material.getMaterial(P.p.parseInt(ingredient)); // convert to Material
if (mat != null) { Material mat = LegacyUtil.getMaterial(P.p.parseInt(ingredient));
ingredients.put(mat.name(), matSection.getInt(ingredient)); if (mat != null) {
} ingredients.put(mat.name(), matSection.getInt(ingredient));
} }
section.set(id + ".mats", ingredients); }
} else { section.set(id + ".mats", ingredients);
P.p.errorLog("Ingredient id: '" + id + "' incomplete in data.yml"); } else {
} P.p.errorLog("Ingredient id: '" + id + "' incomplete in data.yml");
} }
} }
} catch (Exception e) { }
// Getting Material by id may not work in the future } catch (Exception e) {
P.p.errorLog("Error Converting Ingredient Section of the Data File, newer versions of Bukkit may not support the old Save File anymore:"); // Getting Material by id may not work in the future
e.printStackTrace(); P.p.errorLog("Error Converting Ingredient Section of the Data File, newer versions of Bukkit may not support the old Save File anymore:");
} e.printStackTrace();
}
section = data.getConfigurationSection("BCauldron");
if (section != null) { section = data.getConfigurationSection("BCauldron");
try { if (section != null) {
for (String uuid : section.getKeys(false)) { try {
ConfigurationSection cauldrons = section.getConfigurationSection(uuid); for (String uuid : section.getKeys(false)) {
if (cauldrons != null) { ConfigurationSection cauldrons = section.getConfigurationSection(uuid);
for (String id : cauldrons.getKeys(false)) { if (cauldrons != null) {
ConfigurationSection ingredientSection = cauldrons.getConfigurationSection(id + ".ingredients"); for (String id : cauldrons.getKeys(false)) {
if (ingredientSection != null) { ConfigurationSection ingredientSection = cauldrons.getConfigurationSection(id + ".ingredients");
// has all the materials + amount as Integers if (ingredientSection != null) {
Map<String, Integer> ingredients = new HashMap<>(); // has all the materials + amount as Integers
for (String ingredient : ingredientSection.getKeys(false)) { Map<String, Integer> ingredients = new HashMap<>();
// convert to Material for (String ingredient : ingredientSection.getKeys(false)) {
Material mat = Material.getMaterial(P.p.parseInt(ingredient)); // convert to Material
if (mat != null) { Material mat = LegacyUtil.getMaterial(P.p.parseInt(ingredient));
ingredients.put(mat.name(), ingredientSection.getInt(ingredient)); if (mat != null) {
} ingredients.put(mat.name(), ingredientSection.getInt(ingredient));
} }
cauldrons.set(id + ".ingredients", ingredients); }
} else { cauldrons.set(id + ".ingredients", ingredients);
P.p.errorLog("BCauldron " + id + " is missing Ingredient Section"); } else {
} P.p.errorLog("BCauldron " + id + " is missing Ingredient Section");
} }
} }
} }
} catch (Exception e) { }
// Getting Material by id may not work in the future } catch (Exception e) {
P.p.errorLog("Error Converting Ingredient Section of Cauldrons, newer versions of Bukkit may not support the old Save File anymore:"); // Getting Material by id may not work in the future
e.printStackTrace(); P.p.errorLog("Error Converting Ingredient Section of Cauldrons, newer versions of Bukkit may not support the old Save File anymore:");
} e.printStackTrace();
} }
} }
} }
}
@@ -1,81 +1,92 @@
package com.dre.brewery.integration; package com.dre.brewery.integration;
import java.util.ArrayList; import com.dre.brewery.LegacyUtil;
import java.util.List; import com.dre.brewery.P;
import org.bukkit.Location; import java.util.ArrayList;
import org.bukkit.entity.HumanEntity; import java.util.List;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.Location;
import org.bukkit.entity.HumanEntity;
import de.diddiz.LogBlock.Consumer; import org.bukkit.inventory.Inventory;
import de.diddiz.LogBlock.LogBlock; import org.bukkit.inventory.ItemStack;
import de.diddiz.LogBlock.Logging;
import static de.diddiz.LogBlock.config.Config.isLogging; import de.diddiz.LogBlock.Consumer;
import static de.diddiz.util.BukkitUtils.compareInventories; import de.diddiz.LogBlock.LogBlock;
import static de.diddiz.util.BukkitUtils.compressInventory; import de.diddiz.LogBlock.Logging;
import static de.diddiz.util.BukkitUtils.rawData; import static de.diddiz.LogBlock.config.Config.isLogging;
import static de.diddiz.util.BukkitUtils.compareInventories;
public class LogBlockBarrel { import static de.diddiz.util.BukkitUtils.compressInventory;
private static final List<LogBlockBarrel> opened = new ArrayList<>(); import static de.diddiz.util.BukkitUtils.rawData;
public static Consumer consumer = LogBlock.getInstance().getConsumer();
public class LogBlockBarrel {
private HumanEntity player; private static final List<LogBlockBarrel> opened = new ArrayList<>();
private ItemStack[] items; public static Consumer consumer = LogBlock.getInstance().getConsumer();
private Location loc;
private HumanEntity player;
public LogBlockBarrel(HumanEntity player, ItemStack[] items, Location spigotLoc) { private ItemStack[] items;
this.player = player; private Location loc;
this.items = items;
this.loc = spigotLoc; public LogBlockBarrel(HumanEntity player, ItemStack[] items, Location spigotLoc) {
opened.add(this); this.player = player;
} this.items = items;
this.loc = spigotLoc;
private void compareInv(final ItemStack[] after) { opened.add(this);
if (consumer == null) { }
return;
} private void compareInv(final ItemStack[] after) {
final ItemStack[] diff = compareInventories(items, after); if (consumer == null) {
for (final ItemStack item : diff) { return;
consumer.queueChestAccess(player.getName(), loc, loc.getWorld().getBlockTypeIdAt(loc), (short) item.getTypeId(), (short) item.getAmount(), rawData(item)); }
} final ItemStack[] diff = compareInventories(items, after);
} for (final ItemStack item : diff) {
if (P.use1_13) {
public static LogBlockBarrel get(HumanEntity player) { consumer.queueChestAccess(player.getName(), loc, LegacyUtil.getBlockTypeIdAt(loc), (short) item.getType().getId(), (short) item.getAmount(), rawData(item));
for (LogBlockBarrel open : opened) { } else {
if (open.player.equals(player)) { // TODO: New method for LogBlock for 1.13+
return open; }
} }
} }
return null;
} public static LogBlockBarrel get(HumanEntity player) {
for (LogBlockBarrel open : opened) {
public static void openBarrel(HumanEntity player, Inventory inv, Location spigotLoc) { if (open.player.equals(player)) {
if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) return; return open;
new LogBlockBarrel(player, compressInventory(inv.getContents()), spigotLoc); }
} }
return null;
public static void closeBarrel(HumanEntity player, Inventory inv) { }
if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) return;
LogBlockBarrel open = get(player); public static void openBarrel(HumanEntity player, Inventory inv, Location spigotLoc) {
if (open != null) { if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) return;
open.compareInv(compressInventory(inv.getContents())); new LogBlockBarrel(player, compressInventory(inv.getContents()), spigotLoc);
opened.remove(open); }
}
} public static void closeBarrel(HumanEntity player, Inventory inv) {
if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) return;
public static void breakBarrel(String playerName, ItemStack[] contents, Location spigotLoc) { LogBlockBarrel open = get(player);
if (consumer == null) { if (open != null) {
return; open.compareInv(compressInventory(inv.getContents()));
} opened.remove(open);
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)); public static void breakBarrel(String playerName, ItemStack[] contents, Location spigotLoc) {
} if (consumer == null) {
} return;
}
public static void clear() { if (!isLogging(spigotLoc.getWorld(), Logging.CHESTACCESS)) return;
opened.clear(); final ItemStack[] items = compressInventory(contents);
} for (final ItemStack item : items) {
} 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+
}
}
}
public static void clear() {
opened.clear();
}
}