Detect online player when his/her head is clicked on.

Discussion in 'Plugin Development' started by JifiGamingYT, Oct 11, 2015.

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

    JifiGamingYT

    Spigot is down right now, so i guess i'll have to post here.
    I made a GUI, and i want to sponsor them something. How do i make it detect from when i click the head to when i'm finished?
    Here is my code:
    Code:
    private int i = -1;
        private int j = 0;
        private Inventory inv;
        private Inventory inv0;
    
        @EventHandler
        public void on(PlayerInteractEvent event) {
            if (!(event.getItem().getType() == Material.COMPASS)) {
                return;
            }
            if (!(event.getAction() == Action.RIGHT_CLICK_AIR) && !(event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                return;
            }
    
            this.inv = Bukkit.createInventory(null, 54, "Tributes left");
            Player p = event.getPlayer();
    
            for (Player plz : Bukkit.getOnlinePlayers()) {
                if (this.i < this.j) {
                    this.i++;
                    this.j++;
                }
                this.inv.setItem(this.i, owner(plz.getName()));
            }
    
            p.openInventory(this.inv);
            this.i = -1;
            this.j = 0;
        }
    
        @EventHandler
        public void on(InventoryClickEvent event) {
            if (!(event.getWhoClicked() instanceof Player)) {
                return;
            }
            Player p = (Player) event.getWhoClicked();
            ItemStack item = event.getCurrentItem();
    
            if (item.getType() == Material.SKULL_ITEM) {
                if (!(event.getInventory().equals(this.inv))) {
                    return;
                }
                event.setCancelled(true);
                ItemStack spawnItem = name(Material.DIAMOND_SWORD, "Sponsor");
    
                this.inv0 = Bukkit.createInventory(null, 9, "Player Choices");
                this.inv0.setItem(0, spawnItem);
    
                p.closeInventory();
                p.openInventory(this.inv0);
            }
    
            if (item.getType() == Material.DIAMOND_SWORD) {
                if (!(event.getInventory().equals(this.inv0))) {
                    return;
                }
    
                event.setCancelled(true);
                ItemStack spawnItem = new ItemStack(Material.WOOD_SWORD);
    
                this.inv0 = Bukkit.createInventory(null, 9, "Sponsor");
                this.inv0.setItem(0, spawnItem);
    
                p.closeInventory();
                p.openInventory(this.inv0);
            }
    
            if (item.getType() == Material.WOOD_SWORD) {
                if (!(event.getInventory().equals(this.inv0))) {
                    return;
                }
    
                event.setCancelled(true);
    
                ItemStack random = null;
                Random r = new Random();
                int ite = r.nextInt(3);
                switch (ite) {
                case 0:
                    random = new ItemStack(Material.WOOD_AXE);
                    break;
                case 1:
                    random = new ItemStack(Material.WOOD_SWORD);
                    break;
                case 2:
                    random = new ItemStack(Material.GOLD_AXE);
                    break;
                case 3:
                    random = new ItemStack(Material.GOLD_SWORD);
                    break;
                }
                p.closeInventory();
                p.getInventory().addItem(random);
            }
        }
    
        private ItemStack owner(String owner) {
            ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
            SkullMeta meta = (SkullMeta) item.getItemMeta();
            meta.setOwner(owner);
            item.setItemMeta(meta);
            return item;
        }
    
        private ItemStack name(ItemStack item, String name) {
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(name);
            item.setItemMeta(meta);
            return item;
        }
    
        private ItemStack name(Material item, String name) {
            return name(new ItemStack(item), name);
        }
    }
    
    Can some1 like @timtower help me with this?
     
    Last edited: Oct 11, 2015
  2. Offline

    ark9026

    @JifiGamingYT
    Perhaps make an invisible entity floating around every players head, and check if they hit that?
     
  3. Offline

    JifiGamingYT

    It's actaully in a GUI, sorry for the misunderstanding. I right click a compass, it's pops up with all the online players heads, and when i click one. You can do things like tp, give items, etc.
     
  4. Offline

    Zombie_Striker

    It saddens me that you view this place as as being something less than the spigot forums.

    If it's in an inventory, just listen for the InventoryClickEvent, check if the Inventory has the same atributes to the one the head is in. If it is, check if the item clicked is the one you are looking for and cancel the event.
     
  5. Offline

    JifiGamingYT

    @Zombie_Striker
    Two things:
    1. I use spigot because people respond quicker to my post then here(Most of the time)
    2. How would i get the player because it's from another event.
     
  6. Offline

    DoggyCode™

    Don't randomly tag.
     
  7. Offline

    Zombie_Striker

    @JifiGamingYT
    You would need to store the the instance of that player someplace that is accessible to that class.

    @DoggyCode™
    he wasn't.
     
  8. Offline

    JifiGamingYT

  9. Offline

    Zombie_Striker

    @JifiGamingYT
    This is basic. Create a field that stores the player information as need. If it has to be ONE specific player , then you can use a simple one object field of Player. If you nmeed multiple players being stored, look ingot hashmaps.
     
Thread Status:
Not open for further replies.

Share This Page