From 1c6fe6cc33e646be5317b51ccd26d9dde0fc42aa Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Tue, 8 Apr 2014 16:11:20 +0200 Subject: [PATCH] Listener for Barrel breaking -> LWC Support --- config.yml | 7 +- plugin.yml | 3 +- pom.xml | 19 +- src/com/dre/brewery/BCauldron.java | 13 +- src/com/dre/brewery/BIngredients.java | 15 +- src/com/dre/brewery/BPlayer.java | 2 - src/com/dre/brewery/BRecipe.java | 15 +- src/com/dre/brewery/Barrel.java | 454 ++++++++++++++---- src/com/dre/brewery/Brew.java | 9 +- src/com/dre/brewery/ConfigUpdater.java | 116 ++++- src/com/dre/brewery/P.java | 77 ++- src/com/dre/brewery/Wakeup.java | 2 +- src/com/dre/brewery/Words.java | 6 +- .../dre/brewery/integration/LWCBarrel.java | 96 ++++ src/com/dre/brewery/integration/WGBarrel.java | 31 ++ .../dre/brewery/listeners/BlockListener.java | 50 +- .../dre/brewery/listeners/EntityListener.java | 28 ++ .../brewery/listeners/InventoryListener.java | 5 +- .../dre/brewery/listeners/PlayerListener.java | 69 ++- 19 files changed, 790 insertions(+), 227 deletions(-) create mode 100644 src/com/dre/brewery/integration/LWCBarrel.java create mode 100644 src/com/dre/brewery/integration/WGBarrel.java diff --git a/config.yml b/config.yml index 7578309..615ca2d 100644 --- a/config.yml +++ b/config.yml @@ -30,7 +30,7 @@ enableLoginDisallow: true enableKickOnOverdrink: true # If the Player vomits on high drunkeness (drops item defined below) [true] -# The item can not be collected and stays on the ground until it despawns. (Warning: May be collected after Server restart, or through a hopper) +# The item can not be collected and stays on the ground until it despawns. (Warning: May be collected after Server restart!) enablePuke: true # Item that is dropped multiple times uncollectable when puking [SOUL_SAND] @@ -48,11 +48,14 @@ hangoverDays: 7 colorInBarrels: true colorInBrewer: false +# If a Large Barrel can be opened by clicking on any of its blocks, not just Spigot or Sign. This is always true for Small Barrels. [true] +openLargeBarrelEverywhere: true + # Autosave interval in minutes [3] autosave: 3 # Config Version -version: '1.1' +version: '1.2' # -- Recipes for Potions -- diff --git a/plugin.yml b/plugin.yml index f3ec6f8..f769306 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,7 +1,8 @@ name: Brewery -version: 1.1 +version: 1.2 main: com.dre.brewery.P authors: [Milan Albrecht, Frank Baumann] +softdepend: [LWC] commands: brewery: description: Command for Administration diff --git a/pom.xml b/pom.xml index e376f2e..db32ff9 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.dre brewery - 1.1.1 + 1.2 Brewery @@ -85,6 +85,15 @@ + + lwc-repo + http://repo.sacredlabyrinth.net:8081/artifactory/repo/ + + true + always + + + Plugin Metrics http://repo.mcstats.org/content/repositories/public @@ -116,6 +125,14 @@ jar + + com.griefcraft + LWC + 4.3.1 + compile + jar + + org.mcstats metrics diff --git a/src/com/dre/brewery/BCauldron.java b/src/com/dre/brewery/BCauldron.java index b4c22fc..65a5973 100644 --- a/src/com/dre/brewery/BCauldron.java +++ b/src/com/dre/brewery/BCauldron.java @@ -10,8 +10,6 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.Effect; import org.bukkit.configuration.ConfigurationSection; -import com.dre.brewery.BIngredients; - public class BCauldron { public static CopyOnWriteArrayList bcauldrons = new CopyOnWriteArrayList(); @@ -87,6 +85,7 @@ public class BCauldron { } // fills players bottle with cooked brew + @SuppressWarnings("deprecation") public static boolean fill(Player player, Block block) { BCauldron bcauldron = get(block); if (bcauldron != null) { @@ -128,9 +127,11 @@ public class BCauldron { // reset to normal cauldron public static void remove(Block block) { - BCauldron bcauldron = get(block); - if (bcauldron != null) { - bcauldrons.remove(bcauldron); + if (block.getData() != 0) { + BCauldron bcauldron = get(block); + if (bcauldron != null) { + bcauldrons.remove(bcauldron); + } } } @@ -151,7 +152,7 @@ public class BCauldron { int id = 0; for (BCauldron cauldron : bcauldrons) { String worldName = cauldron.block.getWorld().getName(); - String prefix = null; + String prefix; if (worldName.startsWith("DXL_")) { prefix = P.p.getDxlName(worldName) + "." + id; diff --git a/src/com/dre/brewery/BIngredients.java b/src/com/dre/brewery/BIngredients.java index 8db01c5..4700ee3 100644 --- a/src/com/dre/brewery/BIngredients.java +++ b/src/com/dre/brewery/BIngredients.java @@ -10,9 +10,6 @@ import org.bukkit.Material; import org.bukkit.potion.PotionEffectType; import org.bukkit.inventory.meta.PotionMeta; -import com.dre.brewery.BRecipe; -import com.dre.brewery.Brew; - public class BIngredients { public static ArrayList possibleIngredients = new ArrayList(); public static ArrayList recipes = new ArrayList(); @@ -121,10 +118,10 @@ public class BIngredients { // correct one... public BRecipe getBestRecipe(float wood, float time, boolean distilled) { float quality = 0; - int ingredientQuality = 0; - int cookingQuality = 0; - int woodQuality = 0; - int ageQuality = 0; + int ingredientQuality; + int cookingQuality; + int woodQuality; + int ageQuality; BRecipe bestRecipe = null; for (BRecipe recipe : recipes) { ingredientQuality = getIngredientQuality(recipe); @@ -204,7 +201,7 @@ public class BIngredients { // no recipe is near them public int getIngredientQuality(BRecipe recipe) { float quality = 10; - int count = 0; + int count; int badStuff = 0; if (recipe.isMissingIngredients(ingredients)) { // when ingredients are not complete @@ -278,7 +275,7 @@ public class BIngredients { // returns the quality regarding the ageing time conditioning given Recipe public int getAgeQuality(BRecipe recipe, float time) { - int quality = 10 - (int) Math.round(Math.abs(time - recipe.getAge()) * ((float) recipe.getDifficulty() / 2)); + int quality = 10 - Math.round(Math.abs(time - recipe.getAge()) * ((float) recipe.getDifficulty() / 2)); if (quality > 0) { return quality; diff --git a/src/com/dre/brewery/BPlayer.java b/src/com/dre/brewery/BPlayer.java index d5a01af..c2490d1 100644 --- a/src/com/dre/brewery/BPlayer.java +++ b/src/com/dre/brewery/BPlayer.java @@ -15,8 +15,6 @@ import org.bukkit.Location; import org.bukkit.potion.PotionEffectType; import org.bukkit.configuration.ConfigurationSection; -import com.dre.brewery.Brew; - public class BPlayer { public static Map players = new HashMap();// Players name and BPlayer private static Map pTasks = new HashMap();// Player and count diff --git a/src/com/dre/brewery/BRecipe.java b/src/com/dre/brewery/BRecipe.java index ece8ec3..2d5467a 100644 --- a/src/com/dre/brewery/BRecipe.java +++ b/src/com/dre/brewery/BRecipe.java @@ -113,24 +113,15 @@ public class BRecipe { } public boolean isCookingOnly() { - if (age == 0 && distillruns == 0) { - return true; - } - return false; + return age == 0 && distillruns == 0; } public boolean needsDistilling() { - if (distillruns == 0) { - return false; - } - return true; + return distillruns != 0; } public boolean needsToAge() { - if (age == 0) { - return false; - } - return true; + return age != 0; } // true if given map misses an ingredient diff --git a/src/com/dre/brewery/Barrel.java b/src/com/dre/brewery/Barrel.java index ea40600..3f3909c 100644 --- a/src/com/dre/brewery/Barrel.java +++ b/src/com/dre/brewery/Barrel.java @@ -1,74 +1,146 @@ package com.dre.brewery; +import java.util.ArrayList; import java.util.concurrent.CopyOnWriteArrayList; import java.util.Map; +import org.bukkit.Material; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.block.Block; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.plugin.Plugin; + +import com.dre.brewery.integration.LWCBarrel; +import com.dre.brewery.integration.WGBarrel; +import org.apache.commons.lang.ArrayUtils; public class Barrel { public static CopyOnWriteArrayList barrels = new CopyOnWriteArrayList(); - // private CopyOnWriteArrayList brews = new - // CopyOnWriteArrayList(); private Block spigot; + private int[] woodsloc = null; // location of wood Blocks + private int[] stairsloc = null; // location of stair Blocks + private byte signoffset; + private boolean checked = false; private Inventory inventory; private float time; - public Barrel(Block spigot) { + public Barrel(Block spigot, byte signoffset) { this.spigot = spigot; + this.signoffset = signoffset; } // load from file - public Barrel(Block spigot, Map items, float time) { + public Barrel(Block spigot, byte sign, String[] st, String[] wo, Map items, float time) { this.spigot = spigot; + this.signoffset = sign; if (isLarge()) { this.inventory = org.bukkit.Bukkit.createInventory(null, 27, P.p.languageReader.get("Etc_Barrel")); } else { this.inventory = org.bukkit.Bukkit.createInventory(null, 9, P.p.languageReader.get("Etc_Barrel")); } - for (String slot : items.keySet()) { - if (items.get(slot) instanceof ItemStack) { - this.inventory.setItem(P.p.parseInt(slot), (ItemStack) items.get(slot)); + if (items != null) { + for (String slot : items.keySet()) { + if (items.get(slot) instanceof ItemStack) { + this.inventory.setItem(P.p.parseInt(slot), (ItemStack) items.get(slot)); + } } } this.time = time; - barrels.add(this); - } - // load from file (without inventory) - public Barrel(Block spigot, float time) { - this.spigot = spigot; - if (isLarge()) { - this.inventory = org.bukkit.Bukkit.createInventory(null, 27, P.p.languageReader.get("Etc_Barrel")); - } else { - this.inventory = org.bukkit.Bukkit.createInventory(null, 9, P.p.languageReader.get("Etc_Barrel")); + int i = 0; + if (wo.length > 1) { + woodsloc = new int[wo.length]; + for (String wos : wo) { + woodsloc[i] = P.p.parseInt(wos); + i++; + } + i = 0; } - this.time = time; + if (st.length > 1) { + stairsloc = new int[st.length]; + for (String sts : st) { + stairsloc[i] = P.p.parseInt(sts); + i++; + } + } + + if (woodsloc == null && stairsloc == null) { + Block broken = getBrokenBlock(true); + if (broken != null) { + remove(broken); + return; + } + } + barrels.add(this); } public static void onUpdate() { Block broken; for (Barrel barrel : barrels) { - broken = getBrokenBlock(barrel.spigot); - // remove the barrel if it was destroyed - if (broken != null) { - barrel.remove(broken); - } else { - // Minecraft day is 20 min, so add 1/20 to the time every minute - barrel.time += 1.0 / 20.0; + if (!barrel.checked) { + broken = barrel.getBrokenBlock(false); + if (broken != null) { + // remove the barrel if it was destroyed + barrel.remove(broken); + continue; + } else { + // Dont check this barrel again, its enough to check it once after every restart + // as now this is only the backup if we dont register the barrel breaking, as sample + // when removing it with some world editor + barrel.checked = true; + } } + + // Minecraft day is 20 min, so add 1/20 to the time every minute + barrel.time += (1.0 / 20.0); } } + public boolean hasPermsOpen(Player player, PlayerInteractEvent event) { + Plugin plugin = P.p.getServer().getPluginManager().getPlugin("WorldGuard"); + if (plugin != null) { + if (!WGBarrel.checkAccess(player, spigot, plugin)) { + return false; + } + } + + + if (event != null) { + plugin = P.p.getServer().getPluginManager().getPlugin("LWC"); + if (plugin != null) { + + // If the Clicked Block was the Sign, LWC already knows and we dont need to do anything here + if (!isSign(event.getClickedBlock())) { + Block sign = getSignOfSpigot(); + // If the Barrel does not have a Sign, it cannot be locked + if (!sign.equals(event.getClickedBlock())) { + return LWCBarrel.checkAccess(player, sign, event, plugin); + } + } + } + } + + return true; + } + + public boolean hasPermsDestroy(Player player) { + if (P.p.hasLWC) { + return LWCBarrel.checkDestroy(player, this); + } + + return true; + } + // player opens the barrel + @SuppressWarnings("deprecation") public void open(Player player) { if (inventory == null) { if (isLarge()) { @@ -104,23 +176,162 @@ public class Barrel { player.openInventory(inventory); } - public static Barrel get(Block spigot) { + // Returns true if this Block is part of this Barrel + public boolean hasBlock(Block block) { + if (block != null) { + if (block.getType().equals(Material.WOOD)) { + if (hasWoodBlock(block)) { + return true; + } + } else if (isStairs(block.getType())) { + if (hasStairsBlock(block)) { + return true; + } + } + } + return false; + } + + public boolean hasWoodBlock(Block block) { + if (woodsloc != null) { + if (spigot.getWorld() != null && spigot.getWorld().equals(block.getWorld())) { + if (woodsloc.length > 2) { + int x = block.getX(); + if (Math.abs(x - woodsloc[0]) < 10) { + for (int i = 0; i < woodsloc.length - 2; i += 3) { + if (woodsloc[i] == x) { + if (woodsloc[i + 1] == block.getY()) { + if (woodsloc[i + 2] == block.getZ()) { + return true; + } + } + } + } + } + } + } + } + return false; + } + + public boolean hasStairsBlock(Block block) { + if (stairsloc != null) { + if (spigot.getWorld() != null && spigot.getWorld().equals(block.getWorld())) { + if (stairsloc.length > 2) { + int x = block.getX(); + if (Math.abs(x - stairsloc[0]) < 10) { + for (int i = 0; i < stairsloc.length - 2; i += 3) { + if (stairsloc[i] == x) { + if (stairsloc[i + 1] == block.getY()) { + if (stairsloc[i + 2] == block.getZ()) { + return true; + } + } + } + } + } + } + } + } + return false; + } + + // Returns true if the Offset of the clicked Sign matches the Barrel. + // This prevents adding another sign to the barrel and clicking that. + public boolean isSignOfBarrel(byte offset) { + return offset == 0 || signoffset == 0 || signoffset == offset; + } + + // Get the Barrel by Block, null if that block is not part of a barrel + public static Barrel get(Block block) { + if (block != null) { + switch (block.getType()) { + case FENCE: + case NETHER_FENCE: + case SIGN: + case WALL_SIGN: + Barrel barrel = getBySpigot(block); + if (barrel != null) { + return barrel; + } + return null; + case WOOD: + case WOOD_STAIRS: + case ACACIA_STAIRS: + case BIRCH_WOOD_STAIRS: + case DARK_OAK_STAIRS: + case JUNGLE_WOOD_STAIRS: + case SPRUCE_WOOD_STAIRS: + Barrel barrel2 = getByWood(block); + if (barrel2 != null) { + return barrel2; + } + } + } + return null; + } + + // Get the Barrel by Sign or Spigot (Fastest) + public static Barrel getBySpigot(Block sign) { // convert spigot if neccessary - spigot = getSpigotOfSign(spigot); + Block spigot = getSpigotOfSign(sign); + + byte signoffset = 0; + if (!spigot.equals(sign)) { + signoffset = (byte) (sign.getY() - spigot.getY()); + } + for (Barrel barrel : barrels) { - if (barrel.spigot.equals(spigot)) { - return barrel; + if (barrel.isSignOfBarrel(signoffset)) { + if (barrel.spigot.equals(spigot)) { + if (barrel.signoffset == 0 && signoffset != 0) { + // Barrel has no signOffset even though we clicked a sign, may be old + barrel.signoffset = signoffset; + } + return barrel; + } + } + } + return null; + } + + // Get the barrel by its corpus (Wood Planks, Stairs) + public static Barrel getByWood(Block wood) { + if (wood.getType().equals(Material.WOOD)) { + for (Barrel barrel : barrels) { + if (barrel.hasWoodBlock(wood)) { + return barrel; + } + } + } else if (isStairs(wood.getType())) { + for (Barrel barrel : Barrel.barrels) { + if (barrel.hasStairsBlock(wood)) { + return barrel; + } } } return null; } // creates a new Barrel out of a sign - public static boolean create(Block spigot) { - spigot = getSpigotOfSign(spigot); - if (getBrokenBlock(spigot) == null) { - if (get(spigot) == null) { - barrels.add(new Barrel(spigot)); + public static boolean create(Block sign) { + Block spigot = getSpigotOfSign(sign); + + byte signoffset = 0; + if (!spigot.equals(sign)) { + signoffset = (byte) (sign.getY() - spigot.getY()); + } + + Barrel barrel = getBySpigot(spigot); + if (barrel == null) { + barrel = new Barrel(spigot, signoffset); + if (barrel.getBrokenBlock(true) == null) { + barrels.add(barrel); + return true; + } + } else { + if (barrel.signoffset == 0 && signoffset != 0) { + barrel.signoffset = signoffset; return true; } } @@ -155,6 +366,7 @@ public class Barrel { } } } + barrels.remove(this); } @@ -167,6 +379,11 @@ public class Barrel { } } + // If the Sign of a Large Barrel gets destroyed, set signOffset to 0 + public void destroySign() { + signoffset = 0; + } + // Saves all data public static void save(ConfigurationSection config, ConfigurationSection oldData) { P.p.createWorldSections(config); @@ -176,7 +393,7 @@ public class Barrel { for (Barrel barrel : barrels) { String worldName = barrel.spigot.getWorld().getName(); - String prefix = null; + String prefix; if (worldName.startsWith("DXL_")) { prefix = P.p.getDxlName(worldName) + "." + id; @@ -187,9 +404,27 @@ public class Barrel { // block: x/y/z config.set(prefix + ".spigot", barrel.spigot.getX() + "/" + barrel.spigot.getY() + "/" + barrel.spigot.getZ()); + if (barrel.signoffset != 0) { + config.set(prefix + ".sign", barrel.signoffset); + } + if (barrel.stairsloc != null && barrel.stairsloc.length > 0) { + StringBuilder st = new StringBuilder(); + for (int i : barrel.stairsloc) { + st.append(i).append(","); + } + config.set(prefix + ".st", st.substring(0, st.length() - 1)); + } + if (barrel.woodsloc != null && barrel.woodsloc.length > 0) { + StringBuilder wo = new StringBuilder(); + for (int i : barrel.woodsloc) { + wo.append(i).append(","); + } + config.set(prefix + ".wo", wo.substring(0, wo.length() - 1)); + } + if (barrel.inventory != null) { int slot = 0; - ItemStack item = null; + ItemStack item; ConfigurationSection invConfig = null; while (slot < barrel.inventory.getSize()) { item = barrel.inventory.getItem(slot); @@ -223,14 +458,15 @@ public class Barrel { } // direction of the barrel from the spigot + @SuppressWarnings("deprecation") public static int getDirection(Block spigot) { int direction = 0;// 1=x+ 2=x- 3=z+ 4=z- int typeId = spigot.getRelative(0, 0, 1).getTypeId(); - if (typeId == 5 || typeId == 53 || typeId == 134 || typeId == 135 || typeId == 136) { + if (typeId == 5 || isStairs(typeId)) { direction = 3; } typeId = spigot.getRelative(0, 0, -1).getTypeId(); - if (typeId == 5 || typeId == 53 || typeId == 134 || typeId == 135 || typeId == 136) { + if (typeId == 5 || isStairs(typeId)) { if (direction == 0) { direction = 4; } else { @@ -238,7 +474,7 @@ public class Barrel { } } typeId = spigot.getRelative(1, 0, 0).getTypeId(); - if (typeId == 5 || typeId == 53 || typeId == 134 || typeId == 135 || typeId == 136) { + if (typeId == 5 || isStairs(typeId)) { if (direction == 0) { direction = 1; } else { @@ -246,7 +482,7 @@ public class Barrel { } } typeId = spigot.getRelative(-1, 0, 0).getTypeId(); - if (typeId == 5 || typeId == 53 || typeId == 134 || typeId == 135 || typeId == 136) { + if (typeId == 5 || isStairs(typeId)) { if (direction == 0) { direction = 2; } else { @@ -258,24 +494,20 @@ public class Barrel { // is this a Large barrel? public boolean isLarge() { - if (spigot.getTypeId() == 63 || spigot.getTypeId() == 68) { - return false; - } - return true; + return !isSign(spigot); } // true for small barrels + @SuppressWarnings("deprecation") public static boolean isSign(Block spigot) { - if (spigot.getTypeId() == 63 || spigot.getTypeId() == 68) { - return true; - } - return false; + return spigot.getTypeId() == 63 || spigot.getTypeId() == 68; } // woodtype of the block the spigot is attached to + @SuppressWarnings("deprecation") public byte getWood() { int direction = getDirection(this.spigot);// 1=x+ 2=x- 3=z+ 4=z- - Block wood = null; + Block wood; if (direction == 0) { return 0; } else if (direction == 1) { @@ -314,45 +546,75 @@ public class Barrel { return 0; } + // returns the Sign of a large barrel, the spigot if there is none + public Block getSignOfSpigot() { + if (signoffset != 0) { + if (isSign(spigot)) { + return spigot; + } + + if (isSign(spigot.getRelative(0, signoffset, 0))) { + return spigot.getRelative(0, signoffset, 0); + } else { + signoffset = 0; + } + } + return spigot; + } + // returns the fence above/below a block, itself if there is none + @SuppressWarnings("deprecation") public static Block getSpigotOfSign(Block block) { int y = -2; while (y <= 1) { // Fence and Netherfence - if (block.getRelative(0, y, 0).getTypeId() == 85 || block.getRelative(0, y, 0).getTypeId() == 113) { - return (block.getRelative(0, y, 0)); + Block relative = block.getRelative(0, y, 0); + if (relative.getTypeId() == 85 || relative.getTypeId() == 113) { + return (relative); } y++; } return block; } - // returns null if Barrel is correctly placed; the block that is missing - // when not + @SuppressWarnings("deprecation") + public static boolean isStairs(Material mat) { + return isStairs(mat.getId()); + } + + public static boolean isStairs(int id) { + return id == 53 || id == 134 || id == 135 || id == 136 || id == 163 || id == 164; + } + + // returns null if Barrel is correctly placed; the block that is missing when not // the barrel needs to be formed correctly - public static Block getBrokenBlock(Block spigot) { - if (spigot.getChunk().isLoaded()) { + // flag force to also check if chunk is not loaded + public Block getBrokenBlock(boolean force) { + if (force || spigot.getChunk().isLoaded()) { spigot = getSpigotOfSign(spigot); if (isSign(spigot)) { - return checkSBarrel(spigot); + return checkSBarrel(); } else { - return checkLBarrel(spigot); + return checkLBarrel(); } } return null; } - public static Block checkSBarrel(Block spigot) { + @SuppressWarnings("deprecation") + public Block checkSBarrel() { int direction = getDirection(spigot);// 1=x+ 2=x- 3=z+ 4=z- if (direction == 0) { return spigot; } - int startX = 0; - int startZ = 0; + int startX; + int startZ; int endX; int endZ; + ArrayList stairs = new ArrayList(); + if (direction == 1) { startX = 1; endX = startX + 1; @@ -382,17 +644,20 @@ public class Barrel { while (y <= 1) { while (x <= endX) { while (z <= endZ) { - typeId = spigot.getRelative(x, y, z).getTypeId(); + Block block = spigot.getRelative(x, y, z); + typeId = block.getTypeId(); - if (typeId == 53 || typeId == 134 || typeId == 135 || typeId == 136) { + if (isStairs(typeId)) { if (y == 0) { // stairs have to be upside down - if (spigot.getRelative(x, y, z).getData() < 4) { - return spigot.getRelative(x, y, z); + if (block.getData() < 4) { + return block; } } + stairs.add(block.getX()); + stairs.add(block.getY()); + stairs.add(block.getZ()); z++; - continue; } else { return spigot.getRelative(x, y, z); } @@ -404,20 +669,24 @@ public class Barrel { x = startX; y++; } + stairsloc = ArrayUtils.toPrimitive(stairs.toArray(new Integer[stairs.size()])); return null; - } - public static Block checkLBarrel(Block spigot) { + @SuppressWarnings("deprecation") + public Block checkLBarrel() { int direction = getDirection(spigot);// 1=x+ 2=x- 3=z+ 4=z- if (direction == 0) { return spigot; } - int startX = 0; - int startZ = 0; + int startX; + int startZ; int endX; int endZ; + ArrayList stairs = new ArrayList(); + ArrayList woods = new ArrayList(); + if (direction == 1) { startX = 1; endX = startX + 3; @@ -447,37 +716,42 @@ public class Barrel { while (y <= 2) { while (x <= endX) { while (z <= endZ) { - typeId = spigot.getRelative(x, y, z).getTypeId(); + Block block = spigot.getRelative(x, y, z); + typeId = block.getTypeId(); if (direction == 1 || direction == 2) { if (y == 1 && z == 0) { - if (x == -2 || x == -3 || x == 2 || x == 3) { - z++; - continue; - } else if (x == -1 || x == -4 || x == 1 || x == 4) { - if (typeId != 0) { - z++; - continue; - } + if (x == -1 || x == -4 || x == 1 || x == 4) { + woods.add(block.getX()); + woods.add(block.getY()); + woods.add(block.getZ()); } + z++; + continue; } } else { if (y == 1 && x == 0) { - if (z == -2 || z == -3 || z == 2 || z == 3) { - z++; - continue; - } else if (z == -1 || z == -4 || z == 1 || z == 4) { - if (typeId != 0) { - z++; - continue; - } + if (z == -1 || z == -4 || z == 1 || z == 4) { + woods.add(block.getX()); + woods.add(block.getY()); + woods.add(block.getZ()); } + z++; + continue; } } - if (typeId == 5 || typeId == 53 || typeId == 134 || typeId == 135 || typeId == 136) { + if (typeId == 5 || isStairs(typeId)) { + if (typeId == 5) { + woods.add(block.getX()); + woods.add(block.getY()); + woods.add(block.getZ()); + } else { + stairs.add(block.getX()); + stairs.add(block.getY()); + stairs.add(block.getZ()); + } z++; - continue; } else { - return spigot.getRelative(x, y, z); + return block; } } z = startZ; @@ -487,8 +761,10 @@ public class Barrel { x = startX; y++; } - return null; + stairsloc = ArrayUtils.toPrimitive(stairs.toArray(new Integer[stairs.size()])); + woodsloc = ArrayUtils.toPrimitive(woods.toArray(new Integer[woods.size()])); + return null; } } diff --git a/src/com/dre/brewery/Brew.java b/src/com/dre/brewery/Brew.java index 077daa4..1f3f56e 100644 --- a/src/com/dre/brewery/Brew.java +++ b/src/com/dre/brewery/Brew.java @@ -12,8 +12,6 @@ import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.bukkit.inventory.BrewerInventory; -import com.dre.brewery.BIngredients; - public class Brew { // represents the liquid in the brewed Potions @@ -222,7 +220,7 @@ public class Brew { } else { quality /= 2; } - return (int) Math.round(quality); + return Math.round(quality); } public int getQuality() { @@ -529,10 +527,7 @@ public class Brew { // True if the PotionMeta has colored Lore public static Boolean hasColorLore(PotionMeta meta) { - if (meta.hasLore()) { - return !meta.getLore().get(1).startsWith(P.p.color("&7")); - } - return false; + return meta.hasLore() && !meta.getLore().get(1).startsWith(P.p.color("&7")); } // gets the Color that represents a quality in Lore diff --git a/src/com/dre/brewery/ConfigUpdater.java b/src/com/dre/brewery/ConfigUpdater.java index a85a1b4..c2eaa8b 100644 --- a/src/com/dre/brewery/ConfigUpdater.java +++ b/src/com/dre/brewery/ConfigUpdater.java @@ -47,7 +47,7 @@ public class ConfigUpdater { public void saveConfig() { StringBuilder stringBuilder = new StringBuilder(""); for (String line : config) { - stringBuilder.append(line + "\n"); + stringBuilder.append(line).append("\n"); } String configString = stringBuilder.toString().trim(); @@ -87,24 +87,35 @@ public class ConfigUpdater { lang = "de"; } } + if (fromVersion.equals("0.5") || fromVersion.equals("1.0")) { if (lang.equals("de")) { update05de(); } else { update10en(); } - } else { + fromVersion = "1.1"; + } + if (fromVersion.equals("1.1") || fromVersion.equals("1.1.1")) { + if (lang.equals("de")) { + update11de(); + } else { + update11en(); + } + fromVersion = "1.2"; + } + + if (!fromVersion.equals("1.2")) { P.p.log(P.p.languageReader.get("Error_ConfigUpdate", fromVersion)); return; } saveConfig(); } - // Updates de from 0.5 to 1.1 - private void update05de() { - // Update version String + // Update the Version String + private void updateVersion(String to) { int index = indexOfStart("version"); - String line = "version: '1.1'"; + String line = "version: '" + to + "'"; if (index != -1) { setLine(index, line); } else { @@ -118,9 +129,14 @@ public class ConfigUpdater { addLines(index, line); } } + } + + // Updates de from 0.5 to 1.1 + private void update05de() { + updateVersion("1.1"); // Default language to de - index = indexOfStart("language: en"); + int index = indexOfStart("language: en"); if (index != -1) { setLine(index, "language: de"); P.p.language = "de"; @@ -164,7 +180,7 @@ public class ConfigUpdater { } // Add some new separators for overview - line = "# -- Verschiedene Einstellungen --"; + String line = "# -- Verschiedene Einstellungen --"; index = indexOfStart("# Verschiedene Einstellungen"); if (index != -1) { setLine(index, line); @@ -180,21 +196,7 @@ public class ConfigUpdater { // Updates en from 1.0 to 1.1 private void update10en() { // Update version String - int index = indexOfStart("version"); - String line = "version: '1.1'"; - if (index != -1) { - setLine(index, line); - } else { - index = indexOfStart("# Config Version"); - if (index == -1) { - index = indexOfStart("autosave"); - } - if (index == -1) { - appendLines(line); - } else { - addLines(index, line); - } - } + updateVersion("1.1"); // Add the new entries for the Word Distortion above the words section String[] entries = { @@ -217,7 +219,7 @@ public class ConfigUpdater { "- '[,]'", "" }; - index = indexOfStart("# words"); + int index = indexOfStart("# words"); if (index == -1) { index = indexOfStart("# Will be processed"); } @@ -234,7 +236,7 @@ public class ConfigUpdater { } // Add some new separators for overview - line = "# -- Settings --"; + String line = "# -- Settings --"; index = indexOfStart("# Settings"); if (index != -1) { setLine(index, line); @@ -247,4 +249,68 @@ public class ConfigUpdater { } } + // Updates de from 1.1 to 1.2 + private void update11de() { + updateVersion("1.2"); + + int index = indexOfStart("# Das Item kann nicht aufgesammelt werden"); + if (index != -1) { + setLine(index, "# Das Item kann nicht aufgesammelt werden und bleibt bis zum Despawnen liegen. (Achtung: Kann nach Serverrestart aufgesammelt werden!)"); + } + + // Add the BarrelAccess Setting + String[] lines = { + "# Ob große Fässer an jedem Block geöffnet werden können, nicht nur an Zapfhahn und Schild. Bei kleinen Fässern geht dies immer. [true]", + "openLargeBarrelEverywhere: true", + "" + }; + index = indexOfStart("colorInBrewer") + 2; + if (index == 1) { + index = indexOfStart("colorInBarrels") + 2; + } + if (index == 1) { + index = indexOfStart("# Autosave"); + } + if (index == -1) { + index = indexOfStart("language") + 2; + } + if (index == 1) { + addLines(3, lines); + } else { + addLines(index, lines); + } + } + + // Updates en from 1.1 to 1.2 + private void update11en() { + updateVersion("1.2"); + + int index = indexOfStart("# The item can not be collected"); + if (index != -1) { + setLine(index, "# The item can not be collected and stays on the ground until it despawns. (Warning: Can be collected after Server restart!)"); + } + + // Add the BarrelAccess Setting + String[] lines = { + "# If a Large Barrel can be opened by clicking on any of its blocks, not just Spigot or Sign. This is always true for Small Barrels. [true]", + "openLargeBarrelEverywhere: true", + "" + }; + index = indexOfStart("colorInBrewer") + 2; + if (index == 1) { + index = indexOfStart("colorInBarrels") + 2; + } + if (index == 1) { + index = indexOfStart("# Autosave"); + } + if (index == -1) { + index = indexOfStart("language") + 2; + } + if (index == 1) { + addLines(3, lines); + } else { + addLines(index, lines); + } + } + } diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index ddc0651..b1acad5 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -8,6 +8,7 @@ import java.util.HashMap; import java.io.IOException; import java.io.File; +import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -32,6 +33,9 @@ public class P extends JavaPlugin { public static int lastSave = 1; public static int autosave = 3; + // Third Party Enabled + public boolean hasLWC; + // Listeners public BlockListener blockListener; public PlayerListener playerListener; @@ -47,14 +51,14 @@ public class P extends JavaPlugin { public void onEnable() { p = this; + // Check Third Party + hasLWC = getServer().getPluginManager().isPluginEnabled("LWC"); + readConfig(); readData(); // Setup Metrics setupMetrics(); - - // Load LanguageReader - languageReader = new LanguageReader(new File(p.getDataFolder(), "languages/" + language + ".yml")); // Listeners blockListener = new BlockListener(); @@ -170,6 +174,9 @@ public class P extends JavaPlugin { // Set the Language language = config.getString("language", "en"); + // Load LanguageReader + languageReader = new LanguageReader(new File(p.getDataFolder(), "languages/" + language + ".yml")); + // Check if config is the newest version String version = config.getString("version", null); if (version != null) { @@ -193,6 +200,7 @@ public class P extends JavaPlugin { BPlayer.homeType = config.getString("homeType", null); Brew.colorInBarrels = config.getBoolean("colorInBarrels", false); Brew.colorInBrewer = config.getBoolean("colorInBrewer", false); + PlayerListener.openEverywhere = config.getBoolean("openLargeBarrelEverywhere", false); Words.log = config.getBoolean("logRealChat", false); Words.commands = config.getStringList("distortCommands"); Words.doSigns = config.getBoolean("distortSignText", false); @@ -390,12 +398,15 @@ public class P extends JavaPlugin { ConfigurationSection invSection = section.getConfigurationSection(barrel + ".inv"); Block block = world.getBlockAt(parseInt(splitted[0]), parseInt(splitted[1]), parseInt(splitted[2])); float time = (float) section.getDouble(barrel + ".time", 0.0); + byte sign = (byte) section.getInt(barrel + ".sign", 0); + String[] st = section.getString(barrel + ".st", "").split(","); + String[] wo = section.getString(barrel + ".wo", "").split(","); if (invSection != null) { - new Barrel(block, invSection.getValues(true), time); + new Barrel(block, sign, st, wo, invSection.getValues(true), time); } else { // Barrel has no inventory - new Barrel(block, time); + new Barrel(block, sign, st, wo, null, time); } } else { @@ -561,6 +572,62 @@ public class P extends JavaPlugin { } } + // Returns true if the Block can be destroyed by the Player + public boolean blockDestroy(Block block, Player player) { + switch (block.getType()) { + case CAULDRON: + // will only remove when existing + BCauldron.remove(block); + return true; + case FENCE: + case NETHER_FENCE: + // remove barrel and throw potions on the ground + Barrel barrel = Barrel.getBySpigot(block); + if (barrel != null) { + if (barrel.hasPermsDestroy(player)) { + barrel.remove(null); + return true; + } else { + return false; + } + } + return true; + case SIGN: + case WALL_SIGN: + // remove small Barrels + Barrel barrel2 = Barrel.getBySpigot(block); + if (barrel2 != null) { + if (!barrel2.isLarge()) { + if (barrel2.hasPermsDestroy(player)) { + barrel2.remove(null); + return true; + } else { + return false; + } + } else { + barrel2.destroySign(); + } + } + return true; + case WOOD: + case WOOD_STAIRS: + case ACACIA_STAIRS: + case BIRCH_WOOD_STAIRS: + case DARK_OAK_STAIRS: + case JUNGLE_WOOD_STAIRS: + case SPRUCE_WOOD_STAIRS: + Barrel barrel3 = Barrel.getByWood(block); + if (barrel3 != null) { + if (barrel3.hasPermsDestroy(player)) { + barrel3.remove(block); + } else { + return false; + } + } + } + return true; + } + public String color(String msg) { if (msg != null) { msg = ChatColor.translateAlternateColorCodes('&', msg); diff --git a/src/com/dre/brewery/Wakeup.java b/src/com/dre/brewery/Wakeup.java index 597daa1..af20fea 100644 --- a/src/com/dre/brewery/Wakeup.java +++ b/src/com/dre/brewery/Wakeup.java @@ -241,7 +241,7 @@ public class Wakeup { } String worldName = wakeup.loc.getWorld().getName(); - String prefix = null; + String prefix; if (worldName.startsWith("DXL_")) { prefix = p.getDxlName(worldName) + "." + id; diff --git a/src/com/dre/brewery/Words.java b/src/com/dre/brewery/Words.java index 6961715..dfba9b8 100644 --- a/src/com/dre/brewery/Words.java +++ b/src/com/dre/brewery/Words.java @@ -11,8 +11,6 @@ import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.block.SignChangeEvent; -import com.dre.brewery.BPlayer; - public class Words { // represends Words and letters, that are replaced in drunk players messages @@ -231,7 +229,7 @@ public class Words { // remove all "from" and split "words" there String[] splitted = words.split(from); int index = 0; - String part = null; + String part; // if there are occurences of "from" if (splitted.length > 1) { @@ -272,7 +270,7 @@ public class Words { boolean isBefore = !match; if (pre != null) { for (String pr : pre) { - if (match == true) { + if (match) { // if one is correct, it is enough if (part.endsWith(pr) == match) { isBefore = true; diff --git a/src/com/dre/brewery/integration/LWCBarrel.java b/src/com/dre/brewery/integration/LWCBarrel.java new file mode 100644 index 0000000..67a7474 --- /dev/null +++ b/src/com/dre/brewery/integration/LWCBarrel.java @@ -0,0 +1,96 @@ +package com.dre.brewery.integration; + +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.EventException; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.RegisteredListener; + +import com.dre.brewery.Barrel; +import com.dre.brewery.P; +import com.griefcraft.listeners.LWCPlayerListener; +import com.griefcraft.lwc.LWC; +import com.griefcraft.model.Flag; +import com.griefcraft.model.Protection; +import com.griefcraft.scripting.event.LWCProtectionDestroyEvent; + +public class LWCBarrel { + + + public static boolean checkDestroy(Player player, Barrel barrel) { + LWC lwc = LWC.getInstance(); + Block sign = barrel.getSignOfSpigot(); + //if (!Boolean.parseBoolean(lwc.resolveProtectionConfiguration(sign, "ignoreBlockDestruction"))) { + Protection protection = lwc.findProtection(sign); + if (protection != null) { + boolean canAccess = lwc.canAccessProtection(player, protection); + boolean canAdmin = lwc.canAdminProtection(player, protection); + + try { + LWCProtectionDestroyEvent evt = new LWCProtectionDestroyEvent(player, protection, LWCProtectionDestroyEvent.Method.BLOCK_DESTRUCTION, canAccess, canAdmin); + lwc.getModuleLoader().dispatchEvent(evt); + + if (evt.isCancelled()) { + return false; + } + } catch (Exception e) { + lwc.sendLocale(player, "protection.internalerror", "id", "BLOCK_BREAK"); + P.p.errorLog("Failed to dispatch LWCProtectionDestroyEvent"); + e.printStackTrace(); + return false; + } + } + //} + + return true; + } + + public static boolean checkAccess(Player player, Block sign, PlayerInteractEvent event, Plugin plugin) { + LWC lwc = LWC.getInstance(); + + // Disallow Chest Access with these permissions + if (!lwc.hasPermission(player, "lwc.protect") && lwc.hasPermission(player, "lwc.deny") && !lwc.isAdmin(player) && !lwc.isMod(player)) { + lwc.sendLocale(player, "protection.interact.error.blocked"); + return false; + } + + // We just fake a BlockInteractEvent on the Sign for LWC, it handles it nicely. Otherwise we could copy LWCs listener in here... + PlayerInteractEvent lwcEvent = new PlayerInteractEvent(player, event.getAction(), event.getItem(), sign, event.getBlockFace()); + for (RegisteredListener listener : HandlerList.getRegisteredListeners(plugin)) { + if (listener.getListener() instanceof LWCPlayerListener) { + try { + listener.callEvent(lwcEvent); + if (lwcEvent.isCancelled()) { + return false; + } + } catch (EventException e) { + lwc.sendLocale(player, "protection.internalerror", "id", "PLAYER_INTERACT"); + P.p.errorLog("Block Interact could not be passed to LWC"); + e.printStackTrace(); + return false; + } + } + } + + return true; + } + + // 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); + return false; + } + + if (protection.hasFlag(Flag.Type.ALLOWEXPLOSIONS)) { + protection.remove(); + barrel.remove(block); + return false; + } + return true; + } +} diff --git a/src/com/dre/brewery/integration/WGBarrel.java b/src/com/dre/brewery/integration/WGBarrel.java new file mode 100644 index 0000000..bd405c6 --- /dev/null +++ b/src/com/dre/brewery/integration/WGBarrel.java @@ -0,0 +1,31 @@ +package com.dre.brewery.integration; + +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import com.dre.brewery.P; +import com.sk89q.worldguard.LocalPlayer; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.flags.DefaultFlag; + +public class WGBarrel { + + public static boolean checkAccess(Player player, Block spigot, Plugin plugin) { + WorldGuardPlugin wg = (WorldGuardPlugin) plugin; + if (!wg.getGlobalRegionManager().hasBypass(player, player.getWorld())) { + ApplicableRegionSet region = wg.getRegionManager(player.getWorld()).getApplicableRegions(spigot.getLocation()); + if (region != null) { + LocalPlayer localPlayer = wg.wrapPlayer(player); + if (!region.allows(DefaultFlag.CHEST_ACCESS, localPlayer)) { + if (!region.canBuild(localPlayer)) { + P.p.msg(player, P.p.languageReader.get("Error_NoBarrelAccess")); + return false; + } + } + } + } + return true; + } +} diff --git a/src/com/dre/brewery/listeners/BlockListener.java b/src/com/dre/brewery/listeners/BlockListener.java index f1e17d6..38a544e 100644 --- a/src/com/dre/brewery/listeners/BlockListener.java +++ b/src/com/dre/brewery/listeners/BlockListener.java @@ -1,14 +1,14 @@ package com.dre.brewery.listeners; +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.BlockPistonExtendEvent; +import org.bukkit.event.block.BlockPistonRetractEvent; import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.Material; -import org.bukkit.block.Block; -import com.dre.brewery.BCauldron; import com.dre.brewery.Barrel; import com.dre.brewery.BPlayer; import com.dre.brewery.Words; @@ -27,7 +27,7 @@ public class BlockListener implements Listener { } } - @EventHandler(priority = EventPriority.LOWEST) + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onSignChangeLow(SignChangeEvent event) { if (Words.doSigns) { if (BPlayer.players.containsKey(event.getPlayer().getName())) { @@ -36,28 +36,30 @@ public class BlockListener implements Listener { } } - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onBlockBreak(BlockBreakEvent event) { - Block block = event.getBlock(); - // remove cauldron - if (block.getType() == Material.CAULDRON) { - if (block.getData() != 0) { - // will only remove when existing - BCauldron.remove(block); + if (!P.p.blockDestroy(event.getBlock(), event.getPlayer())) { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onPistonRetract(BlockPistonRetractEvent event) { + if (event.isSticky()) { + Block block = event.getRetractLocation().getBlock(); + + if (Barrel.get(block) != null) { + event.setCancelled(true); } - // remove barrel and throw potions on the ground - } else if (block.getType() == Material.FENCE || block.getType() == Material.NETHER_FENCE) { - Barrel barrel = Barrel.get(block); - if (barrel != null) { - barrel.remove(null); - } - // remove small Barrels - } else if (block.getType() == Material.SIGN || block.getType() == Material.WALL_SIGN) { - Barrel barrel = Barrel.get(block); - if (barrel != null) { - if (!barrel.isLarge()) { - barrel.remove(null); - } + } + } + + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onPistonExtend(BlockPistonExtendEvent event) { + for (Block block : event.getBlocks()) { + if (Barrel.get(block) != null) { + event.setCancelled(true); + return; } } } diff --git a/src/com/dre/brewery/listeners/EntityListener.java b/src/com/dre/brewery/listeners/EntityListener.java index 402843b..c5463e4 100644 --- a/src/com/dre/brewery/listeners/EntityListener.java +++ b/src/com/dre/brewery/listeners/EntityListener.java @@ -1,15 +1,22 @@ package com.dre.brewery.listeners; +import java.util.ListIterator; + +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.EntityExplodeEvent; import org.bukkit.event.entity.ItemDespawnEvent; import org.bukkit.event.entity.EntityCombustEvent; import org.bukkit.entity.Entity; import org.bukkit.entity.Item; import org.bukkit.inventory.ItemStack; +import com.dre.brewery.Barrel; import com.dre.brewery.Brew; +import com.dre.brewery.P; +import com.dre.brewery.integration.LWCBarrel; public class EntityListener implements Listener { @@ -35,4 +42,25 @@ public class EntityListener implements Listener { } } + // --- Barrel Breaking --- + + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onExplode(EntityExplodeEvent event) { + ListIterator iter = event.blockList().listIterator(); + Barrel barrel = null; + while (iter.hasNext()) { + Block block = iter.next(); + if (barrel == null || !barrel.hasBlock(block)) { + barrel = Barrel.get(block); + } + if (barrel != null) { + if (P.p.hasLWC) { + if (LWCBarrel.blockExplosion(barrel, block)) { + iter.remove(); + } + } + } + } + } + } diff --git a/src/com/dre/brewery/listeners/InventoryListener.java b/src/com/dre/brewery/listeners/InventoryListener.java index c56f24f..3d632f0 100644 --- a/src/com/dre/brewery/listeners/InventoryListener.java +++ b/src/com/dre/brewery/listeners/InventoryListener.java @@ -1,6 +1,5 @@ package com.dre.brewery.listeners; -import org.bukkit.entity.Item; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -87,9 +86,7 @@ public class InventoryListener implements Listener { // block the pickup of items where getPickupDelay is > 1000 (puke) @EventHandler(ignoreCancelled = true) public void onInventoryPickupItem(InventoryPickupItemEvent event){ - Item item = event.getItem(); - - if (item.getPickupDelay() > 1000) { + if (event.getItem().getPickupDelay() > 1000) { event.setCancelled(true); } } diff --git a/src/com/dre/brewery/listeners/PlayerListener.java b/src/com/dre/brewery/listeners/PlayerListener.java index 4574233..8711254 100644 --- a/src/com/dre/brewery/listeners/PlayerListener.java +++ b/src/com/dre/brewery/listeners/PlayerListener.java @@ -17,7 +17,6 @@ import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; -import org.bukkit.plugin.Plugin; import com.dre.brewery.BCauldron; import com.dre.brewery.BIngredients; @@ -27,12 +26,12 @@ import com.dre.brewery.BPlayer; import com.dre.brewery.Words; import com.dre.brewery.Wakeup; import com.dre.brewery.P; -import com.sk89q.worldguard.LocalPlayer; -import com.sk89q.worldguard.bukkit.WorldGuardPlugin; -import com.sk89q.worldguard.protection.ApplicableRegionSet; -import com.sk89q.worldguard.protection.flags.DefaultFlag; + public class PlayerListener implements Listener { + public static boolean openEverywhere; + + @SuppressWarnings("deprecation") @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onPlayerInteract(PlayerInteractEvent event) { Block clickedBlock = event.getClickedBlock(); @@ -41,9 +40,10 @@ public class PlayerListener implements Listener { if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { Player player = event.getPlayer(); if (!player.isSneaking()) { - if (clickedBlock.getType() == Material.CAULDRON) { - if (clickedBlock.getRelative(BlockFace.DOWN).getType() == Material.FIRE || clickedBlock.getRelative(BlockFace.DOWN).getType() == Material.STATIONARY_LAVA - || clickedBlock.getRelative(BlockFace.DOWN).getType() == Material.LAVA) { + Material type = clickedBlock.getType(); + if (type == Material.CAULDRON) { + Block down = clickedBlock.getRelative(BlockFace.DOWN); + if (down.getType() == Material.FIRE || down.getType() == Material.STATIONARY_LAVA || down.getType() == Material.LAVA) { Material materialInHand = event.getMaterial(); ItemStack item = event.getItem(); @@ -90,38 +90,37 @@ public class PlayerListener implements Listener { } else { event.setCancelled(true); } + return; } - // access a barrel - } else if (clickedBlock.getType() == Material.FENCE || clickedBlock.getType() == Material.NETHER_FENCE || clickedBlock.getType() == Material.SIGN - || clickedBlock.getType() == Material.WALL_SIGN) { - Barrel barrel = Barrel.get(clickedBlock); - if (barrel != null) { - event.setCancelled(true); + } - Plugin plugin = P.p.getServer().getPluginManager().getPlugin("WorldGuard"); - if (plugin != null && plugin instanceof WorldGuardPlugin) { - WorldGuardPlugin wg = (WorldGuardPlugin) plugin; - if (!wg.getGlobalRegionManager().hasBypass(player, player.getWorld())) { - ApplicableRegionSet region = wg.getRegionManager(player.getWorld()).getApplicableRegions(clickedBlock.getLocation()); - if (region != null) { - LocalPlayer localPlayer = wg.wrapPlayer(player); - if (!region.allows(DefaultFlag.CHEST_ACCESS, localPlayer)) { - if (!region.canBuild(localPlayer)) { - P.p.msg(player, P.p.languageReader.get("Error_NoBarrelAccess")); - return; - } - } - } + // Access a Barrel + Barrel barrel = null; + if (type == Material.WOOD) { + if (openEverywhere) { + barrel = Barrel.get(clickedBlock); + } + } else if (Barrel.isStairs(type)) { + for (Barrel barrel2 : Barrel.barrels) { + if (barrel2.hasStairsBlock(clickedBlock)) { + if (openEverywhere || !barrel2.isLarge()) { + barrel = barrel2; } - } - Block broken = Barrel.getBrokenBlock(clickedBlock); - // barrel is built correctly - if (broken == null) { - barrel.open(player); - } else { - barrel.remove(broken); + break; } } + } else if (type == Material.FENCE || type == Material.NETHER_FENCE || type == Material.SIGN || type == Material.WALL_SIGN) { + barrel = Barrel.getBySpigot(clickedBlock); + } + + if (barrel != null) { + event.setCancelled(true); + + if (!barrel.hasPermsOpen(player, event)) { + return; + } + + barrel.open(player); } } }