Getting a number from an index in an Arraylist

Discussion in 'Plugin Development' started by DotDash, Apr 21, 2014.

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

    DotDash

    I can't figure out how to get a number from an Arraylist that contains more than that number.

    For Example:
    Code:java
    1. package me.dragona397.hclives;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerJoinEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class HCLives extends JavaPlugin implements Listener, CommandExecutor {
    15.  
    16. public Logger log = Logger.getLogger("Minecraft");
    17.  
    18. public void onEnable() {
    19.  
    20. }
    21.  
    22. public void onDisable() {
    23.  
    24. }
    25.  
    26. ArrayList<String> lifecredit = new ArrayList<String>();
    27.  
    28. @EventHandler
    29. public void onPlayerJoin(PlayerJoinEvent e) {
    30. if(!e.getPlayer().hasPlayedBefore()) {
    31. Player p = e.getPlayer();
    32. lifecredit.add( p + " " + 1);
    33. p.sendMessage(ChatColor.RED + "Welcome, You have ");
    34. }
    35. }
    36.  
    37. }
    38.  


    Any help would gladly be accepted.
    Also Please notify me if this is actually simple to do. :D
     
  2. Offline

    beeselmane

    I would create a map of players to credits, and access it that way:

    Code:java
    1. Map<Player, Integer> lifecredit = new HashMap<Player, Integer>();
    2.  
    3. @EventHandler
    4. public void onPlayerJoin(PlayerJoinEvent e)
    5. {
    6. Player p = e.getPlayer();
    7.  
    8. if (!p.hasPlayedBefore())
    9. {
    10. lifecredit.set(p, 1);
    11. }
    12.  
    13. p.sendMessage(ChatColor.RED + "Welcome, you have " + life credit.get(p));
    14. }


    (I'm assuming you would be somehow storing life credits for people, and accessing it by their player object :p)
     
  3. Offline

    amhokies

    DotDash
    What are you trying to do exactly?
     
  4. Offline

    Musician101

    I would recommend using a Map to store a player's UUID and how many credits they have.
     
  5. Offline

    DotDash

  6. Offline

    Musician101

    DotDash The code below is like beeselmane's, however, storing Player in Lists and Maps are somewhat frowned upon. This code also preps your plugin for the inevitable implementation of UUIDs.
    Code:java
    1. Map<String, Integer> lifecredit = new HashMap<String, Integer>();
    2.  
    3. @EventHandler
    4. public void onPlayerJoin(PlayerJoinEvent e)
    5. {
    6. Player p = e.getPlayer();
    7.  
    8. if (!p.hasPlayedBefore())
    9. {
    10. lifecredit.set(p.getUniqueId().toString(), 1);
    11. }
    12.  
    13. p.sendMessage(ChatColor.RED + "Welcome, you have " + life credit.get(p.getUniqueId().toString()));
    14. }
     
  7. Offline

    DotDash

    Musician101 Thanks that helped a lot! :D

    Musician101 Will this be able to store every players credit though?

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

    beeselmane

    yes, you just add a new key(Player's name) with their credits:
    Code:java
    1. lifecredit.set(player.getName(), <credit number>);

    and get it with:
    Code:java
    1. lifecredit.get(player.getName());
     
  9. Offline

    DotDash

    beeselmane .add gives me an error message:


    The method add(String, int) is undefined for the type Map<String,Integer>
     
  10. Offline

    beeselmane

    DotDash oh sorry, I was thinking about array list :p fixed in the post above
     
  11. Offline

    DotDash

    beeselmane Sorry for the work, but I have quit the project for other things. Sorry :(
     
  12. Offline

    Musician101

    beeselmane Player names won't be constant when 1.8 comes out. That's why I suggested UUIDs
     
  13. Offline

    beeselmane

    Musician101 oh yeah, sorry didn't see that DotDash you should use player.getUniqueId() like he said instead of player.getName() like I did
     
  14. Offline

    DotDash

    .set doesn't work either :eek:

    would .put work instead?
     
Thread Status:
Not open for further replies.

Share This Page