From d29b8defd4db944309600d5d1aa90662bc5808e7 Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Tue, 30 Oct 2018 15:16:17 +0100 Subject: [PATCH] Fixed Brewing Stands not showing the correct Time Changed some whitespaces Less String comparisons again --- src/com/dre/brewery/BCauldron.java | 1 - src/com/dre/brewery/LegacyUtil.java | 26 +- .../brewery/integration/LogBlockBarrel.java | 238 +++++++++--------- .../brewery/listeners/InventoryListener.java | 8 +- 4 files changed, 147 insertions(+), 126 deletions(-) diff --git a/src/com/dre/brewery/BCauldron.java b/src/com/dre/brewery/BCauldron.java index 0526efb..2264d40 100644 --- a/src/com/dre/brewery/BCauldron.java +++ b/src/com/dre/brewery/BCauldron.java @@ -10,7 +10,6 @@ import org.bukkit.block.BlockFace; import org.bukkit.inventory.ItemStack; import org.bukkit.Effect; import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.material.Cauldron; public class BCauldron { public static CopyOnWriteArrayList bcauldrons = new CopyOnWriteArrayList<>(); diff --git a/src/com/dre/brewery/LegacyUtil.java b/src/com/dre/brewery/LegacyUtil.java index e76de4c..0466580 100644 --- a/src/com/dre/brewery/LegacyUtil.java +++ b/src/com/dre/brewery/LegacyUtil.java @@ -15,6 +15,8 @@ import org.bukkit.material.Wood; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; @SuppressWarnings("JavaReflectionMemberAccess") public class LegacyUtil { @@ -32,6 +34,22 @@ public class LegacyUtil { SET_DATA = Class.forName(Bukkit.getServer().getClass().getPackage().getName() + ".block.CraftBlock").getDeclaredMethod("setData", byte.class); } catch (ClassNotFoundException | NoSuchMethodException | SecurityException ignored) { } + + List planks = new ArrayList<>(6); + for (Material m : Material.values()) { + if (m.name().endsWith("PLANKS")) { + planks.add(m); + } + } + PLANKS = planks; + + List fences = new ArrayList<>(7); + for (Material m : Material.values()) { + if (m.name().endsWith("FENCE")) { + fences.add(m); + } + } + FENCES = fences; } public static final Material CLOCK = get("CLOCK", "WATCH"); @@ -41,6 +59,8 @@ public class LegacyUtil { 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"); + public static final List PLANKS; + public static final List FENCES; // Materials removed in 1.13 public static final Material STATIONARY_LAVA = get("STATIONARY_LAVA"); @@ -64,7 +84,7 @@ public class LegacyUtil { } public static boolean isWoodPlanks(Material type) { - return type.name().endsWith("PLANKS") || (WOOD != null && type == WOOD); + return (WOOD != null && type == WOOD) || PLANKS.contains(type); } public static boolean isWoodStairs(Material type) { @@ -73,7 +93,7 @@ public class LegacyUtil { } public static boolean isFence(Material type) { - return type.name().endsWith("FENCE"); + return FENCES.contains(type); } public static boolean isSign(Material type) { @@ -176,7 +196,7 @@ public class LegacyUtil { } /* - * only used in a very rare case to convert a very old Datafile from a very old version + * only used to convert a very old Datafile or config from a very old version */ public static Material getMaterial(int id) { try { diff --git a/src/com/dre/brewery/integration/LogBlockBarrel.java b/src/com/dre/brewery/integration/LogBlockBarrel.java index ec8ae75..b290f23 100644 --- a/src/com/dre/brewery/integration/LogBlockBarrel.java +++ b/src/com/dre/brewery/integration/LogBlockBarrel.java @@ -1,119 +1,119 @@ -package com.dre.brewery.integration; - -import com.dre.brewery.LegacyUtil; -import com.dre.brewery.P; -import de.diddiz.LogBlock.Actor; - -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 de.diddiz.util.BukkitUtils; -import static de.diddiz.util.BukkitUtils.compareInventories; -import static de.diddiz.util.BukkitUtils.compressInventory; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -public class LogBlockBarrel { - private static final List opened = new ArrayList<>(); - public static Consumer consumer = LogBlock.getInstance().getConsumer(); - private static Method rawData; - private static Method queueChestAccess; - - static { - if (!P.use1_13) { - try { - rawData = BukkitUtils.class.getDeclaredMethod("rawData", ItemStack.class); - queueChestAccess = Consumer.class.getDeclaredMethod("queueChestAccess", String.class, Location.class, int.class, short.class, short.class, short.class); - } catch (NoSuchMethodException e) { - P.p.errorLog("Failed to hook into LogBlock to log barrels. Logging barrel contents is not going to work."); - P.p.errorLog("Brewery was tested with version 1.12 to 1.13.1 of LogBlock."); - P.p.errorLog("Disable LogBlock support in the configuration file and type /brew reload."); - e.printStackTrace(); - } - } - } - - 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) { - try { - queueChestAccess.invoke(consumer, player.getName(), loc, LegacyUtil.getBlockTypeIdAt(loc), (short) item.getType().getId(), (short) item.getAmount(), (short) rawData.invoke(null, item)); - } catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - e.printStackTrace(); - } - } else { - consumer.queueChestAccess(Actor.actorFromEntity(player), loc, loc.getBlock().getBlockData(), item, false); - } - } - } - - 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) { - try { - queueChestAccess.invoke(consumer, playerName, spigotLoc, LegacyUtil.getBlockTypeIdAt(spigotLoc), (short) item.getType().getId(), (short) (item.getAmount() * -1), rawData.invoke(null, item)); - } catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - e.printStackTrace(); - } - } else { - consumer.queueChestAccess(Actor.actorFromString(playerName), spigotLoc, spigotLoc.getBlock().getBlockData(), item, false); - } - } - } - - public static void clear() { - opened.clear(); - } -} +package com.dre.brewery.integration; + +import com.dre.brewery.LegacyUtil; +import com.dre.brewery.P; +import de.diddiz.LogBlock.Actor; + +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 de.diddiz.util.BukkitUtils; +import static de.diddiz.util.BukkitUtils.compareInventories; +import static de.diddiz.util.BukkitUtils.compressInventory; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class LogBlockBarrel { + private static final List opened = new ArrayList<>(); + public static Consumer consumer = LogBlock.getInstance().getConsumer(); + private static Method rawData; + private static Method queueChestAccess; + + static { + if (!P.use1_13) { + try { + rawData = BukkitUtils.class.getDeclaredMethod("rawData", ItemStack.class); + queueChestAccess = Consumer.class.getDeclaredMethod("queueChestAccess", String.class, Location.class, int.class, short.class, short.class, short.class); + } catch (NoSuchMethodException e) { + P.p.errorLog("Failed to hook into LogBlock to log barrels. Logging barrel contents is not going to work."); + P.p.errorLog("Brewery was tested with version 1.12 to 1.13.1 of LogBlock."); + P.p.errorLog("Disable LogBlock support in the configuration file and type /brew reload."); + e.printStackTrace(); + } + } + } + + 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) { + try { + queueChestAccess.invoke(consumer, player.getName(), loc, LegacyUtil.getBlockTypeIdAt(loc), (short) item.getType().getId(), (short) item.getAmount(), (short) rawData.invoke(null, item)); + } catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } else { + consumer.queueChestAccess(Actor.actorFromEntity(player), loc, loc.getBlock().getBlockData(), item, false); + } + } + } + + 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) { + try { + queueChestAccess.invoke(consumer, playerName, spigotLoc, LegacyUtil.getBlockTypeIdAt(spigotLoc), (short) item.getType().getId(), (short) (item.getAmount() * -1), rawData.invoke(null, item)); + } catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } else { + consumer.queueChestAccess(Actor.actorFromString(playerName), spigotLoc, spigotLoc.getBlock().getBlockData(), item, false); + } + } + } + + public static void clear() { + opened.clear(); + } +} diff --git a/src/com/dre/brewery/listeners/InventoryListener.java b/src/com/dre/brewery/listeners/InventoryListener.java index cacef20..e6835f3 100644 --- a/src/com/dre/brewery/listeners/InventoryListener.java +++ b/src/com/dre/brewery/listeners/InventoryListener.java @@ -48,7 +48,7 @@ public class InventoryListener implements Listener { if (!P.use1_9) return; HumanEntity player = event.getPlayer(); Inventory inv = event.getInventory(); - if (player == null || inv == null || !(inv instanceof BrewerInventory)) return; + if (player == null || !(inv instanceof BrewerInventory)) return; P.p.debugLog("Starting brew inventory tracking"); trackedBrewmen.add(player.getUniqueId()); @@ -63,7 +63,7 @@ public class InventoryListener implements Listener { if (!P.use1_9) return; HumanEntity player = event.getPlayer(); Inventory inv = event.getInventory(); - if (player == null || inv == null || !(inv instanceof BrewerInventory)) return; + if (player == null || !(inv instanceof BrewerInventory)) return; P.p.debugLog("Stopping brew inventory tracking"); trackedBrewmen.remove(player.getUniqueId()); @@ -90,7 +90,7 @@ public class InventoryListener implements Listener { if (!P.use1_9) return; HumanEntity player = event.getWhoClicked(); Inventory inv = event.getInventory(); - if (player == null || inv == null || !(inv instanceof BrewerInventory)) return; + if (player == null || !(inv instanceof BrewerInventory)) return; UUID puid = player.getUniqueId(); if (!trackedBrewmen.contains(puid)) return; @@ -106,6 +106,7 @@ public class InventoryListener implements Listener { if (curTask != null) { Bukkit.getScheduler().cancelTask(curTask); // cancel prior brewer.getHolder().setBrewingTime(0); // Fixes brewing continuing without fuel for normal potions + brewer.getHolder().update(); } final int fuel = brewer.getHolder().getFuelLevel(); @@ -159,6 +160,7 @@ public class InventoryListener implements Listener { P.p.debugLog("Can distill more! Continuing."); } } + stand.update(); } else { this.cancel(); trackedBrewers.remove(brewery);