MerchantAPI - Create new merchants.

Discussion in 'Resources' started by Cybermaxke, Dec 31, 2012.

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

    Cybermaxke

    Description
    The merchants api is a expansion of the bukkit api to be able to create custom merchants without any need of an attached entity and being able to modify it.

    Download?
    You can find the builds for the api and all the implementation versions are in this dropbox.

    Source?
    The source code is available on github and there is also a wiki that provides extra info.

    How to use it?
    Code:
    // Access the merchants api
    MerchantAPI api = Merchants.get();
    // Create a new merchant
    Merchant merchant = api.newMerchant("My Name");
    // Add some offers
    // Arg: 1=result; 2=first offer item; 3=(optional)second offer item
    merchant.addOffer(api.newOffer(new ItemStack(Material.EMERALD, 10), new ItemStack(Material.DIAMOND, 3));
    merchant.addOffer(api.newOffer(new ItemStack(Material.EMERALD, 30), new ItemStack(Material.DIAMOND, 3), new ItemStack(Material.STONE, 64));
    // Open the inventory for a player
    Player player = ...; // Access by any way
    merchant.addCustomer(player);
    
    How can I use the merchant trade listener?
    Code:
    Merchant merchant = ...; // Access by any way
    merchant.addListener(new MerchantTradeListener() {
    
        @Override
        public void onTrade(Merchant merchant, MerchantOffer offer, Player customer) {
            // Do stuff
        }
    
    });
    
    How can I access the merchant inside inventory events?
    Code:
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        Inventory inventory = event.getInventory();
    
        // Check the instance, make sure that imported the inventory class from the library
        // and not the bukkit one (they are named the same), or it won't work
        if (inventory instanceof MerchantInventory) {
            MerchantInventory merchantInventory = (MerchantInventory) inventory;
            // Gets the merchant
            Merchant merchant = merchantInventory.getMerchant();
            // Gets the currently selected offer
            MerchantOffer offer = merchantInventory.getSelectedOffer();
    
            // Do stuff
        }
    }
     
    Last edited: Jun 2, 2015
  2. Offline

    phondeux

    Is this single-player or multiplayer? The other thread mentioned issues with getting this to work in multiplayer.
     
  3. Offline

    Cybermaxke

    This is multiplayer and I had no problems with getting it working. :)
     
    phondeux likes this.
  4. Offline

    Metamaniac

    Is there a way to remove default trades from villagers?

    For example, could i keep the Diamond Sword trade option that the Blacksmith sells from ever popping up?
     
  5. Offline

    Cybermaxke

    EDIT: It basicly clears everything first.

    #Update
    - Fixed setToVillager() method from throwing errors.

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

    Cybermaxke

    #Update
    - Updated to 1.4.7
     
  7. Offline

    lucasdidur

    Cool, I will try to implement in my Shop Plugin.
     
  8. Offline

    Cybermaxke

    Update
    - Ability to attach merchants to entities.
    - Added witch merchant, has a updated ai.
     
  9. Offline

    jtjj222

    You released this just after I spent a couple hours looking through nms code to figure out how to do it.... :D
     
  10. Offline

    Cybermaxke

    I made and posted it on the 31 december :p
     
  11. Hmm, did you also make a pull request with this API for Bukkit ?
     
  12. Offline

    Cybermaxke

    Nope
     
  13. Offline

    jtjj222

    I am pretty sure that was just after I looked into implementing an IMerchant, and using player.openTrade() (I think that's what you use, correct?). I may be mistaken :D
    Anyway, great job, and you should make a pr.
     
  14. Why not ? It would be better if we won't have to include libraries for game related API :/ and the fact that it absolutely needs updates on major versions because of versioning policy :p
     
  15. Offline

    Cybermaxke

    - Reverted the entity part, should be added by the other devs.
     
  16. Offline

    Minnymin3

    How could i make the trade only accept an item with a custom name/data?

    Is there a way to check if an inventory is an instanceof a merchantAPI inventory?
     
  17. Offline

    chasechocolate

  18. Offline

    Minnymin3

  19. Offline

    Minnymin3

    Im working on a leveling system w/ your api where i want to cancel clicks of items that are not part of the trade (ie I dont want people trading a default blazerod for a superawesomeoverpowered rod of fire) but I don't want to disable vanilla trades. So on to my question: Is there an easy way to name the inventory on the openTrading?

    I tried getTitle but that didnt work either it returns mob.villager but I want to get the header any ideas?
     
  20. Offline

    Cybermaxke

  21. Offline

    phips99

    Cybermaxke It's throwing an NoClassDefError when I try to right click a villager...
     
  22. Offline

    Cybermaxke

    Can you post the error on pastie.org?
     
  23. Offline

    phips99

  24. Offline

    Cybermaxke

    You also need to load the plugin and you need to add 'depend: [MerchantAPI]' to your plugin.yml
     
  25. Offline

    phips99

  26. Offline

    Cybermaxke

    I didn't update it to 1.6.2 yet, I will do it as soon I can.
     
  27. Offline

    phips99

    Cybermaxke Thanks!

    Cybermaxke Is this ok for you when I use a modified version of MerchantAPI because I updated it and fixed the bugs. And is this also ok for you when I implement this in my project so the people don't have to download the plugin first? I'll credit you in the project of course...
     
  28. Offline

    Cybermaxke

    Sure :)
     
  29. Offline

    Tudedude

    I did the basic code in the tutorial above, but it gave me errors here: merchant.addOffers(o1, o2); and here: MerchantAPI.setToVillager(villager, merchant); Errors are underlined. Error is, Syntax error on token "(", delete this token.
     
  30. Offline

    ArthurMaker

    Please, update it! ;-;
     
    UbuntuFreak likes this.
Thread Status:
Not open for further replies.

Share This Page