How to give item?

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

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

    JifiGamingYT

    I don't know how to give an item to a player that's skull was clicked on. THis is what i mean by it:
    upload_2015-10-18_15-12-39.png
    It gets the online players in puts them in head form. how would i give an item to them? Here is my code:
    Code:
    import java.util.ArrayList;
    import java.util.Random;
    
    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.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.SkullMeta;
    
    public class Sponsor implements Listener {
    
        private int i = -1;
        private int j = 0;
        private Inventory inv;
        private Inventory inv0;
        private ArrayList<String> sp = new ArrayList<String>();
    
        @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;
        }
    
        @SuppressWarnings({ "deprecation" })
        @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;
                }
                Player skullOwner = Bukkit.getPlayer(((SkullMeta) item.getItemMeta()).getOwner());
    
                event.setCancelled(true);
    
                if (skullOwner != null) {
                    sp.add(skullOwner.getName());
                } else {
                    p.sendMessage(ChatColor.RED + "Player is null");
                }
                ItemStack spawnItem = name(Material.DIAMOND_SWORD, skullOwner.getName());
    
                this.inv0 = Bukkit.createInventory(null, 9, skullOwner.getName());
                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 = name(Material.WOOD_SWORD, "Weapon Sponsor");
    
                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;
                }
    
                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.sendMessage(ite + "");
                p.sendMessage(random.getType().name());
                Player owner = Bukkit.getPlayer(((SkullMeta) item.getItemMeta()).getOwner());
                SkullMeta skullMeta = (SkullMeta) item.getItemMeta();
                p.sendMessage(skullMeta.hasOwner() + "");
                owner.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);
        }
    }
    
     
  2. Where you have
    Code:
    Player skullOwner = Bukkit.getPlayer(((SkullMeta) item.getItemMeta()).getOwner());
    Just create your ItemStack
    Code:
    ItemStack item = new ItemStack(Material.WhatEver, amount, (byte) data);
    And give it to the player using .getInventory().addItem(...)
    Code:
    skullOwner.getInventory().addItem(item);
     
  3. Offline

    Zombie_Striker

    @JifiGamingYT
    All your fields have hard to understand names. Please revise the names.

    @FisheyLP posted the comment before I even scrolled down :p
     
  4. Offline

    JifiGamingYT

    I guess the title is misleading... I know how to give the item, but how do i give it to the player that heads was clicked on?
     
  5. Offline

    Zombie_Striker

    There are two ways I can take this:
    1. You want to give the player (who clicked the head) the head that was clicked on. Which is simply
      Code:
      ((Player)e.getWhoClicked()).getInventory().addItem/setItem
    2. Or, You want to give the Player (Who's head was clicked) the head that was clicked on (idk why, but The way you put that "clearification" was unclear.). You would do this by using
      Code:
      Bukkit.getServer().getPlayer((SkullMeta)ItemStack.getItemMeta).getOwner()).getInventory.addItem/SetItem
     
  6. You are already getting the the player whose head was clicked
     
  7. Offline

    JifiGamingYT

    There are errors.
    Code:
    - The method getPlayer(String) in the type Server is not applicable for the arguments
         (SkullMeta)
        - getItemMeta cannot be resolved or is not a field
     
  8. @JifiGamingYT Your using SkullMeta as the argument for getPlayer... It needs a string.
     
  9. Offline

    JifiGamingYT

    I'm confused. I'm not good at working with do this or do that because i don't how to do it and i don't know what you're saying.
     
  10. Offline

    Scimiguy

    @JifiGamingYT

    The error is saying that the method getPlayer() requires a String argument.
    You aren't giving it a String argument, you're giving it a SkullMeta argument.
     
  11. Offline

    JifiGamingYT

    how do i give it a string arg?
     
  12. Offline

    Scimiguy

  13. Offline

    JifiGamingYT

    what string tho?
     
  14. Offline

    mcdorli

    I think SkullMeta.getOwner().getName() or somethinf from this category
     
  15. Zombie_Striker and mcdorli like this.
  16. Offline

    JifiGamingYT

  17. Offline

    mcdorli

    Thanks, I don't have anything here to test it, and I really don't feel like opening the javadocs.
     
  18. Offline

    JifiGamingYT

    Where???
     
  19. Offline

    FabeGabeMC

    Just like you did right here:
    Code:
    (SkullMeta) item.getItemMeta()
     
  20. Offline

    JifiGamingYT

    Doesn't give me an item.
     
  21. Offline

    Zombie_Striker

    @JifiGamingYT
    I'm sorry, I forgot add an open bracket to the getPlayer(). Try the following
    .............I forgot this bracket...V
    Bukkit.getServer().getPlayer( ( (SkullMeta)ItemStack.getItemMeta).getOwner()).getInventory.addItem/SetItem
     
  22. Offline

    JifiGamingYT

    Dude it's the same thing that i have and it doesn't work. It just gets the head, not the name of the player. I need the name of the player.
     
  23. @Zombie_Striker That still wont work :p getItemMeta isn't a variable.

    @JifiGamingYT You should be able to fix this with a little Java knowledge. Pretty sure Eclipse will even tell you what needs to be added.
     
  24. Offline

    Zombie_Striker

    @JifiGamingYT
    If you just need the name, then use the following:
    ( (SkullMeta)ItemStack.getItemMeta).getOwner()

    @bwfcwalshy You spotted my noob trap!
     
  25. Offline

    JifiGamingYT

    I mean i want to give the players head that was clicked on an item.
     
  26. Offline

    Zombie_Striker

    @JifiGamingYT
    Code:
    event.getCurrnetItem()
    This returns the head that was clicked.
    Code:
    targetPlayer.addItem/setItem(event.getCurrentItem())
    This adds the item (in this case a head) to the targetPlayer's inventory (this would be who you would want to give it to)
     
  27. Offline

    JifiGamingYT

    You clearly don't see what i'm saying and you clearly don't look at my code. I want to give a random item to the person whose head was clicked on. Right now when i click the thing that's supposed to give that person the item, it doesn't do anything. That's what i need help on.
     
  28. Offline

    Scimiguy

    @JifiGamingYT

    Its not our job to read your code, we do that if we need to/want to.
    It is your job to explain it to us
    If someone cant understand what youre saying, then it is actually your fault for not explaining it well enough

    If there is no errors, then do yourself a favor and do system.out.println debug messages for the random.getType() and see what your random is at each stage
     
    Chloe-chan and DoggyCodeâ„¢ like this.
  29. Offline

    Chloe-chan

    I agree. He is too dependant on others for his own work.
     
Thread Status:
Not open for further replies.

Share This Page