add player to list, oop.

Discussion in 'Plugin Development' started by DevManABCD, Aug 27, 2014.

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

    DevManABCD

    Hey i want to create arenamanager, and i had problem in add player to arraylist in arena.

    I'm using that function:

    Code:java
    1. private ArrayList<Player> players;
    2.  
    3. public void AddPlayer(Player p){
    4. players.add(p);
    5. }



    then i add with this:

    Arena a = ArenaManager.getArena(1);
    a.AddPlayer(e.getPlayer());

    And im sure that arena exists :)

    But when i wan to add this player finaly:

    Code:
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[Craftbukkit.jar:git-Craftbukkit-1387]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Craftbukkit.jar:git-Craftbukkit-1387]
        at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredListener.java:30) ~[Craftbukkit.jar:git-Craftbukkit-1387]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [Craftbukkit.jar:git-Craftbukkit-1387]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [Craftbukkit.jar:git-Craftbukkit-1387]
        at net.minecraft.server.v1_7_R2.PlayerList.c(PlayerList.java:249) [Craftbukkit.jar:git-Craftbukkit-1387]
        at net.minecraft.server.v1_7_R2.PlayerList.a(PlayerList.java:135) [Craftbukkit.jar:git-Craftbukkit-1387]
        at net.minecraft.server.v1_7_R2.LoginListener.c(LoginListener.java:97) [Craftbukkit.jar:git-Craftbukkit-1387]
        at net.minecraft.server.v1_7_R2.LoginListener.a(LoginListener.java:42) [Craftbukkit.jar:git-Craftbukkit-1387]
        at net.minecraft.server.v1_7_R2.NetworkManager.a(NetworkManager.java:151) [Craftbukkit.jar:git-Craftbukkit-1387]
        at net.minecraft.server.v1_7_R2.ServerConnection.c(ServerConnection.java:77) [Craftbukkit.jar:git-Craftbukkit-1387]
        at net.minecraft.server.v1_7_R2.MinecraftServer.v(MinecraftServer.java:705) [Craftbukkit.jar:git-Craftbukkit-1387]
        at net.minecraft.server.v1_7_R2.DedicatedServer.v(DedicatedServer.java:273) [Craftbukkit.jar:git-Craftbukkit-1387]
        at net.minecraft.server.v1_7_R2.MinecraftServer.u(MinecraftServer.java:568) [Craftbukkit.jar:git-Craftbukkit-1387]
        at net.minecraft.server.v1_7_R2.MinecraftServer.run(MinecraftServer.java:474) [Craftbukkit.jar:git-Craftbukkit-1387]
        at net.minecraft.server.v1_7_R2.ThreadServerApplication.run(SourceFile:618) [Craftbukkit.jar:git-Craftbukkit-1387]
    Caused by: java.lang.NullPointerException
        at pl.diverse.mercenaryDeathmatch.Managers.Arena.AddPlayer(Arena.java:28) ~[?:?]
        at pl.diverse.mercenaryDeathmatch.Listeners.Join.onJoin(Join.java:17) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_51]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_51]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_51]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[Craftbukkit.jar:git-Craftbukkit-1387]
        ... 15 more
    I dont know what is bad ;/ Can someone help me?
     
  2. Offline

    teej107

    You need to initialize your ArrayList. I hope you continue learning Java first as it will make these problems more easy to avoid and solve.
     
  3. Offline

    DevManABCD

    human learns from his mistakes

    Can yyou help me with this? I'm really fresh ;/
     
  4. Offline

    fireblast709

    DevManABCD like teej107 said, you need to initialize your ArrayList. If you are incapable of doing so, then you are currently incapable of creating a Bukkit plugin.
     
  5. Offline

    DevManABCD

    I did mistake in loading configurationsection to arena, sorry for problem.
     
  6. Offline

    ProStriker123

    Try this its could help you very well this is my method

    public List<String> redteam = new ArrayList<String>();
    if(label.equalsIgnoreCase("red")) {
    if(redteam.contains(p.getName())) {
    redteam.add(p.getName());
    p.sendMessage("You have joined the red Team");
    }

    }
     
  7. Offline

    TheMintyMate

    DevManABCD
    ProStriker123
    Due to current changes in how players are identified, it is recommended that you save the UUID of the
    player instead of the players name. Generally speaking, this is only necessary for when identifying a individual
    player more than once, but it is good practise.
    Try something along these lines:
    Code:
    /* You may want to change the first "String" to "Long" for the capturing of player's UUID
    public HashMap<String,String> redTeam = new HashMap<>();
    if(/*player opts for red team*/){
    if(/*player not in team - not in HashMap*/){
    /* if you setup HashMap as <String,String> use method 1, if you setup as <Long,String> use method 2*/
    //method 1
    redTeam.put(player.getUniqueId().toString(),player.getName());
    //method 2
    redTeam.put(player.getUniquId(),player.getName());
    /*only use one of the methods*/
    }
    }
    
    Note 1: This code is untested and may contain errors/bugs
    Note 2: In this code example I saved players name alongside UUID

    Hope this helped!

    - Minty
     
  8. Offline

    fireblast709

    TheMintyMate Assuming he removes them on PlayerQuitEvent, it is perfectly fine to use names. They are perfectly fine for session storage, and you only need to go to UUIDs if you want to extend beyond the session (not removing onQuit, or saving data)
     
    DevManABCD likes this.
  9. Offline

    TheMintyMate

    Please note what I said in my post:
    " but it is good practise."
     
Thread Status:
Not open for further replies.

Share This Page