Fixed Villager Trades for the last time

This commit is contained in:
Auxilor
2021-04-25 15:54:50 +01:00
parent 4359bd17ae
commit a130935c62
4 changed files with 71 additions and 13 deletions

View File

@@ -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();
}

View File

@@ -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();
}

View File

@@ -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();
}

View File

@@ -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<MerchantRecipe> recipes = packet.getMerchantRecipeLists().readSafely(0);
recipes = recipes.stream().peek(merchantRecipe -> InternalProxyUtils.getProxy(VillagerTradeProxy.class).displayTrade(merchantRecipe)).collect(Collectors.toList());
List<MerchantRecipe> 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);
}
}