From 7a2f486768afeb6a54c39b19079e9f31f3adad1a Mon Sep 17 00:00:00 2001 From: Parker Hawke Date: Sat, 29 Sep 2018 19:24:51 -0400 Subject: Implement PlayerRecipeDiscoverEvent and methods to (un/)discover recipes --- .../craftbukkit/entity/CraftHumanEntity.java | 41 ++++++++++++++++++++++ .../craftbukkit/event/CraftEventFactory.java | 7 ++++ 2 files changed, 48 insertions(+) (limited to 'src') diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java index 289e267b..497783d4 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 deb166d7..67d2eb1b 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(); + } } -- cgit v1.2.3