Solved Custom Shop w/ Anvils

Discussion in 'Plugin Development' started by aGuy_, May 9, 2017.

Thread Status:
Not open for further replies.
  1. Hello you awesome plugin creators!

    I've been recently trying to learn how to code. Because I want to make plugins I can use for a server were I'm staff on.
    We want to make a custom shop. Right now we are using the plugin 'ChestCommands', however u need to use /craft to open the shop GUI, and we can't figure out how to make it so you need multiple items to craft a certain thing.

    For example:
    When we type in chat '/craft', it opens a GUI.
    There would be some renamed items like
    A Diamond Sword named ''Tools'' (In light-red, &c or something like that)
    Hover over it you would see one or more lore(s), something like:
    'Click me to open the Tool Shop Menu' (In gray, &7)

    When you would click the Tool Item, a new GUI would show up.
    U would see items like Swords, Axes, Shovels and Axes.

    for example I want to make an Iron Sword,
    I would hover over the Iron Sword, named Iron Sword in the default color White (&f) to see what items I need to make one. Something like:

    'Click me to craft a(n): Iron Sword', the first part would be in Light-Red (&c) and Iron Sword in White (&f)
    'You will need:' (In Light-Red, &c)
    '12x Iron Ingot(s)' (In Gray, &7)
    '12x Stick(s)' (In Gray, &7)

    When you click the Iron Sword 3 things could happen.
    *
    If u have the right amount of items, they would get taken away and an Iron Sword would pop up in your Inventory

    *
    If u don't have the right amount of items, you would see in chat 'You don't have the right amount of items!' in Light-Red,
    OR
    You would hear a 'DING' sound. (probably both)

    *
    If u have the right amount of items, but you don't have enough inventory space. you would see in chat 'You don't have enough Inventory Space!'
    OR
    You would hear a 'DING' sound. (again, probably both)

    Get it? I hope so.


    I want to make or find a plugin, that allows us to right-click on an Anvil to open a Shop GUI,
    Then click on (for example:) a Diamond Sword to go to ANOTHER GUI with tool-based items. (Swords, Axes, Shovels etc.)

    Then click on an item (for example) an Iron Sword, then it would check if you have 12 Iron Ingots and 12 Sticks in your inventory, if you do. It would take those items, and put an Iron Sword in your inventory.
    If you don't have those items however, it would show a red text in chat, or just a 'DING' sound. Because you don't have enough items to craft the Iron Sword.

    If you still don't get it, then I'm sorry. I'm not the best at english, I'm very young (11) and I'm dutch. aka it's already very impressive that I'm this good in english for my age obviously.
    (Yes I'm 11, I know. It doesn't matter :p)

    Video of what I mean: (Use subtitles so you will understand me better)



    If any of you knows a video tutorial or could make a video.. Or just type, on how to make this.
    Then I would really appreciate it.

    Thank you for your time to read this!
    I hope you can help me

    Have a nice day,
    -aGuy!
     
  2. Offline

    Zombie_Striker

    @aGuy_
    Assuming this is your first time coding, you need to learn Java before working on bukkit. Bukkit is written in Java, so you need to understand how to write programs in java Alone before working on bukkit. You can find tutorials where to learn Java here:
    https://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.395662/

    After that, if you need to learn how to write plugins with Java, I would recommend the Youtuber PogoStickDev. Here is a playlist of all of his bukkit videos:
    https://www.youtube.com/playlist?list=PLAF3anQEEkzREsHA8yZzVhc3_GHcPnqxR

    For what you want, you need to do two things:
    1. onCommand, if the command sent is equal to craft, open a custom inventory with the "tools" sword.
    2. On InventoryClickEvent, if the clicked item is a sword, check if they have enough items and enough spaces. If so, pop it into your inventory.
     
  3. @Zombie_Striker
    Well thank you.
    Indeed its pretty much my first time coding..
    And I prefer video tutorials instead of text, obviously I will use it though.

    I just can't figure out any way to open a Chest GUI when you right-click on an Anvil. Every video tutorial I find, are just right-clicking with items. It's so frustrating!
    And then when I try to look for 'Shop' or 'Crafting' plugin tutorials,
    You only find custom recipes with a Crafting Table or just Eco Payment instead of items..

    That's why I made this thread
     
  4. Offline

    Zombie_Striker

    @aGuy_
    For Anvils, use PlayerInteractEvent (the same event most likely used in the tutorials). What you need to then do is cancel the event (so the anvil inventory does not open) and instead open your Chest GUI inventory.

    [Edit] Just in case you meant right clicking on an item in an inventory, use InventoryClickEvent. Check if the click type is equal to RIGHT, and if so, cancel the event and open the GUI.
     
  5. @Zombie_Striker
    I swear I could kiss you right now..
    I love you SO MUCH, no homo.

    Now I just need to make it so the GUI pops up when I try to open the Anvil.
    Or it's a bad thing that I made it so u can't open an Anvil.. xd

    If anyone needs the code:

    MainClass (EventHandle.java)
    Pastebin: https://pastebin.com/TUguc9Ee
    Screenshot: https://gyazo.com/ae7bad1ea8236030fce1ce8016d96007

    PlayerListener (PlayerListener.java)
    Pastebin: https://pastebin.com/f3fa1eLJ
    Screenshot: https://gyazo.com/6f473dbd77bc745ca61707baa55fa37e

    plugin.yml
    https://gyazo.com/91ba44864fdd727766863b7002025d62


    Thank
    you
    so
    ducking
    much
     
  6. Offline

    Zombie_Striker

    @aGuy_
    Before we try to open the inventory, we need to create the inventory first. Try doing the following:
    1. Create a new method that will return an Inventory. This will return the new GUI.
    2. Inside this method, create a new inventory using Bukkit.createInventory(null, SIZE, NAME);
    3. null needs to stay null (this is the 'owner' of the inventory), SIZE should be equal to the size of the inventory (The size must be multiples of 9. So, 1 line would be '9'. 2 lines would be '18'...) and NAME will be the name of the inventory(I would recommend something like "CustomShopGUI" so it does not conflict with other inventory gui plugins (if you have any) ).
    4. After creating the inventory, add items to each slot by using Inventory#setItem(SLOT, ITEMSTACK), where slot is the slots number (top left will be 0, top right will be 8...) and Itemstack is the itemstack instance.
    5. After you are done setting up the GUI, return that inventory instance.
     
  7. I tried everything I could.

     
  8. Offline

    Zombie_Striker

    @aGuy_
    Your issue here is you forgot to encapsulate the inventory type check. Because there are no brackets around event lines, the event will be canceled for all inventories, and a new inventory will be opened for all inventories, which means the player will continually open inventories forever, which then locks up the server (as shown in the video).

    Also, you don't need to cancel the event twice. It only needs to be done once.
     
  9. @Zombie_Striker
    Sorry to ask, it's probably very simple. But what do you mean with 'encapsulate'? You mean putting an '{' at the end of a code right?
    Well, that doesn't work.
     
  10. Offline

    Zombie_Striker

    @aGuy_
    Well, sort of. You want to put an open bracket after the if statement, and a closed bracket after all the code that needs to run. So, it should look like:
    Code:
    if(e.getInventory.getType() == InventoryType.ANVIL) {
       e.setCanceled(true);
       openGUI(e.getPlayer());
    }
    If that still does not fix the problem, can you post the full error log?
     
  11. @Zombie_Striker I did that before, but I had an error. but now it's fixed.. thanks, I will now move on and try to get the GUI all done.

    EDIT:
    Pretty much solved now. I will still use this thread if I still need some help though.
     
Thread Status:
Not open for further replies.

Share This Page