SkullMeta + Inventory help

Discussion in 'Plugin Development' started by Benwager12, Jan 2, 2016.

Thread Status:
Not open for further replies.
  1. I'm making a plugin for a friend, he gave me specifics on what to do, And I have been making it, but then I run into what I see as a brick wall, the situation is that you have a inventory and when you click on the player skulls (All the players in the game) It sends a teleport request to them, I have 2 classes, this is my code:
    Main class:
    Code:
    package com.benwager12.tprequests;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
       
        public void onEnable() {
            registerCommands();
        }
    
        public void onDisable() {
           
        }
       
        public void registerCommands() {
            setcmd("tpr");
        }
       
        public void setcmd(String cmd) {
            getCommand(cmd).setExecutor(new Commands(this));
        }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("TPREQUESTS")) {
                return;
            }
                Player p = (Player) e.getWhoClicked();
                e.setCancelled(true);
               
                if (e.getCurrentItem() == null || e.getCurrentItem().getType()==Material.AIR||!e.getCurrentItem().hasItemMeta()) {
                    p.closeInventory();
                    return;
                }
               
                switch (e.getCurrentItem().getType()) {
                case SKULL_ITEM:
                    //Wait what???!
                }
        }
    }
    
    Command class:
    Code:
    package com.benwager12.tprequests;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.SkullMeta;
    
    public class Commands implements CommandExecutor {
    
        String errorMessage = ChatColor.DARK_RED + "" + ChatColor.BOLD + "ERROR: " + ChatColor.RED;
       
        private Main main;
        public Commands(Main main) {
            this.main = main;
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("tpr")) {
                if (sender instanceof Player) {
                    Player p = (Player) sender;
                    if (p.hasPermission("tpr.gui")) {
                        if (args.length == 0) {
                            Inventory gui = Bukkit.createInventory(null, 9*6, ChatColor.RED + "TPREQUESTS");
                            for (Player online : Bukkit.getOnlinePlayers()) {
                                ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1);
                                String name = online.getName();
                                SkullMeta meta = (SkullMeta) skull.getItemMeta();
                           
                                meta.setOwner(name);
                                meta.setDisplayName(ChatColor.WHITE + "send " + ChatColor.YELLOW + online.getName() + ChatColor.WHITE + " a tprequest");
                                skull.setItemMeta(meta);
                                gui.addItem(skull);
                                //Bukkit.getServer().dispatchCommand(p, "/tpr " + )
                            }
                        } else if (args.length >= 0) {
                                   
                            }
                       
                    } else {
                        p.sendMessage(errorMessage + "You do not have permission to execute this command.");
                    }
                } else {
                    sender.sendMessage(errorMessage + "Only players can do this command.");
                }
            }
            return false;
        }
    
    }
    
     
  2. Offline

    Gorbit99

    Aaaaand... what's your problem?
    Is it giving you errors?
    If you want to get a skull's owner and stuff, you need to check if it's a skull first, then cast it to a "skullitem", then you can get the Owner with getOwner();
     
  3. Thank you, I'll try that now!

    Where would I cast it? Sorry, I'm kinda new to bukkit @Gorbit99

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  4. Offline

    elian1203

    Well first you need to check if it is a skull item, I don't know why you're using switch. Then cast it by doing
    SkullMeta skullMeta = (SkullMeta) e.getCurrentItem(); You may have to check if it's a player skull also.

    Edit:
    Sorry did that wrong, SkullMeta skullMeta = (SkullMeta) e.getCurrentItem().getItemMeta();
     
Thread Status:
Not open for further replies.

Share This Page