From c71bb9ca986e836fffbd6f118145156e84426e10 Mon Sep 17 00:00:00 2001 From: Parker Hawke Date: Sat, 29 Sep 2018 19:22:59 -0400 Subject: Add PlayerRecipeDiscoverEvent and methods to (un/)discover recipes --- src/main/java/org/bukkit/entity/HumanEntity.java | 53 ++++++++++++++++++++++ .../event/player/PlayerRecipeDiscoverEvent.java | 50 ++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/main/java/org/bukkit/event/player/PlayerRecipeDiscoverEvent.java diff --git a/src/main/java/org/bukkit/entity/HumanEntity.java b/src/main/java/org/bukkit/entity/HumanEntity.java index 518aa2a9..144e432c 100644 --- a/src/main/java/org/bukkit/entity/HumanEntity.java +++ b/src/main/java/org/bukkit/entity/HumanEntity.java @@ -1,8 +1,10 @@ package org.bukkit.entity; +import java.util.Collection; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.inventory.MainHand; import org.bukkit.inventory.Merchant; import org.bukkit.inventory.Inventory; @@ -256,6 +258,57 @@ public interface HumanEntity extends LivingEntity, AnimalTamer, Permissible, Inv */ public int getExpToLevel(); + /** + * Discover a recipe for this player such that it has not already been + * discovered. This method will add the key's associated recipe to the + * player's recipe book. + * + * @param recipe the key of the recipe to discover + * + * @return whether or not the recipe was newly discovered + */ + public boolean discoverRecipe(NamespacedKey recipe); + + /** + * Discover a collection of recipes for this player such that they have not + * already been discovered. This method will add the keys' associated + * recipes to the player's recipe book. If a recipe in the provided + * collection has already been discovered, it will be silently ignored. + * + * @param recipes the keys of the recipes to discover + * + * @return the amount of newly discovered recipes where 0 indicates that + * none were newly discovered and a number equal to {@code recipes.size()} + * indicates that all were new + */ + public int discoverRecipes(Collection recipes); + + /** + * Undiscover a recipe for this player such that it has already been + * discovered. This method will remove the key's associated recipe from the + * player's recipe book. + * + * @param recipe the key of the recipe to undiscover + * + * @return whether or not the recipe was successfully undiscovered (i.e. it + * was previously discovered) + */ + public boolean undiscoverRecipe(NamespacedKey recipe); + + /** + * Undiscover a collection of recipes for this player such that they have + * already been discovered. This method will remove the keys' associated + * recipes from the player's recipe book. If a recipe in the provided + * collection has not yet been discovered, it will be silently ignored. + * + * @param recipes the keys of the recipes to undiscover + * + * @return the amount of undiscovered recipes where 0 indicates that none + * were undiscovered and a number equal to {@code recipes.size()} indicates + * that all were undiscovered + */ + public int undiscoverRecipes(Collection recipes); + /** * Gets the entity currently perched on the left shoulder or null if no * entity. diff --git a/src/main/java/org/bukkit/event/player/PlayerRecipeDiscoverEvent.java b/src/main/java/org/bukkit/event/player/PlayerRecipeDiscoverEvent.java new file mode 100644 index 00000000..68d1d129 --- /dev/null +++ b/src/main/java/org/bukkit/event/player/PlayerRecipeDiscoverEvent.java @@ -0,0 +1,50 @@ +package org.bukkit.event.player; + +import org.bukkit.NamespacedKey; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; + +/** + * Called when a player discovers a new recipe in the recipe book. + */ +public class PlayerRecipeDiscoverEvent extends PlayerEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + + private boolean cancel = false; + private final NamespacedKey recipe; + + public PlayerRecipeDiscoverEvent(Player who, NamespacedKey recipe) { + super(who); + this.recipe = recipe; + } + + /** + * Get the namespaced key of the discovered recipe. + * + * @return the discovered recipe + */ + public NamespacedKey getRecipe() { + return recipe; + } + + @Override + public boolean isCancelled() { + return cancel; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancel = cancel; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} -- cgit v1.2.3