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 efd711b7..c6d6d44e 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 @@ -10,6 +10,26 @@ import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; public final class VillagerTrade implements VillagerTradeProxy { + /** + * Handle. + */ + private final Field handle; + + /** + * Create new Villager Trade. + */ + public VillagerTrade() { + try { + handle = CraftMerchantRecipe.class.getDeclaredField("handle"); + handle.setAccessible(true); + return; + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + throw new RuntimeException("Error!"); + } + @Override public MerchantRecipe displayTrade(@NotNull final MerchantRecipe recipe) { CraftMerchantRecipe oldRecipe = (CraftMerchantRecipe) recipe; @@ -35,10 +55,8 @@ public final class VillagerTrade implements VillagerTradeProxy { @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) { + } catch (IllegalAccessException e) { e.printStackTrace(); } 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 fa7c615d..41129edf 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 @@ -10,6 +10,26 @@ import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; public final class VillagerTrade implements VillagerTradeProxy { + /** + * Handle. + */ + private final Field handle; + + /** + * Create new Villager Trade. + */ + public VillagerTrade() { + try { + handle = CraftMerchantRecipe.class.getDeclaredField("handle"); + handle.setAccessible(true); + return; + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + throw new RuntimeException("Error!"); + } + @Override public MerchantRecipe displayTrade(@NotNull final MerchantRecipe recipe) { CraftMerchantRecipe oldRecipe = (CraftMerchantRecipe) recipe; @@ -35,10 +55,8 @@ public final class VillagerTrade implements VillagerTradeProxy { @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) { + } catch (IllegalAccessException e) { e.printStackTrace(); } 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 d544b17a..febf0923 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 @@ -10,6 +10,26 @@ import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; public final class VillagerTrade implements VillagerTradeProxy { + /** + * Handle. + */ + private final Field handle; + + /** + * Create new Villager Trade. + */ + public VillagerTrade() { + try { + handle = CraftMerchantRecipe.class.getDeclaredField("handle"); + handle.setAccessible(true); + return; + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + throw new RuntimeException("Error!"); + } + @Override public MerchantRecipe displayTrade(@NotNull final MerchantRecipe recipe) { CraftMerchantRecipe oldRecipe = (CraftMerchantRecipe) recipe; @@ -35,10 +55,8 @@ public final class VillagerTrade implements VillagerTradeProxy { @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) { + } catch (IllegalAccessException e) { e.printStackTrace(); } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketOpenWindowMerchant.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketOpenWindowMerchant.java index ee1ffda3..8a6ed07a 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketOpenWindowMerchant.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketOpenWindowMerchant.java @@ -12,8 +12,8 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.MerchantRecipe; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; public class PacketOpenWindowMerchant extends AbstractPacketAdapter { /** @@ -29,10 +29,14 @@ public class PacketOpenWindowMerchant extends AbstractPacketAdapter { public void onSend(@NotNull final PacketContainer packet, @NotNull final Player player, @NotNull final PacketEvent event) { - List recipes = packet.getMerchantRecipeLists().readSafely(0); - recipes = recipes.stream().peek(merchantRecipe -> InternalProxyUtils.getProxy(VillagerTradeProxy.class).displayTrade(merchantRecipe)).collect(Collectors.toList()); + List recipes = new ArrayList<>(); - packet.getMerchantRecipeLists().writeSafely(0, recipes); + for (MerchantRecipe recipe : packet.getMerchantRecipeLists().read(0)) { + MerchantRecipe newRecipe = InternalProxyUtils.getProxy(VillagerTradeProxy.class).displayTrade(recipe); + recipes.add(newRecipe); + } + + packet.getMerchantRecipeLists().write(0, recipes); } }