Making GUI Shop

Discussion in 'Plugin Development' started by jay275475, Sep 5, 2013.

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

    jay275475

    Hey I need Help Make a GUI Shop Can anyone help Me?
     
  2. Offline

    Retherz_

    What do you need help with? Use vault and an inventory?
     
  3. Offline

    jay275475

    I'm Working on A GUI Shop where you can buy things Ik the Vault Part But I well need Help With the GUI itself
     
  4. Offline

    xCyanide

  5. Offline

    Retherz_

    first google result: http://www.youtube.com/watch?v=v3AvBAvZURc


    private Menu menu;

    @EventHandler

    public void onPlayerInteract(PlayerInteractEvent event) {

    if (event.getAction() == Action.RIGHT_CLICK_AIR
    || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    Player p = event.getPlayer();
    if (p.getItemInHand().getType().equals(Material.PAPER)) {
    menu = new Menu(this, p);
    menu.show(p);
    }
    }

    public class Menu implements Listener {

    private Inventory inv;

    public Menu(Plugin p, Player pl) {
    inv = Bukkit.getServer().createInventory(
    null,
    9,
    ChatColor.GOLD+ "Shop");

    Bukkit.getServer().getPluginManager().registerEvents(this, p);

    }
    watch the video

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
    PogoStick29 likes this.
  6. Offline

    Loogeh

    jay275475 On my server I do it like this. I have two HashMaps, one for items which are buyable and their price and another for items which are sellable and their price and where they're sold.
    Code:
    public static HashMap<String, Integer> buyPrice = new HashMap<String, Integer>();
        public static HashMap<String, SellItem> sellPrice = new HashMap<String, SellItem>();
    You'll notice that I don't store an integer as the value of sellPrice. Instead I store a constructor

    Code:
    public class SellItem {
     
        public int sell_cost;
        public String alternate_name;
        public ItemType type; 
     
    public SellItem(int sell_cost, ItemType type) {
            this.sell_cost = sell_cost;
            this.type = type;
        }
     
     
        public int getSellCost() {
            return this.sell_cost;
        }
     
        public String getAlternate() {
            return this.alternate_name;
        }
     
        public ItemType getType() {
            return this.type;
        }
    }
    The ItemType is just an enumeration of all the shops I have which in my case goes like this

    Code:
        public enum ItemType {
            WEAPONRY,
            BUILDER,
            ENCHANTER,
            MISCELLANEOUS,
            MINER,
            BAKER;
        }
    The reason why I use a constructor in the sellPrice map and not in the buyPrice map is so that people can't sell their items at any shop, they have to be at the correct one. If they're not at the correct shop, it says that the item which they're trying to sell is sold at <item_shop_here>.
    As for creating the inventories MercilessPvP has posted all you need.
     
  7. Offline

    jay275475

    Thank You guys!

    I have another Question
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    4. menu.show(e.getPlayer());

    How would I make it a Certain Block Besides Any block?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  8. Offline

    Loogeh

    jay275475 What do you mean? Like this?
    Code:
    if(event.getClickedBlock().getType() != Material.YOUR_BLOCK)
     
  9. Offline

    xTrollxDudex

    jay275475
    Why Do You Write Like This? It makes it very difficult to read
     
    Loogeh likes this.
  10. Offline

    CeramicTitan Retired Staff

  11. Offline

    Drkmaster83

  12. Offline

    kreashenz

    It's simple. When they interact with an item, cancel the event and create & open a new inventory. Then listen to the InventoryClickEvent and if the inventory has the same name as the inventory you just created, cancel the event, check the item and item's name. If the item and item name are the ones you want, do something in it.
    I've used this in a few of my plugins, and it shouldn't be too hard to make.
     
  13. Offline

    JPG2000

    jay275475 Like kreashenz said its SOOO simple! (PS sorry for not using Java format, on Ipad D:)

    To create:
    Inventory customInv = bukkit.createInventory(null, 9, "My Custom Inv Name!");
    //NOTE: The 9 represents the # of slots, must be a multiple of 9.

    To set items in the Inv:
    customInv.setItem(0, new ItemStack(Material.ENDER_PEARL, 1));
    //NOTE: The 0 represents the slot that the item goes to

    The inv click event:
    @EventHandler
    public void onClick(InventoryClickEvent event) {
    if (event.getInventory().getName().equalsIgnoreCase(CustomInv.getName()) {
    //Makes it so the code will only work if its in the CustomInv inventory
    ItemStack clicked = event.getCurrentItem();
    //The item that was clicked
    if (clicked.getType() == Material.ENDER_PEARL) {
    event.setCanceled(true);
    //Stops the player from taking the item,
    }
    }
    }

    Thats basicly it. The ony hard part is the inv click event. Remember to show the player first:
    player.openInventory(customInv);
     
  14. Offline

    tylersyme

    I always set the display names of items before adding them to the inventory GUI
    And when a player clicks on the item I check to see what the display name is
    Depending on what the display name is, I do.. whatever I want to have happen afterwards
     
Thread Status:
Not open for further replies.

Share This Page