How to retrieve a player from an ArrayList ~ doing this on behalf of a friend.

Discussion in 'Plugin Development' started by PumpMelon, Aug 21, 2016.

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

    PumpMelon

    Hey!
    My friend and I (I've tried helping him) have been having troubles after storing a player in an ArrayList but then getting the player out has been giving us issues.

    Code:
    package me.PumpMelon.punish;
    
    
    import java.util.ArrayList;
    import java.util.UUID;
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.ClickType;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin implements Listener{
       
        ArrayList<UUID> playername = new ArrayList<UUID>();
       
        Logger myPluginLogger = Bukkit.getLogger();
        @Override
        public void onEnable(){
         myPluginLogger.info("Plugin is starting up!");
        }
        @Override
        public void onDisable(){
            myPluginLogger.info("Plugin is shutting down!");
        }
       
        public Inventory inv = Bukkit.createInventory(null, 27, ChatColor.DARK_RED + "" + ChatColor.BOLD + "           Punishment" + ChatColor.DARK_GRAY + "" + ChatColor.BOLD + " Selector");{
       
       
        }           
        @EventHandler
        public void onClick(InventoryClickEvent e){   
            Inventory inventory = e.getInventory(); 
            ItemStack clicked = e.getCurrentItem(); 
            Player t = playername.get(t.getUniqueId()); //This is where we are lost at, we want to get the target's UUID from the playername list.
    
           
                if(inventory.getName().equals(inv.getName())){
                    if(clicked.getType() == Material.ICE){
                        t.setSprinting(false);
                        t.setWalkSpeed(0);
                        t.sendMessage(ChatColor.RED + "You have been frozen! Listen to staff!");
                }
            }
           
        }
        public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    
            if(Sender instanceof Player){
                Player player = (Player) Sender;
               
                if(commandLabel.equalsIgnoreCase("punish")){
                   
                        if(args.length > 1){
                            return false;
                        }
                            if(args.length == 1){
                                Player targetPlayer = Bukkit.getPlayer(args[0]);
                                UUID tUUID = targetPlayer.getUniqueId(); 
                                playername.add(tUUID);
                                player.openInventory(inv);
                            }
                }
               
            }
         return false;    
        }
            public ItemStack freeze = new ItemStack(Material.ICE);{
                ItemMeta fim = freeze.getItemMeta();
                fim.setDisplayName(ChatColor.BLUE + "Freeze");
                freeze.setItemMeta(fim);
                inv.setItem(10, freeze);
            }
            public ItemStack ban = new ItemStack(Material.BARRIER);{
                ItemMeta bim = ban.getItemMeta();
                bim.setDisplayName(ChatColor.RED + "Ban");
                ban.setItemMeta(bim);
                inv.setItem(12, ban);
            }
            public ItemStack kick = new ItemStack(Material.LEATHER_BOOTS);{
                ItemMeta kim = kick.getItemMeta();
                kim.setDisplayName(ChatColor.GREEN + "Kick");
                kick.setItemMeta(kim);
                inv.setItem(14, kick);
            }
            public ItemStack mute = new ItemStack(Material.IRON_FENCE);{
                ItemMeta mim = mute.getItemMeta();
                mim.setDisplayName(ChatColor.GRAY + "Mute");
                mute.setItemMeta(mim);
                inv.setItem(16, mute);
            }   
    }
    
    I know some of the naming conventions aren't right, again, my friend made majority of the code I helped edit it.
     
  2. Offline

    Zombie_Striker

    First, "get" requires an int, which represents the index of the item you want to get. This is not what you want. Even still, what are you even trying to do? You have the player's UUID. If you though "get" worked the same way it works for maps, then why are you trying to get a player's UUID by using his UUID? That just returns the same UUID you gave it.

    To convert a UUID to a player ,use Bukkit.getPlayer(UUID);

    [Edit] You can reference an object you are creating. This does not make any sense. It like saying "The person to your left is the person to your left"; It does not actually define what it is.

    Finally, e.getWhoClicked() returns the person who clicked the item. That is what you're looking for.
     
  3. Offline

    PumpMelon

    Hey, can you give me an example please? This is what I have...
    Code:
     @EventHandler
        public void onClick(InventoryClickEvent e){   
            Inventory inventory = e.getInventory(); 
            ItemStack clicked = e.getCurrentItem(); 
            Player player1 = (Player) e.getWhoClicked(); 
            Player t = playername.contains(Bukkit.getPlayer(UUID)); //This is where we are lost at, we want to get the target's UUID from the playername list.
    
           
                if(inventory.getName().equals(inv.getName())){
                    if(clicked.getType() == Material.ICE){
                        t.setSprinting(false);
                        t.setWalkSpeed(0);
                        t.sendMessage(ChatColor.RED + "You have been frozen! Listen to staff!");
                }
            }
           
        }
     
  4. Offline

    Zombie_Striker

    @PumpMelon
    First, I need to know what you're trying to achieve.
     
  5. Offline

    PumpMelon

    We are trying out create a /punish <player> and we want the player to be saved in the arraylist and then called upon after an item indicating the punishment has been clicked. Right now all we need is how to get that player back from the arraylist. The entire code is in the OP post.




    Sent from my iPhone using Tapatalk

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

    N00BHUN73R

    @PumpMelon
    PHP:
    Player t playername.contains(Bukkit.getPlayer(UUID));
    This will never work because you are basically saying, if playername contains a Player which it cant.
    What you need to do, is this:
    Code:
    Player t = Bukkit.getplayer(playername.get(whoclicked.getuuid));


    EDIT:
    Oops, I thought it was a HashMap lol.. Use Zombie_Striker's Answer
     
    Last edited: Aug 22, 2016
  7. Offline

    Zombie_Striker

    @N00BHUN73R @PumpMelon
    And even that can be simplified and fixed, since .get(*) requires an int, not an object, and you already have the UUID and the player instance. You can simplify it down to just:
    Code:
    Player t = (Player)event.getWhoClicked();
    If you want to make sure the array contains player, use
    Code:
    if(playername.contains(t.getUniqueId())){
     
Thread Status:
Not open for further replies.

Share This Page