Need help getting PlayerData

Discussion in 'Plugin Development' started by DividedByZero, Aug 5, 2014.

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

    DividedByZero

    Code:
    package me.hiros.freezetag.Hub;
    
    import java.util.ArrayList;
    import java.util.UUID;
    
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;
    
    public class Hub {
    	private Plugin plugin;
    
    	public static ArrayList<PlayerData> hubplayers;
    
    	public Hub(Plugin plugin) {
    		this.plugin = plugin;
    		Hub.hubplayers = new ArrayList<PlayerData>();
    	}
    
    	public void addPlayer(Player player) {
    		hubplayers.add(new PlayerData(player));
    	}
    
    	public void removePlayer(Player player) {
    		PlayerData data = getPlayerData(player);
    		if(data == null) {
    			plugin.getLogger().severe("Could not find player in hub storage.");
    			return;
    		}
    		hubplayers.remove(data);
    	}
    
    	public PlayerData getPlayerData(Player player) {
    		for(PlayerData data : getHubPlayers()) {
    			if(data.getName().equalsIgnoreCase(player.getName())) return data;
    		}
    		return null;
    	}
    
    	public ArrayList<PlayerData> getHubPlayers() {
    		return hubplayers;
    	}
    }
    
    class PlayerData {
    	private String name;
    	private UUID playerid;
    	private ItemStack[] inv;
    	private ItemStack[] armor;
    	private Location loc;
    
    	public PlayerData(Player player) {
    		this.name = player.getName();
    		this.playerid = player.getUniqueId();
    		this.inv = player.getInventory().getContents();
    		this.armor = player.getInventory().getArmorContents();
    		this.loc = player.getLocation();
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public UUID getPlayerId() {
    		return playerid;
    	}
    
    	public ItemStack[] getInv() {
    		return inv;
    	}
    
    	public ItemStack[] getArmor() {
    		return armor;
    	}
    
    	public Location getLocation() {
    		return loc;
    	}
    }
    
    I have been having this problem for maybe a month now and I'm just now revisiting it. Basically the issue is when I call getPlayerData(player) it returns null idk why.
    It's like some weird bug. Also if you need my source to further answer my question it is at http://github.com/DividedByZeros/FreezeTag
    Could someone explain to me why this is not detecting whether the player is in the hub by the player object.
     
  2. Offline

    Gater12

    DividedByZero
    Add debug lines. Print out data.getName(). Actually see if there is anything in the hubplayers list.
     
  3. Offline

    DividedByZero

    They're is. Already tested it
     
  4. Offline

    Gater12

    DividedByZero
    Well you are returning null if there isn't any data for the Player.
     
  5. Offline

    Dragonphase

    DividedByZero

    Your entire plugin is set up weird. Every time you call getHub().getPlayerData(player), you're instantiating a new Hub. This in turn sets your static hubplayers array to a new ArrayList<PlayerData>();

    • Don't use statics.
    • Create one instance of Hub to store and retrieve the player data, instead of creating new instances of Hub every time you call getHub();
     
  6. Offline

    DividedByZero

    That's not the problem. Also when I do hub players.contains(PlayerData) is doesn't work it returns false event though they're a stuff in it that contains the right data
     
  7. Offline

    Dragonphase

    DividedByZero

    This is why your players.contains(PlayerData) method returns false. Your ArrayList is empty.
     
  8. Offline

    DividedByZero

    What would be the most efficient way to fix this issue do you think. I am at a loss

    Dragonphase how would you go about fixing this. Would u not store objects in arrays?

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

    Dragonphase

    DividedByZero

    Create this in your plugin's main class for example. Create a getter for that instance, and use that to store and retrieve the player data.
     
Thread Status:
Not open for further replies.

Share This Page