Make a per player based boolean?

Discussion in 'Plugin Development' started by Qwahchees, Feb 28, 2013.

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

    Qwahchees

    Hey guys

    I wanted to create a boolean that is only accessible per player. Currently I have a regular boolean and when triggered by an event, goes to true. Problem is, everyone on the server gets the true, I only want the player who triggered it to get the "true".

    How would I do this?
     
  2. Offline

    Tirelessly

    In order of efficiency/sense:
    Store their names in a set.
    Store their names in a list.
    Store them in a hashmap<string, boolean>.
    Make a custom player wrapper object.
     
  3. Offline

    Jeebiss

    Im no expert, but a couple ideas.

    1 - Have a seperate instance of the class for each player needed the boolean.
    2 - Keep a map<String, Boolean> to keep track. So you could do if (map.getValue(PlayerName)) .
     
  4. Offline

    danslayerx

    Edit: Already beaten to it.

    HashMap.

    HashMap<String, Boolean> PlayerBoolean = new HashMap<String, Boolean>();

    then...

    PlayerBoolean.put(player.getName(), true/false);
     
  5. Offline

    Qwahchees

    Ha, alright lemme try to do it. I'm not sure how :$
     
  6. Offline

    ZeusAllMighty11

    danslayerx

    Just for general housekeeping variables are like helloWorld not HelloWorld, which implies it's a class/object etc
     
  7. Offline

    Qwahchees

    Alright, how do I return the value? I have the HashMap setup (new to this) but now I need to check if the player's data is true or false
     
  8. Offline

    Rprrr

    Code:
    Boolean someBoolean = yourHashMap.get(player.getName());
     
  9. It would be easier to use a Set<String>:
    Code:
    Set<String> playerStuff = new HashSet<String>();
    
    playerStuff.add(player.getName()); // set to "true"
    
    boolean playerHasStuff = playerStuff.contains(player.getName());
    
    playerStuff.remove(player.getName()); // set to "false"
    Because with hashmap you'd have to also handle null value which comes when the key-value pair does not exist (or the value is actually null).
     
    jorisk322 likes this.
  10. Offline

    gomeow

    A Set would be best
     
  11. Offline

    Qwahchees

    I used set and it worked, problem now is I'm getting issues like:

    Code:
    17:25:10 [WARNING] Failed to handle packet for criminallol/86.155.50.90: java.ut
    il.NoSuchElementException
    java.util.NoSuchElementException
            at org.bukkit.craftbukkit.v1_4_R1.util.UnsafeList$Itr.next(UnsafeList.ja
    va:247)
            at net.minecraft.server.v1_4_R1.BlockPressurePlate.l(BlockPressurePlate.
    java:116)
            at net.minecraft.server.v1_4_R1.BlockPressurePlate.a(BlockPressurePlate.
    java:74)
            at net.minecraft.server.v1_4_R1.Entity.D(Entity.java:748)
            at net.minecraft.server.v1_4_R1.Entity.move(Entity.java:700)
            at net.minecraft.server.v1_4_R1.EntityLiving.e(EntityLiving.java:1065)
            at net.minecraft.server.v1_4_R1.EntityHuman.e(EntityHuman.java:1145)
            at net.minecraft.server.v1_4_R1.EntityLiving.c(EntityLiving.java:1306)
            at net.minecraft.server.v1_4_R1.EntityHuman.c(EntityHuman.java:306)
            at net.minecraft.server.v1_4_R1.EntityLiving.j_(EntityLiving.java:534)
            at net.minecraft.server.v1_4_R1.EntityHuman.j_(EntityHuman.java:155)
            at net.minecraft.server.v1_4_R1.EntityPlayer.g(EntityPlayer.java:194)
            at net.minecraft.server.v1_4_R1.PlayerConnection.a(PlayerConnection.java
    :352)
            at net.minecraft.server.v1_4_R1.Packet10Flying.handle(SourceFile:136)
            at net.minecraft.server.v1_4_R1.NetworkManager.b(NetworkManager.java:290
    )
            at net.minecraft.server.v1_4_R1.PlayerConnection.d(PlayerConnection.java
    :113)
            at net.minecraft.server.v1_4_R1.ServerConnection.b(SourceFile:39)
            at net.minecraft.server.v1_4_R1.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_4_R1.MinecraftServer.r(MinecraftServer.java:5
    98)
            at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:2
    24)
            at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:4
    94)
            at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java
    :427)
            at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:8
    49)
    17:25:34 [INFO] hi987[/24.128.131.7:61835] logged in with entity id 285 at ([dru
    neverse] 40.851251649873596, 66.0, 86.59190950811403)
    17:25:34 [INFO] [RoyalCommands] Updating the IP for hi987.
     
  12. Offline

    Tirelessly

    Might not be related to your plugin.
     
  13. Offline

    Qwahchees

    It is, I removed it and it never happened :/
     
  14. Qwahchees
    Coincidence maybe... do serval tests with it on/off and see if it happens again, otherwise it was just a packet error as the name suggests.
     
  15. Offline

    Qwahchees

    Alright, thanks Digi!
    I cleaned up the code and removed some redundant if statements, will that help?
     
  16. Offline

    Zach_1919

    Hey I am really new to HashMaps and HashSets so can you help me explain what to do? Can Qwahchees post the code for your hashset?
     
Thread Status:
Not open for further replies.

Share This Page