Solved Custom Villager Trades Error ?!

Discussion in 'Plugin Development' started by mikekats, Aug 8, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    mikekats

    So, i have this event :
    Code:
    @EventHandler
        public void VillagerSpawn(CreatureSpawnEvent e) {
            if(e.getEntity().getType() == EntityType.VILLAGER) {
               
                Villager v = (Villager) e.getEntity();
               
                v.setAdult();
                v.setAgeLock(true);
                v.setCanPickupItems(false);
                v.setCustomName(ChatColor.DARK_AQUA + "" + ChatColor.BOLD + "Shop Keeper");
                v.setCustomNameVisible(true);
                v.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 99999999, 127));
                v.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 99999999, 127));
                v.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 99999999, 127));
               
                MerchantRecipe rec1 = new MerchantRecipe(stone, 100);
                MerchantRecipe rec2 = new MerchantRecipe(steak, 100);
               
                MerchantRecipe rec3 = new MerchantRecipe(arm11, 100);
                MerchantRecipe rec4 = new MerchantRecipe(arm12, 100);
                MerchantRecipe rec5 = new MerchantRecipe(arm13, 100);
                MerchantRecipe rec6 = new MerchantRecipe(arm14, 100);
                MerchantRecipe rec7 = new MerchantRecipe(swd1, 100);
               
                MerchantRecipe rec8 = new MerchantRecipe(arm21, 100);
                MerchantRecipe rec9 = new MerchantRecipe(arm22, 100);
                MerchantRecipe rec10 = new MerchantRecipe(arm23, 100);
                MerchantRecipe rec11 = new MerchantRecipe(arm24, 100);
                MerchantRecipe rec12 = new MerchantRecipe(swd2, 100);
               
                MerchantRecipe rec13 = new MerchantRecipe(pick1, 100);
                MerchantRecipe rec14 = new MerchantRecipe(pick2, 100);
               
                MerchantRecipe rec15 = new MerchantRecipe(obi, 100);
               
                rec1.addIngredient(ing1); //stone
                rec2.addIngredient(ing2); //steak
                rec3.addIngredient(ing5); //chain armor
                rec4.addIngredient(ing5); //chain armor
                rec5.addIngredient(ing5); //chain armor
                rec6.addIngredient(ing5); //chain armor
                rec7.addIngredient(ing3); //stone sword
                rec8.addIngredient(ing6); //iron armor
                rec8.addIngredient(ing7); //iron armor
                rec9.addIngredient(ing6); //iron armor
                rec9.addIngredient(ing7); //iron armor
                rec10.addIngredient(ing6); //iron armor
                rec10.addIngredient(ing7); //iron armor
                rec11.addIngredient(ing6); //iron armor
                rec11.addIngredient(ing7); //iron armor
                rec12.addIngredient(ing4); //iron sword
                rec12.addIngredient(ing4); //iron sword
                rec13.addIngredient(ing3); //stone pickaxe
                rec14.addIngredient(ing6); //iron pickaxe
                rec15.addIngredient(ing5); //obsidian
               
                v.setRecipe(1, rec1);
                v.setRecipe(2, rec2);
                v.setRecipe(3, rec3);
                v.setRecipe(4, rec4);
                v.setRecipe(5, rec5);
                v.setRecipe(6, rec6);
                v.setRecipe(7, rec7);
                v.setRecipe(8, rec8);
                v.setRecipe(9, rec9);
                v.setRecipe(10, rec10);
                v.setRecipe(11, rec11);
                v.setRecipe(12, rec12);
                v.setRecipe(13, rec13);
                v.setRecipe(14, rec14);
                v.setRecipe(15, rec15);
               
               
               
               
            }
        }
    And these are the item stacks :
    Code:
    ItemStack ingA = new ItemStack(Material.GOLD_NUGGET, 5);
        ItemStack ing1 = new ItemStack(Material.GOLD_NUGGET, 10);
        ItemStack ing2 = new ItemStack(Material.GOLD_NUGGET, 20);
        ItemStack ing3 = new ItemStack(Material.GOLD_NUGGET, 30);
        ItemStack ing4 = new ItemStack(Material.GOLD_NUGGET, 40);
        ItemStack ing5 = new ItemStack(Material.GOLD_NUGGET, 50);
        ItemStack ing6 = new ItemStack(Material.GOLD_NUGGET, 64);
        ItemStack ing7 = new ItemStack(Material.GOLD_NUGGET, 36);
       
        ItemStack stone = new ItemStack(Material.STONE, 5);
        ItemStack obi = new ItemStack(Material.OBSIDIAN, 2);
       
        ItemStack steak = new ItemStack(Material.COOKED_BEEF, 10);
       
        ItemStack pick1 = new ItemStack(Material.STONE_PICKAXE, 1);
        ItemStack pick2 = new ItemStack(Material.IRON_PICKAXE, 1);
       
        ItemStack arm11 = new ItemStack(Material.CHAINMAIL_HELMET, 1);
        ItemStack arm12 = new ItemStack(Material.CHAINMAIL_CHESTPLATE, 1);
        ItemStack arm13 = new ItemStack(Material.CHAINMAIL_LEGGINGS, 1);
        ItemStack arm14 = new ItemStack(Material.CHAINMAIL_BOOTS, 1);
        ItemStack swd1 = new ItemStack(Material.STONE_SWORD, 1);
       
        ItemStack arm21 = new ItemStack(Material.IRON_HELMET, 1);
        ItemStack arm22 = new ItemStack(Material.IRON_CHESTPLATE, 1);
        ItemStack arm23 = new ItemStack(Material.IRON_LEGGINGS, 1);
        ItemStack arm24 = new ItemStack(Material.IRON_BOOTS, 1);
        ItemStack swd2 = new ItemStack(Material.IRON_SWORD, 1);
    I get this error when spawning a villager ( The villager spawns with the name and the potion effects but not the trades ) :

    Code:
    Could not pass event CreatureSpawnEvent to SleeperZ v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:509) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:494) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callCreatureSpawnEvent(CraftEventFactory.java:291) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.World.addEntity(World.java:1095) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.ItemMonsterEgg.spawnCreature(ItemMonsterEgg.java:116) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.ItemMonsterEgg.a(ItemMonsterEgg.java:97) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.ItemMonsterEgg.interactWith(ItemMonsterEgg.java:37) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.ItemStack.placeItem(ItemStack.java:102) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PlayerInteractManager.interact(PlayerInteractManager.java:398) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:662) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:50) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace.handle(PacketPlayInBlockPlace.java:86) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
    Caused by: java.lang.NoClassDefFoundError: org/bukkit/inventory/MerchantRecipe
            at me.spooky.main.SleeperZListener.VillagerSpawn(SleeperZListener.java:106) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            ... 20 more
    Caused by: java.lang.ClassNotFoundException: org.bukkit.inventory.MerchantRecipe
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:91) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_101]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_101]
            at me.spooky.main.SleeperZListener.VillagerSpawn(SleeperZListener.java:106) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_101]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[spigot.jar:git-Spigot-1.7.9-R0.2-205-g6a8d903]
            ... 20 more
    >
     
  2. Offline

    MrGeneralQ


    As the console says, it doesn't find the class.
    Code:
    Caused by: java.lang.ClassNotFoundException: org.bukkit.inventory.MerchantRecipe
     
  3. Offline

    mikekats

    I have to make a class of what ??? the recipies ? how would i then call them in the Listener class wherethe Event is ?
     
  4. Offline

    MrGeneralQ

    @mikekats

    Could be something wrong with the imports.
     
  5. Offline

    mikekats

    These are the imports, if you can find anything wrong .. tell me .. :
    Code:
    import java.util.HashMap;
    import java.util.UUID;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Villager;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerBedEnterEvent;
    import org.bukkit.event.player.PlayerBedLeaveEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.MerchantRecipe;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
  6. Offline

    MrGeneralQ

    Refering to the code error, there is something wrong with the server jar you are using. Try another jar, it should work. If it doesn't try spigot one. Make sure it's a stable version.
    @mikekats
     
  7. Offline

    mikekats

    I tried on a craftbukkit 1.10.2 server

    Now the villager has a normal trade and 1 custom trade ....

    How to remoe and dont allow default trades and also i guess make the trade max over 2

    error now :
    Code:
    Could not pass event CreatureSpawnEvent to SleeperZ v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit.jar:git-Bukkit-a625e45]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-a625e45]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-a625e45]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-a625e45]
            at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.callCreatureSpawnEvent(CraftEventFactory.java:280) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.World.addEntity(World.java:934) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.ItemMonsterEgg.spawnCreature(ItemMonsterEgg.java:151) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.ItemMonsterEgg.a(ItemMonsterEgg.java:134) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.ItemMonsterEgg.a(ItemMonsterEgg.java:55) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.ItemStack.placeItem(ItemStack.java:121) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.PlayerInteractManager.a(PlayerInteractManager.java:534) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:877) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.PacketPlayInUseItem.a(SourceFile:55) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.PacketPlayInUseItem.a(SourceFile:11) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-a625e45]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_101]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_101]
            at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:692) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:360) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:647) [craftbukkit.jar:git-Bukkit-a625e45]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:551) [craftbukkit.jar:git-Bukkit-a625e45]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
            at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_101]
            at java.util.ArrayList.set(Unknown Source) ~[?:1.8.0_101]
            at org.bukkit.craftbukkit.v1_10_R1.entity.CraftVillager.setRecipe(CraftVillager.java:82) ~[craftbukkit.jar:git-Bukkit-a625e45]
            at me.spooky.main.SleeperZListener.VillagerSpawn(SleeperZListener.java:148) ~[?:?]
            at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_101]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_101]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit.jar:git-Bukkit-a625e45]
            ... 22 more
     
  8. Offline

    MrGeneralQ

    As the error say's , you are out range of your array in your CraftVillager class line 82
     
  9. Offline

    mikekats

    I dont even have a CraftVillager class -.- whaaaat?
     
  10. Offline

    MrGeneralQ

    Okay hold on please.

    UPDATE: Euhm, what is the name of your plugin?
     
  11. Offline

    mikekats

    SleeperZ

    ( my replies may take a while cause they need approval )
     
  12. Offline

    MrGeneralQ

    Wathever is in the class "
    SleeperZListener.java" LINE:148 is your problem.
     
  13. Offline

    mikekats

    That is where add the recipes to the villager.

    here is where i start adding, starting from line 147 :
    Code:
    v.setRecipe(1, rec1);
                v.setRecipe(2, rec2);
                v.setRecipe(3, rec3);
                v.setRecipe(4, rec4);
                v.setRecipe(5, rec5);
                v.setRecipe(6, rec6);
                v.setRecipe(7, rec7);
                v.setRecipe(8, rec8);
                v.setRecipe(9, rec9);
                v.setRecipe(10, rec10);
                v.setRecipe(11, rec11);
                v.setRecipe(12, rec12);
                v.setRecipe(13, rec13);
                v.setRecipe(14, rec14);
                v.setRecipe(15, rec15);
     
  14. Offline

    MrGeneralQ

  15. Offline

    Zombie_Striker

    What is/was line 148? On that line, you were trying to get an object at an index that is greater than the size of the array. Most likely, it is this line:
    This means that there were only two 'recipes' before you added these new recipes. To fix this, try storing all the MerchentRecipes inside of a List and add those recipes using Villager.setRecipes()
     
  16. Offline

    mikekats

Thread Status:
Not open for further replies.

Share This Page