Solved How to give a Map 3 arguments?

Discussion in 'Plugin Development' started by kayc01, Nov 8, 2015.

Thread Status:
Not open for further replies.
  1. Hey quick question i hope, been doing some research but not found anything really.

    I have a list that is used for a cooldown time after entering a queue, however other queue's/lists seem to clash.
    This is because all queue's use the same task at the end of the cooldown which is to remove the player UUID from the "cooldownTime".

    So, i want to make the cooldown time specific. It needs to be an:

    Integer, UUID, Integer.
    The first Integer is a number between 1 and 32, this can have duplicates as other players will be in the same cooldown (for the same gym (gym1 to gym32)).

    The UUID is the user id that is in the cooldown for that specific gym.

    There won't be duplicates of this UUID in that certain number gym. But there might be in other first Integer lists. If that makes sense.

    The third Integer is a minute that is subtracted every 1200 L.

    The list i need to make have 3 arguments:

    Code:
    private Map<UUID, Integer> cooldownTime;
    The current onEnable for cooldownTime:

    Code:
    cooldownTime = new HashMap<UUID, Integer>();
    How it is currently used:


    Code:
    queues.get(gym).remove(0);
                                           
                                            settings.getLogs().set("Leaders." + p.getName() + "." + args[1]+ "." + playerLost.getName()+ ".[" +format.format(now) + "]", "Lost");
                                            settings.saveLogs();
                                           
                                            cooldownTime.put(po, 60);
                                            cooldownGym.get(gym).add(po);
                                            cooldownTask.put(po, new BukkitRunnable() {
                                               
                                                @Override
                                                public void run() {
                                                    cooldownTime.put(po, cooldownTime.get(po) - 1);
                                                    if (cooldownTime.get(po) == 0) {
                                                        cooldownTime.remove(po);
                                                        cooldownTask.remove(po);
                                                        PixelGym.get().cooldownGym.get(gym3).remove(po);
                                                        cancel();
    
                                                    }
                                                   
                                                }
                                               
                                            });
                                           
                                            cooldownTask.get(po).runTaskTimer(this, 20, 1200);
    Any easy way or simple corrections to make it have 3 arguments that would work for what i want?

    Remember,
    First int = possible number from 1-32
    2nd = UUID
    Third Int = Time left on cooldown.

    Thanks!
     
  2. Offline

    Scimiguy

    You could try using a Multimap

    But your best bet is easily to make your own Class for storage, or find another way to store it

    HashSet<CustomCooldown>

    class CustomCooldown {
    Integer something;
    UUID somethingElse;
    Integer evenMoreSomething;

    methodsForHandlingGetAndSet() {}
    }
     
  3. I get UUID and the first Integer from onCommand though.
    So i can put it in a separate method.

    Is there not some sort of combination for Map's and Lists that make this work?
    Ill also have a go at MultiMap
     
  4. Offline

    Scimiguy

    MultiMap would just be the same as making your own class, except less suited to your needs.

    Never worked with making your own class instances before?
     
  5. Never from onCommand that is not final.
    It would not be able to get args[1] etc... because it is not in the correct class.

    To my understanding. :3

    And i am guessing there is no easier way like just changing the Private Map<Integer, .... etc.. ?
     
  6. Offline

    Scimiguy

    \You could, but I'm not going to suggest it because it'd be ugly and poor-performing.

    You're seriously best off making your own storage class as far as I know.

    By that I mean you create an entirely new class in your plugin.
    Inside that you have the variable you need (See my post above)
    have your initialization method:
    Code:
    public someClass(Integer int1, UUID uuid, Integer int2) {
    something = int1;
    somethingElse = uuid;
    evenMoreSomething = int2;
    }
    Then you'd have get/set methods:

    Code:
    public UUID getUUID() {
    return somethingElse
    }
    Code:
    public setMinute(Integer minute) {
    int2 = minute;
    }
    etc...

    Depending on how you set it up, you can either keep a HashSet of your someClass's, or a HashMap<UUID, SomeClass>
     
  7. I know you are just trying to explain it here, but its not going in.
    And i need this urgently xD

    I don't see how a map/list with 3 arguments would be ugly or not work well.
    I know you are wanting me to do something else for learning, and thanks for it. I am just not in the mind set to get my head around something new when there is a easier option.

    Like for example, this does not give me error's in eclipse:

    Code:
    private Map<Integer, Map<UUID, Integer>> cooldownTime;
    Code:
    cooldownTime = new HashMap<Integer, Map<UUID, Integer>>();
    However i do not know how to set int, uuid, int.

    Code:
    cooldownTime.put(gym, po , 60);
    Says: The method put(Integer, Map<UUID,Integer>) in the type Map<Integer,Map<UUID,Integer>> is not applicable for the arguments (int, UUID, int)

    Only thing i can find that works is:
    Code:
    ooldownTime.get(gym3).put(po , 60);
    However i want to put gym3, not get it.
    Unless i need to set 32 HashMap's to get from in the onEnable.

    As well as something like:
    Code:
    cooldownTime.put(po, cooldownTime.get(po) - 1);
    Needs to be something like:
    cooldownTime.get(gym3).put(po, cooldownTime.get(gym3).get(po) - 1);

    (No error's on that one however i wanted to check if it sounded right?)

    EDIT:
    Confusing myself here but would this work?


    Code:
    cooldownTime = new HashMap<Integer, Map<UUID, Integer>>();
           
            for (int i = 1; i <= 32; i++) {
    
                cooldownTime.put(i, new HashMap<UUID, Integer>());
    
            }
    To add 32 int's, then UUID and time?
     
    Last edited: Nov 8, 2015
  8. Offline

    Scimiguy

    @kayc01
    That's because you're storing a map inside a map
    So you have to go new Map<UUID, Integer>()#put(UUID, Integer)
    Then Map<Integer, Map<UUID, Integer>>#put(newMap);

    Basically you have to put into a map, then put that map inside the other map.
     
  9. This seems to work:

    Code:
     private Map<Integer, Map<UUID, Integer>> cooldownTime;
    Code:
    cooldownTime = new HashMap<Integer, Map<UUID, Integer>>();
           
            for (int i = 1; i <= 32; i++) {
    
                cooldownTime.put(i, new HashMap<UUID, Integer>());
    
            }

    Code:
    cooldownTime.get(gym).put(po, 60);
    cooldownTime.get(gym3).put(po, cooldownTime.get(gym3).get(po) - 1);
                                                    if (cooldownTime.get(gym3).get(po) == 0) {
                                                        cooldownTime.get(gym3).remove(po);
    }
    Messing about pays off (I solved it before your last reply)

    If anyone needs, i can explain what it is doing.
     
  10. Offline

    Scimiguy

    @kayc01
    IF anyone needs, they should be creating their own storage class ;)

    Mark this thread as solved please (edit the title of the thread, add a solved marker)
     
Thread Status:
Not open for further replies.

Share This Page