From 7a2f486768afeb6a54c39b19079e9f31f3adad1a Mon Sep 17 00:00:00 2001 From: Parker Hawke Date: Sat, 29 Sep 2018 19:24:51 -0400 Subject: [PATCH] Implement PlayerRecipeDiscoverEvent and methods to (un/)discover recipes --- nms-patches/RecipeBookServer.patch | 19 +++++++++ .../craftbukkit/entity/CraftHumanEntity.java | 41 +++++++++++++++++++ .../craftbukkit/event/CraftEventFactory.java | 7 ++++ 3 files changed, 67 insertions(+) create mode 100644 nms-patches/RecipeBookServer.patch diff --git a/nms-patches/RecipeBookServer.patch b/nms-patches/RecipeBookServer.patch new file mode 100644 index 0000000000..3d60bc7944 --- /dev/null +++ b/nms-patches/RecipeBookServer.patch @@ -0,0 +1,19 @@ +--- a/net/minecraft/server/RecipeBookServer.java ++++ b/net/minecraft/server/RecipeBookServer.java +@@ -8,6 +8,7 @@ + import java.util.List; + import org.apache.logging.log4j.LogManager; + import org.apache.logging.log4j.Logger; ++import org.bukkit.craftbukkit.event.CraftEventFactory; // CraftBukkit + + public class RecipeBookServer extends RecipeBook { + +@@ -27,7 +28,7 @@ + IRecipe irecipe = (IRecipe) iterator.next(); + MinecraftKey minecraftkey = irecipe.getKey(); + +- if (!this.a.contains(minecraftkey) && !irecipe.c()) { ++ if (!this.a.contains(minecraftkey) && !irecipe.c() && CraftEventFactory.handlePlayerRecipeListUpdateEvent(entityplayer, minecraftkey)) { // CraftBukkit + this.a(minecraftkey); + this.c(minecraftkey); + arraylist.add(minecraftkey); diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java index 289e267bdd..497783d4f3 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java @@ -1,6 +1,9 @@ package org.bukkit.craftbukkit.entity; import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; import java.util.Set; import net.minecraft.server.*; @@ -10,6 +13,7 @@ import org.bukkit.Location; import org.bukkit.inventory.MainHand; import org.bukkit.inventory.Merchant; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Villager; import org.bukkit.event.inventory.InventoryType; @@ -27,6 +31,7 @@ import org.bukkit.craftbukkit.inventory.CraftItemStack; import org.bukkit.craftbukkit.inventory.CraftMerchant; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.util.CraftMagicNumbers; +import org.bukkit.craftbukkit.util.CraftNamespacedKey; import org.bukkit.inventory.EntityEquipment; import org.bukkit.permissions.PermissibleBase; import org.bukkit.permissions.Permission; @@ -455,6 +460,42 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity { getHandle().getCooldownTracker().a(CraftMagicNumbers.getItem(material), ticks); } + @Override + public boolean discoverRecipe(NamespacedKey recipe) { + return discoverRecipes(Arrays.asList(recipe)) != 0; + } + + @Override + public int discoverRecipes(Collection recipes) { + return getHandle().a(bukkitKeysToMinecraftRecipes(recipes)); // PAIL rename discoverRecipes + } + + @Override + public boolean undiscoverRecipe(NamespacedKey recipe) { + return undiscoverRecipes(Arrays.asList(recipe)) != 0; + } + + @Override + public int undiscoverRecipes(Collection recipes) { + return getHandle().b(bukkitKeysToMinecraftRecipes(recipes)); // PAIL rename undiscoverRecipes + } + + private Collection bukkitKeysToMinecraftRecipes(Collection recipeKeys) { + Collection recipes = new ArrayList<>(); + CraftingManager manager = getHandle().world.getMinecraftServer().getCraftingManager(); + + for (NamespacedKey recipeKey : recipeKeys) { + IRecipe recipe = manager.a(CraftNamespacedKey.toMinecraft(recipeKey)); + if (recipe == null) { + continue; + } + + recipes.add(recipe); + } + + return recipes; + } + @Override public org.bukkit.entity.Entity getShoulderEntityLeft() { if (!getHandle().getShoulderEntityLeft().isEmpty()) { diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index deb166d787..67d2eb1b63 100644 --- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -34,6 +34,7 @@ import org.bukkit.craftbukkit.inventory.CraftMetaBook; import org.bukkit.craftbukkit.potion.CraftPotionUtil; import org.bukkit.craftbukkit.util.CraftDamageSource; import org.bukkit.craftbukkit.util.CraftMagicNumbers; +import org.bukkit.craftbukkit.util.CraftNamespacedKey; import org.bukkit.entity.AreaEffectCloud; import org.bukkit.entity.Arrow; import org.bukkit.entity.Bat; @@ -1129,4 +1130,10 @@ public class CraftEventFactory { Bukkit.getPluginManager().callEvent(event); return !event.isCancelled(); } + + public static boolean handlePlayerRecipeListUpdateEvent(EntityHuman who, MinecraftKey recipe) { + PlayerRecipeDiscoverEvent event = new PlayerRecipeDiscoverEvent((Player) who.getBukkitEntity(), CraftNamespacedKey.fromMinecraft(recipe)); + Bukkit.getPluginManager().callEvent(event); + return !event.isCancelled(); + } }