Add menu to my code

Discussion in 'Plugin Development' started by CraterHater, May 30, 2015.

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

    CraterHater

    Hello, i am making a server and i want a plugin that opens up an menu of 9 slots when you right-click an netherstar. The 9 slots must be clickable and must refer to another menu. I already got this:

    EDIT: i now got this code can anyone say whats wrong:
    Code:
    package First;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    public class first extends JavaPlugin implements Listener{
        public void onEnable()
        {
            getServer().getPluginManager().registerEvents(this, this);
        }
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
    if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
    if (e.getPlayer().getItemInHand().getType().equals(Material.NETHER_STAR)) {
    e.getPlayer().sendMessage("Hi");
    Inventory myInventory = Bukkit.createInventory(null, 9, "My custom Inventory!");
    static {
    myInventory.setItem(0, new ItemStack(Material.DIRT, 1));
    myInventory.setItem(8, new ItemStack(Material.GOLD_BLOCK, 1));
    public void onInventoryClick(InventoryClickEvent event) {
    Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    Inventory inventory = event.getInventory(); // The inventory that was clicked in
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
    Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    Inventory inventory = event.getInventory(); // The inventory that was clicked in
    if (inventory.getName().equals(myInventory.getName())) {
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked(); // The player that clicked the item
        ItemStack clicked = event.getCurrentItem(); // The item that was clicked
        Inventory inventory = event.getInventory(); // The inventory that was clicked in
        if (inventory.getName().equals(myInventory.getName())) { // The inventory is our custom Inventory
        if (clicked.getType() == Material.DIRT) { // The item that the player clicked it dirt
        event.setCancelled(true); // Make it so the dirt is back in its original spot
        player.closeInventory(); // Closes there inventory
        player.getInventory().addItem(new ItemStack(Material.DIRT, 1)); // Adds dirt
        }
        }
        }
     
    Last edited: May 30, 2015
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    CraterHater

  4. Offline

    timtower Administrator Administrator Moderator

    @CraterHater I used to spoonfeed, learned from it, not doing that.
    It even has an example class
     
  5. Offline

    CraterHater

    I didn't ask for spoonfeed just let me know where i have to insert those codes
     
  6. Offline

    JoelyBob

  7. Offline

    CraterHater

  8. Offline

    JoelyBob

    ...

    Did you read the thread? It tells you how to...
     
  9. Offline

    CraterHater

    It keeps giving me errors. Could you be kind and tell me how it is done?
     
  10. Offline

    CraterHater

    I don't know how to put them in my code so i don't have real errors cause when i place them the errors are that its on the wrong place.
     
  11. Offline

    Rbt

    http://bukkit.org/threads/tutorial-create-a-inventory-menu.173571/ First, create your inventory with bukkit.createInventory() and set it to Inventory inv.

    Add the items using inv.setItem(0, new ItemStack(Material.MATERIAL)

    Then, if you want to get the click event, use inventoryclickevent and check to make sure the item that you clicked and inventory name are the same. Also, you want to place this right where you sent the player a message in your code. If you imported everything correctly, you should get no errors. If you stated that you have an error, you must have the ability to post the error, right?
     
    Last edited: May 30, 2015
  12. Will be better check inventoryHolder
     
  13. Offline

    CraterHater

    Well i imported it all:

    Code:
    package First;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    public class first extends JavaPlugin implements Listener{
        public void onEnable()
        {
            getServer().getPluginManager().registerEvents(this, this);
        }
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
    if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
    if (e.getPlayer().getItemInHand().getType().equals(Material.NETHER_STAR)) {
    e.getPlayer().sendMessage("Hi");
    
    Inventory myInventory = Bukkit.createInventory(null, 9, "My custom Inventory!");
    static {
    myInventory.setItem(0, new ItemStack(Material.DIRT, 1));
    myInventory.setItem(8, new ItemStack(Material.GOLD_BLOCK, 1));
    public void onInventoryClick(InventoryClickEvent event) {
    Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    Inventory inventory = event.getInventory(); // The inventory that was clicked in
    
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
    Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    Inventory inventory = event.getInventory(); // The inventory that was clicked in
    if (inventory.getName().equals(myInventory.getName())) {
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked(); // The player that clicked the item
        ItemStack clicked = event.getCurrentItem(); // The item that was clicked
        Inventory inventory = event.getInventory(); // The inventory that was clicked in
        if (inventory.getName().equals(myInventory.getName())) { // The inventory is our custom Inventory
        if (clicked.getType() == Material.DIRT) { // The item that the player clicked it dirt
        event.setCancelled(true); // Make it so the dirt is back in its original spot
        player.closeInventory(); // Closes there inventory
        player.getInventory().addItem(new ItemStack(Material.DIRT, 1)); // Adds dirt
        }
        }
        }
    I pretty much get tons of errors. i guess you guys can notice them in an instance cause you guys are pro :D

    Right i posted the code with tons of mistakes unfortunatly.
     
    Last edited by a moderator: May 30, 2015
  14. Offline

    Rbt

    Code:
    package First;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    public class first extends JavaPlugin implements Listener{
        public void onEnable()
        {
            getServer().getPluginManager().registerEvents(this, this);
        }
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
      if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
        if (e.getPlayer().getItemInHand().getType().equals(Material.NETHER_STAR)) {
        e.getPlayer().sendMessage("Hi");
        Inventory myInventory = Bukkit.createInventory(null, 9, "My custom Inventory!");
        myInventory.setItem(0, new ItemStack(Material.DIRT, 1));
        myInventory.setItem(8, new ItemStack(Material.GOLD_BLOCK, 1));
    
    }
    
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
      Player player = (Player) event.getWhoClicked(); // The player that clicked the item
      ItemStack clicked = event.getCurrentItem(); // The item that was clicked
      Inventory inventory = event.getInventory(); // The inventory that was clicked in
      
      if (inventory.getName().equals("My custom Inventory!") {
        Player player = (Player) event.getWhoClicked(); // The player that clicked the item
        ItemStack clicked = event.getCurrentItem(); // The item that was clicked
        Inventory inventory = event.getInventory(); // The inventory that was clicked in
        if (clicked.getType() == Material.DIRT) { // The item that the player clicked it dirt
        event.setCancelled(true); // Make it so the dirt is back in its original spot
        player.closeInventory(); // Closes there inventory
        player.getInventory().addItem(new ItemStack(Material.DIRT, 1)); // Adds dirt
        }
      }
     }
    
    
    Would something like that work? I'm not sure if this is exactly correct, but you get the idea. I don't think you have done much with Java before, but you not access local variables that apply to another event. For example, in the oninventoryclick, you can not access myInventory. Also, you can not nest multiple events into each other.
     
  15. Offline

    CraterHater

    Thx but this part isn't fully working:

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
    Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    Inventory inventory = event.getInventory(); // The inventory that was clicked in

    if (inventory.getName().equals("My custom Inventory!") {
    Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    Inventory inventory = event.getInventory(); // The inventory that was clicked in
    if (clicked.getType() == Material.DIRT) { // The item that the player clicked it dirt
    event.setCancelled(true); // Make it so the dirt is back in its original spot
    player.closeInventory(); // Closes there inventory
    player.getInventory().addItem(new ItemStack(Material.DIRT, 1)); // Adds dirt
    }
    }
    }
    }
    }
     
  16. Offline

    Rbt

    This should work. Nice clean code. :)


    Code:
    @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked(); // The player that clicked the item
            ItemStack clicked = event.getCurrentItem(); // The item that was clicked
            Inventory inventory = event.getInventory(); // The inventory that was clicked in
          
            if(!(inventory.getName() == "My custom Inventory!")) return; //If inventory does not equal the created inventory's name
          
            if(clicked.getType() == Material.DIRT){ //if the clicked item is dirt
                event.setCancelled(true); //set cancelled and give dirt
                player.closeInventory();
                player.getInventory().addItem(new ItemStack(Material.DIRT, 1));
                player.sendMessage("Yay! Free dirt.");
            }
      
        }
     
  17. Offline

    CraterHater

    Right thx but i still got some problems this is my code:
    it just doesn't work and i am sure its not about the plugin.yml cause the plugin loads properly, it also doesn't give any errors.

    Code:
    package First;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    public class first extends JavaPlugin implements Listener{
        public void onEnable()
        {
            getServer().getPluginManager().registerEvents(this, this);
        }
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
      if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
        if (e.getPlayer().getItemInHand().getType().equals(Material.NETHER_STAR)) {
        Inventory myInventory = Bukkit.createInventory(null, 9, "My custom Inventory!");
        myInventory.setItem(0, new ItemStack(Material.DIRT, 1));
        myInventory.setItem(8, new ItemStack(Material.GOLD_BLOCK, 1));
     
        }
    }
    }
     
Thread Status:
Not open for further replies.

Share This Page