Solved Second gui

Discussion in 'Plugin Development' started by Ethan Rocks 365, Feb 21, 2016.

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

    Ethan Rocks 365

    Hello. Im trying to make a plugin where i click something in a gui and it opens another gui.
    Code:
    package me.ethanrocks356.gui;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    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.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class GUI extends JavaPlugin implements Listener {
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
    
        private void teleportInWorld(Player player, int x, int y, int z) {
            player.teleport(new Location(player.getWorld(), x, y, z));
        }
    
        private void openGUI(Player player) {
            Inventory inv = Bukkit.createInventory(null, 27, ChatColor.RED + "Start");
    
            ItemStack spawn = new ItemStack(Material.MAP);
            ItemMeta spawnMeta = spawn.getItemMeta();
    
            spawnMeta.setDisplayName(ChatColor.GREEN + "Spawn");
            spawn.setItemMeta(spawnMeta);
           
            inv.setItem(13, spawn);
           
           
            player.openInventory(inv);
        
        }
        private void openGUI1(Player player) {
            Inventory inv1 = Bukkit.createInventory(null, 27, ChatColor.RED + "Teleporter");
    
            ItemStack spawn = new ItemStack(Material.MAP);
            ItemMeta spawnMeta = spawn.getItemMeta();
    
            spawnMeta.setDisplayName(ChatColor.GREEN + "Spawn");
            spawn.setItemMeta(spawnMeta);
           
            inv1.setItem(13, spawn);
           
           
            player.openInventory(inv1);
        }
    
        @EventHandler
        public void onInventoryClickEvent(InventoryClickEvent e){
            if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Start"))
            return;
            Player player = (Player) e.getWhoClicked();
            e.setCancelled(true);
           
            if(e.getCurrentItem() == null || e.getCurrentItem().getType() ==Material.AIR || !e.getCurrentItem().hasItemMeta()){
                    player.closeInventory();
                    return;
        }
        }   
            @EventHandler
            public void onInventoryClickEvent1(InventoryClickEvent e){
                if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Teleporter"))
                return;
                Player player = (Player) e.getWhoClicked();
                e.setCancelled(true);
               
                if(e.getCurrentItem() == null || e.getCurrentItem().getType() ==Material.AIR || !e.getCurrentItem().hasItemMeta()){
                        player.closeInventory();
                        return;
            }
        switch (e.getCurrentItem().getType()){
        case MAP:
            player.closeInventory();
            player.openInventory(inv1)
            break;
        default:
            player.closeInventory();break;
        }
        }
       
        @EventHandler
        public void onPlayerJoinEvent(PlayerJoinEvent e) {
            e.getPlayer().getInventory().addItem(new ItemStack(Material.COMPASS));
        }
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Action a = e.getAction();
            if (a == Action.PHYSICAL || e.getItem() == null || e.getItem().getType() == Material.AIR)
                return;
            if (e.getItem().getType() == Material.COMPASS) {
                openGUI(e.getPlayer());
            }
    
        }
    }
    // ===================
    // =Inventory Setup =
    // ===================
    // =E - - - - - - - -=
    // =- - - - S - - - -=
    // =- - - - - - - - -=
    // ===================
    
     
  2. Offline

    MCMastery

  3. Offline

    Gerov

  4. Offline

    Lightspeed

    Before you open the second gui player.closeInventory(); or somthing if this is completly off from the question well I tried to guess what you wanted.
     
  5. Offline

    Ethan Rocks 365

    Im not shure but i cant get the second gui, inv1, to open

    The problem i see is in the picture but i just straight up have no clue how i would open a second gui

    EDIT: and i did do player.colseInventory(); didnt you see it in the code?
    EDIT#2:
    1. switch (e.getCurrentItem().getType()){
    2. case MAP:
    3. player.closeInventory();
    4. player.openInventory(inv1)
    5. break;
     

    Attached Files:

  6. Offline

    Gerov

    @Ethan Rocks 365 That's because are trying to open an inventory object that doesn't exist.
     
  7. Offline

    Lordloss

    btw you dont have to close an inventory bevore opening a different one.
     
  8. Offline

    Ethan Rocks 365

    But I created inv1
     
  9. Offline

    Lordloss

    You defined it in a different method.. its not valid in your event handler. #JavaBasics
     
  10. Offline

    CoolDude53

    Zombie_Striker likes this.
  11. Offline

    Zombie_Striker

  12. Offline

    Go Hard

    First off why do you have 2 InventoryClickEvents? Put them both in the same event. Second use your methods.... Third on your first InventoryClickEvent you check to see if the player clicks in the "Start" inventory but you don't open the second gui. Check to see if the player clicks the map on the first InventoryClickEvent then open the SECOND GUI.

    So add this to your first InventoryClickEvent
    Code:
    switch (e.getCurrentItem().getType()){
      case MAP:
        player.closeInventory();
        openGUI1(player); //Opens "teleporter' inv
    break;
      default:
        player.closeInventory();break;
    }
    
    Then add this to your second InventoryClickEvent
    Code:
    switch (e.getCurrentItem().getType()){
      case MAP:
        player.closeInventory();
        openGUI(player); //Opens "Start" inv
    break;
      default:
        player.closeInventory();break;
    }
    
    I don't use switches but this should fix your problem. Let alone you only need to have 1 InventoryClickEvent....
     
    Zombie_Striker likes this.
  13. Offline

    Ethan Rocks 365

  14. Offline

    Lordloss

Thread Status:
Not open for further replies.

Share This Page