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
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.
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.
Thank You guys! I have another Question Code:java @EventHandler public void onPlayerInteract(PlayerInteractEvent e) { if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return; 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.
jay275475 What do you mean? Like this? Code: if(event.getClickedBlock().getType() != Material.YOUR_BLOCK)
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.
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);
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