From 595df50ca587d869310110f722a6fd15e2cb5d2e Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Mon, 14 Oct 2019 21:02:59 +0200 Subject: [PATCH] We only need one ModifyEvent Move PotionColor into its own Class --- src/com/dre/brewery/BIngredients.java | 23 +- src/com/dre/brewery/BRecipe.java | 84 +++----- src/com/dre/brewery/BUtil.java | 91 +++++--- src/com/dre/brewery/Brew.java | 197 +++++++----------- src/com/dre/brewery/PotionColor.java | 102 +++++++++ .../api/events/brew/BrewModifiedEvent.java | 50 ----- ...nModifyEvent.java => BrewModifyEvent.java} | 19 +- .../brewery/listeners/CommandListener.java | 33 ++- .../dre/brewery/listeners/EntityListener.java | 2 + 9 files changed, 315 insertions(+), 286 deletions(-) create mode 100644 src/com/dre/brewery/PotionColor.java delete mode 100644 src/com/dre/brewery/api/events/brew/BrewModifiedEvent.java rename src/com/dre/brewery/api/events/brew/{BrewBeginModifyEvent.java => BrewModifyEvent.java} (64%) diff --git a/src/com/dre/brewery/BIngredients.java b/src/com/dre/brewery/BIngredients.java index f887e89..8eca978 100644 --- a/src/com/dre/brewery/BIngredients.java +++ b/src/com/dre/brewery/BIngredients.java @@ -1,7 +1,6 @@ package com.dre.brewery; -import com.dre.brewery.api.events.brew.BrewBeginModifyEvent; -import com.dre.brewery.api.events.brew.BrewModifiedEvent; +import com.dre.brewery.api.events.brew.BrewModifyEvent; import com.dre.brewery.lore.BrewLore; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; @@ -96,7 +95,7 @@ public class BIngredients { lore.addOrReplaceEffects(brew.getEffects(), brew.getQuality()); cookedName = cookRecipe.getName(quality); - Brew.PotionColor.fromString(cookRecipe.getColor()).colorBrew(potionMeta, potion, false); + PotionColor.fromString(cookRecipe.getColor()).colorBrew(potionMeta, potion, false); } else { // new base potion @@ -104,14 +103,14 @@ public class BIngredients { if (state <= 1) { cookedName = P.p.languageReader.get("Brew_ThickBrew"); - Brew.PotionColor.BLUE.colorBrew(potionMeta, potion, false); + PotionColor.BLUE.colorBrew(potionMeta, potion, 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); - Brew.PotionColor.CYAN.colorBrew(potionMeta, potion, true); + PotionColor.CYAN.colorBrew(potionMeta, potion, true); } } } @@ -120,12 +119,7 @@ public class BIngredients { if (cookedName == null) { // if no name could be found cookedName = P.p.languageReader.get("Brew_Undefined"); - Brew.PotionColor.CYAN.colorBrew(potionMeta, potion, true); - } - BrewBeginModifyEvent modifyEvent = new BrewBeginModifyEvent(brew, potionMeta, BrewBeginModifyEvent.Type.FILL); - P.p.getServer().getPluginManager().callEvent(modifyEvent); - if (modifyEvent.isCancelled()) { - return null; + PotionColor.CYAN.colorBrew(potionMeta, potion, true); } potionMeta.setDisplayName(P.p.color("&f" + cookedName)); @@ -138,8 +132,11 @@ public class BIngredients { //potionMeta.addCustomEffect((PotionEffectType.REGENERATION).createEffect((uid * 4), 0), true); brew.touch(); - BrewModifiedEvent modifiedEvent = new BrewModifiedEvent(brew, potionMeta, BrewModifiedEvent.Type.FILL); - P.p.getServer().getPluginManager().callEvent(modifiedEvent); + BrewModifyEvent modifyEvent = new BrewModifyEvent(brew, potionMeta, BrewModifyEvent.Type.FILL); + P.p.getServer().getPluginManager().callEvent(modifyEvent); + if (modifyEvent.isCancelled()) { + return null; + } brew.save(potionMeta); potion.setItemMeta(potionMeta); diff --git a/src/com/dre/brewery/BRecipe.java b/src/com/dre/brewery/BRecipe.java index 35d01f1..b764ceb 100644 --- a/src/com/dre/brewery/BRecipe.java +++ b/src/com/dre/brewery/BRecipe.java @@ -1,15 +1,10 @@ package com.dre.brewery; -import com.dre.brewery.api.events.brew.BrewBeginModifyEvent; -import com.dre.brewery.api.events.brew.BrewModifiedEvent; -import com.dre.brewery.lore.BrewLore; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.PotionMeta; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class BRecipe { @@ -113,7 +108,7 @@ public class BRecipe { if (effect.isValid()) { effects.add(effect); } else { - P.p.errorLog("Error adding Effect to Recipe: " + getName(5)); + P.p.errorLog("Error adding Effect to Recipe: " + getRecipeName()); } } } @@ -125,45 +120,45 @@ public class BRecipe { P.p.errorLog("Recipe Name missing or invalid!"); return false; } - if (getName(5) == null || getName(5).length() < 1) { + if (getRecipeName() == null || getRecipeName().length() < 1) { P.p.errorLog("Recipe Name invalid"); return false; } if (ingredients == null || ingredients.isEmpty()) { - P.p.errorLog("No ingredients could be loaded for Recipe: " + getName(5)); + P.p.errorLog("No ingredients could be loaded for Recipe: " + getRecipeName()); return false; } if (cookingTime < 1) { - P.p.errorLog("Invalid cooking time '" + cookingTime + "' in Recipe: " + getName(5)); + P.p.errorLog("Invalid cooking time '" + cookingTime + "' in Recipe: " + getRecipeName()); return false; } if (distillruns < 0) { - P.p.errorLog("Invalid distillruns '" + distillruns + "' in Recipe: " + getName(5)); + P.p.errorLog("Invalid distillruns '" + distillruns + "' in Recipe: " + getRecipeName()); return false; } if (distillTime < 0) { - P.p.errorLog("Invalid distilltime '" + distillTime + "' in Recipe: " + getName(5)); + P.p.errorLog("Invalid distilltime '" + distillTime + "' in Recipe: " + getRecipeName()); return false; } if (wood < 0 || wood > 6) { - P.p.errorLog("Invalid wood type '" + wood + "' in Recipe: " + getName(5)); + P.p.errorLog("Invalid wood type '" + wood + "' in Recipe: " + getRecipeName()); return false; } if (age < 0) { - P.p.errorLog("Invalid age time '" + age + "' in Recipe: " + getName(5)); + P.p.errorLog("Invalid age time '" + age + "' in Recipe: " + getRecipeName()); return false; } String c = getColor(); - if (!c.equals("WATER") && Brew.PotionColor.fromString(c) == Brew.PotionColor.WATER) { - P.p.errorLog("Invalid Color '" + color + "' in Recipe: " + getName(5)); + if (!c.equals("WATER") && PotionColor.fromString(c) == PotionColor.WATER) { + P.p.errorLog("Invalid Color '" + color + "' in Recipe: " + getRecipeName()); return false; } if (difficulty < 0 || difficulty > 10) { - P.p.errorLog("Invalid difficulty '" + difficulty + "' in Recipe: " + getName(5)); + P.p.errorLog("Invalid difficulty '" + difficulty + "' in Recipe: " + getRecipeName()); return false; } if (alcohol < 0) { - P.p.errorLog("Invalid alcohol '" + alcohol + "' in Recipe: " + getName(5)); + P.p.errorLog("Invalid alcohol '" + alcohol + "' in Recipe: " + getRecipeName()); return false; } return true; @@ -250,42 +245,20 @@ public class BRecipe { return recipeItem.getDurability() == -1 || recipeItem.getDurability() == usedItem.getDurability(); } - // Create a Potion from this Recipe with best values. Quality can be set, but will reset to 10 if put in a barrel + /** + * Create a Potion from this Recipe with best values. Quality can be set, but will reset to 10 if put in a barrel + * @param quality The Quality of the Brew + * @return The Created Item + */ public ItemStack create(int quality) { - ItemStack potion = new ItemStack(Material.POTION); - PotionMeta potionMeta = (PotionMeta) potion.getItemMeta(); - - Brew brew = createBrew(quality); - - BrewBeginModifyEvent modifyEvent = new BrewBeginModifyEvent(brew, potionMeta, BrewBeginModifyEvent.Type.CREATE); - P.p.getServer().getPluginManager().callEvent(modifyEvent); - if (modifyEvent.isCancelled()) { - return null; - } - - Brew.PotionColor.fromString(getColor()).colorBrew(potionMeta, potion, false); - potionMeta.setDisplayName(P.p.color("&f" + getName(quality))); - //if (!P.use1_14) { - // Before 1.14 the effects duration would strangely be only a quarter of what we tell it to be - // This is due to the Duration Modifier, that is removed in 1.14 - // uid *= 4; - //} - // This effect stores the UID in its Duration - //potionMeta.addCustomEffect((PotionEffectType.REGENERATION).createEffect((uid * 4), 0), true); - - BrewLore lore = new BrewLore(brew, potionMeta); - lore.convertLore(false); - lore.addOrReplaceEffects(effects, quality); - lore.write(); - BrewModifiedEvent modifiedEvent = new BrewModifiedEvent(brew, potionMeta, BrewModifiedEvent.Type.CREATE); - P.p.getServer().getPluginManager().callEvent(modifiedEvent); - brew.touch(); - brew.save(potionMeta); - - potion.setItemMeta(potionMeta); - return potion; + return createBrew(quality).createItem(this); } + /** + * Create a Brew from this Recipe with best values. Quality can be set, but will reset to 10 if put in a barrel + * @param quality The Quality of the Brew + * @return The created Brew + */ public Brew createBrew(int quality) { ArrayList list = new ArrayList<>(ingredients.size()); for (ItemStack item : ingredients) { @@ -298,7 +271,7 @@ public class BRecipe { BIngredients bIngredients = new BIngredients(list, cookingTime); - return new Brew(bIngredients, quality, distillruns, getAge(), wood, getName(5), false, true, 0); + return new Brew(bIngredients, quality, distillruns, getAge(), wood, getRecipeName(), false, true, 0); } @@ -314,6 +287,11 @@ public class BRecipe { return 0; } + // Same as getName(5) + public String getRecipeName() { + return getName(5); + } + // name that fits the quality public String getName(int quality) { if (name.length > 2) { @@ -381,12 +359,12 @@ public class BRecipe { @Override public String toString() { - return "BRecipe{" + getName(5) + '}'; + return "BRecipe{" + getRecipeName() + '}'; } public static BRecipe get(String name) { for (BRecipe recipe : BIngredients.recipes) { - if (recipe.getName(5).equalsIgnoreCase(name)) { + if (recipe.getRecipeName().equalsIgnoreCase(name)) { return recipe; } } diff --git a/src/com/dre/brewery/BUtil.java b/src/com/dre/brewery/BUtil.java index df62db1..84ff2f3 100644 --- a/src/com/dre/brewery/BUtil.java +++ b/src/com/dre/brewery/BUtil.java @@ -24,13 +24,15 @@ import java.util.UUID; public class BUtil { - /******************************************/ - /********** **********/ - /********** Bukkit Utils **********/ - /********** **********/ - /******************************************/ + /* **************************************** */ + /* ********* ********* */ + /* ********* Bukkit Utils ********* */ + /* ********* ********* */ + /* **************************************** */ - // Check if the Chunk of a Block is loaded !without loading it in the process! + /** + * Check if the Chunk of a Block is loaded !without loading it in the process! + */ public static boolean isChunkLoaded(Block block) { return block.getWorld().isChunkLoaded(block.getX() >> 4, block.getZ() >> 4); } @@ -42,7 +44,9 @@ public class BUtil { return msg; } - // Returns either uuid or Name of player, depending on bukkit version + /** + * Returns either uuid or Name of player, depending on bukkit version + */ public static String playerString(Player player) { if (P.useUUID) { return player.getUniqueId().toString(); @@ -63,8 +67,11 @@ public class BUtil { return Bukkit.getPlayerExact(name); } - // Apply a Potion Effect, if player already has this effect, overwrite the existing effect. - // Optionally only overwrite if the new one is stronger, i.e. has higher level or longer duration + /** + * Apply a Potion Effect, if player already has this effect, overwrite the existing effect. + * + * @param onlyIfStronger Optionally only overwrite if the new one is stronger, i.e. has higher level or longer duration + */ public static void reapplyPotionEffect(Player player, PotionEffect effect, boolean onlyIfStronger) { final PotionEffectType type = effect.getType(); if (player.hasPotionEffect(type)) { @@ -74,7 +81,9 @@ public class BUtil { } else { plEffect = player.getActivePotionEffects().stream().filter(e -> e.getType().equals(type)).findAny().get(); } - if (plEffect.getAmplifier() < effect.getAmplifier() || (plEffect.getAmplifier() == effect.getAmplifier() && plEffect.getDuration() < effect.getDuration())) { + if (!onlyIfStronger || + plEffect.getAmplifier() < effect.getAmplifier() || + (plEffect.getAmplifier() == effect.getAmplifier() && plEffect.getDuration() < effect.getDuration())) { player.removePotionEffect(type); } else { return; @@ -83,13 +92,18 @@ public class BUtil { effect.apply(player); } - /******************************************/ - /********** **********/ - /********** String Utils **********/ - /********** **********/ - /******************************************/ + /* **************************************** */ + /* ********* ********* */ + /* ********* String Utils ********* */ + /* ********* ********* */ + /* **************************************** */ - // Returns the Index of a String from the list that contains this substring + /** + * Returns the Index of a String from the list that contains this substring + * + * @param list The List in which to search for a substring + * @param substring Part of the String to search for in each of list + */ public static int indexOfSubstring(List list, String substring) { if (list.isEmpty()) return -1; for (int index = 0, size = list.size(); index < size; index++) { @@ -101,7 +115,9 @@ public class BUtil { return -1; } - // Returns the index of a String from the list that starts with 'lineStart', returns -1 if not found; + /** + * Returns the index of a String from the list that starts with 'lineStart', returns -1 if not found; + */ public static int indexOfStart(List list, String lineStart) { for (int i = 0, size = list.size(); i < size; i++) { if (list.get(i).startsWith(lineStart)) { @@ -111,13 +127,15 @@ public class BUtil { return -1; } - /******************************************/ - /********** **********/ - /********** Brewery Utils **********/ - /********** **********/ - /******************************************/ + /* **************************************** */ + /* ********* ********* */ + /* ********* Brewery Utils ********* */ + /* ********* ********* */ + /* **************************************** */ - // create empty World save Sections + /** + * create empty World save Sections + */ public static void createWorldSections(ConfigurationSection section) { for (World world : P.p.getServer().getWorlds()) { String worldName = world.getName(); @@ -130,7 +148,12 @@ public class BUtil { } } - // Returns true if the Block can be destroyed by the Player or something else (null) + /** + * Returns true if the Block can be destroyed by the Player or something else (null) + * + * @param player The Player that destroyed a Block, Null if no Player involved + * @return True if the Block can be destroyed + */ public static boolean blockDestroy(Block block, Player player, BarrelDestroyEvent.Reason reason) { Material type = block.getType(); if (type == Material.CAULDRON) { @@ -181,13 +204,17 @@ public class BUtil { return true; } - /******************************************/ - /********** **********/ - /********** Other Utils **********/ - /********** **********/ - /******************************************/ + /* **************************************** */ + /* ********* ********* */ + /* ********* Other Utils ********* */ + /* ********* ********* */ + /* **************************************** */ - // prints a list of Strings at the specified page + /** + * prints a list of Strings at the specified page + * + * @param sender The CommandSender to send the Page to + */ public static void list(CommandSender sender, ArrayList strings, int page) { int pages = (int) Math.ceil(strings.size() / 7F); if (page > pages || page < 1) { @@ -207,7 +234,9 @@ public class BUtil { } } - // gets the Name of a DXL World + /** + * gets the Name of a DXL World + */ public static String getDxlName(String worldName) { File dungeonFolder = new File(worldName); if (dungeonFolder.isDirectory()) { diff --git a/src/com/dre/brewery/Brew.java b/src/com/dre/brewery/Brew.java index c6db994..9c4394d 100644 --- a/src/com/dre/brewery/Brew.java +++ b/src/com/dre/brewery/Brew.java @@ -1,20 +1,15 @@ package com.dre.brewery; -import com.dre.brewery.api.events.brew.BrewBeginModifyEvent; -import com.dre.brewery.api.events.brew.BrewModifiedEvent; +import com.dre.brewery.api.events.brew.BrewModifyEvent; import com.dre.brewery.lore.*; -import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.BrewerInventory; -import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.PotionMeta; -import org.bukkit.potion.PotionData; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -import org.bukkit.potion.PotionType; import java.io.DataInputStream; import java.io.DataOutputStream; @@ -23,9 +18,7 @@ import java.security.InvalidKeyException; import java.security.SecureRandom; import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.function.BiConsumer; public class Brew { @@ -41,7 +34,7 @@ public class Brew { private byte distillRuns; private float ageTime; private float wood; - private BRecipe currentRecipe; + private BRecipe currentRecipe; // Recipe this Brew is currently Based off. May change between modifications and is often null when not modifying private boolean unlabeled; private boolean persistent; // Only for legacy private boolean immutable; // static/immutable potions should not be changed @@ -189,7 +182,7 @@ public class Brew { currentRecipe = null; if (name != null && !name.equals("")) { for (BRecipe recipe : BIngredients.recipes) { - if (recipe.getName(5).equalsIgnoreCase(name)) { + if (recipe.getRecipeName().equalsIgnoreCase(name)) { currentRecipe = recipe; return true; } @@ -201,7 +194,7 @@ public class Brew { if (!immutable) { this.quality = calcQuality(); } - P.p.log("Brew was made from Recipe: '" + name + "' which could not be found. '" + currentRecipe.getName(5) + "' used instead!"); + P.p.log("Brew was made from Recipe: '" + name + "' which could not be found. '" + currentRecipe.getRecipeName() + "' used instead!"); return true; } else { P.p.errorLog("Brew was made from Recipe: '" + name + "' which could not be found!"); @@ -212,7 +205,7 @@ public class Brew { } public boolean reloadRecipe() { - return currentRecipe == null || setRecipeFromString(currentRecipe.getName(5)); + return currentRecipe == null || setRecipeFromString(currentRecipe.getRecipeName()); } // Copy a Brew with a new unique ID and return its item @@ -467,9 +460,6 @@ public class Brew { // distill custom potion in given slot public void distillSlot(ItemStack slotItem, PotionMeta potionMeta) { if (immutable) return; - BrewBeginModifyEvent modifyEvent = new BrewBeginModifyEvent(this, potionMeta, BrewBeginModifyEvent.Type.DISTILL); - P.p.getServer().getPluginManager().callEvent(modifyEvent); - if (modifyEvent.isCancelled()) return; distillRuns += 1; BrewLore lore = new BrewLore(this, potionMeta); @@ -499,8 +489,13 @@ public class Brew { lore.updateDistillLore(colorInBrewer); lore.write(); touch(); - BrewModifiedEvent modifiedEvent = new BrewModifiedEvent(this, potionMeta, BrewModifiedEvent.Type.DISTILL); - P.p.getServer().getPluginManager().callEvent(modifiedEvent); + BrewModifyEvent modifyEvent = new BrewModifyEvent(this, potionMeta, BrewModifyEvent.Type.DISTILL); + P.p.getServer().getPluginManager().callEvent(modifyEvent); + if (modifyEvent.isCancelled()) { + // As the brew and everything connected to it is only saved on the meta from now on, + // not saving the brew into potionMeta is enough to not change anything in case of cancel + return; + } save(potionMeta); slotItem.setItemMeta(potionMeta); @@ -527,9 +522,6 @@ public class Brew { public void age(ItemStack item, float time, byte woodType) { if (immutable) return; PotionMeta potionMeta = (PotionMeta) item.getItemMeta(); - BrewBeginModifyEvent modifyEvent = new BrewBeginModifyEvent(this, potionMeta, BrewBeginModifyEvent.Type.AGE); - P.p.getServer().getPluginManager().callEvent(modifyEvent); - if (modifyEvent.isCancelled()) return; BrewLore lore = new BrewLore(this, potionMeta); ageTime += time; @@ -573,8 +565,13 @@ public class Brew { } lore.write(); touch(); - BrewModifiedEvent modifiedEvent = new BrewModifiedEvent(this, potionMeta, BrewModifiedEvent.Type.AGE); - P.p.getServer().getPluginManager().callEvent(modifiedEvent); + BrewModifyEvent modifyEvent = new BrewModifyEvent(this, potionMeta, BrewModifyEvent.Type.AGE); + P.p.getServer().getPluginManager().callEvent(modifyEvent); + if (modifyEvent.isCancelled()) { + // As the brew and everything connected to it is only saved on the meta from now on, + // not saving the brew into potionMeta is enough to not change anything in case of cancel + return; + } save(potionMeta); item.setItemMeta(potionMeta); } @@ -601,6 +598,60 @@ public class Brew { } } + /** + * Create a new Item of this Brew. A BrewModifyEvent type CREATE will be called. + * + * @param recipe Recipe is required if the brew doesn't have a currentRecipe + * @return The created Item, null if the Event is cancelled + */ + public ItemStack createItem(BRecipe recipe) { + return createItem(recipe, true); + } + + /** + * Create a new Item of this Brew. + * + * @param recipe Recipe is required if the brew doesn't have a currentRecipe + * @param event Set event to true if a BrewModifyEvent type CREATE should be called and may be cancelled. Only then may this method return null + * @return The created Item, null if the Event is cancelled + */ + public ItemStack createItem(BRecipe recipe, boolean event) { + if (recipe == null) { + recipe = getCurrentRecipe(); + } + if (recipe == null) { + throw new IllegalArgumentException("Recipe can't be null if the brew doesn't have a currentRecipe"); + } + ItemStack potion = new ItemStack(Material.POTION); + PotionMeta potionMeta = (PotionMeta) potion.getItemMeta(); + + PotionColor.fromString(recipe.getColor()).colorBrew(potionMeta, potion, false); + potionMeta.setDisplayName(P.p.color("&f" + recipe.getName(quality))); + //if (!P.use1_14) { + // Before 1.14 the effects duration would strangely be only a quarter of what we tell it to be + // This is due to the Duration Modifier, that is removed in 1.14 + // uid *= 4; + //} + // This effect stores the UID in its Duration + //potionMeta.addCustomEffect((PotionEffectType.REGENERATION).createEffect((uid * 4), 0), true); + + BrewLore lore = new BrewLore(this, potionMeta); + lore.convertLore(false); + lore.addOrReplaceEffects(recipe.getEffects(), quality); + lore.write(); + touch(); + if (event) { + BrewModifyEvent modifyEvent = new BrewModifyEvent(this, potionMeta, BrewModifyEvent.Type.CREATE); + P.p.getServer().getPluginManager().callEvent(modifyEvent); + if (modifyEvent.isCancelled()) { + return null; + } + } + save(potionMeta); + potion.setItemMeta(potionMeta); + return potion; + } + private static Brew load(ItemMeta meta) { LoreLoadStream loreStream; try { @@ -715,7 +766,7 @@ public class Brew { out.writeFloat(wood); } if (currentRecipe != null) { - out.writeUTF(currentRecipe.getName(5)); + out.writeUTF(currentRecipe.getRecipeName()); } ingredients.save(out); } @@ -734,6 +785,10 @@ public class Brew { } } + public static boolean noLegacy() { + return legacyPotions.isEmpty(); + } + // Load potion data from data file for backwards compatibility public static void loadLegacy(BIngredients ingredients, int uid, int quality, byte distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean persistent, boolean stat, int lastUpdate) { Brew brew = new Brew(ingredients, quality, distillRuns, ageTime, wood, recipe, unlabeled, stat, lastUpdate); @@ -793,7 +848,7 @@ public class Brew { idConfig.set("wood", brew.wood); } if (brew.currentRecipe != null) { - idConfig.set("recipe", brew.currentRecipe.getName(5)); + idConfig.set("recipe", brew.currentRecipe.getRecipeName()); } if (brew.unlabeled) { idConfig.set("unlabeled", true); @@ -812,98 +867,4 @@ public class Brew { } } - public static class PotionColor { - public static final PotionColor PINK = new PotionColor(1, PotionType.REGEN, Color.FUCHSIA); - public static final PotionColor CYAN = new PotionColor(2, PotionType.SPEED, Color.AQUA); - public static final PotionColor ORANGE = new PotionColor(3, PotionType.FIRE_RESISTANCE, Color.ORANGE); - public static final PotionColor GREEN = new PotionColor(4, PotionType.POISON, Color.GREEN); - public static final PotionColor BRIGHT_RED = new PotionColor(5, PotionType.INSTANT_HEAL, Color.fromRGB(255,0,0)); - public static final PotionColor BLUE = new PotionColor(6, PotionType.NIGHT_VISION, Color.NAVY); - public static final PotionColor BLACK = new PotionColor(8, PotionType.WEAKNESS, Color.BLACK); - public static final PotionColor RED = new PotionColor(9, PotionType.STRENGTH, Color.fromRGB(196,0,0)); - public static final PotionColor GREY = new PotionColor(10, PotionType.SLOWNESS, Color.GRAY); - public static final PotionColor WATER = new PotionColor(11, P.use1_9 ? PotionType.WATER_BREATHING : null, Color.BLUE); - public static final PotionColor DARK_RED = new PotionColor(12, PotionType.INSTANT_DAMAGE, Color.fromRGB(128,0,0)); - public static final PotionColor BRIGHT_GREY = new PotionColor(14, PotionType.INVISIBILITY, Color.SILVER); - - private final int colorId; - private final PotionType type; - private final Color color; - - PotionColor(int colorId, PotionType type, Color color) { - this.colorId = colorId; - this.type = type; - this.color = color; - } - - public PotionColor(Color color) { - colorId = -1; - type = WATER.getType(); - this.color = color; - } - - // 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 PotionType getType() { - return type; - } - - public Color getColor() { - return color; - } - - @SuppressWarnings("deprecation") - public void colorBrew(PotionMeta meta, ItemStack potion, boolean destillable) { - if (P.use1_9) { - meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS); - if (P.use1_11) { - // BasePotionData was only used for the Color, so starting with 1.12 we can use setColor instead - meta.setColor(getColor()); - } else { - meta.setBasePotionData(new PotionData(getType())); - } - } else { - potion.setDurability(getColorId(destillable)); - } - } - - public static PotionColor fromString(String string) { - switch (string) { - case "PINK": return PINK; - case "CYAN": return CYAN; - case "ORANGE": return ORANGE; - case "GREEN": return GREEN; - case "BRIGHT_RED": return BRIGHT_RED; - case "BLUE": return BLUE; - case "BLACK": return BLACK; - case "RED": return RED; - case "GREY": return GREY; - case "WATER": return WATER; - case "DARK_RED": return DARK_RED; - case "BRIGHT_GREY": return BRIGHT_GREY; - default: - try{ - if (string.length() >= 7) { - string = string.substring(1); - } - return new PotionColor(Color.fromRGB( - Integer.parseInt(string.substring( 0, 2 ), 16 ), - Integer.parseInt(string.substring( 2, 4 ), 16 ), - Integer.parseInt(string.substring( 4, 6 ), 16 ) - )); - } catch (Exception e) { - return WATER; - } - } - } - - } - } diff --git a/src/com/dre/brewery/PotionColor.java b/src/com/dre/brewery/PotionColor.java new file mode 100644 index 0000000..8dd4359 --- /dev/null +++ b/src/com/dre/brewery/PotionColor.java @@ -0,0 +1,102 @@ +package com.dre.brewery; + +import org.bukkit.Color; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.potion.PotionData; +import org.bukkit.potion.PotionType; + +public class PotionColor { + public static final PotionColor PINK = new PotionColor(1, PotionType.REGEN, Color.FUCHSIA); + public static final PotionColor CYAN = new PotionColor(2, PotionType.SPEED, Color.AQUA); + public static final PotionColor ORANGE = new PotionColor(3, PotionType.FIRE_RESISTANCE, Color.ORANGE); + public static final PotionColor GREEN = new PotionColor(4, PotionType.POISON, Color.GREEN); + public static final PotionColor BRIGHT_RED = new PotionColor(5, PotionType.INSTANT_HEAL, Color.fromRGB(255,0,0)); + public static final PotionColor BLUE = new PotionColor(6, PotionType.NIGHT_VISION, Color.NAVY); + public static final PotionColor BLACK = new PotionColor(8, PotionType.WEAKNESS, Color.BLACK); + public static final PotionColor RED = new PotionColor(9, PotionType.STRENGTH, Color.fromRGB(196,0,0)); + public static final PotionColor GREY = new PotionColor(10, PotionType.SLOWNESS, Color.GRAY); + public static final PotionColor WATER = new PotionColor(11, P.use1_9 ? PotionType.WATER_BREATHING : null, Color.BLUE); + public static final PotionColor DARK_RED = new PotionColor(12, PotionType.INSTANT_DAMAGE, Color.fromRGB(128,0,0)); + public static final PotionColor BRIGHT_GREY = new PotionColor(14, PotionType.INVISIBILITY, Color.SILVER); + + private final int colorId; + private final PotionType type; + private final Color color; + + PotionColor(int colorId, PotionType type, Color color) { + this.colorId = colorId; + this.type = type; + this.color = color; + } + + public PotionColor(Color color) { + colorId = -1; + type = WATER.getType(); + this.color = color; + } + + // 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 PotionType getType() { + return type; + } + + public Color getColor() { + return color; + } + + @SuppressWarnings("deprecation") + public void colorBrew(PotionMeta meta, ItemStack potion, boolean destillable) { + if (P.use1_9) { + meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS); + if (P.use1_11) { + // BasePotionData was only used for the Color, so starting with 1.12 we can use setColor instead + meta.setColor(getColor()); + } else { + meta.setBasePotionData(new PotionData(getType())); + } + } else { + potion.setDurability(getColorId(destillable)); + } + } + + public static PotionColor fromString(String string) { + switch (string) { + case "PINK": return PINK; + case "CYAN": return CYAN; + case "ORANGE": return ORANGE; + case "GREEN": return GREEN; + case "BRIGHT_RED": return BRIGHT_RED; + case "BLUE": return BLUE; + case "BLACK": return BLACK; + case "RED": return RED; + case "GREY": return GREY; + case "WATER": return WATER; + case "DARK_RED": return DARK_RED; + case "BRIGHT_GREY": return BRIGHT_GREY; + default: + try{ + if (string.length() >= 7) { + string = string.substring(1); + } + return new PotionColor(Color.fromRGB( + Integer.parseInt(string.substring( 0, 2 ), 16 ), + Integer.parseInt(string.substring( 2, 4 ), 16 ), + Integer.parseInt(string.substring( 4, 6 ), 16 ) + )); + } catch (Exception e) { + return WATER; + } + } + } + +} diff --git a/src/com/dre/brewery/api/events/brew/BrewModifiedEvent.java b/src/com/dre/brewery/api/events/brew/BrewModifiedEvent.java deleted file mode 100644 index f624395..0000000 --- a/src/com/dre/brewery/api/events/brew/BrewModifiedEvent.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.dre.brewery.api.events.brew; - -import com.dre.brewery.Brew; -import com.dre.brewery.lore.BrewLore; -import org.bukkit.event.HandlerList; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.inventory.meta.PotionMeta; - -/* - * A Brew has been created or modified - * Usually happens on Filling from cauldron, distilling and aging. - * Final Modifications to the Brew or the PotionMeta can be done now - */ -public class BrewModifiedEvent extends BrewEvent { - private static final HandlerList handlers = new HandlerList(); - private final Type type; - - - public BrewModifiedEvent(Brew brew, ItemMeta meta, Type type) { - super(brew, meta); - this.type = type; - } - - public Type getType() { - return type; - } - - public BrewLore getLore() { - return new BrewLore(getBrew(), (PotionMeta) getItemMeta()); - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - - public enum Type { - CREATE, // A new Brew is created with arbitrary ways, like the create command - FILL, // Filled from a Cauldron into a new Brew - DISTILL, // Distilled in the Brewing stand - AGE, // Aged in a Barrel - UNLABEL, // Unlabeling Brew with command - STATIC, // Making Brew static with command - UNKNOWN // Unknown modification, unused - } -} diff --git a/src/com/dre/brewery/api/events/brew/BrewBeginModifyEvent.java b/src/com/dre/brewery/api/events/brew/BrewModifyEvent.java similarity index 64% rename from src/com/dre/brewery/api/events/brew/BrewBeginModifyEvent.java rename to src/com/dre/brewery/api/events/brew/BrewModifyEvent.java index 1d9297a..def9f4b 100644 --- a/src/com/dre/brewery/api/events/brew/BrewBeginModifyEvent.java +++ b/src/com/dre/brewery/api/events/brew/BrewModifyEvent.java @@ -1,20 +1,25 @@ package com.dre.brewery.api.events.brew; import com.dre.brewery.Brew; +import com.dre.brewery.lore.BrewLore; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.PotionMeta; /* - * A Brew is starting to be created or modified + * A Brew has been created or modified * Usually happens on Filling from cauldron, distilling and aging. + * Modifications to the Brew or the PotionMeta can be done now + * Cancelling reverts the Brew to the state it was before the modification */ -public class BrewBeginModifyEvent extends BrewEvent implements Cancellable { +public class BrewModifyEvent extends BrewEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); private final Type type; private boolean cancelled; - public BrewBeginModifyEvent(Brew brew, ItemMeta meta, Type type) { + + public BrewModifyEvent(Brew brew, ItemMeta meta, Type type) { super(brew, meta); this.type = type; } @@ -23,11 +28,19 @@ public class BrewBeginModifyEvent extends BrewEvent implements Cancellable { return type; } + public BrewLore getLore() { + return new BrewLore(getBrew(), (PotionMeta) getItemMeta()); + } + @Override public boolean isCancelled() { return cancelled; } + /* + * Setting the Event cancelled cancels all modificatons to the brew. + * Modifications to the Brew or ItemMeta will not be applied + */ @Override public void setCancelled(boolean cancelled) { this.cancelled = cancelled; diff --git a/src/com/dre/brewery/listeners/CommandListener.java b/src/com/dre/brewery/listeners/CommandListener.java index 535953f..f200c45 100644 --- a/src/com/dre/brewery/listeners/CommandListener.java +++ b/src/com/dre/brewery/listeners/CommandListener.java @@ -1,8 +1,7 @@ package com.dre.brewery.listeners; import com.dre.brewery.*; -import com.dre.brewery.api.events.brew.BrewBeginModifyEvent; -import com.dre.brewery.api.events.brew.BrewModifiedEvent; +import com.dre.brewery.api.events.brew.BrewModifyEvent; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -480,12 +479,6 @@ public class CommandListener implements CommandExecutor { if (hand != null) { Brew brew = Brew.get(hand); if (brew != null) { - ItemMeta meta = hand.getItemMeta(); - BrewBeginModifyEvent modifyEvent = new BrewBeginModifyEvent(brew, meta, BrewBeginModifyEvent.Type.STATIC); - P.p.getServer().getPluginManager().callEvent(modifyEvent); - if (modifyEvent.isCancelled()) { - return; - } if (brew.isStatic()) { if (!brew.isPersistent()) { brew.setStatic(false, hand); @@ -498,8 +491,12 @@ public class CommandListener implements CommandExecutor { p.msg(sender, p.languageReader.get("CMD_Static")); } brew.touch(); - BrewModifiedEvent modifiedEvent = new BrewModifiedEvent(brew, meta, BrewModifiedEvent.Type.STATIC); - P.p.getServer().getPluginManager().callEvent(modifiedEvent); + ItemMeta meta = hand.getItemMeta(); + BrewModifyEvent modifyEvent = new BrewModifyEvent(brew, meta, BrewModifyEvent.Type.STATIC); + P.p.getServer().getPluginManager().callEvent(modifyEvent); + if (modifyEvent.isCancelled()) { + return; + } brew.save(meta); hand.setItemMeta(meta); return; @@ -521,16 +518,16 @@ public class CommandListener implements CommandExecutor { if (hand != null) { Brew brew = Brew.get(hand); if (brew != null) { - ItemMeta meta = hand.getItemMeta(); - BrewBeginModifyEvent modifyEvent = new BrewBeginModifyEvent(brew, meta, BrewBeginModifyEvent.Type.UNLABEL); - P.p.getServer().getPluginManager().callEvent(modifyEvent); - if (modifyEvent.isCancelled()) { - return; - } + ItemMeta origMeta = hand.getItemMeta(); brew.unLabel(hand); brew.touch(); - BrewModifiedEvent modifiedEvent = new BrewModifiedEvent(brew, meta, BrewModifiedEvent.Type.UNLABEL); - P.p.getServer().getPluginManager().callEvent(modifiedEvent); + ItemMeta meta = hand.getItemMeta(); + BrewModifyEvent modifyEvent = new BrewModifyEvent(brew, meta, BrewModifyEvent.Type.UNLABEL); + P.p.getServer().getPluginManager().callEvent(modifyEvent); + if (modifyEvent.isCancelled()) { + hand.setItemMeta(origMeta); + return; + } brew.save(meta); hand.setItemMeta(meta); p.msg(sender, p.languageReader.get("CMD_UnLabel")); diff --git a/src/com/dre/brewery/listeners/EntityListener.java b/src/com/dre/brewery/listeners/EntityListener.java index d7cd084..b0ba0a4 100644 --- a/src/com/dre/brewery/listeners/EntityListener.java +++ b/src/com/dre/brewery/listeners/EntityListener.java @@ -27,6 +27,7 @@ public class EntityListener implements Listener { // Remove the Potion from Brew when it despawns @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onItemDespawn(ItemDespawnEvent event) { + if (Brew.noLegacy()) return; ItemStack item = event.getEntity().getItemStack(); if (item.getType() == Material.POTION) { Brew.removeLegacy(item); @@ -35,6 +36,7 @@ public class EntityListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onEntityCombust(EntityCombustEvent event) { + if (!Brew.noLegacy()) return; Entity entity = event.getEntity(); if (entity.getType() == EntityType.DROPPED_ITEM) { if (entity instanceof Item) {