Context : I'm developping a plugin for Spigot (Minecraft server plugin).
I want to define several regions for multiple purposes. I store them in a config file like this (YAML) :
Regions
Region1:
P1:
X: 0
Y: 0
Z: 0
P2:
X: 1
Y: 1
Z: 1
Region2:
P1:
X: -1
Y: -1
Z: -1
P2:
X: 2
Y: 2
Z: 2
As you can see, I'm storing the regions with 2 opposite coordinates.
I trying to come up with an algorithm than can store in an array all the regions that contains given coordinates.
For example, (0,0,0)
=> [Region1, Region2]
and (2,2,2)
=> [Region1]
What I'm doing right now is :
- Population an array with all regions
- Checking if the X coordinate is between the 2 X coordinates defining the region
- If not, remove the region from the array. If true, then go to 2. and check the Z coordinates, then Y.
This solution seems viable for a few regions (not exceeding 20), but since this will be used in an event that can be triggered multiple times a second, I would like to be able to do this with a better solution which can handle more regions and do it faster.
I looked at Data structures that can map a range of values to a key?, but my regions can overlap, so I can't use it this way.
Do you have any idea ?
I don't want to use Worldedit / Worldguard API, but "universal" Java APIs are fine.