diff --git a/src/com/dre/brewery/BPlayer.java b/src/com/dre/brewery/BPlayer.java index 0e8a338..cb73347 100644 --- a/src/com/dre/brewery/BPlayer.java +++ b/src/com/dre/brewery/BPlayer.java @@ -1,7 +1,6 @@ package com.dre.brewery; -import com.dre.brewery.api.events.PlayerAlcEffectEvent; -import com.dre.brewery.api.events.PlayerDrinkEffectEvent; +import com.dre.brewery.api.events.PlayerEffectEvent; import com.dre.brewery.api.events.PlayerPukeEvent; import com.dre.brewery.api.events.PlayerPushEvent; import com.dre.brewery.api.events.brew.BrewDrinkEvent; @@ -160,21 +159,21 @@ public class BPlayer { if (brewAlc < 1) { //no alcohol so we dont need to add a BPlayer - applyDrinkEffects(effects, player); + applyEffects(effects, player, PlayerEffectEvent.EffectType.DRINK); if (bPlayer.drunkeness <= 0) { bPlayer.remove(); } return true; } - effects.addAll(getQualityEffects(drinkEvent.getQuality(), brewAlc)); bPlayer.drunkeness += brewAlc; if (quality > 0) { bPlayer.quality += quality * brewAlc; } else { bPlayer.quality += brewAlc; } - applyDrinkEffects(effects, player); + applyEffects(effects, player, PlayerEffectEvent.EffectType.DRINK); + applyEffects(getQualityEffects(drinkEvent.getQuality(), brewAlc), player, PlayerEffectEvent.EffectType.QUALITY); if (bPlayer.drunkeness > 100) { bPlayer.drinkCap(player); @@ -490,8 +489,8 @@ public class BPlayer { // #### Effects #### - public static void applyDrinkEffects(List effects, Player player) { - PlayerDrinkEffectEvent event = new PlayerDrinkEffectEvent(player, effects); + public static void applyEffects(List effects, Player player, PlayerEffectEvent.EffectType effectType) { + PlayerEffectEvent event = new PlayerEffectEvent(player, effectType, effects); P.p.getServer().getPluginManager().callEvent(event); effects = event.getEffects(); if (event.isCancelled() || effects == null) { @@ -517,7 +516,7 @@ public class BPlayer { List l = new ArrayList<>(1); l.add(PotionEffectType.CONFUSION.createEffect(duration, 0)); - PlayerAlcEffectEvent event = new PlayerAlcEffectEvent(player, l); + PlayerEffectEvent event = new PlayerEffectEvent(player, PlayerEffectEvent.EffectType.ALCOHOL, l); P.p.getServer().getPluginManager().callEvent(event); l = event.getEffects(); if (event.isCancelled() || l == null) { @@ -565,7 +564,14 @@ public class BPlayer { } public static void addQualityEffects(int quality, int brewAlc, Player player) { - for (PotionEffect effect : getQualityEffects(quality, brewAlc)) { + List list = getQualityEffects(quality, brewAlc); + PlayerEffectEvent event = new PlayerEffectEvent(player, PlayerEffectEvent.EffectType.QUALITY, list); + P.p.getServer().getPluginManager().callEvent(event); + list = event.getEffects(); + if (event.isCancelled() || list == null) { + return; + } + for (PotionEffect effect : list) { BUtil.reapplyPotionEffect(player, effect, true); } } @@ -599,8 +605,19 @@ public class BPlayer { } int amplifier = getHangoverQuality() / 3; - BUtil.reapplyPotionEffect(player, PotionEffectType.SLOW.createEffect(duration, amplifier), true); - BUtil.reapplyPotionEffect(player, PotionEffectType.HUNGER.createEffect(duration, amplifier), true); + List list = new ArrayList<>(2); + list.add(PotionEffectType.SLOW.createEffect(duration, amplifier)); + list.add(PotionEffectType.HUNGER.createEffect(duration, amplifier)); + + PlayerEffectEvent event = new PlayerEffectEvent(player, PlayerEffectEvent.EffectType.HANGOVER, list); + P.p.getServer().getPluginManager().callEvent(event); + list = event.getEffects(); + if (event.isCancelled() || list == null) { + return; + } + for (PotionEffect effect : list) { + BUtil.reapplyPotionEffect(player, effect, true); + } } diff --git a/src/com/dre/brewery/Barrel.java b/src/com/dre/brewery/Barrel.java index 3178f3d..a2e9f99 100644 --- a/src/com/dre/brewery/Barrel.java +++ b/src/com/dre/brewery/Barrel.java @@ -325,9 +325,15 @@ public class Barrel implements InventoryHolder { return false; } - // removes a barrel, throwing included potions to the ground - public void remove(Block broken, Player breaker) { - BarrelRemoveEvent event = new BarrelRemoveEvent(this); + /** + * Removes a barrel, throwing included potions to the ground + * + * @param broken The Block that was broken + * @param breaker The Player that broke it, or null if not known + * @param dropItems If the items in the barrels inventory should drop to the ground + */ + public void remove(@Nullable Block broken, @Nullable Player breaker, boolean dropItems) { + BarrelRemoveEvent event = new BarrelRemoveEvent(this, dropItems); // Listened to by LWCBarrel (IntegrationListener) P.p.getServer().getPluginManager().callEvent(event); @@ -348,7 +354,7 @@ public class Barrel implements InventoryHolder { e.printStackTrace(); } } - if (event.willItemsDrop()) { + if (event.willDropItems()) { for (ItemStack item : items) { if (item != null) { Brew brew = Brew.get(item); @@ -480,7 +486,7 @@ public class Barrel implements InventoryHolder { P.p.debugLog("Barrel at " + broken.getWorld().getName() + "/" + broken.getX() + "/" + broken.getY() + "/" + broken.getZ() + " has been destroyed unexpectedly, contents will drop"); // remove the barrel if it was destroyed - barrel.remove(broken, null); + barrel.remove(broken, null, true); } else { // Dont check this barrel again, its enough to check it once after every restart // as now this is only the backup if we dont register the barrel breaking, as sample diff --git a/src/com/dre/brewery/BarrelBody.java b/src/com/dre/brewery/BarrelBody.java index 88795a9..c3c9efa 100644 --- a/src/com/dre/brewery/BarrelBody.java +++ b/src/com/dre/brewery/BarrelBody.java @@ -34,7 +34,7 @@ public class BarrelBody { // This will only be done in those extreme cases. Block broken = getBrokenBlock(true); if (broken != null) { - barrel.remove(broken, null); + barrel.remove(broken, null, true); } } else { this.bounds = bounds; @@ -150,6 +150,9 @@ public class BarrelBody { */ public boolean hasBlock(Block block) { if (block != null) { + if (spigot.equals(block)) { + return true; + } if (spigot.getWorld().equals(block.getWorld())) { return bounds != null && bounds.contains(block.getX(), block.getY(), block.getZ()); } diff --git a/src/com/dre/brewery/Brew.java b/src/com/dre/brewery/Brew.java index 68aca17..cf9011f 100644 --- a/src/com/dre/brewery/Brew.java +++ b/src/com/dre/brewery/Brew.java @@ -7,6 +7,7 @@ import com.dre.brewery.lore.*; import com.dre.brewery.recipe.BEffect; import com.dre.brewery.recipe.BRecipe; import com.dre.brewery.recipe.PotionColor; +import com.dre.brewery.utility.BUtil; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.BrewerInventory; @@ -25,9 +26,12 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; -public class Brew { - // represents the liquid in the brewed Potions +/** + * Represents the liquid in the brewed Potions + */ +public class Brew implements Cloneable { public static final byte SAVE_VER = 1; private static long saveSeed; private static List prevSaveSeeds = new ArrayList<>(); // Save Seeds that have been used in the past, stored to decode brews made at that time @@ -47,12 +51,17 @@ public class Brew { private int lastUpdate; // last update in hours after install time private boolean needsSave; // There was a change that has not yet been saved + /** + * A new Brew with only ingredients + */ public Brew(BIngredients ingredients) { this.ingredients = ingredients; touch(); } - // quality already set + /** + * A Brew with quality, alc and recipe already set + */ public Brew(int quality, int alc, BRecipe recipe, BIngredients ingredients) { this.ingredients = ingredients; this.quality = quality; @@ -61,7 +70,9 @@ public class Brew { touch(); } - // loading with all values set + /** + * Loading a Brew with all values set + */ public Brew(BIngredients ingredients, int quality, int alc, byte distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean immutable, int lastUpdate) { this.ingredients = ingredients; this.quality = quality; @@ -79,7 +90,12 @@ public class Brew { private Brew() { } - // returns a Brew by ItemMeta + /** + * returns a Brew by ItemMeta + * + * @param meta The meta to get the brew from + * @return The Brew if meta is a brew, null if not + */ @Nullable public static Brew get(ItemMeta meta) { if (!P.useNBT && !meta.hasLore()) return null; @@ -93,7 +109,12 @@ public class Brew { return brew; } - // returns a Brew by ItemStack + /** + * returns a Brew by ItemStack + * + * @param item The Item to get the brew from + * @return The Brew if item is a brew, null if not + */ @Nullable public static Brew get(ItemStack item) { if (item.getType() != Material.POTION) return null; @@ -197,7 +218,9 @@ public class Brew { return uid; }*/ - //returns the recipe with the given name, recalculates if not found + /** + * returns the recipe with the given name, recalculates if not found + */ public boolean setRecipeFromString(String name) { currentRecipe = null; if (name != null && !name.equals("")) { @@ -256,20 +279,21 @@ public class Brew { persistent == brew.persistent && immutable == brew.immutable && ingredients.equals(brew.ingredients) && - (currentRecipe != null ? currentRecipe.equals(brew.currentRecipe) : brew.currentRecipe == null); + (Objects.equals(currentRecipe, brew.currentRecipe)); } - // Clones this instance + /** + * Clones this instance + */ @Override - public Brew clone() throws CloneNotSupportedException { - super.clone(); - Brew brew = new Brew(quality, alc, currentRecipe, ingredients); - brew.distillRuns = distillRuns; - brew.ageTime = ageTime; - brew.unlabeled = unlabeled; - brew.persistent = persistent; - brew.immutable = immutable; - return brew; + public Brew clone() { + try { + Brew brew = (Brew) super.clone(); + brew.ingredients = ingredients.copy(); + return brew; + } catch (CloneNotSupportedException e) { + throw new InternalError(e); + } } @Override @@ -295,7 +319,9 @@ public class Brew { } }*/ - // calculate alcohol from recipe + /** + * calculate alcohol from recipe + */ @Contract(pure = true) public int calcAlcohol() { if (quality == 0) { @@ -338,7 +364,9 @@ public class Brew { return 0; } - // calculating quality + /** + * calculating quality + */ @Contract(pure = true) public int calcQuality() { // calculate quality from all of the factors @@ -373,7 +401,11 @@ public class Brew { return null; } - // Set unlabeled to true to hide the numbers in Lore + /** + * Set unlabeled to true to hide the numbers in Lore + * + * @param item The Item this Brew is on + */ public void unLabel(ItemStack item) { unlabeled = true; ItemMeta meta = item.getItemMeta(); @@ -392,7 +424,9 @@ public class Brew { } } - // Do some regular updates + /** + * Do some regular updates + */ public void touch() { lastUpdate = (int) ((double) (System.currentTimeMillis() - installTime) / 3600000D); } @@ -470,7 +504,9 @@ public class Brew { this.needsSave = needsSave; } - // Set the Static flag, so potion is unchangeable + /** + * Set the Static flag, so potion is unchangeable + */ public void setStatic(boolean immutable, ItemStack potion) { this.immutable = immutable; if (currentRecipe != null && canDistill()) { @@ -488,7 +524,12 @@ public class Brew { // Distilling section --------------- - // distill all custom potions in the brewer + /** + * distill all custom potions in the brewer + * + * @param inv The Inventory of the Distiller + * @param contents The Brews in the 3 slots of the Inventory + */ public static void distillAll(BrewerInventory inv, Brew[] contents) { for (int slot = 0; slot < 3; slot++) { if (contents[slot] != null) { @@ -499,7 +540,12 @@ public class Brew { } } - // distill custom potion in given slot + /** + * distill custom potion in a distiller slot + * + * @param slotItem The item in the slot + * @param potionMeta The meta of the item + */ public void distillSlot(ItemStack slotItem, PotionMeta potionMeta) { if (immutable) return; @@ -626,7 +672,9 @@ public class Brew { item.setItemMeta(potionMeta); } - // Slowly shift the wood of the Brew to the new Type + /** + * Slowly shift the wood of the Brew to the new Type + */ public void woodShift(float time, byte to) { float factor = 1; if (ageTime > 5) { @@ -703,6 +751,35 @@ public class Brew { return potion; } + /** + * Performant way of checking if this item is a Brew + * Does not give any guarantees that get() will return notnull for this item, i.e. if it is a brew but the data is corrupt + * + * @param item The Item to check + * @return True if the item is a brew + */ + public static boolean isBrew(ItemStack item) { + if (item == null || item.getType() != Material.POTION) return false; + if (!item.hasItemMeta()) return false; + + ItemMeta meta = item.getItemMeta(); + assert meta != null; + if (!P.useNBT && !meta.hasLore()) return false; + + if (P.useNBT) { + // Check for Data on PersistentDataContainer + if (NBTLoadStream.hasDataInMeta(meta)) { + return true; + } + } + // If either NBT is not supported or no data was found in NBT, try finding data in lore + if (meta.hasLore()) { + // Find the Data Identifier in Lore + return BUtil.indexOfStart(meta.getLore(), LoreLoadStream.IDENTIFIER) > -1; + } + return false; + } + private static Brew load(ItemMeta meta) { InputStream itemLoadStream = null; if (P.useNBT) { @@ -795,7 +872,10 @@ public class Brew { setRecipeFromString(recipe); } - // Save brew data into meta: lore/nbt + /** + * Save brew data into meta: lore/nbt + * Should be called after any changes made to the brew + */ public void save(ItemMeta meta) { OutputStream itemSaveStream; if (P.useNBT) { @@ -819,8 +899,12 @@ public class Brew { } } - // Save brew data into the meta/lore of the specified item - // The meta on the item changes, so to make further changes to the meta, item.getItemMeta() has to be called again after this + /** + * Save brew data into the meta/lore of the specified item + * The meta on the item changes, so to make further changes to the meta, item.getItemMeta() has to be called again after this + * + * @param item The item to save this brew into + */ public void save(ItemStack item) { ItemMeta meta; if (!item.hasItemMeta()) { @@ -899,7 +983,9 @@ public class Brew { return legacyPotions.isEmpty(); } - // Load potion data from data file for backwards compatibility + /** + * Load potion data from data file for backwards compatibility + */ public static void loadLegacy(BIngredients ingredients, int uid, int quality, int alc, byte distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean persistent, boolean stat, int lastUpdate) { Brew brew = new Brew(ingredients, quality, alc, distillRuns, ageTime, wood, recipe, unlabeled, stat, lastUpdate); brew.persistent = persistent; @@ -910,7 +996,9 @@ public class Brew { legacyPotions.put(uid, brew); } - // remove legacy potiondata for an item + /** + * remove legacy potiondata for an item + */ public static void removeLegacy(ItemStack item) { if (legacyPotions.isEmpty()) return; if (!item.hasItemMeta()) return; @@ -936,8 +1024,10 @@ public class Brew { item.setItemMeta(potionMeta); } - // Saves all data - // Legacy method to save to data file + /** + * Saves all data + * Legacy method to save to data file + */ public static void saveLegacy(ConfigurationSection config) { for (Map.Entry entry : legacyPotions.entrySet()) { int uid = entry.getKey(); diff --git a/src/com/dre/brewery/DistortChat.java b/src/com/dre/brewery/DistortChat.java index 9fb091e..362faab 100644 --- a/src/com/dre/brewery/DistortChat.java +++ b/src/com/dre/brewery/DistortChat.java @@ -1,5 +1,6 @@ package com.dre.brewery; +import com.dre.brewery.api.events.PlayerChatDistortEvent; import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; @@ -79,9 +80,15 @@ public class DistortChat { P.p.log(P.p.languageReader.get("Player_TriedToSay", name, chat)); } String message = chat.substring(command.length() + 1); - message = distortMessage(message, bPlayer.getDrunkeness()); + String distorted = distortMessage(message, bPlayer.getDrunkeness()); + PlayerChatDistortEvent call = new PlayerChatDistortEvent(event.isAsynchronous(), event.getPlayer(), bPlayer, message, distorted); + P.p.getServer().getPluginManager().callEvent(call); + if (call.isCancelled()) { + return; + } + distorted = call.getDistortedMessage(); - event.setMessage(chat.substring(0, command.length() + 1) + message); + event.setMessage(chat.substring(0, command.length() + 1) + distorted); waitPlayers.put(name, System.currentTimeMillis()); return; } @@ -101,12 +108,17 @@ public class DistortChat { int index = 0; for (String message : event.getLines()) { if (message.length() > 1) { - message = distortMessage(message, bPlayer.getDrunkeness()); + String distorted = distortMessage(message, bPlayer.getDrunkeness()); + PlayerChatDistortEvent call = new PlayerChatDistortEvent(event.isAsynchronous(), event.getPlayer(), bPlayer, message, distorted); + P.p.getServer().getPluginManager().callEvent(call); + if (!call.isCancelled()) { + distorted = call.getDistortedMessage(); - if (message.length() > 15) { - message = message.substring(0, 14); + if (distorted.length() > 15) { + distorted = distorted.substring(0, 14); + } + event.setLine(index, distorted); } - event.setLine(index, message); } index++; } @@ -123,7 +135,16 @@ public class DistortChat { if (log) { P.p.log(P.p.languageReader.get("Player_TriedToSay", event.getPlayer().getName(), message)); } - event.setMessage(distortMessage(message, bPlayer.getDrunkeness())); + + String distorted = distortMessage(message, bPlayer.getDrunkeness()); + PlayerChatDistortEvent call = new PlayerChatDistortEvent(event.isAsynchronous(), event.getPlayer(), bPlayer, message, distorted); + P.p.getServer().getPluginManager().callEvent(call); + if (call.isCancelled()) { + return; + } + distorted = call.getDistortedMessage(); + + event.setMessage(distorted); } } } diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index bd7c860..fd177b4 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -514,6 +514,10 @@ public class P extends JavaPlugin { BConfig.reloader = null; } + public P getInstance() { + return p; + } + // Utility public void msg(CommandSender sender, String msg) { diff --git a/src/com/dre/brewery/api/BreweryApi.java b/src/com/dre/brewery/api/BreweryApi.java index 4f8d229..5b256aa 100644 --- a/src/com/dre/brewery/api/BreweryApi.java +++ b/src/com/dre/brewery/api/BreweryApi.java @@ -1,10 +1,13 @@ package com.dre.brewery.api; import com.dre.brewery.BCauldron; +import com.dre.brewery.BPlayer; +import com.dre.brewery.filedata.BConfig; import com.dre.brewery.recipe.BCauldronRecipe; import com.dre.brewery.recipe.BRecipe; import com.dre.brewery.Barrel; import com.dre.brewery.Brew; +import com.dre.brewery.utility.Tuple; import org.apache.commons.lang.NotImplementedException; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -28,7 +31,7 @@ public class BreweryApi { */ public static boolean removeAny(Block block) { if (removeCauldron(block)) return true; - return removeBarrel(block); + return removeBarrel(block, true); } /** @@ -37,7 +40,59 @@ public class BreweryApi { */ public static boolean removeAnyByPlayer(Block block, Player player) { if (removeCauldron(block)) return true; - return removeBarrelByPlayer(block, player); + return removeBarrelByPlayer(block, player, true); + } + + + // # # # # # # # # # # # # + // # # # # # Player # # # # # + // # # # # # # # # # # # # + + public static BPlayer getBPlayer(Player player) { + return BPlayer.get(player); + } + + /** + * Set the Players drunkeness state + * + * @param player The Player to set the drunkeness on + * @param drunkeness The amount of drunkeness 0-100 to apply to the player + * @param quality The Quality 1-10 the drunkeness of the player should have + * zero Quality keeps the players current quality + */ + public static void setPlayerDrunk(Player player, int drunkeness, int quality) { + if (drunkeness < 0) { + throw new IllegalArgumentException("Drunkeness can not be <0"); + } + if (quality > 10) { + throw new IllegalArgumentException("Quality can not be >10"); + } + BPlayer bPlayer = BPlayer.get(player); + if (bPlayer == null && player != null) { + if (drunkeness == 0) { + return; + } + bPlayer = BPlayer.addPlayer(player); + } + if (bPlayer == null) { + return; + } + + if (drunkeness == 0) { + bPlayer.remove(); + } else { + bPlayer.setData(drunkeness, quality); + } + + if (drunkeness > 100) { + if (player != null) { + bPlayer.drinkCap(player); + } else { + if (!BConfig.overdrinkKick) { + bPlayer.setData(100, 0); + } + } + } } @@ -67,6 +122,24 @@ public class BreweryApi { return Brew.get(meta); } + /** + * Performant way to check if an item is a brew. + * Does not give any guarantees that getBrew() will return notnull for this item, i.e. if it is a brew but couldn't be loaded + */ + public static boolean isBrew(ItemStack item) { + return Brew.isBrew(item); + } + + /** + * Create a Brew from the given Recipe + * + * @param recipe The Recipe to create a brew from + * @return The Brew that was created. Can use brew.createItem() to get an ItemStack + */ + public static Brew createBrew(BRecipe recipe, int quality) { + return recipe.createBrew(quality); + } + // # # # # # # # # # # # # // # # # # # Barrel # # # # # @@ -98,22 +171,29 @@ public class BreweryApi { /** * Remove any Barrel that this Block may be Part of - * Returns true if a Barrel was removed * Does not remove any actual Block + * + * @param block The Block thats part of the barrel, potions will drop there + * @param dropItems If the items in the barrels inventory should drop to the ground + * @return True if a Barrel was removed */ - public static boolean removeBarrel(Block block) { // TODO add dropItems flag - return removeBarrelByPlayer(block, null); + public static boolean removeBarrel(Block block, boolean dropItems) { + return removeBarrelByPlayer(block, null, dropItems); } /** * Remove any Barrel that this Block may be Part of, as if broken by the Player - * Returns true if a Barrel was removed * Does not remove any actual Block from the World + * + * @param block The Block thats part of the barrel, potions will drop there + * @param player The Player that broke the Block + * @param dropItems If the items in the barrels inventory should drop to the ground + * @return True if a Barrel was removed */ - public static boolean removeBarrelByPlayer(Block block, Player player) { + public static boolean removeBarrelByPlayer(Block block, Player player, boolean dropItems) { Barrel barrel = Barrel.get(block); if (barrel != null) { - barrel.remove(block, player); + barrel.remove(block, player, dropItems); return true; } return false; @@ -170,7 +250,7 @@ public class BreweryApi { public static void addRecipe(BRecipe recipe, boolean saveForever) { //recipe.setSaveInData(saveForever); if (saveForever) { - throw new NotImplementedException(); + throw new NotImplementedException("SaveForever is not implemented yet"); } BRecipe.getAddedRecipes().add(recipe); recipe.updateAcceptedLists(); diff --git a/src/com/dre/brewery/api/events/ConfigLoadEvent.java b/src/com/dre/brewery/api/events/ConfigLoadEvent.java index c3fec33..5af5893 100644 --- a/src/com/dre/brewery/api/events/ConfigLoadEvent.java +++ b/src/com/dre/brewery/api/events/ConfigLoadEvent.java @@ -9,6 +9,9 @@ import org.jetbrains.annotations.NotNull; import java.util.List; +/** + * The Brewery Config was reloaded + */ public class ConfigLoadEvent extends Event { private static final HandlerList handlers = new HandlerList(); @@ -39,4 +42,9 @@ public class ConfigLoadEvent extends Event { public HandlerList getHandlers() { return handlers; } + + // Required by Bukkit + public static HandlerList getHandlerList() { + return handlers; + } } diff --git a/src/com/dre/brewery/api/events/IngedientAddEvent.java b/src/com/dre/brewery/api/events/IngedientAddEvent.java index ecedda3..d576f51 100644 --- a/src/com/dre/brewery/api/events/IngedientAddEvent.java +++ b/src/com/dre/brewery/api/events/IngedientAddEvent.java @@ -11,8 +11,10 @@ import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; import org.bukkit.event.player.PlayerEvent; import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -/* +/** * Player adding an ingredient to a cauldron * Always one item added at a time * If needed use the caudrons add method to manually add more Items @@ -51,36 +53,55 @@ public class IngedientAddEvent extends PlayerEvent implements Cancellable { return rItem; } - // Get the item currently being added to the cauldron by the player - // Can be changed directly (mutable) or with the setter Method - // The amount is ignored and always one added + /** + * Get the item currently being added to the cauldron by the player + * Can be changed directly (mutable) or with the setter Method + * The amount is ignored and always one added + * + * @return The item being added + */ public ItemStack getIngredient() { return ingredient; } - // Set the ingredient added to the cauldron to something else - // Will always be accepted, even when not in a recipe or the cooked list - // The amount is ignored and always one added - // This also recalculates the recipeItem! + /** + * Set the ingredient added to the cauldron to something else + * Will always be accepted, even when not in a recipe or the cooked lis + * The amount is ignored and always one added + * This also recalculates the recipeItem! + * + * @param ingredient The item to add instead + */ public void setIngredient(ItemStack ingredient) { this.ingredient = ingredient; // The Ingredient has been changed. Recalculate RecipeItem! rItem = RecipeItem.getMatchingRecipeItem(ingredient, true); } - // If the amount of the item in the players hand should be decreased - // Default true + /** + * If the amount of the item in the players hand should be decreased + * Default true + */ public boolean willTakeItem() { return takeItem; } - // Set if the amount of the item in the players hand should be decreased + /** + * Set if the amount of the item in the players hand should be decreased + * + * @param takeItem if the item amount in the hand should be decreased + */ public void setTakeItem(boolean takeItem) { this.takeItem = takeItem; } - // Get the BlockData of the Cauldron - // May be null if the Cauldron does not exist anymore + /** + * Get the BlockData of the Cauldron + * May be null if the Cauldron does not exist anymore + * + * @return The BlockData of the cauldron + */ + @Nullable public Levelled getCauldronData() { BlockData data = block.getBlockData(); if (data instanceof Levelled) { @@ -89,9 +110,13 @@ public class IngedientAddEvent extends PlayerEvent implements Cancellable { return null; } - // Get the Water Fill level of the Cauldron - // 0 = empty, 1 = something in, 2 = full - // Can use BCauldron.EMPTY, BCauldron.SOME, BCauldron.FULL + /** + * Get the water fill level of the Cauldron + * 0 = empty, 1 = something in, 2 = full + * Can use BCauldron.EMPTY, BCauldron.SOME, BCauldron.FULL + * + * @return The fill level as a byte 0-2 + */ public byte getFillLevel() { return LegacyUtil.getFillLevel(block); } @@ -101,16 +126,21 @@ public class IngedientAddEvent extends PlayerEvent implements Cancellable { return cancelled; } + /** + * If the event is cancelled, no item will be added or taken from the player + */ @Override public void setCancelled(boolean cancelled) { this.cancelled = cancelled; } + @NotNull @Override public HandlerList getHandlers() { return handlers; } + // Required by Bukkit public static HandlerList getHandlerList() { return handlers; } diff --git a/src/com/dre/brewery/api/events/PlayerAlcEffectEvent.java b/src/com/dre/brewery/api/events/PlayerAlcEffectEvent.java deleted file mode 100644 index 7d371e2..0000000 --- a/src/com/dre/brewery/api/events/PlayerAlcEffectEvent.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.dre.brewery.api.events; - -import org.bukkit.entity.Player; -import org.bukkit.event.Cancellable; -import org.bukkit.event.HandlerList; -import org.bukkit.event.player.PlayerEvent; -import org.bukkit.potion.PotionEffect; - -import java.util.List; - -/* - * Called when the Alcohol in the Player demands its toll - * These effects are applied regularly to the Player depending on his alcohol level - * By default it is just one Confusion effect - * Can be changed or cancelled - */ -public class PlayerAlcEffectEvent extends PlayerEvent implements Cancellable { - private static final HandlerList handlers = new HandlerList(); - private List effects; - private boolean cancelled; - - public PlayerAlcEffectEvent(Player who, List effects) { - super(who); - this.effects = effects; - } - - public List getEffects() { - return effects; - } - - public void setEffects(List effects) { - this.effects = effects; - } - - @Override - public boolean isCancelled() { - return cancelled; - } - - @Override - public void setCancelled(boolean cancelled) { - this.cancelled = cancelled; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - -} diff --git a/src/com/dre/brewery/api/events/PlayerChatDistortEvent.java b/src/com/dre/brewery/api/events/PlayerChatDistortEvent.java new file mode 100644 index 0000000..107ad10 --- /dev/null +++ b/src/com/dre/brewery/api/events/PlayerChatDistortEvent.java @@ -0,0 +1,95 @@ +package com.dre.brewery.api.events; + +import com.dre.brewery.BPlayer; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; + +/** + * The Player writes something in Chat or on a Sign and his words are distorted. + * + * This Event may be Async if the Chat Event is Async! + */ +public class PlayerChatDistortEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + + private final Player player; + private final BPlayer bPlayer; + private final String prevMsg; + private String distortMsg; + private boolean cancelled; + + public PlayerChatDistortEvent(boolean async, Player player, BPlayer bPlayer, String prevMsg, String distortMsg) { + super(async); + this.player = player; + this.bPlayer = bPlayer; + this.prevMsg = prevMsg; + this.distortMsg = distortMsg; + } + + @NotNull + public Player getPlayer() { + return player; + } + + @NotNull + public BPlayer getbPlayer() { + return bPlayer; + } + + /** + * @return The Message the Player had actually written + */ + @NotNull + public String getWrittenMessage() { + return prevMsg; + } + + /** + * @return The message after it was distorted + */ + @NotNull + public String getDistortedMessage() { + return distortMsg; + } + + /** + * @return The drunkeness of the player that is writing the message + */ + public int getDrunkeness() { + return bPlayer.getDrunkeness(); + } + + /** + * Set the Message that the player will say instead of what he wrote + */ + public void setDistortedMessage(String distortMsg) { + this.distortMsg = Objects.requireNonNull(distortMsg); + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return handlers; + } + + // Required by Bukkit + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/com/dre/brewery/api/events/PlayerDrinkEffectEvent.java b/src/com/dre/brewery/api/events/PlayerDrinkEffectEvent.java deleted file mode 100644 index 6b19e1f..0000000 --- a/src/com/dre/brewery/api/events/PlayerDrinkEffectEvent.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.dre.brewery.api.events; - -import org.bukkit.entity.Player; -import org.bukkit.event.Cancellable; -import org.bukkit.event.HandlerList; -import org.bukkit.event.player.PlayerEvent; -import org.bukkit.potion.PotionEffect; - -import java.util.List; - -/* - * Called when the Effects of a Brew are applied to the player (drinking the Brew) - * These depend on alcohol and quality of the brew - * Can be changed or cancelled - */ -public class PlayerDrinkEffectEvent extends PlayerEvent implements Cancellable { - private static final HandlerList handlers = new HandlerList(); - private List effects; - private boolean cancelled; - - public PlayerDrinkEffectEvent(Player who, List effects) { - super(who); - this.effects = effects; - } - - public List getEffects() { - return effects; - } - - public void setEffects(List effects) { - this.effects = effects; - } - - @Override - public boolean isCancelled() { - return cancelled; - } - - @Override - public void setCancelled(boolean cancelled) { - this.cancelled = cancelled; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} diff --git a/src/com/dre/brewery/api/events/PlayerEffectEvent.java b/src/com/dre/brewery/api/events/PlayerEffectEvent.java new file mode 100644 index 0000000..b980b8b --- /dev/null +++ b/src/com/dre/brewery/api/events/PlayerEffectEvent.java @@ -0,0 +1,94 @@ +package com.dre.brewery.api.events; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; +import org.bukkit.potion.PotionEffect; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * A List of effects is applied to the player. + * This happens for various reasons like Alcohol level, Brew quality, Brew effects, etc. + * Can be changed or cancelled + */ +public class PlayerEffectEvent extends PlayerEvent implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private final EffectType effectType; + private List effects; + private boolean cancelled; + + public PlayerEffectEvent(Player who, EffectType effectType, List effects) { + super(who); + this.effectType = effectType; + this.effects = effects; + } + + /** + * @return The effects being applied. Effects can be added or removed from this list. + */ + public List getEffects() { + return effects; + } + + public void setEffects(List effects) { + this.effects = effects; + } + + /** + * @return What type of effects are applied, sie EffectType + */ + public EffectType getEffectType() { + return effectType; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return handlers; + } + + // Required by Bukkit + public static HandlerList getHandlerList() { + return handlers; + } + + + public enum EffectType { + /** + * The Alcohol level demands its toll. + * Regularly applied depending on the players alcohol level + * By default it is just one Confusion effect + */ + ALCOHOL, + + /** + * Effects of a Brew are applied to the player (drinking the Brew) + * These depend on alcohol and quality of the brew + */ + DRINK, + + /** + * When drinking a Brew with low Quality, these effects are applied + */ + QUALITY, + + /** + * When logging in after drinking, Hangover Effects are applied + */ + HANGOVER + + } +} diff --git a/src/com/dre/brewery/api/events/PlayerPukeEvent.java b/src/com/dre/brewery/api/events/PlayerPukeEvent.java index 4a0e001..d4b8da6 100644 --- a/src/com/dre/brewery/api/events/PlayerPukeEvent.java +++ b/src/com/dre/brewery/api/events/PlayerPukeEvent.java @@ -5,8 +5,9 @@ import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; import org.bukkit.event.player.PlayerEvent; +import org.jetbrains.annotations.NotNull; -/* +/** * The player pukes (throws puke items to the ground) * Those items can never be picked up and despawn after the time set in the config * Number of items to drop can be changed with count @@ -23,10 +24,16 @@ public class PlayerPukeEvent extends PlayerEvent implements Cancellable { this.count = count; } + /** + * @return The Amount of items being dropped this time + */ public int getCount() { return count; } + /** + * @param count Set the amount of items being dropped this time + */ public void setCount(int count) { this.count = count; } @@ -48,11 +55,13 @@ public class PlayerPukeEvent extends PlayerEvent implements Cancellable { this.cancelled = cancelled; } + @NotNull @Override public HandlerList getHandlers() { return handlers; } + // Required by Bukkit public static HandlerList getHandlerList() { return handlers; } diff --git a/src/com/dre/brewery/api/events/PlayerPushEvent.java b/src/com/dre/brewery/api/events/PlayerPushEvent.java index 10f286b..f7406bd 100644 --- a/src/com/dre/brewery/api/events/PlayerPushEvent.java +++ b/src/com/dre/brewery/api/events/PlayerPushEvent.java @@ -6,8 +6,9 @@ import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; import org.bukkit.event.player.PlayerEvent; import org.bukkit.util.Vector; +import org.jetbrains.annotations.NotNull; -/* +/** * The Players movement is hindered because of drunkeness * Called each time before pushing the Player with the Vector push 10 times * The Push Vector can be changed or multiplied @@ -30,12 +31,23 @@ public class PlayerPushEvent extends PlayerEvent implements Cancellable { // The Vector in which direction and magnitude the player is Pushed // Can be changed directly or through setPush + + /** + * Get the Vector in which direction and magnitude the player is pushed + * Can be changed directly or through setPush + * + * @return The current push vector + */ public Vector getPush() { return push; } - // Set the Push vector, can not be null - public void setPush(Vector push) { + /** + * Set the Push vector + * + * @param push The new push vector, not null + */ + public void setPush(@NotNull Vector push) { if (push == null) { throw new NullPointerException("Push Vector is null"); } @@ -52,11 +64,13 @@ public class PlayerPushEvent extends PlayerEvent implements Cancellable { this.cancelled = cancelled; } + @NotNull @Override public HandlerList getHandlers() { return handlers; } + // Required by Bukkit public static HandlerList getHandlerList() { return handlers; } diff --git a/src/com/dre/brewery/api/events/barrel/BarrelAccessEvent.java b/src/com/dre/brewery/api/events/barrel/BarrelAccessEvent.java index 2911ad2..256da96 100644 --- a/src/com/dre/brewery/api/events/barrel/BarrelAccessEvent.java +++ b/src/com/dre/brewery/api/events/barrel/BarrelAccessEvent.java @@ -5,8 +5,9 @@ import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; -/* +/** * A Player opens a Barrel by rightclicking it * The PlayerInteractEvent on the Barrel may be cancelled. In that case this never gets called * Can be cancelled to silently deny opening the Barrel @@ -23,8 +24,10 @@ public class BarrelAccessEvent extends BarrelEvent implements Cancellable { this.clickedBlock = clickedBlock; } - // Gets the Block that was actually clicked. - // For access Permissions getSpigot() should be used + /** + * Gets the Block that was actually clicked. + * For access Permissions getSpigot() should be used + */ public Block getClickedBlock() { return clickedBlock; } @@ -43,11 +46,13 @@ public class BarrelAccessEvent extends BarrelEvent implements Cancellable { return player; } + @NotNull @Override public HandlerList getHandlers() { return handlers; } + // Required by Bukkit public static HandlerList getHandlerList() { return handlers; } diff --git a/src/com/dre/brewery/api/events/barrel/BarrelCreateEvent.java b/src/com/dre/brewery/api/events/barrel/BarrelCreateEvent.java index 560d199..81be00d 100644 --- a/src/com/dre/brewery/api/events/barrel/BarrelCreateEvent.java +++ b/src/com/dre/brewery/api/events/barrel/BarrelCreateEvent.java @@ -4,8 +4,9 @@ import com.dre.brewery.Barrel; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; -/* +/** * Called when a Barrel is created by a Player by placing a Sign * Cancelling this will silently fail the Barrel creation */ @@ -33,11 +34,13 @@ public class BarrelCreateEvent extends BarrelEvent implements Cancellable { this.cancelled = cancelled; } + @NotNull @Override public HandlerList getHandlers() { return handlers; } + // Required by Bukkit public static HandlerList getHandlerList() { return handlers; } diff --git a/src/com/dre/brewery/api/events/barrel/BarrelDestroyEvent.java b/src/com/dre/brewery/api/events/barrel/BarrelDestroyEvent.java index 53b0f94..6ec802d 100644 --- a/src/com/dre/brewery/api/events/barrel/BarrelDestroyEvent.java +++ b/src/com/dre/brewery/api/events/barrel/BarrelDestroyEvent.java @@ -5,11 +5,14 @@ import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -/* +/** * A Barrel is being destroyed by something, may not be by a Player * A BarrelRemoveEvent will be called after this, if this is not cancelled * Use the BarrelRemoveEvent to monitor any and all barrels being removed in a non cancellable way + * Cancelling the Event will stop the barrel from being destroyed */ public class BarrelDestroyEvent extends BarrelEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); @@ -30,42 +33,79 @@ public class BarrelDestroyEvent extends BarrelEvent implements Cancellable { return cancelled; } + /** + * Cancelling the Event will stop the barrel from being destroyed. + * Any Blocks that are part of the barrel will not be destroyed + */ @Override public void setCancelled(boolean cancelled) { this.cancelled = cancelled; } + /** + * @return The Block of the Barrel that was broken + */ public Block getBroken() { return broken; } + /** + * @return The Reason of destruction of this barrel, see Reason + */ public Reason getReason() { return reason; } + /** + * If a Player was recorded destroying the barrel + */ public boolean hasPlayer() { return player != null; } - // MAY BE NULL if no Player is involved + /** + * @return The Player, Null if no Player is involved + */ + @Nullable public Player getPlayerOptional() { return player; } + @NotNull @Override public HandlerList getHandlers() { return handlers; } + // Required by Bukkit public static HandlerList getHandlerList() { return handlers; } public enum Reason { - PLAYER, // A Player Broke the Barrel - BROKEN, // A Block was broken by something - BURNED, // A Block burned away - EXPLODED, // The Barrel exploded somehow - UNKNOWN // The Barrel was broken somehow else + /** + * A Player Broke the Barrel + */ + PLAYER, + + /** + * A Block was broken by something + */ + BROKEN, + + /** + * A Block burned away + */ + BURNED, + + /** + * The Barrel exploded somehow + */ + EXPLODED, + + /** + * The Barrel was broken somehow else + */ + UNKNOWN } } diff --git a/src/com/dre/brewery/api/events/barrel/BarrelEvent.java b/src/com/dre/brewery/api/events/barrel/BarrelEvent.java index 2195274..871b085 100644 --- a/src/com/dre/brewery/api/events/barrel/BarrelEvent.java +++ b/src/com/dre/brewery/api/events/barrel/BarrelEvent.java @@ -3,7 +3,6 @@ package com.dre.brewery.api.events.barrel; import com.dre.brewery.Barrel; import org.bukkit.block.Block; import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; import org.bukkit.inventory.Inventory; public abstract class BarrelEvent extends Event { @@ -21,6 +20,9 @@ public abstract class BarrelEvent extends Event { return barrel.getInventory(); } + /** + * @return The Spigot Block of the Barrel, usually Sign or a Fence + */ public Block getSpigot() { return barrel.getSpigot(); } diff --git a/src/com/dre/brewery/api/events/barrel/BarrelRemoveEvent.java b/src/com/dre/brewery/api/events/barrel/BarrelRemoveEvent.java index b846e6b..d2b0f84 100644 --- a/src/com/dre/brewery/api/events/barrel/BarrelRemoveEvent.java +++ b/src/com/dre/brewery/api/events/barrel/BarrelRemoveEvent.java @@ -2,32 +2,39 @@ package com.dre.brewery.api.events.barrel; import com.dre.brewery.Barrel; import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; -/* - * A Barrel is being removed. There may have been a BarrelDestroyEvent before +/** + * A Barrel is being removed. There may have been a BarrelDestroyEvent before this * If not, Worldedit, other Plugins etc may be the cause for unexpected removal */ public class BarrelRemoveEvent extends BarrelEvent { private static final HandlerList handlers = new HandlerList(); - private boolean itemsDrop = true; + private boolean dropItems; - public BarrelRemoveEvent(Barrel barrel) { + public BarrelRemoveEvent(Barrel barrel, boolean dropItems) { super(barrel); + this.dropItems = dropItems; } - public boolean willItemsDrop() { - return itemsDrop; + public boolean willDropItems() { + return dropItems; } - public void setShouldItemsDrop(boolean itemsDrop) { - this.itemsDrop = itemsDrop; + /** + * @param dropItems Should the Items contained in this Barrel drop to the ground? + */ + public void setShouldDropItems(boolean dropItems) { + this.dropItems = dropItems; } + @NotNull @Override public HandlerList getHandlers() { return handlers; } + // Required by Bukkit public static HandlerList getHandlerList() { return handlers; } diff --git a/src/com/dre/brewery/api/events/brew/BrewDrinkEvent.java b/src/com/dre/brewery/api/events/brew/BrewDrinkEvent.java index de6b7f1..56a6f8b 100644 --- a/src/com/dre/brewery/api/events/brew/BrewDrinkEvent.java +++ b/src/com/dre/brewery/api/events/brew/BrewDrinkEvent.java @@ -6,8 +6,9 @@ import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; -/* +/** * A Player Drinks a Brew * The amount of alcohol and quality that will be added to the player can be get/set here * If cancelled the drinking will fail silently @@ -65,11 +66,13 @@ public class BrewDrinkEvent extends BrewEvent implements Cancellable { this.cancelled = cancelled; } + @NotNull @Override public HandlerList getHandlers() { return handlers; } + // Required by Bukkit public static HandlerList getHandlerList() { return handlers; } diff --git a/src/com/dre/brewery/api/events/brew/BrewModifyEvent.java b/src/com/dre/brewery/api/events/brew/BrewModifyEvent.java index 12c3c07..5a21187 100644 --- a/src/com/dre/brewery/api/events/brew/BrewModifyEvent.java +++ b/src/com/dre/brewery/api/events/brew/BrewModifyEvent.java @@ -8,7 +8,7 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.PotionMeta; import org.jetbrains.annotations.NotNull; -/* +/** * 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 @@ -40,7 +40,7 @@ public class BrewModifyEvent extends BrewEvent implements Cancellable { return cancelled; } - /* + /** * Setting the Event cancelled cancels all modificatons to the brew. * Modifications to the Brew or ItemMeta will not be applied */ @@ -55,6 +55,7 @@ public class BrewModifyEvent extends BrewEvent implements Cancellable { return handlers; } + // Required by Bukkit public static HandlerList getHandlerList() { return handlers; } diff --git a/src/com/dre/brewery/listeners/BlockListener.java b/src/com/dre/brewery/listeners/BlockListener.java index 8f1a6df..37a5a4a 100644 --- a/src/com/dre/brewery/listeners/BlockListener.java +++ b/src/com/dre/brewery/listeners/BlockListener.java @@ -53,7 +53,9 @@ public class BlockListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBlockBurn(BlockBurnEvent event) { - BUtil.blockDestroy(event.getBlock(), null, BarrelDestroyEvent.Reason.BURNED); + if (!BUtil.blockDestroy(event.getBlock(), null, BarrelDestroyEvent.Reason.BURNED)) { + event.setCancelled(true); + } } @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) diff --git a/src/com/dre/brewery/listeners/EntityListener.java b/src/com/dre/brewery/listeners/EntityListener.java index ff1c3cb..6fd00b9 100644 --- a/src/com/dre/brewery/listeners/EntityListener.java +++ b/src/com/dre/brewery/listeners/EntityListener.java @@ -24,7 +24,7 @@ import java.util.ListIterator; public class EntityListener implements Listener { - // Remove the Potion from Brew when it despawns + // Legacy Brew removal @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onItemDespawn(ItemDespawnEvent event) { if (Brew.noLegacy()) return; @@ -77,7 +77,7 @@ public class EntityListener implements Listener { if (breakEvent.isCancelled()) { iter.remove(); } else { - barrel.remove(block, null); + barrel.remove(block, null, true); } } } diff --git a/src/com/dre/brewery/listeners/InventoryListener.java b/src/com/dre/brewery/listeners/InventoryListener.java index 5fd6622..f3f33a5 100644 --- a/src/com/dre/brewery/listeners/InventoryListener.java +++ b/src/com/dre/brewery/listeners/InventoryListener.java @@ -1,6 +1,10 @@ package com.dre.brewery.listeners; -import com.dre.brewery.*; +import com.dre.brewery.BDistiller; +import com.dre.brewery.Barrel; +import com.dre.brewery.Brew; +import com.dre.brewery.MCBarrel; +import com.dre.brewery.P; import com.dre.brewery.filedata.BConfig; import com.dre.brewery.lore.BrewLore; import org.bukkit.Material; diff --git a/src/com/dre/brewery/lore/NBTLoadStream.java b/src/com/dre/brewery/lore/NBTLoadStream.java index 36eea63..46dccd0 100644 --- a/src/com/dre/brewery/lore/NBTLoadStream.java +++ b/src/com/dre/brewery/lore/NBTLoadStream.java @@ -9,13 +9,14 @@ import java.io.ByteArrayInputStream; public class NBTLoadStream extends ByteArrayInputStream { private static final String TAG = "brewdata"; + private static final NamespacedKey KEY = new NamespacedKey(P.p, TAG); public NBTLoadStream(ItemMeta meta) { super(getNBTBytes(meta)); } private static byte[] getNBTBytes(ItemMeta meta) { - byte[] bytes = LegacyUtil.readBytesItem(meta, new NamespacedKey(P.p, TAG)); + byte[] bytes = LegacyUtil.readBytesItem(meta, KEY); if (bytes == null) { return new byte[0]; } @@ -25,4 +26,8 @@ public class NBTLoadStream extends ByteArrayInputStream { public boolean hasData() { return count > 0; } + + public static boolean hasDataInMeta(ItemMeta meta) { + return LegacyUtil.hasBytesItem(meta, KEY); + } } diff --git a/src/com/dre/brewery/lore/NBTSaveStream.java b/src/com/dre/brewery/lore/NBTSaveStream.java index 4cfd5a5..457018d 100644 --- a/src/com/dre/brewery/lore/NBTSaveStream.java +++ b/src/com/dre/brewery/lore/NBTSaveStream.java @@ -10,6 +10,8 @@ import java.io.IOException; public class NBTSaveStream extends ByteArrayOutputStream { private static final String TAG = "brewdata"; + private static final NamespacedKey KEY = new NamespacedKey(P.p, TAG); + private final ItemMeta meta; public NBTSaveStream(ItemMeta meta) { @@ -21,6 +23,6 @@ public class NBTSaveStream extends ByteArrayOutputStream { public void flush() throws IOException { super.flush(); if (size() <= 0) return; - LegacyUtil.writeBytesItem(toByteArray(), meta, new NamespacedKey(P.p, TAG)); + LegacyUtil.writeBytesItem(toByteArray(), meta, KEY); } } diff --git a/src/com/dre/brewery/utility/BUtil.java b/src/com/dre/brewery/utility/BUtil.java index 50edb94..8ba4ee5 100644 --- a/src/com/dre/brewery/utility/BUtil.java +++ b/src/com/dre/brewery/utility/BUtil.java @@ -169,7 +169,7 @@ public class BUtil { Barrel barrel = Barrel.getBySpigot(block); if (barrel != null) { if (barrel.hasPermsDestroy(player, block, reason)) { - barrel.remove(null, player); + barrel.remove(null, player, true); return true; } else { return false; @@ -183,7 +183,7 @@ public class BUtil { if (barrel2 != null) { if (!barrel2.isLarge()) { if (barrel2.hasPermsDestroy(player, block, reason)) { - barrel2.remove(null, player); + barrel2.remove(null, player, true); return true; } else { return false; @@ -198,7 +198,7 @@ public class BUtil { Barrel barrel3 = Barrel.getByWood(block); if (barrel3 != null) { if (barrel3.hasPermsDestroy(player, block, reason)) { - barrel3.remove(block, player); + barrel3.remove(block, player, true); } else { return false; } diff --git a/src/com/dre/brewery/utility/LegacyUtil.java b/src/com/dre/brewery/utility/LegacyUtil.java index 8e3c862..f19aa93 100644 --- a/src/com/dre/brewery/utility/LegacyUtil.java +++ b/src/com/dre/brewery/utility/LegacyUtil.java @@ -291,4 +291,13 @@ public class LegacyUtil { } } + @SuppressWarnings("deprecation") + public static boolean hasBytesItem(ItemMeta meta, NamespacedKey key) { + if (NewNbtVer) { + return meta.getPersistentDataContainer().has(key, org.bukkit.persistence.PersistentDataType.BYTE_ARRAY); + } else { + return meta.getCustomTagContainer().hasCustomTag(key, org.bukkit.inventory.meta.tags.ItemTagType.BYTE_ARRAY); + } + } + }