gettype itemstack

Discussion in 'Plugin Development' started by Condolent, Jul 18, 2015.

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

    Condolent

    So I'm making a custom inventory menu, to open the menu you need to right click a custom itemstack (in this case a compass I named menuCompass), and to actually do that I'm using the PlayerInteractEvent. Now when I do if(e.getItem().getType() == menuCompass) it won't work because the getType() needs a material, not an itemstack. What's another solution for this??
     
  2. .getType() == Material.COMPASS
     
  3. Offline

    Condolent

    @FisheyLP But that's just a normal compass, I made a custom named compass

    Code:
            ItemStack menuCompass = new ItemStack(Material.COMPASS);
            ItemMeta compassMeta = menuCompass.getItemMeta();
            compassMeta.setDisplayName(ChatColor.GREEN + "Server Menu");
            compassMeta.setLore(Arrays.asList("Opens up the server", "menu when right-clicked."));
            menuCompass.setItemMeta(compassMeta);
     
  4. Offline

    Konato_K

    @Condolent You can compare ItemStacks with #equals or #isSimilar.

    It's also possible to compare all this values apart manually, which is fairly easy to do.
     
  5. Offline

    Condolent

    @Konato_K I used
    Code:
    if(a == Action.RIGHT_CLICK_AIR && item.getType().equals(menuCompass))
                openGUI(e.getPlayer());
    and I got 0 errors but instead it didn't do it's work xd

    I'm gonna go ahead here and post my whole class
    Show code (open)
    Code:
    package me.condolent;
    
    import java.util.Arrays;
    import java.util.List;
    
    import net.md_5.bungee.api.ChatColor;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    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.inventory.meta.ItemMeta;
    
    public class Menu implements CommandExecutor, Listener {
       
        ServerMain plugin;
       
        public Menu(ServerMain instance) {
            plugin = instance;
        }
       
        public FileConfiguration getPlayerLog() {
            return plugin.getPlayerLog();
        }
       
        private void openGUI(Player p) {
            List<String> login = getPlayerLog().getStringList("login");
            getPlayerLog().set("login", login);
            plugin.savePlayerLog();
           
            Inventory menu = Bukkit.createInventory(null, 27, ChatColor.BLUE + "§lServer Menu");
           
            // TP to spawn-item
            ItemStack tpspawn = new ItemStack(Material.BEACON);
            ItemMeta tpspawnMeta = tpspawn.getItemMeta();
            tpspawnMeta.setDisplayName(ChatColor.GREEN + "Teleport to spawn");
            tpspawn.setItemMeta(tpspawnMeta);
           
            // Set itemslots
            menu.setItem(0, tpspawn);
           
            p.openInventory(menu);
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
           
            ItemStack menuCompass = new ItemStack(Material.COMPASS);
            ItemMeta compassMeta = menuCompass.getItemMeta();
            compassMeta.setDisplayName(ChatColor.GREEN + "Server Menu");
            compassMeta.setLore(Arrays.asList("Opens up the server", "menu when right-clicked."));
            menuCompass.setItemMeta(compassMeta);
           
            if(cmd.getName().equalsIgnoreCase("menu")) {
                if(!p.getInventory().contains(menuCompass)) {
                    p.sendMessage(ChatColor.YELLOW + "Menu-compass added to your inventory. Right click it while holding it to open the server menu!");
                    p.getInventory().addItem(new ItemStack(menuCompass));
                } else {
                    p.sendMessage(ChatColor.RED + "You already have the menu-compass in your inventory!");
                }
            }
           
           
            return false;
        }
       
        @EventHandler
        public void onInventoryItemClick(InventoryClickEvent e) {
            if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Server Menu"))
                return;
           
            e.setCancelled(true);
        }
       
        @EventHandler
        public void onCompassClick(PlayerInteractEvent e) {
            Action a = e.getAction();
            ItemStack item = e.getItem();
           
            ItemStack menuCompass = new ItemStack(Material.COMPASS);
            ItemMeta compassMeta = menuCompass.getItemMeta();
            compassMeta.setDisplayName(ChatColor.GREEN + "Server Menu");
            compassMeta.setLore(Arrays.asList("Opens up the server", "menu when right-clicked."));
            menuCompass.setItemMeta(compassMeta);
           
            if(a == Action.PHYSICAL || item == null || item.getType() == Material.AIR)
                return;
           
            if(a == Action.RIGHT_CLICK_AIR && item.getType().equals(menuCompass))
                openGUI(e.getPlayer());
        }
    
    }
    


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

    Konato_K

    @Condolent You're supposed to compare the whole ItemStack, not the type.
     
  7. Offline

    567legodude

    @Condolent
    Code:
    if (a == Action.RIGHT_CLICK_AIR && item.isSimilar(menuCompass)) openGUI(e.getPlayer());
     
  8. Offline

    Condolent

    @567legodude haha did exactly that basically the second after my post, realized I was just looking at it the wrong way haha ty anyway!
     
Thread Status:
Not open for further replies.

Share This Page