From 8e11062f5640ac2da7ac94de9f9843207a89e469 Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Sat, 27 Jul 2013 17:31:42 +0200 Subject: [PATCH] Use Watch to display cooking time --- src/com/dre/brewery/BCauldron.java | 80 ++++++++++++------- .../dre/brewery/listeners/PlayerListener.java | 26 +++--- 2 files changed, 66 insertions(+), 40 deletions(-) diff --git a/src/com/dre/brewery/BCauldron.java b/src/com/dre/brewery/BCauldron.java index 35684e8..9e01438 100644 --- a/src/com/dre/brewery/BCauldron.java +++ b/src/com/dre/brewery/BCauldron.java @@ -53,55 +53,75 @@ public class BCauldron { } } + // get cauldron by Block + public static BCauldron get(Block block) { + for (BCauldron bcauldron : bcauldrons) { + if (bcauldron.block.equals(block)) { + return bcauldron; + } + } + return null; + } + // get cauldron from block and add given ingredient public static boolean ingredientAdd(Block block, Material ingredient) { // if not empty if (block.getData() != 0) { - for (BCauldron bcauldron : bcauldrons) { - if (bcauldron.block.equals(block)) { - bcauldron.add(ingredient); - return true; - } + BCauldron bcauldron = get(block); + if (bcauldron != null) { + bcauldron.add(ingredient); + return true; + } else { + new BCauldron(block, ingredient); + return true; } - new BCauldron(block, ingredient); - return true; } return false; } // fills players bottle with cooked brew public static boolean fill(Player player, Block block) { - for (BCauldron bcauldron : bcauldrons) { - if (bcauldron.block.equals(block)) { - ItemStack potion = bcauldron.ingredients.cook(bcauldron.state); - if (potion != null) { - // Bukkit Bug, inventory not updating while in event so this - // will delay the give - // but could also just use deprecated updateInventory() - giveItem(player, potion); - // player.getInventory().addItem(potion); - // player.getInventory().updateInventory(); - if (block.getData() > 3) { - block.setData((byte) 3); - } - block.setData((byte) (block.getData() - 1)); - - if (block.getData() == 0) { - bcauldrons.remove(bcauldron); - } - return true; + BCauldron bcauldron = get(block); + if (bcauldron != null) { + ItemStack potion = bcauldron.ingredients.cook(bcauldron.state); + if (potion != null) { + // Bukkit Bug, inventory not updating while in event so this + // will delay the give + // but could also just use deprecated updateInventory() + giveItem(player, potion); + // player.getInventory().addItem(potion); + // player.getInventory().updateInventory(); + if (block.getData() > 3) { + block.setData((byte) 3); } + block.setData((byte) (block.getData() - 1)); + + if (block.getData() == 0) { + bcauldrons.remove(bcauldron); + } + return true; } } return false; } + // prints the current cooking time to the player + public static void printTime(Player player, Block block) { + BCauldron bcauldron = get(block); + if (bcauldron != null) { + if (bcauldron.state > 1) { + P.p.msg(player, "Dieser Kessel siedet nun seit " + bcauldron.state + " Minuten"); + } else { + P.p.msg(player, "Dieser Kessel siedet seit weniger als einer Minute"); + } + } + } + // reset to normal cauldron public static void remove(Block block) { - for (BCauldron bcauldron : bcauldrons) { - if (bcauldron.block.equals(block)) { - bcauldrons.remove(bcauldron); - } + BCauldron bcauldron = get(block); + if (bcauldron != null) { + bcauldrons.remove(bcauldron); } } diff --git a/src/com/dre/brewery/listeners/PlayerListener.java b/src/com/dre/brewery/listeners/PlayerListener.java index 1feb465..cee0318 100644 --- a/src/com/dre/brewery/listeners/PlayerListener.java +++ b/src/com/dre/brewery/listeners/PlayerListener.java @@ -36,16 +36,10 @@ public class PlayerListener implements Listener { Player player = event.getPlayer(); ItemStack item = event.getItem(); - // add ingredient to cauldron that meet the previous - // contitions - if (BIngredients.possibleIngredients.contains(materialInHand)) { - if (BCauldron.ingredientAdd(clickedBlock, materialInHand)) { - if (item.getAmount() > 1) { - item.setAmount(item.getAmount() - 1); - } else { - player.setItemInHand(new ItemStack(0)); - } - } + + if (materialInHand == Material.WATCH) { + BCauldron.printTime(player, clickedBlock); + // fill a glass bottle with potion } else if (materialInHand == Material.GLASS_BOTTLE) { if (BCauldron.fill(player, clickedBlock)) { @@ -56,6 +50,7 @@ public class PlayerListener implements Listener { player.setItemInHand(new ItemStack(0)); } } + // reset cauldron when refilling to prevent // unlimited source of potions } else if (materialInHand == Material.WATER_BUCKET) { @@ -65,6 +60,17 @@ public class PlayerListener implements Listener { BCauldron.remove(clickedBlock); } } + + // add ingredient to cauldron that meet the previous + // contitions + } else if (BIngredients.possibleIngredients.contains(materialInHand)) { + if (BCauldron.ingredientAdd(clickedBlock, materialInHand)) { + if (item.getAmount() > 1) { + item.setAmount(item.getAmount() - 1); + } else { + player.setItemInHand(new ItemStack(0)); + } + } } } // access a barrel