diff --git a/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/eco/proxy/v1_16_R1/VillagerTrade.java b/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/eco/proxy/v1_16_R1/VillagerTrade.java index 27a66a5e8..efd711b7d 100644 --- a/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/eco/proxy/v1_16_R1/VillagerTrade.java +++ b/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/eco/proxy/v1_16_R1/VillagerTrade.java @@ -2,7 +2,6 @@ import com.willfp.eco.core.display.Display; import com.willfp.eco.proxy.proxies.VillagerTradeProxy; -import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftMerchantRecipe; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.MerchantRecipe; @@ -12,29 +11,37 @@ public final class VillagerTrade implements VillagerTradeProxy { @Override - public void displayTrade(@NotNull final MerchantRecipe merchantRecipe) { - try { - // Bukkit MerchantRecipe result - Field fResult = MerchantRecipe.class.getDeclaredField("result"); - fResult.setAccessible(true); - ItemStack result = merchantRecipe.getResult().clone(); - Display.display(result); - fResult.set(merchantRecipe, result); + public MerchantRecipe displayTrade(@NotNull final MerchantRecipe recipe) { + CraftMerchantRecipe oldRecipe = (CraftMerchantRecipe) recipe; + + CraftMerchantRecipe newRecipe = new CraftMerchantRecipe( + Display.display(recipe.getResult().clone()), + recipe.getUses(), + recipe.getMaxUses(), + recipe.hasExperienceReward(), + recipe.getVillagerExperience(), + recipe.getPriceMultiplier() + ); - // Get NMS MerchantRecipe from CraftMerchantRecipe - Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle"); - fHandle.setAccessible(true); - net.minecraft.server.v1_16_R1.MerchantRecipe handle = (net.minecraft.server.v1_16_R1.MerchantRecipe) fHandle.get(merchantRecipe); // NMS RecipeR + for (ItemStack ingredient : recipe.getIngredients()) { + newRecipe.addIngredient(Display.display(ingredient.clone())); + } - Field fSelling = net.minecraft.server.v1_16_R1.MerchantRecipe.class.getDeclaredField("sellingItem"); - fSelling.setAccessible(true); + getHandle(newRecipe).setSpecialPrice(getHandle(oldRecipe).getSpecialPrice()); - ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem).clone(); - Display.display(selling); + return newRecipe; + } - fSelling.set(handle, CraftItemStack.asNMSCopy(selling)); + @NotNull + private net.minecraft.server.v1_16_R1.MerchantRecipe getHandle(@NotNull final CraftMerchantRecipe recipe) { + try { + Field handle = CraftMerchantRecipe.class.getDeclaredField("handle"); + handle.setAccessible(true); + return (net.minecraft.server.v1_16_R1.MerchantRecipe) handle.get(recipe); } catch (IllegalAccessException | NoSuchFieldException e) { e.printStackTrace(); } + + throw new IllegalArgumentException("Not CMR"); } } diff --git a/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/eco/proxy/v1_16_R2/VillagerTrade.java b/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/eco/proxy/v1_16_R2/VillagerTrade.java index 3cbb338ff..fa7c615d6 100644 --- a/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/eco/proxy/v1_16_R2/VillagerTrade.java +++ b/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/eco/proxy/v1_16_R2/VillagerTrade.java @@ -2,7 +2,6 @@ import com.willfp.eco.core.display.Display; import com.willfp.eco.proxy.proxies.VillagerTradeProxy; -import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftMerchantRecipe; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.MerchantRecipe; @@ -12,29 +11,37 @@ public final class VillagerTrade implements VillagerTradeProxy { @Override - public void displayTrade(@NotNull final MerchantRecipe merchantRecipe) { - try { - // Bukkit MerchantRecipe result - Field fResult = MerchantRecipe.class.getDeclaredField("result"); - fResult.setAccessible(true); - ItemStack result = merchantRecipe.getResult().clone(); - Display.display(result); - fResult.set(merchantRecipe, result); + public MerchantRecipe displayTrade(@NotNull final MerchantRecipe recipe) { + CraftMerchantRecipe oldRecipe = (CraftMerchantRecipe) recipe; + + CraftMerchantRecipe newRecipe = new CraftMerchantRecipe( + Display.display(recipe.getResult().clone()), + recipe.getUses(), + recipe.getMaxUses(), + recipe.hasExperienceReward(), + recipe.getVillagerExperience(), + recipe.getPriceMultiplier() + ); - // Get NMS MerchantRecipe from CraftMerchantRecipe - Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle"); - fHandle.setAccessible(true); - net.minecraft.server.v1_16_R2.MerchantRecipe handle = (net.minecraft.server.v1_16_R2.MerchantRecipe) fHandle.get(merchantRecipe); // NMS RecipeR + for (ItemStack ingredient : recipe.getIngredients()) { + newRecipe.addIngredient(Display.display(ingredient.clone())); + } - Field fSelling = net.minecraft.server.v1_16_R2.MerchantRecipe.class.getDeclaredField("sellingItem"); - fSelling.setAccessible(true); + getHandle(newRecipe).setSpecialPrice(getHandle(oldRecipe).getSpecialPrice()); - ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem).clone(); - Display.display(selling); + return newRecipe; + } - fSelling.set(handle, CraftItemStack.asNMSCopy(selling)); + @NotNull + private net.minecraft.server.v1_16_R2.MerchantRecipe getHandle(@NotNull final CraftMerchantRecipe recipe) { + try { + Field handle = CraftMerchantRecipe.class.getDeclaredField("handle"); + handle.setAccessible(true); + return (net.minecraft.server.v1_16_R2.MerchantRecipe) handle.get(recipe); } catch (IllegalAccessException | NoSuchFieldException e) { e.printStackTrace(); } + + throw new IllegalArgumentException("Not CMR"); } } diff --git a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/VillagerTrade.java b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/VillagerTrade.java index c799ee0dd..d544b17a0 100644 --- a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/VillagerTrade.java +++ b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/VillagerTrade.java @@ -2,7 +2,6 @@ import com.willfp.eco.core.display.Display; import com.willfp.eco.proxy.proxies.VillagerTradeProxy; -import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftMerchantRecipe; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.MerchantRecipe; @@ -12,29 +11,37 @@ public final class VillagerTrade implements VillagerTradeProxy { @Override - public void displayTrade(@NotNull final MerchantRecipe merchantRecipe) { - try { - // Bukkit MerchantRecipe result - Field fResult = MerchantRecipe.class.getDeclaredField("result"); - fResult.setAccessible(true); - ItemStack result = merchantRecipe.getResult().clone(); - Display.display(result); - fResult.set(merchantRecipe, result); + public MerchantRecipe displayTrade(@NotNull final MerchantRecipe recipe) { + CraftMerchantRecipe oldRecipe = (CraftMerchantRecipe) recipe; + + CraftMerchantRecipe newRecipe = new CraftMerchantRecipe( + Display.display(recipe.getResult().clone()), + recipe.getUses(), + recipe.getMaxUses(), + recipe.hasExperienceReward(), + recipe.getVillagerExperience(), + recipe.getPriceMultiplier() + ); - // Get NMS MerchantRecipe from CraftMerchantRecipe - Field fHandle = CraftMerchantRecipe.class.getDeclaredField("handle"); - fHandle.setAccessible(true); - net.minecraft.server.v1_16_R3.MerchantRecipe handle = (net.minecraft.server.v1_16_R3.MerchantRecipe) fHandle.get(merchantRecipe); // NMS RecipeR + for (ItemStack ingredient : recipe.getIngredients()) { + newRecipe.addIngredient(Display.display(ingredient.clone())); + } - Field fSelling = net.minecraft.server.v1_16_R3.MerchantRecipe.class.getDeclaredField("sellingItem"); - fSelling.setAccessible(true); + getHandle(newRecipe).setSpecialPrice(getHandle(oldRecipe).getSpecialPrice()); - ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem).clone(); - Display.display(selling); + return newRecipe; + } - fSelling.set(handle, CraftItemStack.asNMSCopy(selling)); + @NotNull + private net.minecraft.server.v1_16_R3.MerchantRecipe getHandle(@NotNull final CraftMerchantRecipe recipe) { + try { + Field handle = CraftMerchantRecipe.class.getDeclaredField("handle"); + handle.setAccessible(true); + return (net.minecraft.server.v1_16_R3.MerchantRecipe) handle.get(recipe); } catch (IllegalAccessException | NoSuchFieldException e) { e.printStackTrace(); } + + throw new IllegalArgumentException("Not CMR"); } } diff --git a/eco-core/core-proxy/src/main/java/com/willfp/eco/proxy/proxies/VillagerTradeProxy.java b/eco-core/core-proxy/src/main/java/com/willfp/eco/proxy/proxies/VillagerTradeProxy.java index 8a256c6fe..4c779b84b 100644 --- a/eco-core/core-proxy/src/main/java/com/willfp/eco/proxy/proxies/VillagerTradeProxy.java +++ b/eco-core/core-proxy/src/main/java/com/willfp/eco/proxy/proxies/VillagerTradeProxy.java @@ -6,9 +6,10 @@ public interface VillagerTradeProxy extends AbstractProxy { /** - * Display a MerchantRecipe without creating a new one. + * Display a MerchantRecipe. * * @param recipe The recipe. + * @return The new recipe. */ - void displayTrade(@NotNull MerchantRecipe recipe); + MerchantRecipe displayTrade(@NotNull MerchantRecipe recipe); }