Help with villager trading (I need some NMS pros)

Discussion in 'Plugin Development' started by D4rkDev, Sep 9, 2014.

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

    D4rkDev

    Hello guys,

    sorry for my bad english in advance because im from Germany. I´m currently writing a plugin which should change everything of a villager. But there is one thing which is impossible for me: making that the villager handles multiple players.

    In vanilla minecraft:
    If someone is trading with the villager, the GUI wont event open

    In my edit: via CustomBukkit (I think you dont support that :/ but I dont understand NMS)
    If someone is trading with the villager, the GUI opens for I think ca. 10 ticks and closes then at both players


    How can I do it that if someone is trading with the villager the GUI opens / or stays opened for both players?



    I didnt post the sources I already edited because I dont know if I am allowed to do that





    Greetings,
    D4rkDeveloper
     
  2. Offline

    Yekllurt

    You can listen to the PlayerInteractEntityEvent and see if the player click's on a villager, and the open your custom gui.
    Du kannst auf das PlayerInteractEntityEvent hören, wenn der Spieler dann auf einen Villager klickt öffnest du das Inventar.
     
  3. Offline

    D4rkDev

    Hey,

    thank you for your fast answer. I knew about that possibility (Google told me) but I wanted to code via NMS or Custom Bukkit, not over a plugin.


    Greetings,
    D4rkDeveloper
     
  4. Offline

    Yekllurt

    Here if found this this maybe will work, its from Fedmand (Link to the plugin)
    Here is a tutorial on how you need to setup the Inventory Link

    Code:java
    1. import java.lang.reflect.Field;
    2. import net.minecraft.server.v1_7_R3.EntityVillager;
    3. import net.minecraft.server.v1_7_R3.ItemStack;
    4. import net.minecraft.server.v1_7_R3.MerchantRecipe;
    5. import net.minecraft.server.v1_7_R3.MerchantRecipeList;
    6. import org.bukkit.craftbukkit.v1_7_R3.entity.CraftVillager;
    7. import org.bukkit.craftbukkit.v1_7_R3.inventory.CraftItemStack;
    8. import org.bukkit.entity.Villager;
    9. import org.bukkit.inventory.Inventory;
    10.  
    11. public class Trades
    12. {
    13. public static void setTrades(Villager v, Inventory inv)
    14. {
    15. EntityVillager villager = ((CraftVillager)v).getHandle();
    16. try
    17. {
    18. Field recipes = villager.getClass().getDeclaredField("bu");
    19. recipes.setAccessible(true);
    20.  
    21. MerchantRecipeList list = new MerchantRecipeList();
    22.  
    23. for (int i = 0; i < 9; i++) {
    24. if ((inv.getItem(i) != null) && (inv.getItem(i + 18) != null)) {
    25. if (inv.getItem(i + 9) != null) {
    26. ItemStack item1 = CraftItemStack.asNMSCopy(inv
    27. .getItem(i));
    28. ItemStack item2 = CraftItemStack.asNMSCopy(inv
    29. .getItem(i + 9));
    30. ItemStack item3 = CraftItemStack.asNMSCopy(inv
    31. .getItem(i + 18));
    32. list.a(new MerchantRecipe(item1, item2, item3));
    33. } else {
    34. ItemStack item1 = CraftItemStack.asNMSCopy(inv
    35. .getItem(i));
    36. ItemStack item3 = CraftItemStack.asNMSCopy(inv
    37. .getItem(i + 18));
    38. list.a(new MerchantRecipe(item1, item3));
    39. }
    40. }
    41. }
    42.  
    43. recipes.set(villager, list);
    44. }
    45. catch (Exception exc) {
    46. exc.printStackTrace();
    47. }
    48. }
    49. }
     
    D4rkDev likes this.
  5. Offline

    D4rkDev

    Thank you for your fast answer. I think that code should work but If I´m implementig that in my custom bukkit, how would I do it?

    EntityVillager on GitHub/mc-dev


    Greetings,
    D4rkDeveloper
     
  6. Offline

    Yekllurt

    D4rkDev Since im not an NMS Pro i woud do it like that that you ad the stuff when you spawn the Villager
     
  7. Offline

    D4rkDev


    Hey,

    thanks for your help since now :D. I think I will wait here for someone who knows NMS and Bukkit very good and.. yea

    Greetings,
    D4rkDeveloper
     
  8. Offline

    D4rkDev

    >Bump<
     
  9. Offline

    D4rkDev

    >Bump<
     
  10. Offline

    blablubbabc

    D4rkDev likes this.
  11. Offline

    D4rkDev

    +blablubbabc thank you so much!!! Do you know how I can implement that into a custom Bukkit
     
  12. Offline

    D4rkDev

    <Bump>
     
  13. Offline

    D4rkDev

  14. Offline

    fireblast709

    D4rkDev just get their MerchantRecipeList, clear it, and add in new MerchantRecipes. It ain't rocket science.
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page