diff --git a/src/com/dre/brewery/LegacyUtil.java b/src/com/dre/brewery/LegacyUtil.java index f914b86..54bd9f5 100644 --- a/src/com/dre/brewery/LegacyUtil.java +++ b/src/com/dre/brewery/LegacyUtil.java @@ -1,50 +1,81 @@ -package com.dre.brewery; - -import org.bukkit.Material; - -public class LegacyUtil { - - 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"); - public static final Material OAK_STAIRS = get("OAK_STAIRS", "WOOD_STAIRS"); - public static final Material SPRUCE_STAIRS = get("SPRUCE_STAIRS", "SPRUCE_WOOD_STAIRS"); - public static final Material BIRCH_STAIRS = get("BIRCH_STAIRS", "BIRCH_WOOD_STAIRS"); - public static final Material JUNGLE_STAIRS = get("JUNGLE_STAIRS", "JUNGLE_WOOD_STAIRS"); - 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) { - try { - return Material.valueOf(name); - } catch (IllegalArgumentException exception) { - return null; - } - } - - private static Material get(String oldName, String newName) { - try { - return Material.valueOf(P.use1_13 ? newName : oldName); - } catch (IllegalArgumentException exception) { - return null; - } - } - - 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; - } - -} +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"); + public static final Material OAK_STAIRS = get("OAK_STAIRS", "WOOD_STAIRS"); + public static final Material SPRUCE_STAIRS = get("SPRUCE_STAIRS", "SPRUCE_WOOD_STAIRS"); + public static final Material BIRCH_STAIRS = get("BIRCH_STAIRS", "BIRCH_WOOD_STAIRS"); + public static final Material JUNGLE_STAIRS = get("JUNGLE_STAIRS", "JUNGLE_WOOD_STAIRS"); + 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) { + try { + return Material.valueOf(name); + } catch (IllegalArgumentException exception) { + return null; + } + } + + private static Material get(String oldName, String newName) { + try { + return Material.valueOf(P.use1_13 ? newName : oldName); + } catch (IllegalArgumentException exception) { + return null; + } + } + + 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; + } + } + +} diff --git a/src/com/dre/brewery/filedata/DataUpdater.java b/src/com/dre/brewery/filedata/DataUpdater.java index b76de94..0bacfe1 100644 --- a/src/com/dre/brewery/filedata/DataUpdater.java +++ b/src/com/dre/brewery/filedata/DataUpdater.java @@ -1,106 +1,107 @@ -package com.dre.brewery.filedata; - -import java.io.File; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.FileConfiguration; - -import com.dre.brewery.P; - -public class DataUpdater { - - private FileConfiguration data; - private File file; - - public DataUpdater(FileConfiguration data, File file) { - this.data = data; - this.file = file; - } - - - - public void update(String fromVersion) { - if (fromVersion.equalsIgnoreCase("1.0")) { - update10(); - //fromVersion = "1.1"; - } - - try { - data.save(file); - } catch (IOException e) { - e.printStackTrace(); - } - } - - - - - @SuppressWarnings("deprecation") - public void update10() { - - data.set("Version", DataSave.dataVersion); - - ConfigurationSection section = data.getConfigurationSection("Ingredients"); - try { - if (section != null) { - for (String id : section.getKeys(false)) { - ConfigurationSection matSection = section.getConfigurationSection(id + ".mats"); - if (matSection != null) { - // matSection has all the materials + amount as Integers - Map ingredients = new HashMap<>(); - for (String ingredient : matSection.getKeys(false)) { - // convert to Material - Material mat = Material.getMaterial(P.p.parseInt(ingredient)); - if (mat != null) { - ingredients.put(mat.name(), matSection.getInt(ingredient)); - } - } - section.set(id + ".mats", ingredients); - } else { - P.p.errorLog("Ingredient id: '" + id + "' incomplete in data.yml"); - } - } - } - } catch (Exception e) { - // Getting Material by id may not work in the future - 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) { - try { - for (String uuid : section.getKeys(false)) { - ConfigurationSection cauldrons = section.getConfigurationSection(uuid); - if (cauldrons != null) { - for (String id : cauldrons.getKeys(false)) { - ConfigurationSection ingredientSection = cauldrons.getConfigurationSection(id + ".ingredients"); - if (ingredientSection != null) { - // has all the materials + amount as Integers - Map ingredients = new HashMap<>(); - for (String ingredient : ingredientSection.getKeys(false)) { - // convert to Material - Material mat = Material.getMaterial(P.p.parseInt(ingredient)); - if (mat != null) { - ingredients.put(mat.name(), ingredientSection.getInt(ingredient)); - } - } - cauldrons.set(id + ".ingredients", ingredients); - } else { - P.p.errorLog("BCauldron " + id + " is missing Ingredient Section"); - } - } - } - } - } catch (Exception e) { - // Getting Material by id may not work in the future - P.p.errorLog("Error Converting Ingredient Section of Cauldrons, newer versions of Bukkit may not support the old Save File anymore:"); - e.printStackTrace(); - } - } - } -} +package com.dre.brewery.filedata; + +import com.dre.brewery.LegacyUtil; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; + +import com.dre.brewery.P; + +public class DataUpdater { + + private FileConfiguration data; + private File file; + + public DataUpdater(FileConfiguration data, File file) { + this.data = data; + this.file = file; + } + + + + public void update(String fromVersion) { + if (fromVersion.equalsIgnoreCase("1.0")) { + update10(); + //fromVersion = "1.1"; + } + + try { + data.save(file); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + + + @SuppressWarnings("deprecation") + public void update10() { + + data.set("Version", DataSave.dataVersion); + + ConfigurationSection section = data.getConfigurationSection("Ingredients"); + try { + if (section != null) { + for (String id : section.getKeys(false)) { + ConfigurationSection matSection = section.getConfigurationSection(id + ".mats"); + if (matSection != null) { + // matSection has all the materials + amount as Integers + Map ingredients = new HashMap<>(); + for (String ingredient : matSection.getKeys(false)) { + // convert to Material + Material mat = LegacyUtil.getMaterial(P.p.parseInt(ingredient)); + if (mat != null) { + ingredients.put(mat.name(), matSection.getInt(ingredient)); + } + } + section.set(id + ".mats", ingredients); + } else { + P.p.errorLog("Ingredient id: '" + id + "' incomplete in data.yml"); + } + } + } + } catch (Exception e) { + // Getting Material by id may not work in the future + 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) { + try { + for (String uuid : section.getKeys(false)) { + ConfigurationSection cauldrons = section.getConfigurationSection(uuid); + if (cauldrons != null) { + for (String id : cauldrons.getKeys(false)) { + ConfigurationSection ingredientSection = cauldrons.getConfigurationSection(id + ".ingredients"); + if (ingredientSection != null) { + // has all the materials + amount as Integers + Map ingredients = new HashMap<>(); + for (String ingredient : ingredientSection.getKeys(false)) { + // convert to Material + Material mat = LegacyUtil.getMaterial(P.p.parseInt(ingredient)); + if (mat != null) { + ingredients.put(mat.name(), ingredientSection.getInt(ingredient)); + } + } + cauldrons.set(id + ".ingredients", ingredients); + } else { + P.p.errorLog("BCauldron " + id + " is missing Ingredient Section"); + } + } + } + } + } catch (Exception e) { + // Getting Material by id may not work in the future + P.p.errorLog("Error Converting Ingredient Section of Cauldrons, newer versions of Bukkit may not support the old Save File anymore:"); + e.printStackTrace(); + } + } + } +} diff --git a/src/com/dre/brewery/integration/LogBlockBarrel.java b/src/com/dre/brewery/integration/LogBlockBarrel.java index be0406d..d9d915d 100644 --- a/src/com/dre/brewery/integration/LogBlockBarrel.java +++ b/src/com/dre/brewery/integration/LogBlockBarrel.java @@ -1,81 +1,92 @@ -package com.dre.brewery.integration; - -import java.util.ArrayList; -import java.util.List; - -import org.bukkit.Location; -import org.bukkit.entity.HumanEntity; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; - -import de.diddiz.LogBlock.Consumer; -import de.diddiz.LogBlock.LogBlock; -import de.diddiz.LogBlock.Logging; -import static de.diddiz.LogBlock.config.Config.isLogging; -import static de.diddiz.util.BukkitUtils.compareInventories; -import static de.diddiz.util.BukkitUtils.compressInventory; -import static de.diddiz.util.BukkitUtils.rawData; - -public class LogBlockBarrel { - private static final List opened = new ArrayList<>(); - public static Consumer consumer = LogBlock.getInstance().getConsumer(); - - private HumanEntity player; - private ItemStack[] items; - private Location loc; - - public LogBlockBarrel(HumanEntity player, ItemStack[] items, Location spigotLoc) { - this.player = player; - this.items = items; - this.loc = spigotLoc; - opened.add(this); - } - - private void compareInv(final ItemStack[] after) { - if (consumer == null) { - return; - } - 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)); - } - } - - public static LogBlockBarrel get(HumanEntity player) { - for (LogBlockBarrel open : opened) { - if (open.player.equals(player)) { - return open; - } - } - return null; - } - - public static void openBarrel(HumanEntity player, Inventory inv, Location spigotLoc) { - if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) return; - new LogBlockBarrel(player, compressInventory(inv.getContents()), spigotLoc); - } - - public static void closeBarrel(HumanEntity player, Inventory inv) { - if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) return; - LogBlockBarrel open = get(player); - if (open != null) { - open.compareInv(compressInventory(inv.getContents())); - opened.remove(open); - } - } - - public static void breakBarrel(String playerName, ItemStack[] contents, Location spigotLoc) { - if (consumer == null) { - return; - } - 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 clear() { - opened.clear(); - } -} +package com.dre.brewery.integration; + +import com.dre.brewery.LegacyUtil; +import com.dre.brewery.P; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.entity.HumanEntity; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +import de.diddiz.LogBlock.Consumer; +import de.diddiz.LogBlock.LogBlock; +import de.diddiz.LogBlock.Logging; +import static de.diddiz.LogBlock.config.Config.isLogging; +import static de.diddiz.util.BukkitUtils.compareInventories; +import static de.diddiz.util.BukkitUtils.compressInventory; +import static de.diddiz.util.BukkitUtils.rawData; + +public class LogBlockBarrel { + private static final List opened = new ArrayList<>(); + public static Consumer consumer = LogBlock.getInstance().getConsumer(); + + private HumanEntity player; + private ItemStack[] items; + private Location loc; + + public LogBlockBarrel(HumanEntity player, ItemStack[] items, Location spigotLoc) { + this.player = player; + this.items = items; + this.loc = spigotLoc; + opened.add(this); + } + + private void compareInv(final ItemStack[] after) { + if (consumer == null) { + return; + } + final ItemStack[] diff = compareInventories(items, after); + for (final ItemStack item : diff) { + 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+ + } + } + } + + public static LogBlockBarrel get(HumanEntity player) { + for (LogBlockBarrel open : opened) { + if (open.player.equals(player)) { + return open; + } + } + return null; + } + + public static void openBarrel(HumanEntity player, Inventory inv, Location spigotLoc) { + if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) return; + new LogBlockBarrel(player, compressInventory(inv.getContents()), spigotLoc); + } + + public static void closeBarrel(HumanEntity player, Inventory inv) { + if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) return; + LogBlockBarrel open = get(player); + if (open != null) { + open.compareInv(compressInventory(inv.getContents())); + opened.remove(open); + } + } + + public static void breakBarrel(String playerName, ItemStack[] contents, Location spigotLoc) { + if (consumer == null) { + return; + } + if (!isLogging(spigotLoc.getWorld(), Logging.CHESTACCESS)) return; + 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(); + } +}