Villager Inventories

Discussion in 'Plugin Development' started by ActivisionDevReloaded, Jul 6, 2014.

Thread Status:
Not open for further replies.
  1. What I did was I created a custom inventory for the villager which contains items. I did the PlayerInteractEntityEvent and did everything correctly but I get errors when I right click the villager. I think its because it has a inventory already and I don't know how to remove the trading one.

    Here's the code:
    Code:java
    1. public class VillagerInventory implements Listener {
    2.  
    3. private Inventory vinv;
    4.  
    5. public enum Items {
    6. EnderPearl, DiamondChestPlate, GoldenApple;
    7. }
    8.  
    9. public ItemStack getCustomItem(Items item) {
    10. ItemStack is = null;
    11. ItemMeta im = null;
    12. ArrayList<String> lore;
    13. switch(item) {
    14. case EnderPearl:
    15. is = new ItemStack(Material.ENDER_PEARL, 1);
    16. im = is.getItemMeta();
    17. im.setDisplayName("Escape Plan");
    18. lore = new ArrayList<String>();
    19. lore.add("§8[§6Cost§8]§e 100 points");
    20. im.setLore(lore);
    21. is.setItemMeta(im);
    22. default:
    23. break;
    24. }
    25. return is;
    26. }
    27.  
    28. public ItemStack getCustomItem2(Items item) {
    29. ItemStack is = null;
    30. ItemMeta im = null;
    31. ArrayList<String> lore;
    32. switch(item) {
    33. case EnderPearl:
    34. is = new ItemStack(Material.DIAMOND_CHESTPLATE, 1);
    35. im = is.getItemMeta();
    36. im.setDisplayName("Beast Pack");
    37. lore = new ArrayList<String>();
    38. lore.add("§8[§6Cost§8]§e 300 points");
    39. im.setLore(lore);
    40. is.setItemMeta(im);
    41. default:
    42. break;
    43. }
    44. return is;
    45. }
    46.  
    47. public ItemStack getCustomItem3(Items item) {
    48. ItemStack is = null;
    49. ItemMeta im = null;
    50. ArrayList<String> lore;
    51. switch(item) {
    52. case EnderPearl:
    53. is = new ItemStack(Material.GOLDEN_APPLE, 1);
    54. im = is.getItemMeta();
    55. im.setDisplayName("Smart Thinker");
    56. lore = new ArrayList<String>();
    57. lore.add("§8[§6Cost§8]§e 600 points");
    58. im.setLore(lore);
    59. is.setItemMeta(im);
    60. default:
    61. break;
    62. }
    63. return is;
    64. }
    65.  
    66. public VillagerInventory() {
    67. this.vinv = Bukkit.getServer().createInventory(null, 9, "§8[§6CCSG§8]§e Select your kit!");
    68. this.vinv.setItem(1, getCustomItem(Items.EnderPearl));
    69. this.vinv.setItem(4, getCustomItem2(Items.DiamondChestPlate));
    70. this.vinv.setItem(7, getCustomItem3(Items.GoldenApple));
    71. }
    72.  
    73. @EventHandler
    74. public void onVillagerInteract(PlayerInteractEntityEvent event) {
    75. Player rightclick = (Player) event.getRightClicked();
    76. Player p = event.getPlayer();
    77. if(rightclick instanceof Villager){
    78. p.openInventory(vinv);
    79. p.sendMessage(MessageHandler.getMessage("prefix") + "Opening the villager's kit selction...");
    80. }
    81. }
    82.  
    83. }


    If anyone knows how to fix this please reply back. Thanks!
     
  2. Offline

    61352151511

    You should probably post the errors as well so we will be able to help you
     
  3. 61352151511
    Heres the errors:

    [15:39:44 ERROR]: Could not pass event PlayerInteractEntityEvent to SurvivalGames vBeta 1.9 org.bukkit.event.EventException at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:1072) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] at net.minecraft.server.v1_7_R3.PacketPlayInUseEntity.a(SourceFile:55) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] at net.minecraft.server.v1_7_R3.PacketPlayInUseEntity.handle(SourceFile:10) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R3.entity.CraftVillager cannot be cast to org.bukkit.entity.Player at me.nickyc27.survivalgames.kitselector.VillagerInventory.onVillagerInteract(VillagerInventory.java:93) ~[?:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_51] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_51] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_51] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51] at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks] ... 13 more
     
  4. Offline

    61352151511

    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R3.entity.CraftVillager cannot be cast to org.bukkit.entity.Player at me.nickyc27.survivalgames.kitselector.VillagerInventory.onVillagerInteract(VillagerInventory.java:93)

    You're trying to turn the villager into a player

    Player rightclick = (Player) event.getRightClicked();

    You shouldn't be casting player to that
     
  5. @61352151511
    Ok i fixed it. I forgot to chance the Player rightclicked = (Player) event.getRightClicked to Villager.

    But still it says "Opening the villager's kit selection..." and it doesn't open the custom inventory. How do I remove the trade inventory?
     
  6. Offline

    61352151511

    I've never actually tried anything like this but try doing p.closeInventory() and then opening your custom inventory, this is assuming that the villagers inventory is opened on the player before the event is called and they have an inventory open to close, if not you could try scheduling it for 1-5 ticks later and then closing any open inventory and opening yours.
     
  7. Offline

    XgXXSnipz

    make it so when he right clicks a villager do this:
    Code:java
    1. e.setCanceled(true)
    2. player.openInventory(vinv)
     
Thread Status:
Not open for further replies.

Share This Page