Solved Int for all players

Discussion in 'Plugin Development' started by bryansolis13, Jan 31, 2016.

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

    bryansolis13

    i need it so that the int will be different for all players
    Code:
    int pets = 0;
    ive tried some HashMaps but it didn't work since i need some if statements. Like
    Code:
    if(pets == 0){
       //do stuff
    }
    i also need it so you can set the int for that player
    Code:
    if(pets == 1){
        //do stuff
       pets = 0;
    }
     
  2. Offline

    Irantwomiles

    I think hashmap is what you're looking for. Can we see code you've tried
     
  3. Offline

    Zombie_Striker

    @bryansolis13
    Use Hashmaps. Store the player as a key and the integer as the value.
     
  4. Offline

    bryansolis13

    How could i do the if statements?
    like
    Code:
    if(pets == 0}{
      //do something
    }
     
  5. Offline

    blue1

    I'm curious about this too. I thought perhaps ArrayLists could be used, but that would cause alot of issues. Also you could use an externally editable text file, but that could be a pain too. I know little to nothing about Hash Maps.
     
  6. Offline

    teej107

    @bryansolis13 Perhaps looking at the JavaDocs for HashMap will help. Just get the Integer using the get() method by passing a Player object in the parameters. Make sure you remove the Player from the Map when they leave. You can do this by just removing the Player in the PlayerQuitEvent or use a WeakHashMap.
     
  7. Offline

    bryansolis13

    Removed
     
    Last edited: Feb 2, 2016
  8. Offline

    Zombie_Striker

    @bryansolis13
    The idea behind Hashmaps is that you link objects togher. By getting one object, you can get another. The following are the basics.
    Code:
    //Somewhere in the class (not in a method)
    HashMap<OBJECT_TYPE_TO_GET_VALUE , THE_VALUE_TYPE> hashmap = new HashMap<SAME KEY_TYPE, SAME_VALUE_TYPE>();// E.g. HashMap<Player, Integer>
    
    //somewhere within a method
    
    //To add an object to a hashmap.
    hashmap.put(Key, Value); //The value is the object you want to store, and the Key is what you use to get that object. E.g. hashmap.put("Zombie_Striker", "Is cool");
    
    //To get an object
    hashmap.get(Key); // This will return the Value associated with the key. E.g. hashmap.get("Zombie_Striker") will return "Is cool".
     
  9. Offline

    bryansolis13

    i kind of know what your trying to imply but i dont know how i would put this in my plugin. i am very new to making plugins btw
     
  10. Offline

    teej107

    As @Zombie_Striker said before, just create a Map using Player as the key and Integer for the value. When you want to get an player's int, just use the get method. Use the put method to associate a Player with an int.
     
  11. Offline

    bryansolis13

    Removed
     
    Last edited: Feb 2, 2016
  12. Offline

    mythbusterma

    @bryansolis13

    The stack trace says exactly what you need. A KeySet is not a Player, don't use it as one. You need to iterate over the KeySet to get the Players out of it, as it contains more than one Player.
     
  13. Offline

    bryansolis13

    @mythbusterma Im kind of new to java and dont know how i would do that?
     
    Last edited: Feb 1, 2016
  14. Offline

    teej107

    @bryansolis13 I suggest you get comfortable with Java before you try to use the Bukkit API. Writing Bukkit plugins requires a knowledge of Java.
     
  15. Offline

    bryansolis13

    @teej107 Sorry for the late reply. When i typed that i meant coding bukkit. I know java. The only things i havent done in java is hashmaps
     
  16. Offline

    Konato_K

    @bryansolis13 Then you should know you can't cast some stuff to other stuff unless they have a common base

    Don't treat a KeySet as a Player, use it as a KeySet and check it's documentation to know what you can do with it.
     
  17. Offline

    boomboompower

    @bryansolis13 Check the link on my signature for "Reading Stacktraces" might come in handy next time ;)
     
Thread Status:
Not open for further replies.

Share This Page