#Single key-value pair map with dynamic key

14 messages · Page 1 of 1 (latest)

misty vessel
#

Simply put, I want a "map" of a string and an integer, but I want it to only ever have 1 set of a string and an integer, and I want the string (key) to also be replaceable with a new one.
Essentially:

Map<String, Integer> myMap = new HashMap<>();
myMap.put("minutes", 2);
... // myMap has only the minutes key and a value of 2.
myMap.put("hours", 5);
... // myMap now obviously has both the minutes and hours key, but I want only one

The reason I want this is so that I can later just so something like this:

String stringifiedMap = (myMap.key) + myMap.value.toString();

Which isn't possible with a normal map.

steel viperBOT
#

This post has been reserved for your question.

Hey @misty vessel! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

remote charm
misty vessel
#

I just had a realization during my own explanation and figured out how to solve it
you were right in being confused, I'm just being dumb about it...

remote charm
#

i am just going to ask anyway. Where are you planning on implementing this map and what were you confused about ?

misty vessel
#

Well it's a bit embarrassing but I shall satisfy your curiosity haha
Basically I wanted to implement a cooldown system for a Minecraft plugin. I had to store the "cooldown" persistently, and chose to store is as NBT data on the player itself.
Issue is, NBT data only takes a "PersistentDataType", and they are very limited. The plan was literally just to convert my time "type" (minute/hour/whatever) and my "amount" (int) into a single string, then store that in the NBT, and then just split and convert them later to use.
But I realized I can just store a long for milliseconds, then add that together with a new Date().getTime(), and store that long as NBT. Later I can then check the NBT to see if the current time is bigger, and if it is that means enough time has passed and the cooldown duration is over.

#

@remote charm pinged cus you seemed curious about it lol

remote charm
misty vessel
#

only on events
in my case the player has a command to get a "kit", which is just a bunch of items that I have predefined
but I obviously don't want them to get infinite items, hence the cooldown
but it only checks if the player has passed the cooldown or not once they try to claim the kit

remote charm
lucid turtle
old harbor
#

well, Map.Entry exists

#

you don't need to make your own structure, if that's what you ended up with