Solved Custom villager trades but bug

Discussion in 'Plugin Development' started by BlaSfaiMe, Feb 7, 2021.

  1. Offline

    BlaSfaiMe

    Hi !
    I'm trying to make some custom trades with a villager.
    I'm using a simple API that I copy the code in my classes. I've updated all the import for my version (1.16.4), and there's no problem but one. An error, that I don't understand at all, because my code seems good.
    There is the error:

    Code:
    [12:26:11] [Server thread/WARN]: java.lang.ClassCastException: class java.lang.Boolean cannot be cast to class net.minecraft.server.v1_16_R3.MerchantRecipeList (java.lang.Boolean is in module java.base of loader 'bootstrap'; net.minecraft.server.v1_16_R3.MerchantRecipeList is in unnamed module of loader 'app')
    [12:26:11] [Server thread/WARN]:        at fr.kestrel.hui.TradingVillagers.addTrade(TradingVillagers.java:31)
    [12:26:11] [Server thread/WARN]:        at fr.kestrel.hui.commands.TraderSpawnCommand.spawnTrader(TraderSpawnCommand.java:82)
    [12:26:11] [Server thread/WARN]:        at fr.kestrel.hui.commands.TraderSpawnCommand.onCommand(TraderSpawnCommand.java:34)
    [12:26:11] [Server thread/WARN]:        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45)
    [12:26:11] [Server thread/WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149)
    [12:26:11] [Server thread/WARN]:        at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:758)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.PlayerConnection.handleCommand(PlayerConnection.java:1788)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.PlayerConnection.c(PlayerConnection.java:1631)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:1584)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.PacketPlayInChat.a(PacketPlayInChat.java:47)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.PacketPlayInChat.a(PacketPlayInChat.java:1)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.TickTask.run(SourceFile:18)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeTask(SourceFile:144)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeNext(SourceFile:118)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.bb(MinecraftServer.java:941)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext(MinecraftServer.java:934)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.awaitTasks(SourceFile:127)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.sleepForTick(MinecraftServer.java:918)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:850)
    [12:26:11] [Server thread/WARN]:        at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0(MinecraftServer.java:164)
    [12:26:11] [Server thread/WARN]:        at java.base/java.lang.Thread.run(Thread.java:832)
    
    aaaand my code with the line 31 as the 6th


    Code:
    public static void addTrade(Villager villager, TradesUtils villagerTrade) {
            EntityVillager entityVillager = ((CraftVillager)villager).getHandle();
            try {
                Field recipes = entityVillager.getClass().getDeclaredField("bu");
                recipes.setAccessible(true);
                MerchantRecipeList list = (MerchantRecipeList) recipes.get(entityVillager);
                ItemStack item1;
                ItemStack item2;
                if (TradesUtils.hasItem2(villagerTrade)) {
                    item1 = CraftItemStack.asNMSCopy(TradesUtils.getItem1(villagerTrade));
                    item2 = CraftItemStack.asNMSCopy(TradesUtils.getItem2(villagerTrade));
                    ItemStack rewardItem = CraftItemStack.asNMSCopy(TradesUtils.getRewardItem(villagerTrade));
                    list.add(new MerchantRecipe(item1, item2, rewardItem, 0, 99999, 0f));
                } else {
                    item1 = CraftItemStack.asNMSCopy(TradesUtils.getItem1(villagerTrade));
                    item2 = CraftItemStack.asNMSCopy(TradesUtils.getRewardItem(villagerTrade));
                    list.add(new MerchantRecipe(item1, item2, 0, 99999, 0f));
                }
                recipes.set(entityVillager, list);
            } catch (Exception var8) {
                var8.printStackTrace();
            }
    
        }
    Thanks to all the people who cold help me!
     

Share This Page