Skip to content

RegionMap

Redempt edited this page Jul 12, 2020 · 1 revision

RegionMap is a utility class which is mainly used in internal parts of RedLib, such as the listener that calls RegionEnterEvent and RegionExitEvent, or the ProtectionPolicy listeners. Normally, it may make sense to register a listener for each region you want to listen for, or protect. Effectively, though, you are iterating over every region every time an event is called. This is inefficient, especially when there may be hundreds or thousands of these regions, and events such as PlayerMoveEvent or BlockBreakEvent may be sent hundreds of times per second even without many players being online. To optimize these listeners to work at scale, you use a RegionMap. This is a simple utility which allows you to map a Region or Location to any Object, and retrieve the Objects stored at or near a given Location later.

To start, you can instantiate it with either new RegionMap(), or new RegionMap(int scale). The former will simply set a scale of 100. The scale is the interval at which the RegionMap stores objects. A smaller scale is more efficient if there are many objects clustered close together, while a larger scale is more efficient if your objects occupy very large regions.

After being instantiated, you can use set(Region, T) with T being the parameterized type of the RegionMap. This will add the given object to an entire region. You can also use set(Location, T) to add it to a single location. Once set, you can retrieve these objects with get(Location) and getNearby(Location, int), and can remove them with remove(Region, T) or remove(Location, T).

Clone this wiki locally