Solved Get object using location (or other static data) as "pointer"

Discussion in 'Spigot Plugin Development' started by Nuubles, Oct 25, 2019.

  1. Let's say I have 5000 blocks on a map and the player clicks one. The blocks are stored in the code, with each one having a class that uses that specific block (for example one structure would be block -> object[with attributes block, string, int, long]). What would be the most efficient method (both memory and processor) of retrieving the correct object for that block?

    Thus far I've come up with the following solutions (if there's any other static value other than location that could also be used, blockstate/data is not static and may change values):

    List of key(location)-value(object) pairs:
    - long search time
    - duplicate key values

    Map<location, Map<location, Map<location, object>>>:
    + no duplicate values
    - needs to calculate hash 3 times with each use

    Map<String: "x,y,z", object>:
    + 1 hash calculation each time, thus fast retrieval
    - multiple unicode strings; may use more memory than other solutions

    The static key value has to remain the same between server restarts and needs to identify both the block in the world and the object in memory.
  2. As far as I am understanding, you’re mapping a single location to a single object. But that location can be mapped to multiple values.

    If MultiValueMap is already in the spigot.jar, you can use that. Otherwise, you can map the Location to a List of objects.
    • Informative Informative x 1
  3. I don't exactly understand why your second option has 3 nested maps. Why not create a class that contains the x, y, and z value as fields, make it implement equals and hashcode, and use it as key.
    • Agree Agree x 1
  4. Thanks, I'll try it that way!