Solved Showing a custom trading screen

Discussion in 'Plugin Development' started by Hoolean, Nov 18, 2012.

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

    Hoolean

    Hello! I'm trying to make it so villagers can show custom trades! I'd just like to know how to show a trading screen even when a villager hasn't been clicked AND how to change a villagers trades.

    Thank you!
     
  2. Offline

    fireblast709

    maybe making your own implementation of MerchantInventory?
     
  3. Offline

    Hoolean

    Ooh! How would one go about doing that?
     
  4. Offline

    fireblast709

    either extends CraftInventoryMerchant or implement MerchantInventory
     
  5. Offline

    Hoolean

    But hhhhhooooowwwwwww???
     
  6. Offline

    fireblast709

    no clue if it will work, but you could try this:
    Code:java
    1. class MerchantInventory2 implements MerchantInventory
    2. {
    3.  
    4. @Override
    5. public int getSize() {
    6. throw new UnsupportedOperationException("Not supported yet.");
    7. }
    8.  
    9. @Override
    10. public int getMaxStackSize() {
    11. throw new UnsupportedOperationException("Not supported yet.");
    12. }
    13.  
    14. @Override
    15. public void setMaxStackSize(int i) {
    16. throw new UnsupportedOperationException("Not supported yet.");
    17. }
    18.  
    19. @Override
    20. public String getName() {
    21. throw new UnsupportedOperationException("Not supported yet.");
    22. }
    23.  
    24. @Override
    25. public ItemStack getItem(int i) {
    26. throw new UnsupportedOperationException("Not supported yet.");
    27. }
    28.  
    29. @Override
    30. public void setItem(int i, ItemStack is) {
    31. throw new UnsupportedOperationException("Not supported yet.");
    32. }
    33.  
    34. @Override
    35. public HashMap<Integer, ItemStack> addItem(ItemStack... iss) {
    36. throw new UnsupportedOperationException("Not supported yet.");
    37. }
    38.  
    39. @Override
    40. public HashMap<Integer, ItemStack> removeItem(ItemStack... iss) {
    41. throw new UnsupportedOperationException("Not supported yet.");
    42. }
    43.  
    44. @Override
    45. public ItemStack[] getContents() {
    46. throw new UnsupportedOperationException("Not supported yet.");
    47. }
    48.  
    49. @Override
    50. public void setContents(ItemStack[] iss) {
    51. throw new UnsupportedOperationException("Not supported yet.");
    52. }
    53.  
    54. @Override
    55. public boolean contains(int i) {
    56. throw new UnsupportedOperationException("Not supported yet.");
    57. }
    58.  
    59. @Override
    60. public boolean contains(Material mtrl) {
    61. throw new UnsupportedOperationException("Not supported yet.");
    62. }
    63.  
    64. @Override
    65. public boolean contains(ItemStack is) {
    66. throw new UnsupportedOperationException("Not supported yet.");
    67. }
    68.  
    69. @Override
    70. public boolean contains(int i, int i1) {
    71. throw new UnsupportedOperationException("Not supported yet.");
    72. }
    73.  
    74. @Override
    75. public boolean contains(Material mtrl, int i) {
    76. throw new UnsupportedOperationException("Not supported yet.");
    77. }
    78.  
    79. @Override
    80. public boolean contains(ItemStack is, int i) {
    81. throw new UnsupportedOperationException("Not supported yet.");
    82. }
    83.  
    84. @Override
    85. public HashMap<Integer, ? extends ItemStack> all(int i) {
    86. throw new UnsupportedOperationException("Not supported yet.");
    87. }
    88.  
    89. @Override
    90. public HashMap<Integer, ? extends ItemStack> all(Material mtrl) {
    91. throw new UnsupportedOperationException("Not supported yet.");
    92. }
    93.  
    94. @Override
    95. public HashMap<Integer, ? extends ItemStack> all(ItemStack is) {
    96. throw new UnsupportedOperationException("Not supported yet.");
    97. }
    98.  
    99. @Override
    100. public int first(int i) {
    101. throw new UnsupportedOperationException("Not supported yet.");
    102. }
    103.  
    104. @Override
    105. public int first(Material mtrl) {
    106. throw new UnsupportedOperationException("Not supported yet.");
    107. }
    108.  
    109. @Override
    110. public int first(ItemStack is) {
    111. throw new UnsupportedOperationException("Not supported yet.");
    112. }
    113.  
    114. @Override
    115. public int firstEmpty() {
    116. throw new UnsupportedOperationException("Not supported yet.");
    117. }
    118.  
    119. @Override
    120. public void remove(int i) {
    121. throw new UnsupportedOperationException("Not supported yet.");
    122. }
    123.  
    124. @Override
    125. public void remove(Material mtrl) {
    126. throw new UnsupportedOperationException("Not supported yet.");
    127. }
    128.  
    129. @Override
    130. public void remove(ItemStack is) {
    131. throw new UnsupportedOperationException("Not supported yet.");
    132. }
    133.  
    134. @Override
    135. public void clear(int i) {
    136. throw new UnsupportedOperationException("Not supported yet.");
    137. }
    138.  
    139. @Override
    140. public void clear() {
    141. throw new UnsupportedOperationException("Not supported yet.");
    142. }
    143.  
    144. @Override
    145. public List<HumanEntity> getViewers() {
    146. throw new UnsupportedOperationException("Not supported yet.");
    147. }
    148.  
    149. @Override
    150. public String getTitle() {
    151. throw new UnsupportedOperationException("Not supported yet.");
    152. }
    153.  
    154. @Override
    155. public InventoryType getType() {
    156. throw new UnsupportedOperationException("Not supported yet.");
    157. }
    158.  
    159. @Override
    160. public InventoryHolder getHolder() {
    161. throw new UnsupportedOperationException("Not supported yet.");
    162. }
    163.  
    164. @Override
    165. public ListIterator<ItemStack> iterator() {
    166. throw new UnsupportedOperationException("Not supported yet.");
    167. }
    168.  
    169. @Override
    170. public ListIterator<ItemStack> iterator(int i) {
    171. throw new UnsupportedOperationException("Not supported yet.");
    172. }
    173.  
    174. }

    Obviously, you would have to implement the methods
     
    jtjj222 and MrBluebear3 like this.
  7. Offline

    jtjj222

    I doubt that will work, but I guess it is worth a try! I am looking through the cb source right now to see how the server does it, and I will report back here if I find anything useful :D

    Ok, this explains all of the packets involved:
    http://www.wiki.vg/Protocol#Open_Window_.280x64.29
    The window id is 6 for villager trading windows (look at handleOpenWindow in NetClientHandler in mcp, case 6).
    I am trying to get a working code example for you ready.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
    MrBluebear3 likes this.
  8. Offline

    fireblast709

  9. Offline

    jtjj222

    I know, there are other packets for that, but it's a start, and it's the only solution offered so far...

    Ok, so I can spawn a trading window, now I will work on getting trading to work.
    View attachment 11740

    Ok, I made a custom trade. View attachment 11743
    (1 glass for 1 glass)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  10. Offline

    drtshock

    ^fantastic idea. Might have to figure out how to do that and implement a way for players to trade when in a certain area :)
     
  11. Offline

    fireblast709

    and how did you do this? Using the method I described before?
     
  12. Offline

    jtjj222

    Nope :D
    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    3.  
    4. /*******Get a trading window ready***************/
    5. Packet100OpenWindow packet = new Packet100OpenWindow();
    6. packet.a = windowId; //window id
    7. packet.b = 6; //inventory type
    8. packet.c = "Title"; //title
    9. packet.d = 1; //inventory count
    10. /******************************/
    11.  
    12. /*******Send the packet to open a trading window**********/
    13. EntityPlayer p = ((CraftPlayer)((Player)sender)).getHandle();
    14. p.netServerHandler.sendPacket(packet);
    15. /******************************/
    16.  
    17. /********Prepare the offers**************/
    18. MerchantRecipeList r = new MerchantRecipeList();
    19.  
    20. //add a recipie to the list
    21. //The player can select between these using the arrows
    22. r.a(new MerchantRecipe(new ItemStack(Material.GLASS.getId(),1, 0), new ItemStack(Material.STONE.getId(),2,0), new ItemStack(Material.THIN_GLASS.getId(),3,0)));
    23. r.a(new MerchantRecipe(new ItemStack(Material.GLASS.getId(),1, 0), new ItemStack(Material.GLASS.getId(),20,0)));
    24. /**NOTE: ItemStack is the net.minecraft one, not the bukkit one**/
    25. /**The constructor for ItemStack takes the item id, number of items in the stack, and damage*/
    26. /******************************/
    27.  
    28. /****Send the trading offer list using custom payload packet*****/
    29. try {
    30.  
    31. /******Prepare a CustomPayloadPacket***********/
    32.  
    33. /*******Write the window id**********/
    34. out.writeInt(this.windowId);
    35.  
    36. /******Then write the offer*************/
    37. r.a(out);
    38.  
    39. p.netServerHandler.sendPacket(new Packet250CustomPayload("MC|TrList", buffer.toByteArray()));
    40. /******************************/
    41.  
    42. }
    43.  
    44. catch (IOException e) {
    45. e.printStackTrace();
    46. }
    47.  
    48. /*****We're done here!***********/
    49.  
    50. return true;
    51. }
    52.  

    If anyone wants to make a shop plugin with me like this, let me know :D

    Just to clarify, this is what the result of the code I posted looks like (note the arrow being active):
    This is the first trade offer
    View attachment 11744

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  13. Offline

    MCForger

    jtjj222
    I would be interested in helping on a plugin with you. PM me if you need my skype info or email.
     
  14. Offline

    fireblast709

    jtjj222 nice code. Where did you get the custom packet from (like, any resources)?
     
  15. Offline

    jtjj222

    I looked through mcp to figure out how to do everything for that. For the custom packet, I found that code in EntityPlayerMP.
     
  16. Offline

    drtshock

  17. Offline

    MCForger

  18. Offline

    LegacyUnlocking



    I wanna help :D add me on skype thelegacyunlocking | i can do gfx and all the video's and advertising etc... | and maby a bit of coding
     
  19. Offline

    jtjj222

    drtshock likes this.
  20. Offline

    drtshock

  21. Offline

    Hoolean

    Thanks for the code! Also if you guys are going to make a plugin, please make your own thread :D

    Thanks!
     
Thread Status:
Not open for further replies.

Share This Page