From 13abf2475c78a4efc55a01b2cc22ab9a2d1c9a3c Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Wed, 9 Apr 2014 00:35:08 +0200 Subject: [PATCH] LogBlock ChestAccess logging Support --- plugin.yml | 2 +- pom.xml | 17 ++++ src/com/dre/brewery/Barrel.java | 27 ++++++- src/com/dre/brewery/P.java | 20 +++-- .../dre/brewery/integration/LWCBarrel.java | 12 ++- .../brewery/integration/LogBlockBarrel.java | 77 +++++++++++++++++++ .../dre/brewery/listeners/BlockListener.java | 6 ++ .../dre/brewery/listeners/EntityListener.java | 22 +++++- .../brewery/listeners/InventoryListener.java | 15 +++- 9 files changed, 180 insertions(+), 18 deletions(-) create mode 100644 src/com/dre/brewery/integration/LogBlockBarrel.java diff --git a/plugin.yml b/plugin.yml index f769306..f948fab 100644 --- a/plugin.yml +++ b/plugin.yml @@ -2,7 +2,7 @@ name: Brewery version: 1.2 main: com.dre.brewery.P authors: [Milan Albrecht, Frank Baumann] -softdepend: [LWC] +softdepend: [LWC, LogBlock] commands: brewery: description: Command for Administration diff --git a/pom.xml b/pom.xml index db32ff9..fcd6242 100644 --- a/pom.xml +++ b/pom.xml @@ -94,6 +94,15 @@ + + lb-repo + http://junction.at/repo/ + + true + always + + + Plugin Metrics http://repo.mcstats.org/content/repositories/public @@ -133,6 +142,14 @@ jar + + de.diddiz + logblock + LATEST + compile + jar + + org.mcstats metrics diff --git a/src/com/dre/brewery/Barrel.java b/src/com/dre/brewery/Barrel.java index 3f3909c..6ef54ef 100644 --- a/src/com/dre/brewery/Barrel.java +++ b/src/com/dre/brewery/Barrel.java @@ -16,6 +16,7 @@ import org.bukkit.configuration.ConfigurationSection; import org.bukkit.plugin.Plugin; import com.dre.brewery.integration.LWCBarrel; +import com.dre.brewery.integration.LogBlockBarrel; import com.dre.brewery.integration.WGBarrel; import org.apache.commons.lang.ArrayUtils; @@ -74,7 +75,7 @@ public class Barrel { if (woodsloc == null && stairsloc == null) { Block broken = getBrokenBlock(true); if (broken != null) { - remove(broken); + remove(broken, null); return; } } @@ -89,7 +90,8 @@ public class Barrel { broken = barrel.getBrokenBlock(false); if (broken != null) { // remove the barrel if it was destroyed - barrel.remove(broken); + barrel.willDestroy(); + barrel.remove(broken, null); continue; } else { // Dont check this barrel again, its enough to check it once after every restart @@ -131,7 +133,12 @@ public class Barrel { return true; } + // Ask for permission to destroy barrel, remove protection if has public boolean hasPermsDestroy(Player player) { + if (player == null) { + willDestroy(); + return true; + } if (P.p.hasLWC) { return LWCBarrel.checkDestroy(player, this); } @@ -139,6 +146,13 @@ public class Barrel { return true; } + // If something other than the Player is destroying the barrel, inform protection plugins + public void willDestroy() { + if (P.p.hasLWC) { + LWCBarrel.remove(this); + } + } + // player opens the barrel @SuppressWarnings("deprecation") public void open(Player player) { @@ -173,6 +187,10 @@ public class Barrel { } // reset barreltime, potions have new age time = 0; + + if (P.p.hasLB) { + LogBlockBarrel.openBarrel(player, inventory, spigot.getLocation()); + } player.openInventory(inventory); } @@ -339,12 +357,15 @@ public class Barrel { } // removes a barrel, throwing included potions to the ground - public void remove(Block broken) { + public void remove(Block broken, Player breaker) { if (inventory != null) { for (HumanEntity human : inventory.getViewers()) { human.closeInventory(); } ItemStack[] items = inventory.getContents(); + if (P.p.hasLB && breaker != null) { + LogBlockBarrel.breakBarrel(breaker.getName(), items, spigot.getLocation()); + } for (ItemStack item : items) { if (item != null) { Brew brew = Brew.get(item); diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index b1acad5..8a19938 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -13,6 +13,8 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.ConfigurationSection; + +import com.dre.brewery.integration.LogBlockBarrel; import org.apache.commons.lang.math.NumberUtils; import org.bukkit.event.HandlerList; import org.bukkit.Bukkit; @@ -35,6 +37,7 @@ public class P extends JavaPlugin { // Third Party Enabled public boolean hasLWC; + public boolean hasLB; // Listeners public BlockListener blockListener; @@ -51,9 +54,6 @@ public class P extends JavaPlugin { public void onEnable() { p = this; - // Check Third Party - hasLWC = getServer().getPluginManager().isPluginEnabled("LWC"); - readConfig(); readData(); @@ -125,6 +125,9 @@ public class P extends JavaPlugin { BIngredients.cookedNames.clear(); Words.words.clear(); BPlayer.drainItems.clear(); + if (hasLB) { + LogBlockBarrel.clear(); + } // load the Config readConfig(); @@ -164,6 +167,9 @@ public class P extends JavaPlugin { } public void readConfig() { + // Check Third Party + hasLWC = getServer().getPluginManager().isPluginEnabled("LWC"); + hasLB = getServer().getPluginManager().isPluginEnabled("LogBlock"); File file = new File(p.getDataFolder(), "config.yml"); if (!file.exists()) { @@ -572,7 +578,7 @@ public class P extends JavaPlugin { } } - // Returns true if the Block can be destroyed by the Player + // Returns true if the Block can be destroyed by the Player or something else (null) public boolean blockDestroy(Block block, Player player) { switch (block.getType()) { case CAULDRON: @@ -585,7 +591,7 @@ public class P extends JavaPlugin { Barrel barrel = Barrel.getBySpigot(block); if (barrel != null) { if (barrel.hasPermsDestroy(player)) { - barrel.remove(null); + barrel.remove(null, player); return true; } else { return false; @@ -599,7 +605,7 @@ public class P extends JavaPlugin { if (barrel2 != null) { if (!barrel2.isLarge()) { if (barrel2.hasPermsDestroy(player)) { - barrel2.remove(null); + barrel2.remove(null, player); return true; } else { return false; @@ -619,7 +625,7 @@ public class P extends JavaPlugin { Barrel barrel3 = Barrel.getByWood(block); if (barrel3 != null) { if (barrel3.hasPermsDestroy(player)) { - barrel3.remove(block); + barrel3.remove(block, player); } else { return false; } diff --git a/src/com/dre/brewery/integration/LWCBarrel.java b/src/com/dre/brewery/integration/LWCBarrel.java index 67a7474..b3ee6f5 100644 --- a/src/com/dre/brewery/integration/LWCBarrel.java +++ b/src/com/dre/brewery/integration/LWCBarrel.java @@ -77,18 +77,26 @@ public class LWCBarrel { return true; } + // If a Barrel is destroyed without player + public static void remove(Barrel barrel) { + Protection protection = LWC.getInstance().findProtection(barrel.getSignOfSpigot()); + if (protection != null) { + protection.remove(); + } + } + // Returns true if the block that exploded should not be removed public static boolean blockExplosion(Barrel barrel, Block block) { Protection protection = LWC.getInstance().findProtection(barrel.getSignOfSpigot()); if (protection == null) { - barrel.remove(block); + barrel.remove(block, null); return false; } if (protection.hasFlag(Flag.Type.ALLOWEXPLOSIONS)) { protection.remove(); - barrel.remove(block); + barrel.remove(block, null); return false; } return true; diff --git a/src/com/dre/brewery/integration/LogBlockBarrel.java b/src/com/dre/brewery/integration/LogBlockBarrel.java new file mode 100644 index 0000000..a466910 --- /dev/null +++ b/src/com/dre/brewery/integration/LogBlockBarrel.java @@ -0,0 +1,77 @@ +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); + } + + @SuppressWarnings("deprecation") + private void compareInv(final ItemStack[] after) { + 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); + } + } + + @SuppressWarnings("deprecation") + public static void breakBarrel(String playerName, ItemStack[] contents, Location spigotLoc) { + 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(); + } +} diff --git a/src/com/dre/brewery/listeners/BlockListener.java b/src/com/dre/brewery/listeners/BlockListener.java index 38a544e..1f02dc1 100644 --- a/src/com/dre/brewery/listeners/BlockListener.java +++ b/src/com/dre/brewery/listeners/BlockListener.java @@ -4,6 +4,7 @@ import org.bukkit.block.Block; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBurnEvent; import org.bukkit.event.block.BlockPistonExtendEvent; import org.bukkit.event.block.BlockPistonRetractEvent; import org.bukkit.event.block.SignChangeEvent; @@ -43,6 +44,11 @@ public class BlockListener implements Listener { } } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onBlockBurn(BlockBurnEvent event) { + P.p.blockDestroy(event.getBlock(), null); + } + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onPistonRetract(BlockPistonRetractEvent event) { if (event.isSticky()) { diff --git a/src/com/dre/brewery/listeners/EntityListener.java b/src/com/dre/brewery/listeners/EntityListener.java index c5463e4..c834eb4 100644 --- a/src/com/dre/brewery/listeners/EntityListener.java +++ b/src/com/dre/brewery/listeners/EntityListener.java @@ -6,6 +6,7 @@ import org.bukkit.block.Block; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityChangeBlockEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.ItemDespawnEvent; import org.bukkit.event.entity.EntityCombustEvent; @@ -48,19 +49,32 @@ public class EntityListener implements Listener { public void onExplode(EntityExplodeEvent event) { ListIterator iter = event.blockList().listIterator(); Barrel barrel = null; + boolean removedBarrel = false; while (iter.hasNext()) { Block block = iter.next(); if (barrel == null || !barrel.hasBlock(block)) { barrel = Barrel.get(block); + removedBarrel = false; } - if (barrel != null) { - if (P.p.hasLWC) { - if (LWCBarrel.blockExplosion(barrel, block)) { - iter.remove(); + if (!removedBarrel) { + if (barrel != null) { + if (P.p.hasLWC) { + if (LWCBarrel.blockExplosion(barrel, block)) { + iter.remove(); + } else { + removedBarrel = true; + } } } } } } + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onBlockChange(EntityChangeBlockEvent event) { + if (Barrel.get(event.getBlock()) != null) { + event.setCancelled(true); + } + } + } diff --git a/src/com/dre/brewery/listeners/InventoryListener.java b/src/com/dre/brewery/listeners/InventoryListener.java index 3d632f0..9a63e5f 100644 --- a/src/com/dre/brewery/listeners/InventoryListener.java +++ b/src/com/dre/brewery/listeners/InventoryListener.java @@ -5,6 +5,7 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.inventory.BrewEvent; import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.BrewerInventory; @@ -14,6 +15,7 @@ import org.bukkit.Material; import com.dre.brewery.Brew; import com.dre.brewery.P; +import com.dre.brewery.integration.LogBlockBarrel; public class InventoryListener implements Listener { @@ -52,7 +54,7 @@ public class InventoryListener implements Listener { } // convert to non colored Lore when taking out of Barrel/Brewer - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onInventoryClick(InventoryClickEvent event) { if (event.getInventory().getType() == InventoryType.BREWING) { if (event.getSlot() > 2) { @@ -90,4 +92,15 @@ public class InventoryListener implements Listener { event.setCancelled(true); } } + + @EventHandler + public void onInventoryClose(InventoryCloseEvent event) { + if (P.p.hasLB) { + if (event.getInventory().getType() == InventoryType.CHEST) { + if (event.getInventory().getTitle().equals(P.p.languageReader.get("Etc_Barrel"))) { + LogBlockBarrel.closeBarrel(event.getPlayer(), event.getInventory()); + } + } + } + } }