From 7a514d5378907e2fa0c95ca020fba99d96aef504 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Mon, 21 Mar 2016 20:35:12 +0100 Subject: [PATCH 01/10] Update to 1.9 --- pom.xml | 216 ++- resources/plugin.yml | 2 +- src/com/dre/brewery/BIngredients.java | 31 +- src/com/dre/brewery/BPlayer.java | 19 +- src/com/dre/brewery/BRecipe.java | 9 +- src/com/dre/brewery/Brew.java | 1350 +++++++++--------- src/com/dre/brewery/P.java | 72 +- src/com/dre/brewery/listeners/Compat1_9.java | 31 + 8 files changed, 886 insertions(+), 844 deletions(-) create mode 100644 src/com/dre/brewery/listeners/Compat1_9.java diff --git a/pom.xml b/pom.xml index d4ce54f..d77833b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,143 +1,111 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.dre - brewery - 1.3.2 - Brewery + com.dre + brewery + 1.4-SNAPSHOT + Brewery - - UTF-8 - UTF-8 - + + UTF-8 + UTF-8 + - - src + + src - - - - false - ${project.basedir}/resources - - **/*.yml - - - target/** - .travis.yml - - - + + + + false + ${project.basedir}/resources + + **/*.yml + + + target/** + .travis.yml + + + - - - maven-compiler-plugin - 3.1 - - 1.6 - 1.6 - UTF-8 - - - - - - - - spigot-repo - https://hub.spigotmc.org/nexus/content/groups/public/ - - true - always - - + + + maven-compiler-plugin + 2.5.1 + + 1.7 + 1.7 + UTF-8 + + + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/groups/public/ + vault-repo http://nexus.theyeticave.net/content/repositories/pub_releases + + dre2n-repo + http://feuerstern.bplaced.net/repo/ + + + sk89q-repo + http://maven.sk89q.com/artifactory/repo/ + + + addstar-repo + http://maven.addstar.com.au/artifactory/ext-release-local/ + + true + always + + + + md_5-releases + http://repo.md-5.net/content/repositories/releases/ + + - - sk89q-repo - http://maven.sk89q.com/artifactory/repo/ - - true - always - - - - - dre-repo - http://server.die-reiche-erethons.com/maven2 - - true - always - - - - - addstar-repo - http://maven.addstar.com.au/artifactory/ext-release-local/ - - true - always - - - - - - - - - org.spigotmc - spigot-api - 1.8-R0.1-SNAPSHOT - - + + + org.bukkit + bukkit + 1.9-R0.1-SNAPSHOT + net.milkbowl.vault VaultAPI 1.5 provided - - - com.sk89q - worldguard - 6.0.0-SNAPSHOT - - - - com.dre - managerxl - 0.0.1-SNAPSHOT - compile - jar - - - - com.griefcraft - lwc - 4.4.0 - compile - jar - - - - me.ryanhamshire - griefprevention - 7.8 - compile - jar - - - - de.diddiz.LogBlock - LogBlock - 1.80 - compile - jar - - + + com.sk89q + worldguard + 6.1 + + + com.griefcraft + entitylwc + 1.7.2 + + + me.ryanhamshire + GriefPrevention + 13.9.1 + compile + + + de.diddiz + logblock + 1.94 + + diff --git a/resources/plugin.yml b/resources/plugin.yml index bb0bf5f..81f6eda 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -1,5 +1,5 @@ name: Brewery -version: 1.3.2 +version: 1.4-SNAPSHOT main: com.dre.brewery.P authors: [Milan Albrecht, Frank Baumann] softdepend: [LWC, LogBlock, WorldGuard, GriefPrevention, Vault] diff --git a/src/com/dre/brewery/BIngredients.java b/src/com/dre/brewery/BIngredients.java index 2a7874b..e26896c 100644 --- a/src/com/dre/brewery/BIngredients.java +++ b/src/com/dre/brewery/BIngredients.java @@ -1,16 +1,15 @@ package com.dre.brewery; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.Map; -import java.util.HashMap; import java.util.Set; - +import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; -import org.bukkit.Material; -import org.bukkit.potion.PotionEffectType; import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.potion.PotionEffectType; public class BIngredients { public static Set possibleIngredients = new HashSet(); @@ -84,7 +83,11 @@ public class BIngredients { Brew.addOrReplaceEffects(potionMeta, brew.getEffects(), brew.getQuality()); cookedName = cookRecipe.getName(quality); - potion.setDurability(Brew.PotionColor.valueOf(cookRecipe.getColor()).getColorId(false)); + if (P.use1_9) { + potionMeta.setMainEffect(Brew.PotionColor.valueOf(cookRecipe.getColor()).getEffect()); + } else { + potion.setDurability(Brew.PotionColor.valueOf(cookRecipe.getColor()).getColorId(false)); + } } else { // new base potion @@ -92,14 +95,22 @@ public class BIngredients { if (state <= 1) { cookedName = P.p.languageReader.get("Brew_ThickBrew"); - potion.setDurability(Brew.PotionColor.BLUE.getColorId(false)); + if (P.use1_9) { + potionMeta.setMainEffect(Brew.PotionColor.BLUE.getEffect()); + } else { + potion.setDurability(Brew.PotionColor.BLUE.getColorId(false)); + } } else { for (Material ingredient : materials.keySet()) { if (cookedNames.containsKey(ingredient)) { // if more than half of the ingredients is of one kind if (materials.get(ingredient) > (getIngredientsCount() / 2)) { cookedName = cookedNames.get(ingredient); - potion.setDurability(Brew.PotionColor.CYAN.getColorId(true)); + if (P.use1_9) { + potionMeta.setMainEffect(Brew.PotionColor.CYAN.getEffect()); + } else { + potion.setDurability(Brew.PotionColor.CYAN.getColorId(true)); + } } } } @@ -108,7 +119,11 @@ public class BIngredients { if (cookedName == null) { // if no name could be found cookedName = P.p.languageReader.get("Brew_Undefined"); - potion.setDurability(Brew.PotionColor.CYAN.getColorId(true)); + if (P.use1_9) { + potionMeta.setMainEffect(Brew.PotionColor.CYAN.getEffect()); + } else { + potion.setDurability(Brew.PotionColor.CYAN.getColorId(true)); + } } potionMeta.setDisplayName(P.p.color("&f" + cookedName)); diff --git a/src/com/dre/brewery/BPlayer.java b/src/com/dre/brewery/BPlayer.java index ed198a1..c577433 100644 --- a/src/com/dre/brewery/BPlayer.java +++ b/src/com/dre/brewery/BPlayer.java @@ -1,22 +1,21 @@ package com.dre.brewery; import java.util.ArrayList; -import java.util.Map; import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import java.util.UUID; - +import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.OfflinePlayer; -import org.bukkit.event.player.PlayerMoveEvent; -import org.bukkit.entity.Player; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Entity; import org.bukkit.entity.Item; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.inventory.ItemStack; -import org.bukkit.Material; -import org.bukkit.util.Vector; -import org.bukkit.Location; import org.bukkit.potion.PotionEffectType; -import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.util.Vector; public class BPlayer { private static Map players = new HashMap();// Players name/uuid and BPlayer @@ -339,10 +338,6 @@ public class BPlayer { Location home = null; if (homeType.equalsIgnoreCase("bed")) { home = player.getBedSpawnLocation(); - } else if (homeType.equalsIgnoreCase("ManagerXL")) { - if (com.dre.managerxl.MPlayer.get(player.getName()) != null) { - home = com.dre.managerxl.MPlayer.get(player.getName()).getHome(); - } } else if (homeType.startsWith("cmd: ")) { player.performCommand(homeType.substring(5)); } else if (homeType.startsWith("cmd:")) { diff --git a/src/com/dre/brewery/BRecipe.java b/src/com/dre/brewery/BRecipe.java index 320a832..d772dc9 100644 --- a/src/com/dre/brewery/BRecipe.java +++ b/src/com/dre/brewery/BRecipe.java @@ -2,9 +2,8 @@ package com.dre.brewery; import java.util.ArrayList; import java.util.List; - -import org.bukkit.configuration.ConfigurationSection; import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.potion.PotionEffectType; @@ -205,7 +204,11 @@ public class BRecipe { Brew brew = new Brew(uid, bIngredients, quality, distillruns, getAge(), wood, getName(5), false, false, true); - potion.setDurability(Brew.PotionColor.valueOf(getColor()).getColorId(false)); + if (P.use1_9) { + potionMeta.setMainEffect(Brew.PotionColor.valueOf(getColor()).getEffect()); + } else { + potion.setDurability(Brew.PotionColor.valueOf(getColor()).getColorId(false)); + } potionMeta.setDisplayName(P.p.color("&f" + getName(quality))); // This effect stores the UID in its Duration potionMeta.addCustomEffect((PotionEffectType.REGENERATION).createEffect((uid * 4), 0), true); diff --git a/src/com/dre/brewery/Brew.java b/src/com/dre/brewery/Brew.java index 546f131..435932a 100644 --- a/src/com/dre/brewery/Brew.java +++ b/src/com/dre/brewery/Brew.java @@ -1,677 +1,699 @@ package com.dre.brewery; -import java.util.Map; -import java.util.HashMap; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; - +import java.util.Map; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.inventory.BrewerInventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -import org.bukkit.inventory.BrewerInventory; public class Brew { - // represents the liquid in the brewed Potions - - public static Map potions = new HashMap(); - public static Boolean colorInBarrels; // color the Lore while in Barrels - public static Boolean colorInBrewer; // color the Lore while in Brewer - - private BIngredients ingredients; - private int quality; - private int distillRuns; - private float ageTime; - private float wood; - private BRecipe currentRecipe; - private boolean unlabeled; - private boolean persistent; - private boolean stat; // static potions should not be changed - - public Brew(int uid, BIngredients ingredients) { - this.ingredients = ingredients; - potions.put(uid, this); - } - - // quality already set - public Brew(int uid, int quality, BRecipe recipe, BIngredients ingredients) { - this.ingredients = ingredients; - this.quality = quality; - this.currentRecipe = recipe; - potions.put(uid, this); - } - - // loading from file - public Brew(int uid, BIngredients ingredients, int quality, int distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean persistent, boolean stat) { - potions.put(uid, this); - this.ingredients = ingredients; - this.quality = quality; - this.distillRuns = distillRuns; - this.ageTime = ageTime; - this.wood = wood; - this.unlabeled = unlabeled; - this.persistent = persistent; - this.stat = stat; - setRecipeFromString(recipe); - } - - // returns a Brew by its UID - public static Brew get(int uid) { - if (uid < -1) { - if (!potions.containsKey(uid)) { - P.p.errorLog("Database failure! unable to find UID " + uid + " of a custom Potion!"); - return null;// throw some exception? - } - } else { - return null; - } - return potions.get(uid); - } - - // returns a Brew by PotionMeta - public static Brew get(PotionMeta meta) { - return get(getUID(meta)); - } - - // returns a Brew by ItemStack - public static Brew get(ItemStack item) { - if (item.getType() == Material.POTION) { - if (item.hasItemMeta()) { - PotionMeta potionMeta = (PotionMeta) item.getItemMeta(); - return get(potionMeta); - } - } - return null; - } - - - // returns UID of custom Potion item - public static int getUID(ItemStack item) { - return getUID((PotionMeta) item.getItemMeta()); - } - - // returns UID of custom Potion meta - public static int getUID(PotionMeta potionMeta) { - if (potionMeta.hasCustomEffect(PotionEffectType.REGENERATION)) { - for (PotionEffect effect : potionMeta.getCustomEffects()) { - if (effect.getType().equals(PotionEffectType.REGENERATION)) { - if (effect.getDuration() < -1) { - return effect.getDuration(); - } - } - } - } - return 0; - } - - // generate an UID - public static int generateUID() { - int uid = -2; - while (potions.containsKey(uid)) { - uid -= 1; - } - return uid; - } - - //returns the recipe with the given name, recalculates if not found - public boolean setRecipeFromString(String name) { - currentRecipe = null; - if (name != null && !name.equals("")) { - for (BRecipe recipe : BIngredients.recipes) { - if (recipe.getName(5).equalsIgnoreCase(name)) { - currentRecipe = recipe; - return true; - } - } - - if (quality > 0) { - currentRecipe = ingredients.getBestRecipe(wood, ageTime, distillRuns > 0); - if (currentRecipe != null) { - if (!stat) { - this.quality = calcQuality(); - } - P.p.log("Brew was made from Recipe: '" + name + "' which could not be found. '" + currentRecipe.getName(5) + "' used instead!"); - return true; - } else { - P.p.errorLog("Brew was made from Recipe: '" + name + "' which could not be found!"); - } - } - } - return false; - } - - public boolean reloadRecipe() { - if (currentRecipe != null) { - return setRecipeFromString(currentRecipe.getName(5)); - } - return true; - } - - // Copy a Brew with a new unique ID and return its item - public ItemStack copy(ItemStack item) { - ItemStack copy = item.clone(); - int uid = generateUID(); - clone(uid); - PotionMeta meta = (PotionMeta) copy.getItemMeta(); - meta.addCustomEffect((PotionEffectType.REGENERATION).createEffect((uid * 4), 0), true); - copy.setItemMeta(meta); - return copy; - } - - // Clones this instance with a new unique ID - public Brew clone(int uid) { - Brew brew = new Brew(uid, quality, currentRecipe, ingredients); - brew.distillRuns = distillRuns; - brew.ageTime = ageTime; - brew.unlabeled = unlabeled; - if (!brew.persistent) { - brew.stat = stat; - } - return brew; - } - - // remove potion from file (drinking, despawning, combusting, cmdDeleting, should be more!) - public void remove(ItemStack item) { - if (!persistent) { - potions.remove(getUID(item)); - } - } - - // calculate alcohol from recipe - public int calcAlcohol() { - if (quality == 0) { - // Give bad potions some alc - int badAlc = 0; - if (distillRuns > 1) { - badAlc = distillRuns; - } - if (ageTime > 10) { - badAlc += 5; - } else if (ageTime > 2) { - badAlc += 3; - } - if (currentRecipe != null) { - return badAlc; - } else { - return badAlc / 2; - } - } - - if (currentRecipe != null) { - int alc = currentRecipe.getAlcohol(); - if (currentRecipe.needsDistilling()) { - if (distillRuns == 0) { - return 0; - } - // bad quality can decrease alc by up to 40% - alc *= 1 - ((float) (10 - quality) * 0.04); - // distillable Potions should have half alc after one and full alc after all needed distills - alc /= 2; - alc *= 1.0F + ((float) distillRuns / currentRecipe.getDistillRuns()) ; - } else { - // quality decides 10% - 100% - alc *= ((float) quality / 10.0); - } - if (alc > 0) { - return alc; - } - } - return 0; - } - - // calculating quality - public int calcQuality() { - // calculate quality from all of the factors - float quality = ingredients.getIngredientQuality(currentRecipe) + ingredients.getCookingQuality(currentRecipe, distillRuns > 0); - if (currentRecipe.needsToAge() || ageTime > 0.5) { - quality += ingredients.getWoodQuality(currentRecipe, wood) + ingredients.getAgeQuality(currentRecipe, ageTime); - quality /= 4; - } else { - quality /= 2; - } - return Math.round(quality); - } - - public int getQuality() { - return quality; - } - - public boolean canDistill() { - if (currentRecipe != null) { - return currentRecipe.getDistillRuns() > distillRuns; - } else if (distillRuns >= 6) { - return false; - } - return true; - } - - // return special effect - public ArrayList getEffects() { - if (currentRecipe != null && quality > 0) { - return currentRecipe.getEffects(); - } - return null; - } - - // Set unlabeled to true to hide the numbers in Lore - public void unLabel(ItemStack item) { - PotionMeta meta = (PotionMeta) item.getItemMeta(); - if (meta.hasLore()) { - if (distillRuns > 0) { - addOrReplaceLore(meta, P.p.color("&7"), P.p.languageReader.get("Brew_Distilled")); - } - if (ageTime >= 1) { - addOrReplaceLore(meta, P.p.color("&7"), P.p.languageReader.get("Brew_BarrelRiped")); - } - item.setItemMeta(meta); - } - unlabeled = true; - } - - public boolean isPersistent() { - return persistent; - } - - // Make a potion persistent to not delete it when drinking it - public void makePersistent() { - persistent = true; - } - - // Remove the Persistence Flag from a brew, so it will be normally deleted when drinking it - public void removePersistence() { - persistent = false; - } - - public boolean isStatic() { - return stat; - } - - // Set the Static flag, so potion is unchangeable - public void setStatic(boolean stat, ItemStack potion) { - this.stat = stat; - if (currentRecipe != null && canDistill()) { - if (stat) { - potion.setDurability(PotionColor.valueOf(currentRecipe.getColor()).getColorId(false)); - } else { - potion.setDurability(PotionColor.valueOf(currentRecipe.getColor()).getColorId(true)); - } - } - } - - // Distilling section --------------- - - // distill all custom potions in the brewer - public static void distillAll(BrewerInventory inv, Boolean[] contents) { - int slot = 0; - while (slot < 3) { - if (contents[slot]) { - ItemStack slotItem = inv.getItem(slot); - PotionMeta potionMeta = (PotionMeta) slotItem.getItemMeta(); - Brew brew = get(potionMeta); - brew.distillSlot(slotItem, potionMeta); - } - slot++; - } - } - - // distill custom potion in given slot - public void distillSlot(ItemStack slotItem, PotionMeta potionMeta) { - if (stat) { - return; - } - - distillRuns += 1; - BRecipe recipe = ingredients.getdistillRecipe(wood, ageTime); - if (recipe != null) { - // distillRuns will have an effect on the amount of alcohol, not the quality - currentRecipe = recipe; - quality = calcQuality(); - - addOrReplaceEffects(potionMeta, getEffects(), quality); - potionMeta.setDisplayName(P.p.color("&f" + recipe.getName(quality))); - slotItem.setDurability(PotionColor.valueOf(recipe.getColor()).getColorId(canDistill())); - } else { - quality = 0; - removeEffects(potionMeta); - potionMeta.setDisplayName(P.p.color("&f" + P.p.languageReader.get("Brew_DistillUndefined"))); - slotItem.setDurability(PotionColor.GREY.getColorId(canDistill())); - } - - // Distill Lore - if (currentRecipe != null) { - if (colorInBrewer != hasColorLore(potionMeta)) { - convertLore(potionMeta, colorInBrewer); - } - } - String prefix = P.p.color("&7"); - if (colorInBrewer && currentRecipe != null) { - prefix = getQualityColor(ingredients.getDistillQuality(recipe, distillRuns)); - } - updateDistillLore(prefix, potionMeta); - - slotItem.setItemMeta(potionMeta); - } - - // Ageing Section ------------------ - - public void age(ItemStack item, float time, byte woodType) { - if (stat) { - return; - } - - PotionMeta potionMeta = (PotionMeta) item.getItemMeta(); - ageTime += time; - - // if younger than half a day, it shouldnt get aged form - if (ageTime > 0.5) { - if (wood == 0) { - wood = woodType; - } else { - if (wood != woodType) { - woodShift(time, woodType); - } - } - BRecipe recipe = ingredients.getAgeRecipe(wood, ageTime, distillRuns > 0); - if (recipe != null) { - currentRecipe = recipe; - quality = calcQuality(); - - addOrReplaceEffects(potionMeta, getEffects(), quality); - potionMeta.setDisplayName(P.p.color("&f" + recipe.getName(quality))); - item.setDurability(PotionColor.valueOf(recipe.getColor()).getColorId(canDistill())); - } else { - quality = 0; - removeEffects(potionMeta); - potionMeta.setDisplayName(P.p.color("&f" + P.p.languageReader.get("Brew_BadPotion"))); - item.setDurability(PotionColor.GREY.getColorId(canDistill())); - } - } - - // Lore - if (currentRecipe != null) { - if (colorInBarrels != hasColorLore(potionMeta)) { - convertLore(potionMeta, colorInBarrels); - } - } - if (ageTime >= 1) { - String prefix = P.p.color("&7"); - if (colorInBarrels && currentRecipe != null) { - prefix = getQualityColor(ingredients.getAgeQuality(currentRecipe, ageTime)); - } - updateAgeLore(prefix, potionMeta); - } - if (ageTime > 0.5) { - if (colorInBarrels && !unlabeled && currentRecipe != null) { - updateWoodLore(potionMeta); - } - } - item.setItemMeta(potionMeta); - } - - // Slowly shift the wood of the Brew to the new Type - public void woodShift(float time, byte to) { - byte factor = 1; - if (ageTime > 5) { - factor = 2; - } else if (ageTime > 10) { - factor = 2; - factor += Math.round(ageTime / 10); - } - if (wood > to) { - wood -= time / factor; - if (wood < to) { - wood = to; - } - } else { - wood += time / factor; - if (wood > to) { - wood = to; - } - } - } - - // Lore ----------- - - // Converts to/from qualitycolored Lore - public void convertLore(PotionMeta meta, Boolean toQuality) { - if (currentRecipe == null) { - return; - } - meta.setLore(null); - int quality; - String prefix = P.p.color("&7"); - String lore; - - // Ingredients - if (toQuality && !unlabeled) { - quality = ingredients.getIngredientQuality(currentRecipe); - prefix = getQualityColor(quality); - lore = P.p.languageReader.get("Brew_Ingredients"); - addOrReplaceLore(meta, prefix, lore); - } - - // Cooking - if (toQuality && !unlabeled) { - if (distillRuns > 0 == currentRecipe.needsDistilling()) { - quality = ingredients.getCookingQuality(currentRecipe, distillRuns > 0); - prefix = getQualityColor(quality) + ingredients.getCookedTime() + " " + P.p.languageReader.get("Brew_minute"); - if (ingredients.getCookedTime() > 1) { - prefix = prefix + P.p.languageReader.get("Brew_MinutePluralPostfix"); - } - lore = " " + P.p.languageReader.get("Brew_fermented"); - addOrReplaceLore(meta, prefix, lore); - } - } - - // Distilling - if (distillRuns > 0) { - if (toQuality) { - quality = ingredients.getDistillQuality(currentRecipe, distillRuns); - prefix = getQualityColor(quality); - } - updateDistillLore(prefix, meta); - } - - // Ageing - if (ageTime >= 1) { - if (toQuality) { - quality = ingredients.getAgeQuality(currentRecipe, ageTime); - prefix = getQualityColor(quality); - } - updateAgeLore(prefix, meta); - } - - // WoodType - if (toQuality && !unlabeled) { - if (ageTime > 0.5) { - updateWoodLore(meta); - } - } - } - - // sets the DistillLore. Prefix is the color to be used - public void updateDistillLore(String prefix, PotionMeta meta) { - if (!unlabeled) { - if (distillRuns > 1) { - prefix = prefix + distillRuns + P.p.languageReader.get("Brew_-times") + " "; - } - } - addOrReplaceLore(meta, prefix, P.p.languageReader.get("Brew_Distilled")); - } - - // sets the AgeLore. Prefix is the color to be used - public void updateAgeLore(String prefix, PotionMeta meta) { - if (!unlabeled) { - if (ageTime >= 1 && ageTime < 2) { - prefix = prefix + P.p.languageReader.get("Brew_OneYear") + " "; - } else if (ageTime < 201) { - prefix = prefix + (int) Math.floor(ageTime) + " " + P.p.languageReader.get("Brew_Years") + " "; - } else { - prefix = prefix + P.p.languageReader.get("Brew_HundredsOfYears") + " "; - } - } - addOrReplaceLore(meta, prefix, P.p.languageReader.get("Brew_BarrelRiped")); - } - - // updates/sets the color on WoodLore - public void updateWoodLore(PotionMeta meta) { - if (currentRecipe.getWood() > 0) { - int quality = ingredients.getWoodQuality(currentRecipe, wood); - addOrReplaceLore(meta, getQualityColor(quality), P.p.languageReader.get("Brew_Woodtype")); - } else { - if (meta.hasLore()) { - List existingLore = meta.getLore(); - int index = indexOfSubstring(existingLore, P.p.languageReader.get("Brew_Woodtype")); - if (index > -1) { - existingLore.remove(index); - meta.setLore(existingLore); - } - } - } - } - - // Adds or replaces a line of Lore. Searches for Substring lore and replaces it - public static void addOrReplaceLore(PotionMeta meta, String prefix, String lore) { - if (meta.hasLore()) { - List existingLore = meta.getLore(); - int index = indexOfSubstring(existingLore, lore); - if (index > -1) { - existingLore.set(index, prefix + lore); - } else { - existingLore.add(prefix + lore); - } - meta.setLore(existingLore); - return; - } - List newLore = new ArrayList(); - newLore.add(""); - newLore.add(prefix + lore); - meta.setLore(newLore); - } - - // Adds the Effect names to the Items description - public static void addOrReplaceEffects(PotionMeta meta, ArrayList effects, int quality) { - if (effects != null) { - for (BEffect effect : effects) { - if (!effect.isHidden()) { - effect.writeInto(meta, quality); - } - } - } - } - - // Removes all effects except regeneration which stores data - public static void removeEffects(PotionMeta meta) { - if (meta.hasCustomEffects()) { - for (PotionEffect effect : meta.getCustomEffects()) { - PotionEffectType type = effect.getType(); - if (!type.equals(PotionEffectType.REGENERATION)) { - meta.removeCustomEffect(type); - } - } - } - } - - // Returns the Index of a String from the list that contains this substring - public static int indexOfSubstring(List list, String substring) { - for (int index = 0; index < list.size(); index++) { - String string = list.get(index); - if (string.contains(substring)) { - return index; - } - } - return -1; - } - - // True if the PotionMeta has colored Lore - public static Boolean hasColorLore(PotionMeta meta) { - return meta.hasLore() && !meta.getLore().get(1).startsWith(P.p.color("&7")); - } - - // gets the Color that represents a quality in Lore - public static String getQualityColor(int quality) { - String color; - if (quality > 8) { - color = "&a"; - } else if (quality > 6) { - color = "&e"; - } else if (quality > 4) { - color = "&6"; - } else if (quality > 2) { - color = "&c"; - } else { - color = "&4"; - } - return P.p.color(color); - } - - // Saves all data - public static void save(ConfigurationSection config) { - for (Map.Entry entry : potions.entrySet()) { - int uid = entry.getKey(); - Brew brew = entry.getValue(); - ConfigurationSection idConfig = config.createSection("" + uid); - // not saving unneccessary data - if (brew.quality != 0) { - idConfig.set("quality", brew.quality); - } - if (brew.distillRuns != 0) { - idConfig.set("distillRuns", brew.distillRuns); - } - if (brew.ageTime != 0) { - idConfig.set("ageTime", brew.ageTime); - } - if (brew.wood != -1) { - idConfig.set("wood", brew.wood); - } - if (brew.currentRecipe != null) { - idConfig.set("recipe", brew.currentRecipe.getName(5)); - } - if (brew.unlabeled) { - idConfig.set("unlabeled", true); - } - if (brew.persistent) { - idConfig.set("persist", true); - } - if (brew.stat) { - idConfig.set("stat", true); - } - // save the ingredients - idConfig.set("ingId", brew.ingredients.save(config.getParent())); - } - } - - public static enum PotionColor { - PINK(1), - CYAN(2), - ORANGE(3), - GREEN(4), - BRIGHT_RED(5), - BLUE(6), - BLACK(8), - RED(9), - GREY(10), - WATER(11), - DARK_RED(12), - BRIGHT_GREY(14); - - private final int colorId; - - private PotionColor(int colorId) { - this.colorId = colorId; - } - - // gets the Damage Value, that sets a color on the potion - // offset +32 is not accepted by brewer, so not further destillable - public short getColorId(boolean destillable) { - if (destillable) { - return (short) (colorId + 64); - } - return (short) (colorId + 32); - } - } - -} \ No newline at end of file + // represents the liquid in the brewed Potions + public static Map potions = new HashMap(); + public static Boolean colorInBarrels; // color the Lore while in Barrels + public static Boolean colorInBrewer; // color the Lore while in Brewer + + private BIngredients ingredients; + private int quality; + private int distillRuns; + private float ageTime; + private float wood; + private BRecipe currentRecipe; + private boolean unlabeled; + private boolean persistent; + private boolean stat; // static potions should not be changed + + public Brew(int uid, BIngredients ingredients) { + this.ingredients = ingredients; + potions.put(uid, this); + } + + // quality already set + public Brew(int uid, int quality, BRecipe recipe, BIngredients ingredients) { + this.ingredients = ingredients; + this.quality = quality; + this.currentRecipe = recipe; + potions.put(uid, this); + } + + // loading from file + public Brew(int uid, BIngredients ingredients, int quality, int distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean persistent, boolean stat) { + potions.put(uid, this); + this.ingredients = ingredients; + this.quality = quality; + this.distillRuns = distillRuns; + this.ageTime = ageTime; + this.wood = wood; + this.unlabeled = unlabeled; + this.persistent = persistent; + this.stat = stat; + setRecipeFromString(recipe); + } + + // returns a Brew by its UID + public static Brew get(int uid) { + if (uid < -1) { + if (!potions.containsKey(uid)) { + P.p.errorLog("Database failure! unable to find UID " + uid + " of a custom Potion!"); + return null;// throw some exception? + } + } else { + return null; + } + return potions.get(uid); + } + + // returns a Brew by PotionMeta + public static Brew get(PotionMeta meta) { + return get(getUID(meta)); + } + + // returns a Brew by ItemStack + public static Brew get(ItemStack item) { + if (item.getType() == Material.POTION) { + if (item.hasItemMeta()) { + PotionMeta potionMeta = (PotionMeta) item.getItemMeta(); + return get(potionMeta); + } + } + return null; + } + + // returns UID of custom Potion item + public static int getUID(ItemStack item) { + return getUID((PotionMeta) item.getItemMeta()); + } + + // returns UID of custom Potion meta + public static int getUID(PotionMeta potionMeta) { + if (potionMeta.hasCustomEffect(PotionEffectType.REGENERATION)) { + for (PotionEffect effect : potionMeta.getCustomEffects()) { + if (effect.getType().equals(PotionEffectType.REGENERATION)) { + if (effect.getDuration() < -1) { + return effect.getDuration(); + } + } + } + } + return 0; + } + + // generate an UID + public static int generateUID() { + int uid = -2; + while (potions.containsKey(uid)) { + uid -= 1; + } + return uid; + } + + //returns the recipe with the given name, recalculates if not found + public boolean setRecipeFromString(String name) { + currentRecipe = null; + if (name != null && !name.equals("")) { + for (BRecipe recipe : BIngredients.recipes) { + if (recipe.getName(5).equalsIgnoreCase(name)) { + currentRecipe = recipe; + return true; + } + } + + if (quality > 0) { + currentRecipe = ingredients.getBestRecipe(wood, ageTime, distillRuns > 0); + if (currentRecipe != null) { + if (!stat) { + this.quality = calcQuality(); + } + P.p.log("Brew was made from Recipe: '" + name + "' which could not be found. '" + currentRecipe.getName(5) + "' used instead!"); + return true; + } else { + P.p.errorLog("Brew was made from Recipe: '" + name + "' which could not be found!"); + } + } + } + return false; + } + + public boolean reloadRecipe() { + if (currentRecipe != null) { + return setRecipeFromString(currentRecipe.getName(5)); + } + return true; + } + + // Copy a Brew with a new unique ID and return its item + public ItemStack copy(ItemStack item) { + ItemStack copy = item.clone(); + int uid = generateUID(); + clone(uid); + PotionMeta meta = (PotionMeta) copy.getItemMeta(); + meta.addCustomEffect((PotionEffectType.REGENERATION).createEffect((uid * 4), 0), true); + copy.setItemMeta(meta); + return copy; + } + + // Clones this instance with a new unique ID + public Brew clone(int uid) { + Brew brew = new Brew(uid, quality, currentRecipe, ingredients); + brew.distillRuns = distillRuns; + brew.ageTime = ageTime; + brew.unlabeled = unlabeled; + if (!brew.persistent) { + brew.stat = stat; + } + return brew; + } + + // remove potion from file (drinking, despawning, combusting, cmdDeleting, should be more!) + public void remove(ItemStack item) { + if (!persistent) { + potions.remove(getUID(item)); + } + } + + // calculate alcohol from recipe + public int calcAlcohol() { + if (quality == 0) { + // Give bad potions some alc + int badAlc = 0; + if (distillRuns > 1) { + badAlc = distillRuns; + } + if (ageTime > 10) { + badAlc += 5; + } else if (ageTime > 2) { + badAlc += 3; + } + if (currentRecipe != null) { + return badAlc; + } else { + return badAlc / 2; + } + } + + if (currentRecipe != null) { + int alc = currentRecipe.getAlcohol(); + if (currentRecipe.needsDistilling()) { + if (distillRuns == 0) { + return 0; + } + // bad quality can decrease alc by up to 40% + alc *= 1 - ((float) (10 - quality) * 0.04); + // distillable Potions should have half alc after one and full alc after all needed distills + alc /= 2; + alc *= 1.0F + ((float) distillRuns / currentRecipe.getDistillRuns()); + } else { + // quality decides 10% - 100% + alc *= ((float) quality / 10.0); + } + if (alc > 0) { + return alc; + } + } + return 0; + } + + // calculating quality + public int calcQuality() { + // calculate quality from all of the factors + float quality = ingredients.getIngredientQuality(currentRecipe) + ingredients.getCookingQuality(currentRecipe, distillRuns > 0); + if (currentRecipe.needsToAge() || ageTime > 0.5) { + quality += ingredients.getWoodQuality(currentRecipe, wood) + ingredients.getAgeQuality(currentRecipe, ageTime); + quality /= 4; + } else { + quality /= 2; + } + return Math.round(quality); + } + + public int getQuality() { + return quality; + } + + public boolean canDistill() { + if (currentRecipe != null) { + return currentRecipe.getDistillRuns() > distillRuns; + } else if (distillRuns >= 6) { + return false; + } + return true; + } + + // return special effect + public ArrayList getEffects() { + if (currentRecipe != null && quality > 0) { + return currentRecipe.getEffects(); + } + return null; + } + + // Set unlabeled to true to hide the numbers in Lore + public void unLabel(ItemStack item) { + PotionMeta meta = (PotionMeta) item.getItemMeta(); + if (meta.hasLore()) { + if (distillRuns > 0) { + addOrReplaceLore(meta, P.p.color("&7"), P.p.languageReader.get("Brew_Distilled")); + } + if (ageTime >= 1) { + addOrReplaceLore(meta, P.p.color("&7"), P.p.languageReader.get("Brew_BarrelRiped")); + } + item.setItemMeta(meta); + } + unlabeled = true; + } + + public boolean isPersistent() { + return persistent; + } + + // Make a potion persistent to not delete it when drinking it + public void makePersistent() { + persistent = true; + } + + // Remove the Persistence Flag from a brew, so it will be normally deleted when drinking it + public void removePersistence() { + persistent = false; + } + + public boolean isStatic() { + return stat; + } + + // Set the Static flag, so potion is unchangeable + public void setStatic(boolean stat, ItemStack potion) { + this.stat = stat; + if (currentRecipe != null && canDistill()) { + if (stat) { + if (P.use1_9) { + PotionMeta potionMeta = (PotionMeta) potion.getItemMeta(); + potionMeta.setMainEffect(PotionColor.valueOf(currentRecipe.getColor()).getEffect()); + } else { + potion.setDurability(PotionColor.valueOf(currentRecipe.getColor()).getColorId(false)); + } + } else if (P.use1_9) { + PotionMeta potionMeta = (PotionMeta) potion.getItemMeta(); + potionMeta.setMainEffect(PotionColor.valueOf(currentRecipe.getColor()).getEffect()); + } else { + potion.setDurability(PotionColor.valueOf(currentRecipe.getColor()).getColorId(true)); + } + } + } + + // Distilling section --------------- + // distill all custom potions in the brewer + public static void distillAll(BrewerInventory inv, Boolean[] contents) { + int slot = 0; + while (slot < 3) { + if (contents[slot]) { + ItemStack slotItem = inv.getItem(slot); + PotionMeta potionMeta = (PotionMeta) slotItem.getItemMeta(); + Brew brew = get(potionMeta); + brew.distillSlot(slotItem, potionMeta); + } + slot++; + } + } + + // distill custom potion in given slot + public void distillSlot(ItemStack slotItem, PotionMeta potionMeta) { + if (stat) { + return; + } + + distillRuns += 1; + BRecipe recipe = ingredients.getdistillRecipe(wood, ageTime); + if (recipe != null) { + // distillRuns will have an effect on the amount of alcohol, not the quality + currentRecipe = recipe; + quality = calcQuality(); + + addOrReplaceEffects(potionMeta, getEffects(), quality); + potionMeta.setDisplayName(P.p.color("&f" + recipe.getName(quality))); + if (P.use1_9) { + potionMeta.setMainEffect(PotionColor.valueOf(recipe.getColor()).getEffect()); + } else { + slotItem.setDurability(PotionColor.valueOf(recipe.getColor()).getColorId(canDistill())); + } + + } else { + quality = 0; + removeEffects(potionMeta); + potionMeta.setDisplayName(P.p.color("&f" + P.p.languageReader.get("Brew_DistillUndefined"))); + if (P.use1_9) { + potionMeta.setMainEffect(PotionColor.GREY.getEffect()); + } else { + slotItem.setDurability(PotionColor.GREY.getColorId(canDistill())); + } + } + + // Distill Lore + if (currentRecipe != null) { + if (colorInBrewer != hasColorLore(potionMeta)) { + convertLore(potionMeta, colorInBrewer); + } + } + String prefix = P.p.color("&7"); + if (colorInBrewer && currentRecipe != null) { + prefix = getQualityColor(ingredients.getDistillQuality(recipe, distillRuns)); + } + updateDistillLore(prefix, potionMeta); + + slotItem.setItemMeta(potionMeta); + } + + // Ageing Section ------------------ + public void age(ItemStack item, float time, byte woodType) { + if (stat) { + return; + } + + PotionMeta potionMeta = (PotionMeta) item.getItemMeta(); + ageTime += time; + + // if younger than half a day, it shouldnt get aged form + if (ageTime > 0.5) { + if (wood == 0) { + wood = woodType; + } else if (wood != woodType) { + woodShift(time, woodType); + } + BRecipe recipe = ingredients.getAgeRecipe(wood, ageTime, distillRuns > 0); + if (recipe != null) { + currentRecipe = recipe; + quality = calcQuality(); + + addOrReplaceEffects(potionMeta, getEffects(), quality); + potionMeta.setDisplayName(P.p.color("&f" + recipe.getName(quality))); + if (P.use1_9) { + potionMeta.setMainEffect(PotionColor.valueOf(recipe.getColor()).getEffect()); + } else { + item.setDurability(PotionColor.valueOf(recipe.getColor()).getColorId(canDistill())); + } + } else { + quality = 0; + removeEffects(potionMeta); + potionMeta.setDisplayName(P.p.color("&f" + P.p.languageReader.get("Brew_BadPotion"))); + if (P.use1_9) { + potionMeta.setMainEffect(PotionColor.GREY.getEffect()); + } else { + item.setDurability(PotionColor.GREY.getColorId(canDistill())); + } + } + } + + // Lore + if (currentRecipe != null) { + if (colorInBarrels != hasColorLore(potionMeta)) { + convertLore(potionMeta, colorInBarrels); + } + } + if (ageTime >= 1) { + String prefix = P.p.color("&7"); + if (colorInBarrels && currentRecipe != null) { + prefix = getQualityColor(ingredients.getAgeQuality(currentRecipe, ageTime)); + } + updateAgeLore(prefix, potionMeta); + } + if (ageTime > 0.5) { + if (colorInBarrels && !unlabeled && currentRecipe != null) { + updateWoodLore(potionMeta); + } + } + item.setItemMeta(potionMeta); + } + + // Slowly shift the wood of the Brew to the new Type + public void woodShift(float time, byte to) { + byte factor = 1; + if (ageTime > 5) { + factor = 2; + } else if (ageTime > 10) { + factor = 2; + factor += Math.round(ageTime / 10); + } + if (wood > to) { + wood -= time / factor; + if (wood < to) { + wood = to; + } + } else { + wood += time / factor; + if (wood > to) { + wood = to; + } + } + } + + // Lore ----------- + // Converts to/from qualitycolored Lore + public void convertLore(PotionMeta meta, Boolean toQuality) { + if (currentRecipe == null) { + return; + } + meta.setLore(null); + int quality; + String prefix = P.p.color("&7"); + String lore; + + // Ingredients + if (toQuality && !unlabeled) { + quality = ingredients.getIngredientQuality(currentRecipe); + prefix = getQualityColor(quality); + lore = P.p.languageReader.get("Brew_Ingredients"); + addOrReplaceLore(meta, prefix, lore); + } + + // Cooking + if (toQuality && !unlabeled) { + if (distillRuns > 0 == currentRecipe.needsDistilling()) { + quality = ingredients.getCookingQuality(currentRecipe, distillRuns > 0); + prefix = getQualityColor(quality) + ingredients.getCookedTime() + " " + P.p.languageReader.get("Brew_minute"); + if (ingredients.getCookedTime() > 1) { + prefix = prefix + P.p.languageReader.get("Brew_MinutePluralPostfix"); + } + lore = " " + P.p.languageReader.get("Brew_fermented"); + addOrReplaceLore(meta, prefix, lore); + } + } + + // Distilling + if (distillRuns > 0) { + if (toQuality) { + quality = ingredients.getDistillQuality(currentRecipe, distillRuns); + prefix = getQualityColor(quality); + } + updateDistillLore(prefix, meta); + } + + // Ageing + if (ageTime >= 1) { + if (toQuality) { + quality = ingredients.getAgeQuality(currentRecipe, ageTime); + prefix = getQualityColor(quality); + } + updateAgeLore(prefix, meta); + } + + // WoodType + if (toQuality && !unlabeled) { + if (ageTime > 0.5) { + updateWoodLore(meta); + } + } + } + + // sets the DistillLore. Prefix is the color to be used + public void updateDistillLore(String prefix, PotionMeta meta) { + if (!unlabeled) { + if (distillRuns > 1) { + prefix = prefix + distillRuns + P.p.languageReader.get("Brew_-times") + " "; + } + } + addOrReplaceLore(meta, prefix, P.p.languageReader.get("Brew_Distilled")); + } + + // sets the AgeLore. Prefix is the color to be used + public void updateAgeLore(String prefix, PotionMeta meta) { + if (!unlabeled) { + if (ageTime >= 1 && ageTime < 2) { + prefix = prefix + P.p.languageReader.get("Brew_OneYear") + " "; + } else if (ageTime < 201) { + prefix = prefix + (int) Math.floor(ageTime) + " " + P.p.languageReader.get("Brew_Years") + " "; + } else { + prefix = prefix + P.p.languageReader.get("Brew_HundredsOfYears") + " "; + } + } + addOrReplaceLore(meta, prefix, P.p.languageReader.get("Brew_BarrelRiped")); + } + + // updates/sets the color on WoodLore + public void updateWoodLore(PotionMeta meta) { + if (currentRecipe.getWood() > 0) { + int quality = ingredients.getWoodQuality(currentRecipe, wood); + addOrReplaceLore(meta, getQualityColor(quality), P.p.languageReader.get("Brew_Woodtype")); + } else if (meta.hasLore()) { + List existingLore = meta.getLore(); + int index = indexOfSubstring(existingLore, P.p.languageReader.get("Brew_Woodtype")); + if (index > -1) { + existingLore.remove(index); + meta.setLore(existingLore); + } + } + } + + // Adds or replaces a line of Lore. Searches for Substring lore and replaces it + public static void addOrReplaceLore(PotionMeta meta, String prefix, String lore) { + if (meta.hasLore()) { + List existingLore = meta.getLore(); + int index = indexOfSubstring(existingLore, lore); + if (index > -1) { + existingLore.set(index, prefix + lore); + } else { + existingLore.add(prefix + lore); + } + meta.setLore(existingLore); + return; + } + List newLore = new ArrayList(); + newLore.add(""); + newLore.add(prefix + lore); + meta.setLore(newLore); + } + + // Adds the Effect names to the Items description + public static void addOrReplaceEffects(PotionMeta meta, ArrayList effects, int quality) { + if (effects != null) { + for (BEffect effect : effects) { + if (!effect.isHidden()) { + effect.writeInto(meta, quality); + } + } + } + } + + // Removes all effects except regeneration which stores data + public static void removeEffects(PotionMeta meta) { + if (meta.hasCustomEffects()) { + for (PotionEffect effect : meta.getCustomEffects()) { + PotionEffectType type = effect.getType(); + if (!type.equals(PotionEffectType.REGENERATION)) { + meta.removeCustomEffect(type); + } + } + } + } + + // Returns the Index of a String from the list that contains this substring + public static int indexOfSubstring(List list, String substring) { + for (int index = 0; index < list.size(); index++) { + String string = list.get(index); + if (string.contains(substring)) { + return index; + } + } + return -1; + } + + // True if the PotionMeta has colored Lore + public static Boolean hasColorLore(PotionMeta meta) { + return meta.hasLore() && !meta.getLore().get(1).startsWith(P.p.color("&7")); + } + + // gets the Color that represents a quality in Lore + public static String getQualityColor(int quality) { + String color; + if (quality > 8) { + color = "&a"; + } else if (quality > 6) { + color = "&e"; + } else if (quality > 4) { + color = "&6"; + } else if (quality > 2) { + color = "&c"; + } else { + color = "&4"; + } + return P.p.color(color); + } + + // Saves all data + public static void save(ConfigurationSection config) { + for (Map.Entry entry : potions.entrySet()) { + int uid = entry.getKey(); + Brew brew = entry.getValue(); + ConfigurationSection idConfig = config.createSection("" + uid); + // not saving unneccessary data + if (brew.quality != 0) { + idConfig.set("quality", brew.quality); + } + if (brew.distillRuns != 0) { + idConfig.set("distillRuns", brew.distillRuns); + } + if (brew.ageTime != 0) { + idConfig.set("ageTime", brew.ageTime); + } + if (brew.wood != -1) { + idConfig.set("wood", brew.wood); + } + if (brew.currentRecipe != null) { + idConfig.set("recipe", brew.currentRecipe.getName(5)); + } + if (brew.unlabeled) { + idConfig.set("unlabeled", true); + } + if (brew.persistent) { + idConfig.set("persist", true); + } + if (brew.stat) { + idConfig.set("stat", true); + } + // save the ingredients + idConfig.set("ingId", brew.ingredients.save(config.getParent())); + } + } + + public static enum PotionColor { + PINK(1, PotionEffectType.REGENERATION), + CYAN(2, PotionEffectType.SPEED), + ORANGE(3, PotionEffectType.FIRE_RESISTANCE), + GREEN(4, PotionEffectType.POISON), + BRIGHT_RED(5, PotionEffectType.HEAL), + BLUE(6, PotionEffectType.NIGHT_VISION), + BLACK(8, PotionEffectType.WEAKNESS), + RED(9, PotionEffectType.INCREASE_DAMAGE), + GREY(10, PotionEffectType.SLOW), + WATER(11, PotionEffectType.WATER_BREATHING), + DARK_RED(12, PotionEffectType.HARM), + BRIGHT_GREY(14, PotionEffectType.INVISIBILITY); + + private final int colorId; + private final PotionEffectType effect; + + private PotionColor(int colorId, PotionEffectType effect) { + this.colorId = colorId; + this.effect = effect; + } + + // gets the Damage Value, that sets a color on the potion + // offset +32 is not accepted by brewer, so not further destillable + public short getColorId(boolean destillable) { + if (destillable) { + return (short) (colorId + 64); + } + return (short) (colorId + 32); + } + + public PotionEffectType getEffect() { + return effect; + } + + } + +} diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index c69fabb..f71d447 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -1,46 +1,44 @@ package com.dre.brewery; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.Map; -import java.util.List; -import java.util.ArrayList; -import java.util.ListIterator; -import java.util.HashMap; -import java.io.IOException; -import java.io.File; -import java.util.UUID; - -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -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.filedata.*; +import com.dre.brewery.integration.LogBlockBarrel; import com.dre.brewery.integration.WGBarrel; import com.dre.brewery.integration.WGBarrelNew; import com.dre.brewery.integration.WGBarrelOld; -import org.apache.commons.lang.math.NumberUtils; -import org.bukkit.event.HandlerList; -import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.Location; -import org.bukkit.command.CommandSender; -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.block.Block; - import com.dre.brewery.listeners.*; -import com.dre.brewery.filedata.*; -import com.dre.brewery.integration.LogBlockBarrel; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.UUID; +import org.apache.commons.lang.math.NumberUtils; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.java.JavaPlugin; public class P extends JavaPlugin { public static P p; public static String configVersion = "1.3.1"; public static boolean debug; public static boolean useUUID; + public static boolean use1_9; public static boolean updateCheck; // Third Party Enabled @@ -57,6 +55,7 @@ public class P extends JavaPlugin { public EntityListener entityListener; public InventoryListener inventoryListener; public WorldListener worldListener; + public Compat1_9 compat1_9; // Language public String language; @@ -68,7 +67,12 @@ public class P extends JavaPlugin { // Version check String v = Bukkit.getBukkitVersion(); - useUUID = !v.matches(".*1\\.[1-6].*") && !v.matches(".*1\\.7\\.[0-5].*"); + useUUID = !v.matches(".*1\\.[0-6].*") && !v.matches(".*1\\.7\\.[0-5].*"); + use1_9 = !v.matches(".*1\\.[0-8].*"); + if (use1_9) { + log("&cExperimental support for Bukkit 1.9 enabled."); + log("&cPlease note that the changes of 1.9 make a build of Brewery with the same quality as for older versions impossible."); + } // load the Config try { @@ -94,6 +98,7 @@ public class P extends JavaPlugin { entityListener = new EntityListener(); inventoryListener = new InventoryListener(); worldListener = new WorldListener(); + compat1_9 = new Compat1_9(); getCommand("Brewery").setExecutor(new CommandListener()); p.getServer().getPluginManager().registerEvents(blockListener, p); @@ -101,6 +106,9 @@ public class P extends JavaPlugin { p.getServer().getPluginManager().registerEvents(entityListener, p); p.getServer().getPluginManager().registerEvents(inventoryListener, p); p.getServer().getPluginManager().registerEvents(worldListener, p); + if (use1_9) { + p.getServer().getPluginManager().registerEvents(compat1_9, p); + } // Heartbeat p.getServer().getScheduler().runTaskTimer(p, new BreweryRunnable(), 650, 1200); diff --git a/src/com/dre/brewery/listeners/Compat1_9.java b/src/com/dre/brewery/listeners/Compat1_9.java new file mode 100644 index 0000000..bc8686b --- /dev/null +++ b/src/com/dre/brewery/listeners/Compat1_9.java @@ -0,0 +1,31 @@ +package com.dre.brewery.listeners; + +import com.dre.brewery.Brew; +import org.bukkit.Material; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerItemConsumeEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.PotionMeta; + +// Workaround to remove unwanted potion effects +public class Compat1_9 implements Listener { + + @EventHandler + public void onPlayerDrink(PlayerItemConsumeEvent event) { + ItemStack item = event.getItem(); + Brew brew = Brew.get(item); + if (brew == null) { + return; + } + + if (item.getType() == Material.POTION) { + try { + PotionMeta meta = (PotionMeta) item.getItemMeta(); + meta.setMainEffect(null); + item.setItemMeta(meta); + } catch (Exception exception) {} + } + } + +} From ddaa2bdbfc5a1ac7dc524145effd4c0ee588f098 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Mon, 21 Mar 2016 20:36:09 +0100 Subject: [PATCH 02/10] Update to GriefPrevention 13.9.1 --- .../integration/GriefPreventionBarrel.java | 65 ++++++++++--------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/src/com/dre/brewery/integration/GriefPreventionBarrel.java b/src/com/dre/brewery/integration/GriefPreventionBarrel.java index a992d16..bbd8978 100644 --- a/src/com/dre/brewery/integration/GriefPreventionBarrel.java +++ b/src/com/dre/brewery/integration/GriefPreventionBarrel.java @@ -1,42 +1,47 @@ package com.dre.brewery.integration; +import com.dre.brewery.P; +import me.ryanhamshire.GriefPrevention.Claim; +import me.ryanhamshire.GriefPrevention.GriefPrevention; +import me.ryanhamshire.GriefPrevention.PlayerData; import org.bukkit.block.Block; import org.bukkit.entity.Player; -import com.dre.brewery.P; -import me.ryanhamshire.GriefPrevention.Configuration.WorldConfig; -import me.ryanhamshire.GriefPrevention.GriefPrevention; -import me.ryanhamshire.GriefPrevention.Messages; -import me.ryanhamshire.GriefPrevention.PlayerData; -import me.ryanhamshire.GriefPrevention.TextMode; - public class GriefPreventionBarrel { - public static boolean checkAccess(Player player, Block sign) { - WorldConfig wc = GriefPrevention.instance.getWorldCfg(player.getWorld()); - /*if (!wc.Enabled()) { - return true; - }*/ + static P brewery = P.p; + static GriefPrevention griefPrevention = GriefPrevention.instance; - PlayerData playerData = GriefPrevention.instance.dataStore.getPlayerData(player.getName()); + public static boolean checkAccess(Player player, Block sign) { + PlayerData playerData = griefPrevention.dataStore.getPlayerData(player.getUniqueId()); - // block container use during pvp combat - if (playerData.inPvpCombat() && wc.getPvPBlockContainers()) { - GriefPrevention.sendMessage(player, TextMode.Err, Messages.PvPNoContainers); - return false; - } + if (!griefPrevention.claimsEnabledForWorld(player.getWorld()) || playerData.ignoreClaims || !griefPrevention.config_claims_preventTheft) { + return true; + } - // check permissions for the claim the Barrel is in - if (wc.getContainersRules().Allowed(sign.getLocation(), player, true).Denied()) { - P.p.msg(player, P.p.languageReader.get("Error_NoBarrelAccess")); - return false; - } + // block container use during pvp combat + if (playerData.inPvpCombat()) { + brewery.msg(player, brewery.languageReader.get("Error_NoBarrelAccess")); + return false; + } + + // check permissions for the claim the Barrel is in + Claim claim = griefPrevention.dataStore.getClaimAt(sign.getLocation(), false, playerData.lastClaim); + if (claim != null) { + playerData.lastClaim = claim; + String noContainersReason = claim.allowContainers(player); + if (noContainersReason != null) { + brewery.msg(player, brewery.languageReader.get("Error_NoBarrelAccess")); + return false; + } + } + + // drop any pvp protection, as the player opens a barrel + if (playerData.pvpImmune) { + playerData.pvpImmune = false; + } + + return true; + } - // drop any pvp protection, as the player opens a barrel - if (playerData.pvpImmune) { - playerData.pvpImmune = false; - GriefPrevention.sendMessage(player, TextMode.Warn, Messages.PvPImmunityEnd); - } - return true; - } } From 2d0d059008418d88b78c2eed1f456d8ebbaa5e0a Mon Sep 17 00:00:00 2001 From: ProgrammerDan Date: Mon, 18 Apr 2016 18:15:30 -0400 Subject: [PATCH 03/10] Slight adjustment to Sataniel's fixes to preserve tab-based indentation for easy merge compare. --- .editorconfig | 16 + pom.xml | 204 +-- src/com/dre/brewery/Brew.java | 1236 ++++++++--------- .../integration/GriefPreventionBarrel.java | 56 +- src/com/dre/brewery/listeners/Compat1_9.java | 30 +- 5 files changed, 779 insertions(+), 763 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..03a93b3 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true + +[*.java] +indent_style = tab +indent_size = 4 +trim_trailing_whitespace = true + +[*.yml] +indent_style = spaces +indent_size = 2 +trim_trailing_whitespace = true + diff --git a/pom.xml b/pom.xml index d77833b..aa38ef3 100644 --- a/pom.xml +++ b/pom.xml @@ -1,111 +1,111 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.dre - brewery - 1.4-SNAPSHOT - Brewery + com.dre + brewery + 1.4-SNAPSHOT + Brewery - - UTF-8 - UTF-8 - + + UTF-8 + UTF-8 + - - src + + src - - - - false - ${project.basedir}/resources - - **/*.yml - - - target/** - .travis.yml - - - + + + + false + ${project.basedir}/resources + + **/*.yml + + + target/** + .travis.yml + + + - - - maven-compiler-plugin - 2.5.1 - - 1.7 - 1.7 - UTF-8 - - - - + + + maven-compiler-plugin + 2.5.1 + + 1.7 + 1.7 + UTF-8 + + + + - - - spigot-repo - https://hub.spigotmc.org/nexus/content/groups/public/ - - - vault-repo - http://nexus.theyeticave.net/content/repositories/pub_releases - - - dre2n-repo - http://feuerstern.bplaced.net/repo/ - - - sk89q-repo - http://maven.sk89q.com/artifactory/repo/ - - - addstar-repo - http://maven.addstar.com.au/artifactory/ext-release-local/ - - true - always - - - - md_5-releases - http://repo.md-5.net/content/repositories/releases/ - - + + + spigot-repo + https://hub.spigotmc.org/nexus/content/groups/public/ + + + vault-repo + http://nexus.theyeticave.net/content/repositories/pub_releases + + + dre2n-repo + http://feuerstern.bplaced.net/repo/ + + + sk89q-repo + http://maven.sk89q.com/artifactory/repo/ + + + addstar-repo + http://maven.addstar.com.au/artifactory/ext-release-local/ + + true + always + + + + md_5-releases + http://repo.md-5.net/content/repositories/releases/ + + - - - org.bukkit - bukkit - 1.9-R0.1-SNAPSHOT - - - net.milkbowl.vault - VaultAPI - 1.5 - provided - - - com.sk89q - worldguard - 6.1 - - - com.griefcraft - entitylwc - 1.7.2 - - - me.ryanhamshire - GriefPrevention - 13.9.1 - compile - - - de.diddiz - logblock - 1.94 - - + + + org.bukkit + bukkit + 1.9-R0.1-SNAPSHOT + + + net.milkbowl.vault + VaultAPI + 1.5 + provided + + + com.sk89q + worldguard + 6.1 + + + com.griefcraft + entitylwc + 1.7.2 + + + me.ryanhamshire + GriefPrevention + 13.9.1 + compile + + + de.diddiz + logblock + 1.94 + + diff --git a/src/com/dre/brewery/Brew.java b/src/com/dre/brewery/Brew.java index 435932a..eb89a14 100644 --- a/src/com/dre/brewery/Brew.java +++ b/src/com/dre/brewery/Brew.java @@ -14,686 +14,686 @@ import org.bukkit.potion.PotionEffectType; public class Brew { - // represents the liquid in the brewed Potions - public static Map potions = new HashMap(); - public static Boolean colorInBarrels; // color the Lore while in Barrels - public static Boolean colorInBrewer; // color the Lore while in Brewer + // represents the liquid in the brewed Potions + public static Map potions = new HashMap(); + public static Boolean colorInBarrels; // color the Lore while in Barrels + public static Boolean colorInBrewer; // color the Lore while in Brewer - private BIngredients ingredients; - private int quality; - private int distillRuns; - private float ageTime; - private float wood; - private BRecipe currentRecipe; - private boolean unlabeled; - private boolean persistent; - private boolean stat; // static potions should not be changed + private BIngredients ingredients; + private int quality; + private int distillRuns; + private float ageTime; + private float wood; + private BRecipe currentRecipe; + private boolean unlabeled; + private boolean persistent; + private boolean stat; // static potions should not be changed - public Brew(int uid, BIngredients ingredients) { - this.ingredients = ingredients; - potions.put(uid, this); - } + public Brew(int uid, BIngredients ingredients) { + this.ingredients = ingredients; + potions.put(uid, this); + } - // quality already set - public Brew(int uid, int quality, BRecipe recipe, BIngredients ingredients) { - this.ingredients = ingredients; - this.quality = quality; - this.currentRecipe = recipe; - potions.put(uid, this); - } + // quality already set + public Brew(int uid, int quality, BRecipe recipe, BIngredients ingredients) { + this.ingredients = ingredients; + this.quality = quality; + this.currentRecipe = recipe; + potions.put(uid, this); + } - // loading from file - public Brew(int uid, BIngredients ingredients, int quality, int distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean persistent, boolean stat) { - potions.put(uid, this); - this.ingredients = ingredients; - this.quality = quality; - this.distillRuns = distillRuns; - this.ageTime = ageTime; - this.wood = wood; - this.unlabeled = unlabeled; - this.persistent = persistent; - this.stat = stat; - setRecipeFromString(recipe); - } + // loading from file + public Brew(int uid, BIngredients ingredients, int quality, int distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean persistent, boolean stat) { + potions.put(uid, this); + this.ingredients = ingredients; + this.quality = quality; + this.distillRuns = distillRuns; + this.ageTime = ageTime; + this.wood = wood; + this.unlabeled = unlabeled; + this.persistent = persistent; + this.stat = stat; + setRecipeFromString(recipe); + } - // returns a Brew by its UID - public static Brew get(int uid) { - if (uid < -1) { - if (!potions.containsKey(uid)) { - P.p.errorLog("Database failure! unable to find UID " + uid + " of a custom Potion!"); - return null;// throw some exception? - } - } else { - return null; - } - return potions.get(uid); - } + // returns a Brew by its UID + public static Brew get(int uid) { + if (uid < -1) { + if (!potions.containsKey(uid)) { + P.p.errorLog("Database failure! unable to find UID " + uid + " of a custom Potion!"); + return null;// throw some exception? + } + } else { + return null; + } + return potions.get(uid); + } - // returns a Brew by PotionMeta - public static Brew get(PotionMeta meta) { - return get(getUID(meta)); - } + // returns a Brew by PotionMeta + public static Brew get(PotionMeta meta) { + return get(getUID(meta)); + } - // returns a Brew by ItemStack - public static Brew get(ItemStack item) { - if (item.getType() == Material.POTION) { - if (item.hasItemMeta()) { - PotionMeta potionMeta = (PotionMeta) item.getItemMeta(); - return get(potionMeta); - } - } - return null; - } + // returns a Brew by ItemStack + public static Brew get(ItemStack item) { + if (item.getType() == Material.POTION) { + if (item.hasItemMeta()) { + PotionMeta potionMeta = (PotionMeta) item.getItemMeta(); + return get(potionMeta); + } + } + return null; + } - // returns UID of custom Potion item - public static int getUID(ItemStack item) { - return getUID((PotionMeta) item.getItemMeta()); - } + // returns UID of custom Potion item + public static int getUID(ItemStack item) { + return getUID((PotionMeta) item.getItemMeta()); + } - // returns UID of custom Potion meta - public static int getUID(PotionMeta potionMeta) { - if (potionMeta.hasCustomEffect(PotionEffectType.REGENERATION)) { - for (PotionEffect effect : potionMeta.getCustomEffects()) { - if (effect.getType().equals(PotionEffectType.REGENERATION)) { - if (effect.getDuration() < -1) { - return effect.getDuration(); - } - } - } - } - return 0; - } + // returns UID of custom Potion meta + public static int getUID(PotionMeta potionMeta) { + if (potionMeta.hasCustomEffect(PotionEffectType.REGENERATION)) { + for (PotionEffect effect : potionMeta.getCustomEffects()) { + if (effect.getType().equals(PotionEffectType.REGENERATION)) { + if (effect.getDuration() < -1) { + return effect.getDuration(); + } + } + } + } + return 0; + } - // generate an UID - public static int generateUID() { - int uid = -2; - while (potions.containsKey(uid)) { - uid -= 1; - } - return uid; - } + // generate an UID + public static int generateUID() { + int uid = -2; + while (potions.containsKey(uid)) { + uid -= 1; + } + return uid; + } - //returns the recipe with the given name, recalculates if not found - public boolean setRecipeFromString(String name) { - currentRecipe = null; - if (name != null && !name.equals("")) { - for (BRecipe recipe : BIngredients.recipes) { - if (recipe.getName(5).equalsIgnoreCase(name)) { - currentRecipe = recipe; - return true; - } - } + //returns the recipe with the given name, recalculates if not found + public boolean setRecipeFromString(String name) { + currentRecipe = null; + if (name != null && !name.equals("")) { + for (BRecipe recipe : BIngredients.recipes) { + if (recipe.getName(5).equalsIgnoreCase(name)) { + currentRecipe = recipe; + return true; + } + } - if (quality > 0) { - currentRecipe = ingredients.getBestRecipe(wood, ageTime, distillRuns > 0); - if (currentRecipe != null) { - if (!stat) { - this.quality = calcQuality(); - } - P.p.log("Brew was made from Recipe: '" + name + "' which could not be found. '" + currentRecipe.getName(5) + "' used instead!"); - return true; - } else { - P.p.errorLog("Brew was made from Recipe: '" + name + "' which could not be found!"); - } - } - } - return false; - } + if (quality > 0) { + currentRecipe = ingredients.getBestRecipe(wood, ageTime, distillRuns > 0); + if (currentRecipe != null) { + if (!stat) { + this.quality = calcQuality(); + } + P.p.log("Brew was made from Recipe: '" + name + "' which could not be found. '" + currentRecipe.getName(5) + "' used instead!"); + return true; + } else { + P.p.errorLog("Brew was made from Recipe: '" + name + "' which could not be found!"); + } + } + } + return false; + } - public boolean reloadRecipe() { - if (currentRecipe != null) { - return setRecipeFromString(currentRecipe.getName(5)); - } - return true; - } + public boolean reloadRecipe() { + if (currentRecipe != null) { + return setRecipeFromString(currentRecipe.getName(5)); + } + return true; + } - // Copy a Brew with a new unique ID and return its item - public ItemStack copy(ItemStack item) { - ItemStack copy = item.clone(); - int uid = generateUID(); - clone(uid); - PotionMeta meta = (PotionMeta) copy.getItemMeta(); - meta.addCustomEffect((PotionEffectType.REGENERATION).createEffect((uid * 4), 0), true); - copy.setItemMeta(meta); - return copy; - } + // Copy a Brew with a new unique ID and return its item + public ItemStack copy(ItemStack item) { + ItemStack copy = item.clone(); + int uid = generateUID(); + clone(uid); + PotionMeta meta = (PotionMeta) copy.getItemMeta(); + meta.addCustomEffect((PotionEffectType.REGENERATION).createEffect((uid * 4), 0), true); + copy.setItemMeta(meta); + return copy; + } - // Clones this instance with a new unique ID - public Brew clone(int uid) { - Brew brew = new Brew(uid, quality, currentRecipe, ingredients); - brew.distillRuns = distillRuns; - brew.ageTime = ageTime; - brew.unlabeled = unlabeled; - if (!brew.persistent) { - brew.stat = stat; - } - return brew; - } + // Clones this instance with a new unique ID + public Brew clone(int uid) { + Brew brew = new Brew(uid, quality, currentRecipe, ingredients); + brew.distillRuns = distillRuns; + brew.ageTime = ageTime; + brew.unlabeled = unlabeled; + if (!brew.persistent) { + brew.stat = stat; + } + return brew; + } - // remove potion from file (drinking, despawning, combusting, cmdDeleting, should be more!) - public void remove(ItemStack item) { - if (!persistent) { - potions.remove(getUID(item)); - } - } + // remove potion from file (drinking, despawning, combusting, cmdDeleting, should be more!) + public void remove(ItemStack item) { + if (!persistent) { + potions.remove(getUID(item)); + } + } - // calculate alcohol from recipe - public int calcAlcohol() { - if (quality == 0) { - // Give bad potions some alc - int badAlc = 0; - if (distillRuns > 1) { - badAlc = distillRuns; - } - if (ageTime > 10) { - badAlc += 5; - } else if (ageTime > 2) { - badAlc += 3; - } - if (currentRecipe != null) { - return badAlc; - } else { - return badAlc / 2; - } - } + // calculate alcohol from recipe + public int calcAlcohol() { + if (quality == 0) { + // Give bad potions some alc + int badAlc = 0; + if (distillRuns > 1) { + badAlc = distillRuns; + } + if (ageTime > 10) { + badAlc += 5; + } else if (ageTime > 2) { + badAlc += 3; + } + if (currentRecipe != null) { + return badAlc; + } else { + return badAlc / 2; + } + } - if (currentRecipe != null) { - int alc = currentRecipe.getAlcohol(); - if (currentRecipe.needsDistilling()) { - if (distillRuns == 0) { - return 0; - } - // bad quality can decrease alc by up to 40% - alc *= 1 - ((float) (10 - quality) * 0.04); - // distillable Potions should have half alc after one and full alc after all needed distills - alc /= 2; - alc *= 1.0F + ((float) distillRuns / currentRecipe.getDistillRuns()); - } else { - // quality decides 10% - 100% - alc *= ((float) quality / 10.0); - } - if (alc > 0) { - return alc; - } - } - return 0; - } + if (currentRecipe != null) { + int alc = currentRecipe.getAlcohol(); + if (currentRecipe.needsDistilling()) { + if (distillRuns == 0) { + return 0; + } + // bad quality can decrease alc by up to 40% + alc *= 1 - ((float) (10 - quality) * 0.04); + // distillable Potions should have half alc after one and full alc after all needed distills + alc /= 2; + alc *= 1.0F + ((float) distillRuns / currentRecipe.getDistillRuns()); + } else { + // quality decides 10% - 100% + alc *= ((float) quality / 10.0); + } + if (alc > 0) { + return alc; + } + } + return 0; + } - // calculating quality - public int calcQuality() { - // calculate quality from all of the factors - float quality = ingredients.getIngredientQuality(currentRecipe) + ingredients.getCookingQuality(currentRecipe, distillRuns > 0); - if (currentRecipe.needsToAge() || ageTime > 0.5) { - quality += ingredients.getWoodQuality(currentRecipe, wood) + ingredients.getAgeQuality(currentRecipe, ageTime); - quality /= 4; - } else { - quality /= 2; - } - return Math.round(quality); - } + // calculating quality + public int calcQuality() { + // calculate quality from all of the factors + float quality = ingredients.getIngredientQuality(currentRecipe) + ingredients.getCookingQuality(currentRecipe, distillRuns > 0); + if (currentRecipe.needsToAge() || ageTime > 0.5) { + quality += ingredients.getWoodQuality(currentRecipe, wood) + ingredients.getAgeQuality(currentRecipe, ageTime); + quality /= 4; + } else { + quality /= 2; + } + return Math.round(quality); + } - public int getQuality() { - return quality; - } + public int getQuality() { + return quality; + } - public boolean canDistill() { - if (currentRecipe != null) { - return currentRecipe.getDistillRuns() > distillRuns; - } else if (distillRuns >= 6) { - return false; - } - return true; - } + public boolean canDistill() { + if (currentRecipe != null) { + return currentRecipe.getDistillRuns() > distillRuns; + } else if (distillRuns >= 6) { + return false; + } + return true; + } - // return special effect - public ArrayList getEffects() { - if (currentRecipe != null && quality > 0) { - return currentRecipe.getEffects(); - } - return null; - } + // return special effect + public ArrayList getEffects() { + if (currentRecipe != null && quality > 0) { + return currentRecipe.getEffects(); + } + return null; + } - // Set unlabeled to true to hide the numbers in Lore - public void unLabel(ItemStack item) { - PotionMeta meta = (PotionMeta) item.getItemMeta(); - if (meta.hasLore()) { - if (distillRuns > 0) { - addOrReplaceLore(meta, P.p.color("&7"), P.p.languageReader.get("Brew_Distilled")); - } - if (ageTime >= 1) { - addOrReplaceLore(meta, P.p.color("&7"), P.p.languageReader.get("Brew_BarrelRiped")); - } - item.setItemMeta(meta); - } - unlabeled = true; - } + // Set unlabeled to true to hide the numbers in Lore + public void unLabel(ItemStack item) { + PotionMeta meta = (PotionMeta) item.getItemMeta(); + if (meta.hasLore()) { + if (distillRuns > 0) { + addOrReplaceLore(meta, P.p.color("&7"), P.p.languageReader.get("Brew_Distilled")); + } + if (ageTime >= 1) { + addOrReplaceLore(meta, P.p.color("&7"), P.p.languageReader.get("Brew_BarrelRiped")); + } + item.setItemMeta(meta); + } + unlabeled = true; + } - public boolean isPersistent() { - return persistent; - } + public boolean isPersistent() { + return persistent; + } - // Make a potion persistent to not delete it when drinking it - public void makePersistent() { - persistent = true; - } + // Make a potion persistent to not delete it when drinking it + public void makePersistent() { + persistent = true; + } - // Remove the Persistence Flag from a brew, so it will be normally deleted when drinking it - public void removePersistence() { - persistent = false; - } + // Remove the Persistence Flag from a brew, so it will be normally deleted when drinking it + public void removePersistence() { + persistent = false; + } - public boolean isStatic() { - return stat; - } + public boolean isStatic() { + return stat; + } - // Set the Static flag, so potion is unchangeable - public void setStatic(boolean stat, ItemStack potion) { - this.stat = stat; - if (currentRecipe != null && canDistill()) { - if (stat) { - if (P.use1_9) { - PotionMeta potionMeta = (PotionMeta) potion.getItemMeta(); - potionMeta.setMainEffect(PotionColor.valueOf(currentRecipe.getColor()).getEffect()); - } else { - potion.setDurability(PotionColor.valueOf(currentRecipe.getColor()).getColorId(false)); - } - } else if (P.use1_9) { - PotionMeta potionMeta = (PotionMeta) potion.getItemMeta(); - potionMeta.setMainEffect(PotionColor.valueOf(currentRecipe.getColor()).getEffect()); - } else { - potion.setDurability(PotionColor.valueOf(currentRecipe.getColor()).getColorId(true)); - } - } - } + // Set the Static flag, so potion is unchangeable + public void setStatic(boolean stat, ItemStack potion) { + this.stat = stat; + if (currentRecipe != null && canDistill()) { + if (stat) { + if (P.use1_9) { + PotionMeta potionMeta = (PotionMeta) potion.getItemMeta(); + potionMeta.setMainEffect(PotionColor.valueOf(currentRecipe.getColor()).getEffect()); + } else { + potion.setDurability(PotionColor.valueOf(currentRecipe.getColor()).getColorId(false)); + } + } else if (P.use1_9) { + PotionMeta potionMeta = (PotionMeta) potion.getItemMeta(); + potionMeta.setMainEffect(PotionColor.valueOf(currentRecipe.getColor()).getEffect()); + } else { + potion.setDurability(PotionColor.valueOf(currentRecipe.getColor()).getColorId(true)); + } + } + } - // Distilling section --------------- - // distill all custom potions in the brewer - public static void distillAll(BrewerInventory inv, Boolean[] contents) { - int slot = 0; - while (slot < 3) { - if (contents[slot]) { - ItemStack slotItem = inv.getItem(slot); - PotionMeta potionMeta = (PotionMeta) slotItem.getItemMeta(); - Brew brew = get(potionMeta); - brew.distillSlot(slotItem, potionMeta); - } - slot++; - } - } + // Distilling section --------------- + // distill all custom potions in the brewer + public static void distillAll(BrewerInventory inv, Boolean[] contents) { + int slot = 0; + while (slot < 3) { + if (contents[slot]) { + ItemStack slotItem = inv.getItem(slot); + PotionMeta potionMeta = (PotionMeta) slotItem.getItemMeta(); + Brew brew = get(potionMeta); + brew.distillSlot(slotItem, potionMeta); + } + slot++; + } + } - // distill custom potion in given slot - public void distillSlot(ItemStack slotItem, PotionMeta potionMeta) { - if (stat) { - return; - } + // distill custom potion in given slot + public void distillSlot(ItemStack slotItem, PotionMeta potionMeta) { + if (stat) { + return; + } - distillRuns += 1; - BRecipe recipe = ingredients.getdistillRecipe(wood, ageTime); - if (recipe != null) { - // distillRuns will have an effect on the amount of alcohol, not the quality - currentRecipe = recipe; - quality = calcQuality(); + distillRuns += 1; + BRecipe recipe = ingredients.getdistillRecipe(wood, ageTime); + if (recipe != null) { + // distillRuns will have an effect on the amount of alcohol, not the quality + currentRecipe = recipe; + quality = calcQuality(); - addOrReplaceEffects(potionMeta, getEffects(), quality); - potionMeta.setDisplayName(P.p.color("&f" + recipe.getName(quality))); - if (P.use1_9) { - potionMeta.setMainEffect(PotionColor.valueOf(recipe.getColor()).getEffect()); - } else { - slotItem.setDurability(PotionColor.valueOf(recipe.getColor()).getColorId(canDistill())); - } + addOrReplaceEffects(potionMeta, getEffects(), quality); + potionMeta.setDisplayName(P.p.color("&f" + recipe.getName(quality))); + if (P.use1_9) { + potionMeta.setMainEffect(PotionColor.valueOf(recipe.getColor()).getEffect()); + } else { + slotItem.setDurability(PotionColor.valueOf(recipe.getColor()).getColorId(canDistill())); + } - } else { - quality = 0; - removeEffects(potionMeta); - potionMeta.setDisplayName(P.p.color("&f" + P.p.languageReader.get("Brew_DistillUndefined"))); - if (P.use1_9) { - potionMeta.setMainEffect(PotionColor.GREY.getEffect()); - } else { - slotItem.setDurability(PotionColor.GREY.getColorId(canDistill())); - } - } + } else { + quality = 0; + removeEffects(potionMeta); + potionMeta.setDisplayName(P.p.color("&f" + P.p.languageReader.get("Brew_DistillUndefined"))); + if (P.use1_9) { + potionMeta.setMainEffect(PotionColor.GREY.getEffect()); + } else { + slotItem.setDurability(PotionColor.GREY.getColorId(canDistill())); + } + } - // Distill Lore - if (currentRecipe != null) { - if (colorInBrewer != hasColorLore(potionMeta)) { - convertLore(potionMeta, colorInBrewer); - } - } - String prefix = P.p.color("&7"); - if (colorInBrewer && currentRecipe != null) { - prefix = getQualityColor(ingredients.getDistillQuality(recipe, distillRuns)); - } - updateDistillLore(prefix, potionMeta); + // Distill Lore + if (currentRecipe != null) { + if (colorInBrewer != hasColorLore(potionMeta)) { + convertLore(potionMeta, colorInBrewer); + } + } + String prefix = P.p.color("&7"); + if (colorInBrewer && currentRecipe != null) { + prefix = getQualityColor(ingredients.getDistillQuality(recipe, distillRuns)); + } + updateDistillLore(prefix, potionMeta); - slotItem.setItemMeta(potionMeta); - } + slotItem.setItemMeta(potionMeta); + } - // Ageing Section ------------------ - public void age(ItemStack item, float time, byte woodType) { - if (stat) { - return; - } + // Ageing Section ------------------ + public void age(ItemStack item, float time, byte woodType) { + if (stat) { + return; + } - PotionMeta potionMeta = (PotionMeta) item.getItemMeta(); - ageTime += time; + PotionMeta potionMeta = (PotionMeta) item.getItemMeta(); + ageTime += time; - // if younger than half a day, it shouldnt get aged form - if (ageTime > 0.5) { - if (wood == 0) { - wood = woodType; - } else if (wood != woodType) { - woodShift(time, woodType); - } - BRecipe recipe = ingredients.getAgeRecipe(wood, ageTime, distillRuns > 0); - if (recipe != null) { - currentRecipe = recipe; - quality = calcQuality(); + // if younger than half a day, it shouldnt get aged form + if (ageTime > 0.5) { + if (wood == 0) { + wood = woodType; + } else if (wood != woodType) { + woodShift(time, woodType); + } + BRecipe recipe = ingredients.getAgeRecipe(wood, ageTime, distillRuns > 0); + if (recipe != null) { + currentRecipe = recipe; + quality = calcQuality(); - addOrReplaceEffects(potionMeta, getEffects(), quality); - potionMeta.setDisplayName(P.p.color("&f" + recipe.getName(quality))); - if (P.use1_9) { - potionMeta.setMainEffect(PotionColor.valueOf(recipe.getColor()).getEffect()); - } else { - item.setDurability(PotionColor.valueOf(recipe.getColor()).getColorId(canDistill())); - } - } else { - quality = 0; - removeEffects(potionMeta); - potionMeta.setDisplayName(P.p.color("&f" + P.p.languageReader.get("Brew_BadPotion"))); - if (P.use1_9) { - potionMeta.setMainEffect(PotionColor.GREY.getEffect()); - } else { - item.setDurability(PotionColor.GREY.getColorId(canDistill())); - } - } - } + addOrReplaceEffects(potionMeta, getEffects(), quality); + potionMeta.setDisplayName(P.p.color("&f" + recipe.getName(quality))); + if (P.use1_9) { + potionMeta.setMainEffect(PotionColor.valueOf(recipe.getColor()).getEffect()); + } else { + item.setDurability(PotionColor.valueOf(recipe.getColor()).getColorId(canDistill())); + } + } else { + quality = 0; + removeEffects(potionMeta); + potionMeta.setDisplayName(P.p.color("&f" + P.p.languageReader.get("Brew_BadPotion"))); + if (P.use1_9) { + potionMeta.setMainEffect(PotionColor.GREY.getEffect()); + } else { + item.setDurability(PotionColor.GREY.getColorId(canDistill())); + } + } + } - // Lore - if (currentRecipe != null) { - if (colorInBarrels != hasColorLore(potionMeta)) { - convertLore(potionMeta, colorInBarrels); - } - } - if (ageTime >= 1) { - String prefix = P.p.color("&7"); - if (colorInBarrels && currentRecipe != null) { - prefix = getQualityColor(ingredients.getAgeQuality(currentRecipe, ageTime)); - } - updateAgeLore(prefix, potionMeta); - } - if (ageTime > 0.5) { - if (colorInBarrels && !unlabeled && currentRecipe != null) { - updateWoodLore(potionMeta); - } - } - item.setItemMeta(potionMeta); - } + // Lore + if (currentRecipe != null) { + if (colorInBarrels != hasColorLore(potionMeta)) { + convertLore(potionMeta, colorInBarrels); + } + } + if (ageTime >= 1) { + String prefix = P.p.color("&7"); + if (colorInBarrels && currentRecipe != null) { + prefix = getQualityColor(ingredients.getAgeQuality(currentRecipe, ageTime)); + } + updateAgeLore(prefix, potionMeta); + } + if (ageTime > 0.5) { + if (colorInBarrels && !unlabeled && currentRecipe != null) { + updateWoodLore(potionMeta); + } + } + item.setItemMeta(potionMeta); + } - // Slowly shift the wood of the Brew to the new Type - public void woodShift(float time, byte to) { - byte factor = 1; - if (ageTime > 5) { - factor = 2; - } else if (ageTime > 10) { - factor = 2; - factor += Math.round(ageTime / 10); - } - if (wood > to) { - wood -= time / factor; - if (wood < to) { - wood = to; - } - } else { - wood += time / factor; - if (wood > to) { - wood = to; - } - } - } + // Slowly shift the wood of the Brew to the new Type + public void woodShift(float time, byte to) { + byte factor = 1; + if (ageTime > 5) { + factor = 2; + } else if (ageTime > 10) { + factor = 2; + factor += Math.round(ageTime / 10); + } + if (wood > to) { + wood -= time / factor; + if (wood < to) { + wood = to; + } + } else { + wood += time / factor; + if (wood > to) { + wood = to; + } + } + } - // Lore ----------- - // Converts to/from qualitycolored Lore - public void convertLore(PotionMeta meta, Boolean toQuality) { - if (currentRecipe == null) { - return; - } - meta.setLore(null); - int quality; - String prefix = P.p.color("&7"); - String lore; + // Lore ----------- + // Converts to/from qualitycolored Lore + public void convertLore(PotionMeta meta, Boolean toQuality) { + if (currentRecipe == null) { + return; + } + meta.setLore(null); + int quality; + String prefix = P.p.color("&7"); + String lore; - // Ingredients - if (toQuality && !unlabeled) { - quality = ingredients.getIngredientQuality(currentRecipe); - prefix = getQualityColor(quality); - lore = P.p.languageReader.get("Brew_Ingredients"); - addOrReplaceLore(meta, prefix, lore); - } + // Ingredients + if (toQuality && !unlabeled) { + quality = ingredients.getIngredientQuality(currentRecipe); + prefix = getQualityColor(quality); + lore = P.p.languageReader.get("Brew_Ingredients"); + addOrReplaceLore(meta, prefix, lore); + } - // Cooking - if (toQuality && !unlabeled) { - if (distillRuns > 0 == currentRecipe.needsDistilling()) { - quality = ingredients.getCookingQuality(currentRecipe, distillRuns > 0); - prefix = getQualityColor(quality) + ingredients.getCookedTime() + " " + P.p.languageReader.get("Brew_minute"); - if (ingredients.getCookedTime() > 1) { - prefix = prefix + P.p.languageReader.get("Brew_MinutePluralPostfix"); - } - lore = " " + P.p.languageReader.get("Brew_fermented"); - addOrReplaceLore(meta, prefix, lore); - } - } + // Cooking + if (toQuality && !unlabeled) { + if (distillRuns > 0 == currentRecipe.needsDistilling()) { + quality = ingredients.getCookingQuality(currentRecipe, distillRuns > 0); + prefix = getQualityColor(quality) + ingredients.getCookedTime() + " " + P.p.languageReader.get("Brew_minute"); + if (ingredients.getCookedTime() > 1) { + prefix = prefix + P.p.languageReader.get("Brew_MinutePluralPostfix"); + } + lore = " " + P.p.languageReader.get("Brew_fermented"); + addOrReplaceLore(meta, prefix, lore); + } + } - // Distilling - if (distillRuns > 0) { - if (toQuality) { - quality = ingredients.getDistillQuality(currentRecipe, distillRuns); - prefix = getQualityColor(quality); - } - updateDistillLore(prefix, meta); - } + // Distilling + if (distillRuns > 0) { + if (toQuality) { + quality = ingredients.getDistillQuality(currentRecipe, distillRuns); + prefix = getQualityColor(quality); + } + updateDistillLore(prefix, meta); + } - // Ageing - if (ageTime >= 1) { - if (toQuality) { - quality = ingredients.getAgeQuality(currentRecipe, ageTime); - prefix = getQualityColor(quality); - } - updateAgeLore(prefix, meta); - } + // Ageing + if (ageTime >= 1) { + if (toQuality) { + quality = ingredients.getAgeQuality(currentRecipe, ageTime); + prefix = getQualityColor(quality); + } + updateAgeLore(prefix, meta); + } - // WoodType - if (toQuality && !unlabeled) { - if (ageTime > 0.5) { - updateWoodLore(meta); - } - } - } + // WoodType + if (toQuality && !unlabeled) { + if (ageTime > 0.5) { + updateWoodLore(meta); + } + } + } - // sets the DistillLore. Prefix is the color to be used - public void updateDistillLore(String prefix, PotionMeta meta) { - if (!unlabeled) { - if (distillRuns > 1) { - prefix = prefix + distillRuns + P.p.languageReader.get("Brew_-times") + " "; - } - } - addOrReplaceLore(meta, prefix, P.p.languageReader.get("Brew_Distilled")); - } + // sets the DistillLore. Prefix is the color to be used + public void updateDistillLore(String prefix, PotionMeta meta) { + if (!unlabeled) { + if (distillRuns > 1) { + prefix = prefix + distillRuns + P.p.languageReader.get("Brew_-times") + " "; + } + } + addOrReplaceLore(meta, prefix, P.p.languageReader.get("Brew_Distilled")); + } - // sets the AgeLore. Prefix is the color to be used - public void updateAgeLore(String prefix, PotionMeta meta) { - if (!unlabeled) { - if (ageTime >= 1 && ageTime < 2) { - prefix = prefix + P.p.languageReader.get("Brew_OneYear") + " "; - } else if (ageTime < 201) { - prefix = prefix + (int) Math.floor(ageTime) + " " + P.p.languageReader.get("Brew_Years") + " "; - } else { - prefix = prefix + P.p.languageReader.get("Brew_HundredsOfYears") + " "; - } - } - addOrReplaceLore(meta, prefix, P.p.languageReader.get("Brew_BarrelRiped")); - } + // sets the AgeLore. Prefix is the color to be used + public void updateAgeLore(String prefix, PotionMeta meta) { + if (!unlabeled) { + if (ageTime >= 1 && ageTime < 2) { + prefix = prefix + P.p.languageReader.get("Brew_OneYear") + " "; + } else if (ageTime < 201) { + prefix = prefix + (int) Math.floor(ageTime) + " " + P.p.languageReader.get("Brew_Years") + " "; + } else { + prefix = prefix + P.p.languageReader.get("Brew_HundredsOfYears") + " "; + } + } + addOrReplaceLore(meta, prefix, P.p.languageReader.get("Brew_BarrelRiped")); + } - // updates/sets the color on WoodLore - public void updateWoodLore(PotionMeta meta) { - if (currentRecipe.getWood() > 0) { - int quality = ingredients.getWoodQuality(currentRecipe, wood); - addOrReplaceLore(meta, getQualityColor(quality), P.p.languageReader.get("Brew_Woodtype")); - } else if (meta.hasLore()) { - List existingLore = meta.getLore(); - int index = indexOfSubstring(existingLore, P.p.languageReader.get("Brew_Woodtype")); - if (index > -1) { - existingLore.remove(index); - meta.setLore(existingLore); - } - } - } + // updates/sets the color on WoodLore + public void updateWoodLore(PotionMeta meta) { + if (currentRecipe.getWood() > 0) { + int quality = ingredients.getWoodQuality(currentRecipe, wood); + addOrReplaceLore(meta, getQualityColor(quality), P.p.languageReader.get("Brew_Woodtype")); + } else if (meta.hasLore()) { + List existingLore = meta.getLore(); + int index = indexOfSubstring(existingLore, P.p.languageReader.get("Brew_Woodtype")); + if (index > -1) { + existingLore.remove(index); + meta.setLore(existingLore); + } + } + } - // Adds or replaces a line of Lore. Searches for Substring lore and replaces it - public static void addOrReplaceLore(PotionMeta meta, String prefix, String lore) { - if (meta.hasLore()) { - List existingLore = meta.getLore(); - int index = indexOfSubstring(existingLore, lore); - if (index > -1) { - existingLore.set(index, prefix + lore); - } else { - existingLore.add(prefix + lore); - } - meta.setLore(existingLore); - return; - } - List newLore = new ArrayList(); - newLore.add(""); - newLore.add(prefix + lore); - meta.setLore(newLore); - } + // Adds or replaces a line of Lore. Searches for Substring lore and replaces it + public static void addOrReplaceLore(PotionMeta meta, String prefix, String lore) { + if (meta.hasLore()) { + List existingLore = meta.getLore(); + int index = indexOfSubstring(existingLore, lore); + if (index > -1) { + existingLore.set(index, prefix + lore); + } else { + existingLore.add(prefix + lore); + } + meta.setLore(existingLore); + return; + } + List newLore = new ArrayList(); + newLore.add(""); + newLore.add(prefix + lore); + meta.setLore(newLore); + } - // Adds the Effect names to the Items description - public static void addOrReplaceEffects(PotionMeta meta, ArrayList effects, int quality) { - if (effects != null) { - for (BEffect effect : effects) { - if (!effect.isHidden()) { - effect.writeInto(meta, quality); - } - } - } - } + // Adds the Effect names to the Items description + public static void addOrReplaceEffects(PotionMeta meta, ArrayList effects, int quality) { + if (effects != null) { + for (BEffect effect : effects) { + if (!effect.isHidden()) { + effect.writeInto(meta, quality); + } + } + } + } - // Removes all effects except regeneration which stores data - public static void removeEffects(PotionMeta meta) { - if (meta.hasCustomEffects()) { - for (PotionEffect effect : meta.getCustomEffects()) { - PotionEffectType type = effect.getType(); - if (!type.equals(PotionEffectType.REGENERATION)) { - meta.removeCustomEffect(type); - } - } - } - } + // Removes all effects except regeneration which stores data + public static void removeEffects(PotionMeta meta) { + if (meta.hasCustomEffects()) { + for (PotionEffect effect : meta.getCustomEffects()) { + PotionEffectType type = effect.getType(); + if (!type.equals(PotionEffectType.REGENERATION)) { + meta.removeCustomEffect(type); + } + } + } + } - // Returns the Index of a String from the list that contains this substring - public static int indexOfSubstring(List list, String substring) { - for (int index = 0; index < list.size(); index++) { - String string = list.get(index); - if (string.contains(substring)) { - return index; - } - } - return -1; - } + // Returns the Index of a String from the list that contains this substring + public static int indexOfSubstring(List list, String substring) { + for (int index = 0; index < list.size(); index++) { + String string = list.get(index); + if (string.contains(substring)) { + return index; + } + } + return -1; + } - // True if the PotionMeta has colored Lore - public static Boolean hasColorLore(PotionMeta meta) { - return meta.hasLore() && !meta.getLore().get(1).startsWith(P.p.color("&7")); - } + // True if the PotionMeta has colored Lore + public static Boolean hasColorLore(PotionMeta meta) { + return meta.hasLore() && !meta.getLore().get(1).startsWith(P.p.color("&7")); + } - // gets the Color that represents a quality in Lore - public static String getQualityColor(int quality) { - String color; - if (quality > 8) { - color = "&a"; - } else if (quality > 6) { - color = "&e"; - } else if (quality > 4) { - color = "&6"; - } else if (quality > 2) { - color = "&c"; - } else { - color = "&4"; - } - return P.p.color(color); - } + // gets the Color that represents a quality in Lore + public static String getQualityColor(int quality) { + String color; + if (quality > 8) { + color = "&a"; + } else if (quality > 6) { + color = "&e"; + } else if (quality > 4) { + color = "&6"; + } else if (quality > 2) { + color = "&c"; + } else { + color = "&4"; + } + return P.p.color(color); + } - // Saves all data - public static void save(ConfigurationSection config) { - for (Map.Entry entry : potions.entrySet()) { - int uid = entry.getKey(); - Brew brew = entry.getValue(); - ConfigurationSection idConfig = config.createSection("" + uid); - // not saving unneccessary data - if (brew.quality != 0) { - idConfig.set("quality", brew.quality); - } - if (brew.distillRuns != 0) { - idConfig.set("distillRuns", brew.distillRuns); - } - if (brew.ageTime != 0) { - idConfig.set("ageTime", brew.ageTime); - } - if (brew.wood != -1) { - idConfig.set("wood", brew.wood); - } - if (brew.currentRecipe != null) { - idConfig.set("recipe", brew.currentRecipe.getName(5)); - } - if (brew.unlabeled) { - idConfig.set("unlabeled", true); - } - if (brew.persistent) { - idConfig.set("persist", true); - } - if (brew.stat) { - idConfig.set("stat", true); - } - // save the ingredients - idConfig.set("ingId", brew.ingredients.save(config.getParent())); - } - } + // Saves all data + public static void save(ConfigurationSection config) { + for (Map.Entry entry : potions.entrySet()) { + int uid = entry.getKey(); + Brew brew = entry.getValue(); + ConfigurationSection idConfig = config.createSection("" + uid); + // not saving unneccessary data + if (brew.quality != 0) { + idConfig.set("quality", brew.quality); + } + if (brew.distillRuns != 0) { + idConfig.set("distillRuns", brew.distillRuns); + } + if (brew.ageTime != 0) { + idConfig.set("ageTime", brew.ageTime); + } + if (brew.wood != -1) { + idConfig.set("wood", brew.wood); + } + if (brew.currentRecipe != null) { + idConfig.set("recipe", brew.currentRecipe.getName(5)); + } + if (brew.unlabeled) { + idConfig.set("unlabeled", true); + } + if (brew.persistent) { + idConfig.set("persist", true); + } + if (brew.stat) { + idConfig.set("stat", true); + } + // save the ingredients + idConfig.set("ingId", brew.ingredients.save(config.getParent())); + } + } - public static enum PotionColor { - PINK(1, PotionEffectType.REGENERATION), - CYAN(2, PotionEffectType.SPEED), - ORANGE(3, PotionEffectType.FIRE_RESISTANCE), - GREEN(4, PotionEffectType.POISON), - BRIGHT_RED(5, PotionEffectType.HEAL), - BLUE(6, PotionEffectType.NIGHT_VISION), - BLACK(8, PotionEffectType.WEAKNESS), - RED(9, PotionEffectType.INCREASE_DAMAGE), - GREY(10, PotionEffectType.SLOW), - WATER(11, PotionEffectType.WATER_BREATHING), - DARK_RED(12, PotionEffectType.HARM), - BRIGHT_GREY(14, PotionEffectType.INVISIBILITY); + public static enum PotionColor { + PINK(1, PotionEffectType.REGENERATION), + CYAN(2, PotionEffectType.SPEED), + ORANGE(3, PotionEffectType.FIRE_RESISTANCE), + GREEN(4, PotionEffectType.POISON), + BRIGHT_RED(5, PotionEffectType.HEAL), + BLUE(6, PotionEffectType.NIGHT_VISION), + BLACK(8, PotionEffectType.WEAKNESS), + RED(9, PotionEffectType.INCREASE_DAMAGE), + GREY(10, PotionEffectType.SLOW), + WATER(11, PotionEffectType.WATER_BREATHING), + DARK_RED(12, PotionEffectType.HARM), + BRIGHT_GREY(14, PotionEffectType.INVISIBILITY); - private final int colorId; - private final PotionEffectType effect; + private final int colorId; + private final PotionEffectType effect; - private PotionColor(int colorId, PotionEffectType effect) { - this.colorId = colorId; - this.effect = effect; - } + private PotionColor(int colorId, PotionEffectType effect) { + this.colorId = colorId; + this.effect = effect; + } - // gets the Damage Value, that sets a color on the potion - // offset +32 is not accepted by brewer, so not further destillable - public short getColorId(boolean destillable) { - if (destillable) { - return (short) (colorId + 64); - } - return (short) (colorId + 32); - } + // gets the Damage Value, that sets a color on the potion + // offset +32 is not accepted by brewer, so not further destillable + public short getColorId(boolean destillable) { + if (destillable) { + return (short) (colorId + 64); + } + return (short) (colorId + 32); + } - public PotionEffectType getEffect() { - return effect; - } + public PotionEffectType getEffect() { + return effect; + } - } + } } diff --git a/src/com/dre/brewery/integration/GriefPreventionBarrel.java b/src/com/dre/brewery/integration/GriefPreventionBarrel.java index bbd8978..421c42f 100644 --- a/src/com/dre/brewery/integration/GriefPreventionBarrel.java +++ b/src/com/dre/brewery/integration/GriefPreventionBarrel.java @@ -9,39 +9,39 @@ import org.bukkit.entity.Player; public class GriefPreventionBarrel { - static P brewery = P.p; - static GriefPrevention griefPrevention = GriefPrevention.instance; + static P brewery = P.p; + static GriefPrevention griefPrevention = GriefPrevention.instance; - public static boolean checkAccess(Player player, Block sign) { - PlayerData playerData = griefPrevention.dataStore.getPlayerData(player.getUniqueId()); + public static boolean checkAccess(Player player, Block sign) { + PlayerData playerData = griefPrevention.dataStore.getPlayerData(player.getUniqueId()); - if (!griefPrevention.claimsEnabledForWorld(player.getWorld()) || playerData.ignoreClaims || !griefPrevention.config_claims_preventTheft) { - return true; - } + if (!griefPrevention.claimsEnabledForWorld(player.getWorld()) || playerData.ignoreClaims || !griefPrevention.config_claims_preventTheft) { + return true; + } - // block container use during pvp combat - if (playerData.inPvpCombat()) { - brewery.msg(player, brewery.languageReader.get("Error_NoBarrelAccess")); - return false; - } + // block container use during pvp combat + if (playerData.inPvpCombat()) { + brewery.msg(player, brewery.languageReader.get("Error_NoBarrelAccess")); + return false; + } - // check permissions for the claim the Barrel is in - Claim claim = griefPrevention.dataStore.getClaimAt(sign.getLocation(), false, playerData.lastClaim); - if (claim != null) { - playerData.lastClaim = claim; - String noContainersReason = claim.allowContainers(player); - if (noContainersReason != null) { - brewery.msg(player, brewery.languageReader.get("Error_NoBarrelAccess")); - return false; - } - } + // check permissions for the claim the Barrel is in + Claim claim = griefPrevention.dataStore.getClaimAt(sign.getLocation(), false, playerData.lastClaim); + if (claim != null) { + playerData.lastClaim = claim; + String noContainersReason = claim.allowContainers(player); + if (noContainersReason != null) { + brewery.msg(player, brewery.languageReader.get("Error_NoBarrelAccess")); + return false; + } + } - // drop any pvp protection, as the player opens a barrel - if (playerData.pvpImmune) { - playerData.pvpImmune = false; - } + // drop any pvp protection, as the player opens a barrel + if (playerData.pvpImmune) { + playerData.pvpImmune = false; + } - return true; - } + return true; + } } diff --git a/src/com/dre/brewery/listeners/Compat1_9.java b/src/com/dre/brewery/listeners/Compat1_9.java index bc8686b..c42fca8 100644 --- a/src/com/dre/brewery/listeners/Compat1_9.java +++ b/src/com/dre/brewery/listeners/Compat1_9.java @@ -11,21 +11,21 @@ import org.bukkit.inventory.meta.PotionMeta; // Workaround to remove unwanted potion effects public class Compat1_9 implements Listener { - @EventHandler - public void onPlayerDrink(PlayerItemConsumeEvent event) { - ItemStack item = event.getItem(); - Brew brew = Brew.get(item); - if (brew == null) { - return; - } + @EventHandler + public void onPlayerDrink(PlayerItemConsumeEvent event) { + ItemStack item = event.getItem(); + Brew brew = Brew.get(item); + if (brew == null) { + return; + } - if (item.getType() == Material.POTION) { - try { - PotionMeta meta = (PotionMeta) item.getItemMeta(); - meta.setMainEffect(null); - item.setItemMeta(meta); - } catch (Exception exception) {} - } - } + if (item.getType() == Material.POTION) { + try { + PotionMeta meta = (PotionMeta) item.getItemMeta(); + meta.setMainEffect(null); + item.setItemMeta(meta); + } catch (Exception exception) {} + } + } } From bef0dd5fff191ce29a3d6b4d2b62d690141ae782 Mon Sep 17 00:00:00 2001 From: Daniel Boston Date: Mon, 18 Apr 2016 23:16:25 -0400 Subject: [PATCH 04/10] Fixing pom to allow builds. Only things I had to manually install for lack of a repository is GriefPrevention and LWCPlugin --- pom.xml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index aa38ef3..f15c5d9 100644 --- a/pom.xml +++ b/pom.xml @@ -52,13 +52,13 @@ vault-repo http://nexus.theyeticave.net/content/repositories/pub_releases - + sk89q-repo - http://maven.sk89q.com/artifactory/repo/ + http://maven.sk89q.com/repo/ addstar-repo @@ -92,14 +92,14 @@ 6.1 - com.griefcraft - entitylwc + com.griefcraft.lwc + LWCPlugin 1.7.2 me.ryanhamshire GriefPrevention - 13.9.1 + 14.5.1 compile From 950d36823db1aba798c39687cccbff52ad24589a Mon Sep 17 00:00:00 2001 From: Daniel Boston Date: Tue, 19 Apr 2016 07:59:18 -0400 Subject: [PATCH 05/10] Testing illustrates that the brew event is being called for invalid brews any more, which brewery was leveraging. For distillation, a new way of approaching things is now required. --- src/com/dre/brewery/listeners/InventoryListener.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/com/dre/brewery/listeners/InventoryListener.java b/src/com/dre/brewery/listeners/InventoryListener.java index dea31a0..21e8021 100644 --- a/src/com/dre/brewery/listeners/InventoryListener.java +++ b/src/com/dre/brewery/listeners/InventoryListener.java @@ -20,8 +20,13 @@ import com.dre.brewery.integration.LogBlockBarrel; public class InventoryListener implements Listener { - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = false) public void onBrew(BrewEvent event) { + if (event.isCancelled()) { + P.p.log("Got Cancelled Brew Event"); + } else { + P.p.log("Got Brew Event"); + } int slot = 0; BrewerInventory inv = event.getContents(); ItemStack item; From 54e47bcf0294feb0a8b440628a2c47261aa46707 Mon Sep 17 00:00:00 2001 From: Daniel Boston Date: Wed, 20 Apr 2016 00:13:21 -0400 Subject: [PATCH 06/10] Adding new listeners to emulate prior behavior. Needs work. --- .../brewery/listeners/InventoryListener.java | 118 +++++++++++++++++- 1 file changed, 116 insertions(+), 2 deletions(-) diff --git a/src/com/dre/brewery/listeners/InventoryListener.java b/src/com/dre/brewery/listeners/InventoryListener.java index 21e8021..fc3cfc8 100644 --- a/src/com/dre/brewery/listeners/InventoryListener.java +++ b/src/com/dre/brewery/listeners/InventoryListener.java @@ -1,16 +1,29 @@ package com.dre.brewery.listeners; +import java.util.HashMap; +import java.util.HashSet; +import java.util.UUID; + +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.BrewingStand; +import org.bukkit.entity.HumanEntity; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; 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.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.BrewerInventory; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.inventory.ItemStack; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.Bukkit; import org.bukkit.Material; import com.dre.brewery.Barrel; @@ -19,11 +32,112 @@ import com.dre.brewery.P; import com.dre.brewery.integration.LogBlockBarrel; public class InventoryListener implements Listener { - + + /* === Recreating manually the prior BrewEvent behavior. === */ + private HashSet trackedBrewmen = new HashSet(); + private HashMap trackedBrewers = new HashMap(); + + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onBrewerOpen(InventoryOpenEvent event) { + HumanEntity player = event.getPlayer(); + Inventory inv = event.getInventory(); + if (player == null || inv == null || !(inv instanceof BrewerInventory)) return; + + P.p.log("Starting brew inventory tracking"); + trackedBrewmen.add(player.getUniqueId()); + } + + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onBrewerClose(InventoryCloseEvent event) { + HumanEntity player = event.getPlayer(); + Inventory inv = event.getInventory(); + if (player == null || inv == null || !(inv instanceof BrewerInventory)) return; + + P.p.log("Stopping brew inventory tracking"); + trackedBrewmen.remove(player.getUniqueId()); + } + + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + public void onBrewerClick(InventoryClickEvent event) { + HumanEntity player = event.getWhoClicked(); + Inventory inv = event.getInventory(); + if (player == null || inv == null || !(inv instanceof BrewerInventory)) return; + + UUID puid = player.getUniqueId(); + if (!trackedBrewmen.contains(puid)) return; + + P.p.log("Tracking a new brew click event"); + + BrewerInventory brewer = (BrewerInventory) inv; + final Block brewery = brewer.getHolder().getBlock(); + + if (isCustom(brewer)) { + Integer curTask = trackedBrewers.get(brewery); + if (curTask != null) { + Bukkit.getScheduler().cancelTask(curTask); // cancel prior + P.p.log("Cancelling prior brew countdown"); + } + + P.p.log("Starting a new brew countdown"); + trackedBrewers.put(brewery, new BukkitRunnable() { + @Override + public void run() { + BlockState now = brewery.getState(); + if (now instanceof BrewingStand) { + BrewingStand stand = (BrewingStand) now; + // check if still custom + BrewerInventory brewer = stand.getInventory(); + if (isCustom(brewer) ) { + P.p.log("Still a valid brew distillation"); + if (stand.getBrewingTime() == 0) { + stand.setBrewingTime(400); // arbitrary for now + } else { + stand.setBrewingTime(stand.getBrewingTime() - 5); // count down. + } + + if (stand.getBrewingTime() <= 5) { // trigger. + P.p.log("Complete brew distillation!"); + BrewEvent doBrew = new BrewEvent(brewery, brewer); + Bukkit.getServer().getPluginManager().callEvent(doBrew); + if (doBrew.isCancelled()) { + this.cancel(); + trackedBrewers.remove(brewery); + } + stand.setBrewingTime(0); + } + } + } + } + }.runTaskTimer(P.p, 5l, 5l).getTaskId()); + } + } + + private boolean isCustom(BrewerInventory brewer) { + int slot = 0; + ItemStack item; + Boolean[] contents = new Boolean[3]; + while (slot < 3) { + item = brewer.getItem(slot); + contents[slot] = false; + if (item != null) { + if (item.getType() == Material.POTION) { + if (item.hasItemMeta()) { + int uid = Brew.getUID(item); + if (Brew.potions.containsKey(uid)) { + return true; + } + } + } + } + } + return false; + } + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = false) public void onBrew(BrewEvent event) { if (event.isCancelled()) { - P.p.log("Got Cancelled Brew Event"); + P.p.log("Got Cancelled Brew Event"); + return; } else { P.p.log("Got Brew Event"); } From 76976ba12dbca86ba109d68c1488fcfb80bffe07 Mon Sep 17 00:00:00 2001 From: ProgrammerDan Date: Wed, 20 Apr 2016 17:17:23 -0400 Subject: [PATCH 07/10] qfix for infinite loop and brewtime --- src/com/dre/brewery/listeners/InventoryListener.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/com/dre/brewery/listeners/InventoryListener.java b/src/com/dre/brewery/listeners/InventoryListener.java index fc3cfc8..6290018 100644 --- a/src/com/dre/brewery/listeners/InventoryListener.java +++ b/src/com/dre/brewery/listeners/InventoryListener.java @@ -80,6 +80,7 @@ public class InventoryListener implements Listener { P.p.log("Starting a new brew countdown"); trackedBrewers.put(brewery, new BukkitRunnable() { + private int brewTime = 405; @Override public void run() { BlockState now = brewery.getState(); @@ -89,13 +90,10 @@ public class InventoryListener implements Listener { BrewerInventory brewer = stand.getInventory(); if (isCustom(brewer) ) { P.p.log("Still a valid brew distillation"); - if (stand.getBrewingTime() == 0) { - stand.setBrewingTime(400); // arbitrary for now - } else { - stand.setBrewingTime(stand.getBrewingTime() - 5); // count down. - } + brewTime = brewTime - 5; // count down. + stand.setBrewingTime(brewTime); // arbitrary for now - if (stand.getBrewingTime() <= 5) { // trigger. + if (brewTime <= 5) { // trigger. P.p.log("Complete brew distillation!"); BrewEvent doBrew = new BrewEvent(brewery, brewer); Bukkit.getServer().getPluginManager().callEvent(doBrew); @@ -129,6 +127,7 @@ public class InventoryListener implements Listener { } } } + slot++; } return false; } From 0a91fec59171ac53d7dc5ccc42cf6ba161514e8f Mon Sep 17 00:00:00 2001 From: Daniel Boston Date: Wed, 20 Apr 2016 23:55:15 -0400 Subject: [PATCH 08/10] Distilling now functions in MC 1.9 via artificial tracking of distillation requirement satisfaction. Testing has been limited so far, but appears consistent with 1.8 behavior. Restarting the server will effectively cancel any ongoing brewing, and it will not begin again until manual interaction after restart. Other then that, should be as usual. --- .../brewery/listeners/InventoryListener.java | 98 ++++++++++++------- 1 file changed, 65 insertions(+), 33 deletions(-) diff --git a/src/com/dre/brewery/listeners/InventoryListener.java b/src/com/dre/brewery/listeners/InventoryListener.java index 6290018..2a8c2a4 100644 --- a/src/com/dre/brewery/listeners/InventoryListener.java +++ b/src/com/dre/brewery/listeners/InventoryListener.java @@ -8,7 +8,6 @@ import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.BrewingStand; import org.bukkit.entity.HumanEntity; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -18,6 +17,7 @@ import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent; import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.inventory.InventoryType.SlotType; import org.bukkit.inventory.BrewerInventory; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.meta.PotionMeta; @@ -31,14 +31,26 @@ import com.dre.brewery.Brew; import com.dre.brewery.P; import com.dre.brewery.integration.LogBlockBarrel; +/** + * Updated for 1.9 to replicate the "Brewing" process for distilling. + * Because of how metadata has changed, the brewer no longer triggers as previously described. + * So, I've added some event tracking and manual forcing of the brewing "animation" if the + * set of ingredients in the brewer can be distilled. + * Nothing here should interfere with vanilla brewing. + */ public class InventoryListener implements Listener { /* === Recreating manually the prior BrewEvent behavior. === */ private HashSet trackedBrewmen = new HashSet(); private HashMap trackedBrewers = new HashMap(); + /** + * Start tracking distillation for a person when they open the brewer window. + * @param event + */ @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onBrewerOpen(InventoryOpenEvent event) { + if (!P.use1_9) return; HumanEntity player = event.getPlayer(); Inventory inv = event.getInventory(); if (player == null || inv == null || !(inv instanceof BrewerInventory)) return; @@ -47,8 +59,13 @@ public class InventoryListener implements Listener { trackedBrewmen.add(player.getUniqueId()); } + /** + * Stop tracking distillation for a person when they close the brewer window. + * @param event + */ @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onBrewerClose(InventoryCloseEvent event) { + if (!P.use1_9) return; HumanEntity player = event.getPlayer(); Inventory inv = event.getInventory(); if (player == null || inv == null || !(inv instanceof BrewerInventory)) return; @@ -56,31 +73,46 @@ public class InventoryListener implements Listener { P.p.log("Stopping brew inventory tracking"); trackedBrewmen.remove(player.getUniqueId()); } - + + /** + * Clicking can either start or stop the new brew distillation tracking. + * Note that server restart will halt any ongoing brewing processes and + * they will _not_ restart until a new click event. + * + * @param event the Click event. + */ @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onBrewerClick(InventoryClickEvent event) { + if (!P.use1_9) return; HumanEntity player = event.getWhoClicked(); Inventory inv = event.getInventory(); if (player == null || inv == null || !(inv instanceof BrewerInventory)) return; UUID puid = player.getUniqueId(); if (!trackedBrewmen.contains(puid)) return; + P.p.debugLog("This brewery was opened by a monitored agent"); - P.p.log("Tracking a new brew click event"); + if (InventoryType.BREWING != inv.getType()) return; + P.p.debugLog("Clicked inventory was a brewing inventory"); + + if (SlotType.FUEL != event.getSlotType() && SlotType.CRAFTING != event.getSlotType()) return; + P.p.debugLog("Tracking a new brew click event -- slots clicked in Brewer!"); BrewerInventory brewer = (BrewerInventory) inv; final Block brewery = brewer.getHolder().getBlock(); - if (isCustom(brewer)) { - Integer curTask = trackedBrewers.get(brewery); - if (curTask != null) { - Bukkit.getScheduler().cancelTask(curTask); // cancel prior - P.p.log("Cancelling prior brew countdown"); - } - - P.p.log("Starting a new brew countdown"); + // If we were already tracking the brewer, cancel any ongoing event due to the click. + Integer curTask = trackedBrewers.get(brewery); + if (curTask != null) { + Bukkit.getScheduler().cancelTask(curTask); // cancel prior + P.p.debugLog("Cancelling prior brew countdown"); + } + + // Now check if we should bother to track it. + if (isCustomAndDistill(brewer)) { + P.p.debugLog("Starting a new brew countdown"); trackedBrewers.put(brewery, new BukkitRunnable() { - private int brewTime = 405; + private int brewTime = 401; @Override public void run() { BlockState now = brewery.getState(); @@ -88,58 +120,58 @@ public class InventoryListener implements Listener { BrewingStand stand = (BrewingStand) now; // check if still custom BrewerInventory brewer = stand.getInventory(); - if (isCustom(brewer) ) { - P.p.log("Still a valid brew distillation"); - brewTime = brewTime - 5; // count down. + if (isCustomAndDistill(brewer) ) { + + // Still a valid brew distillation + brewTime = brewTime - 1; // count down. stand.setBrewingTime(brewTime); // arbitrary for now - if (brewTime <= 5) { // trigger. - P.p.log("Complete brew distillation!"); + if (brewTime <= 1) { // Done! + P.p.debugLog("Complete brew distillation!"); BrewEvent doBrew = new BrewEvent(brewery, brewer); Bukkit.getServer().getPluginManager().callEvent(doBrew); - if (doBrew.isCancelled()) { + if (!doBrew.isCancelled()) { // BrewEvent _wasn't_ cancelled. this.cancel(); trackedBrewers.remove(brewery); + stand.setBrewingTime(0); + P.p.debugLog("All done distilling"); + } else { + brewTime = 401; // go again. + P.p.debugLog("Can distill more! Continuing."); } - stand.setBrewingTime(0); } } } } - }.runTaskTimer(P.p, 5l, 5l).getTaskId()); + }.runTaskTimer(P.p, 1l, 1l).getTaskId()); } } - private boolean isCustom(BrewerInventory brewer) { - int slot = 0; - ItemStack item; + private boolean isCustomAndDistill(BrewerInventory brewer) { + ItemStack item = brewer.getIngredient(); + if (item == null || Material.GLOWSTONE_DUST != item.getType()) return false; // need dust in the top slot. Boolean[] contents = new Boolean[3]; - while (slot < 3) { + for (int slot = 0; slot < 3; slot++) { item = brewer.getItem(slot); contents[slot] = false; if (item != null) { if (item.getType() == Material.POTION) { if (item.hasItemMeta()) { int uid = Brew.getUID(item); - if (Brew.potions.containsKey(uid)) { + Brew pot = Brew.potions.get(uid); + if (pot != null && pot.canDistill()) { // need at least one distillable potion. return true; } } } } - slot++; } return false; } - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = false) + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onBrew(BrewEvent event) { - if (event.isCancelled()) { - P.p.log("Got Cancelled Brew Event"); - return; - } else { - P.p.log("Got Brew Event"); - } + P.p.debugLog("Got Brew Event"); int slot = 0; BrewerInventory inv = event.getContents(); ItemStack item; From e2c9fcb4061123367710974d31275a94113d0fae Mon Sep 17 00:00:00 2001 From: Daniel Boston Date: Thu, 21 Apr 2016 00:17:41 -0400 Subject: [PATCH 09/10] Updating version to be release candidate --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f15c5d9..a1841df 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.dre brewery - 1.4-SNAPSHOT + 1.4.0 Brewery From 14962a28201a13d454ef3945d4946139e00e8515 Mon Sep 17 00:00:00 2001 From: Daniel Boston Date: Thu, 21 Apr 2016 01:36:26 -0400 Subject: [PATCH 10/10] Testing done, should be ready for live. Fixed some issues with inventory scan timing, gotta love asynch. --- resources/plugin.yml | 6 +- .../brewery/listeners/InventoryListener.java | 90 ++++++++++--------- 2 files changed, 49 insertions(+), 47 deletions(-) diff --git a/resources/plugin.yml b/resources/plugin.yml index 81f6eda..8ff27f3 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -1,7 +1,7 @@ name: Brewery -version: 1.4-SNAPSHOT +version: 1.4.0 main: com.dre.brewery.P -authors: [Milan Albrecht, Frank Baumann] +authors: [Milan Albrecht, Frank Baumann, ProgrammerDan] softdepend: [LWC, LogBlock, WorldGuard, GriefPrevention, Vault] commands: brewery: @@ -110,4 +110,4 @@ permissions: brewery.bypass.overdrink: description: Will despite config-setting not be kicked on overdrink brewery.bypass.teleport: - description: Will despite config-setting not be teleported on login \ No newline at end of file + description: Will despite config-setting not be teleported on login diff --git a/src/com/dre/brewery/listeners/InventoryListener.java b/src/com/dre/brewery/listeners/InventoryListener.java index 2a8c2a4..c75aed6 100644 --- a/src/com/dre/brewery/listeners/InventoryListener.java +++ b/src/com/dre/brewery/listeners/InventoryListener.java @@ -17,7 +17,6 @@ import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent; import org.bukkit.event.inventory.InventoryType; -import org.bukkit.event.inventory.InventoryType.SlotType; import org.bukkit.inventory.BrewerInventory; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.meta.PotionMeta; @@ -36,7 +35,14 @@ import com.dre.brewery.integration.LogBlockBarrel; * Because of how metadata has changed, the brewer no longer triggers as previously described. * So, I've added some event tracking and manual forcing of the brewing "animation" if the * set of ingredients in the brewer can be distilled. - * Nothing here should interfere with vanilla brewing. + * Nothing here should interfere with vanilla brewing. + * + * Note in testing I did discover a few ways to "hack" brewing to distill your brews alongside + * potions; put fuel and at least one "valid" water bottle w/ a brewing component. You can distill + * two brews this way, just remove them before the "final" distillation or you will actually + * brew the potion as well. + * + * @author ProgrammerDan (1.9 distillation update only) */ public class InventoryListener implements Listener { @@ -55,7 +61,7 @@ public class InventoryListener implements Listener { Inventory inv = event.getInventory(); if (player == null || inv == null || !(inv instanceof BrewerInventory)) return; - P.p.log("Starting brew inventory tracking"); + P.p.debugLog("Starting brew inventory tracking"); trackedBrewmen.add(player.getUniqueId()); } @@ -70,7 +76,7 @@ public class InventoryListener implements Listener { Inventory inv = event.getInventory(); if (player == null || inv == null || !(inv instanceof BrewerInventory)) return; - P.p.log("Stopping brew inventory tracking"); + P.p.debugLog("Stopping brew inventory tracking"); trackedBrewmen.remove(player.getUniqueId()); } @@ -90,13 +96,8 @@ public class InventoryListener implements Listener { UUID puid = player.getUniqueId(); if (!trackedBrewmen.contains(puid)) return; - P.p.debugLog("This brewery was opened by a monitored agent"); if (InventoryType.BREWING != inv.getType()) return; - P.p.debugLog("Clicked inventory was a brewing inventory"); - - if (SlotType.FUEL != event.getSlotType() && SlotType.CRAFTING != event.getSlotType()) return; - P.p.debugLog("Tracking a new brew click event -- slots clicked in Brewer!"); BrewerInventory brewer = (BrewerInventory) inv; final Block brewery = brewer.getHolder().getBlock(); @@ -105,50 +106,52 @@ public class InventoryListener implements Listener { Integer curTask = trackedBrewers.get(brewery); if (curTask != null) { Bukkit.getScheduler().cancelTask(curTask); // cancel prior - P.p.debugLog("Cancelling prior brew countdown"); } // Now check if we should bother to track it. - if (isCustomAndDistill(brewer)) { - P.p.debugLog("Starting a new brew countdown"); - trackedBrewers.put(brewery, new BukkitRunnable() { - private int brewTime = 401; - @Override - public void run() { - BlockState now = brewery.getState(); - if (now instanceof BrewingStand) { - BrewingStand stand = (BrewingStand) now; - // check if still custom - BrewerInventory brewer = stand.getInventory(); - if (isCustomAndDistill(brewer) ) { - - // Still a valid brew distillation - brewTime = brewTime - 1; // count down. - stand.setBrewingTime(brewTime); // arbitrary for now - - if (brewTime <= 1) { // Done! - P.p.debugLog("Complete brew distillation!"); - BrewEvent doBrew = new BrewEvent(brewery, brewer); - Bukkit.getServer().getPluginManager().callEvent(doBrew); - if (!doBrew.isCancelled()) { // BrewEvent _wasn't_ cancelled. - this.cancel(); - trackedBrewers.remove(brewery); - stand.setBrewingTime(0); - P.p.debugLog("All done distilling"); - } else { - brewTime = 401; // go again. - P.p.debugLog("Can distill more! Continuing."); - } + trackedBrewers.put(brewery, new BukkitRunnable() { + private int brewTime = 401; + @Override + public void run() { + BlockState now = brewery.getState(); + if (now instanceof BrewingStand) { + BrewingStand stand = (BrewingStand) now; + // check if still custom + BrewerInventory brewer = stand.getInventory(); + if (isCustomAndDistill(brewer) ) { + + // Still a valid brew distillation + brewTime = brewTime - 1; // count down. + stand.setBrewingTime(brewTime); // arbitrary for now + + if (brewTime <= 1) { // Done! + BrewEvent doBrew = new BrewEvent(brewery, brewer); + Bukkit.getServer().getPluginManager().callEvent(doBrew); + if (!doBrew.isCancelled()) { // BrewEvent _wasn't_ cancelled. + this.cancel(); + trackedBrewers.remove(brewery); + stand.setBrewingTime(0); + P.p.debugLog("All done distilling"); + } else { + brewTime = 401; // go again. + P.p.debugLog("Can distill more! Continuing."); } } + } else { + this.cancel(); + trackedBrewers.remove(brewery); } + } else { + this.cancel(); + trackedBrewers.remove(brewery); + P.p.debugLog("The block was replaced; not a brewing stand."); } - }.runTaskTimer(P.p, 1l, 1l).getTaskId()); - } + } + }.runTaskTimer(P.p, 2l, 1l).getTaskId()); } private boolean isCustomAndDistill(BrewerInventory brewer) { - ItemStack item = brewer.getIngredient(); + ItemStack item = brewer.getItem(3); // ingredient if (item == null || Material.GLOWSTONE_DUST != item.getType()) return false; // need dust in the top slot. Boolean[] contents = new Boolean[3]; for (int slot = 0; slot < 3; slot++) { @@ -171,7 +174,6 @@ public class InventoryListener implements Listener { @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onBrew(BrewEvent event) { - P.p.debugLog("Got Brew Event"); int slot = 0; BrewerInventory inv = event.getContents(); ItemStack item;