1.8.8 Inventory won't open!

Discussion in 'Spigot Plugin Development' started by PrayRNGesus, May 15, 2020.

  1. Source code
    boom
    Code (Text):

    package me.PrayRNGesus.Main.Events;
    import java.util.ArrayList;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    public class Events implements Listener {
        @EventHandler
        public void joinEvent(PlayerJoinEvent event) {
            // compass registry
            ItemStack gs = new ItemStack(Material.COMPASS);
            ItemMeta gsm = gs.getItemMeta();
            gsm.setDisplayName(ChatColor.BLUE + "Game Selector");
            ArrayList<String> gslore = new ArrayList<String>();
            gslore.add(ChatColor.DARK_AQUA + "///////////////////////////////////////////////");
            gslore.add(ChatColor.GREEN + "Right click with this compass to select a game!");
            gslore.add(ChatColor.DARK_AQUA + "///////////////////////////////////////////////");
            gsm.setLore(gslore);
            gs.setItemMeta(gsm);
            //commands registry
            Player player = event.getPlayer();
         
            //never played
            if (!player.hasPlayedBefore()) {
                player.sendMessage(ChatColor.GREEN + "Welcome " + player.getName() + "!");
                player.getInventory().addItem(gs);
            //has played
            } else if (player.hasPlayedBefore()) {
                player.sendMessage(ChatColor.GREEN + "Welcome back " + player.getName() + "!");
             
                //doesn't have compass
                if (!player.getInventory().contains(Material.COMPASS)) {
                    player.getInventory().addItem(gs);
                //has compass
                } else {
                    return;
            //Don't mess with this return statement (it is not necessary but can be used as extra precaution.
                }
            } else {
                return;
            }
        }
        @EventHandler
        public void interactEvent(PlayerInteractEvent event) {
            //compass creation
            ItemStack gs = new ItemStack(Material.COMPASS);
            ItemMeta gsm = gs.getItemMeta();
            gsm.setDisplayName(ChatColor.BLUE + "Game Selector");
            ArrayList<String> gslore = new ArrayList<String>();
            gslore.add(ChatColor.DARK_AQUA + "///////////////////////////////////////////////");
            gslore.add(ChatColor.GREEN + "Right click with this compass to select a game!");
            gslore.add(ChatColor.DARK_AQUA + "///////////////////////////////////////////////");
            gsm.setLore(gslore);
            gs.setItemMeta(gsm);
         
            ItemStack uhc = new ItemStack(Material.COMPASS);
            ItemMeta uhcm = uhc.getItemMeta();
            gsm.setDisplayName(ChatColor.BLUE + "Game Selector");
            ArrayList<String> uhclore = new ArrayList<String>();
            gslore.add(ChatColor.DARK_AQUA + "///////////////////////////////////////////////");
            gslore.add(ChatColor.GREEN + "Right click with this compass to select a game!");
            gslore.add(ChatColor.DARK_AQUA + "///////////////////////////////////////////////");
            gsm.setLore(uhclore);
            gs.setItemMeta(uhcm);
         
            // commands registry
            Player player = event.getPlayer();
            Inventory GameSelector = Bukkit.getServer().createInventory(player, 9, ChatColor.RED + "Game Selector!");
            GameSelector.setItem(5 - 1, uhc);
         
            if(player.getInventory().getItemInHand().getItemMeta().equals(gsm)) {
                if(event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) {
                    player.closeInventory();
                    player.openInventory(GameSelector);
                 
                }
            }
        }
    }
     
    my issue
    boom
    Code (Text):

    I can't seem to figure out why the inventory will not open!
    I am getting no errors so I'm confused
     
    Please help
    thanks :)
     
  2. Code (Java):
     if(player.getInventory().getItemInHand().getItemMeta().equals(gsm))
    This line is probably the issue. Try checking displayname instead
     
  3. You cannot use close inventory and open inventory at the same methods (ticks)


    You must close inventory and after that use Bukkit.getScheduler().runTask(plugin, /*i use lambdas so example with lambda/* () -> p.openInventory(inventory)); for open inventory at next tick

    if doesn`t work try to wait 2 ticks (also by Schedulers)
     
    • Agree Agree x 1
  4. That's not the problem, I've done it many times
     
    • Like Like x 1
  5. Code (Text):
    if(player.getInventory().getItemInHand().getItemMeta().equals(gsm))
    Check the displayname.
     
  6. I ran into the same issue a while back and happen to agree with MiMiMiEnot. Waiting 2 ticks to open should work, even 1 may do the trick already. Otherwise the client receives confusing information at the same time.
     
    • Like Like x 1
  7. getitemmeta is incomplete itemmeta includes so many things and here we are checking for the displayname but he has not provided complete check he must use

    player.getInventory().getItemInHand().getItemMeta().getDisplayName().equals("gsm")
     
  8. gsm is an instance of ItemMeta though, and not a String