forked from SteamWar/SteamWar
Add SpawnPathRegion
Add SpawnRegion
This commit is contained in:
+48
-11
@@ -20,30 +20,48 @@
|
||||
package de.steamwar.bausystem.region;
|
||||
|
||||
import de.steamwar.bausystem.BauSystem;
|
||||
import de.steamwar.bausystem.region.dynamic.DynamicRegion;
|
||||
import de.steamwar.bausystem.region.dynamic.MovementListener;
|
||||
import de.steamwar.bausystem.region.dynamic.Tile;
|
||||
import de.steamwar.bausystem.region.dynamic.RegionDataRepository;
|
||||
import de.steamwar.bausystem.region.dynamic.global.GlobalRegion;
|
||||
import de.steamwar.bausystem.region.dynamic.spawn.SpawnPathRegion;
|
||||
import de.steamwar.bausystem.region.dynamic.spawn.SpawnRegion;
|
||||
import lombok.NonNull;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class DynamicRegionSystem implements RegionSystem {
|
||||
|
||||
public static DynamicRegionSystem INSTANCE;
|
||||
private static final World WORLD = Bukkit.getWorlds().get(0);
|
||||
|
||||
private static Map<Tile, UUID> tileMap = new HashMap<>();
|
||||
private static Map<UUID, Region> regionMap = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
INSTANCE = this;
|
||||
RegionDataRepository.loadRegions();
|
||||
Bukkit.getPluginManager().registerEvents(new MovementListener(), BauSystem.getInstance());
|
||||
|
||||
if (regionMap.isEmpty()) {
|
||||
new SpawnRegion(-9, -9);
|
||||
new SpawnPathRegion(-9, -27);
|
||||
new SpawnPathRegion(-9, 10);
|
||||
new SpawnPathRegion(-27, -9);
|
||||
new SpawnPathRegion(10, -9);
|
||||
}
|
||||
}
|
||||
|
||||
public void add(DynamicRegion region) {
|
||||
regionMap.put(region.getID(), region);
|
||||
}
|
||||
|
||||
public void delete(DynamicRegion region) {
|
||||
regionMap.remove(region.getID());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -62,11 +80,10 @@ public class DynamicRegionSystem implements RegionSystem {
|
||||
|
||||
@Override
|
||||
public @NonNull Region get(@NonNull Location location) {
|
||||
Optional<Tile> tile = Tile.fromLocation(location);
|
||||
if (tile.isEmpty()) return GlobalRegion.INSTANCE;
|
||||
UUID uuid = tileMap.get(tile.get());
|
||||
if (uuid == null) return GlobalRegion.INSTANCE;
|
||||
return regionMap.getOrDefault(uuid, GlobalRegion.INSTANCE);
|
||||
return regionMap.values().stream()
|
||||
.filter(region -> region.getArea().inRegion(location, false))
|
||||
.findFirst()
|
||||
.orElse(GlobalRegion.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -79,6 +96,26 @@ public class DynamicRegionSystem implements RegionSystem {
|
||||
return regionMap.values().stream();
|
||||
}
|
||||
|
||||
public Stream<Region> getNeighbours(Region region) {
|
||||
Point minPoint = region.getArea().getMinPoint(false).subtract(19, 0, 19);
|
||||
Point maxPoint = region.getArea().getMaxPoint(false).add(19, 0, 19);
|
||||
Set<Region> neighbours = new HashSet<>();
|
||||
for (int x = minPoint.getX(); x <= maxPoint.getX(); x += 19) {
|
||||
int minZ = minPoint.getZ();
|
||||
int maxZ = maxPoint.getZ();
|
||||
neighbours.add(get(new Location(WORLD, x, 0, minZ)));
|
||||
neighbours.add(get(new Location(WORLD, x, 0, maxZ)));
|
||||
}
|
||||
for (int z = minPoint.getZ() + 19; z <= maxPoint.getZ() - 19; z += 19) {
|
||||
int minX = minPoint.getX();
|
||||
int maxX = maxPoint.getX();
|
||||
neighbours.add(get(new Location(WORLD, minX, 0, z)));
|
||||
neighbours.add(get(new Location(WORLD, maxX, 0, z)));
|
||||
}
|
||||
neighbours.remove(GlobalRegion.INSTANCE);
|
||||
return neighbours.stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isModular() {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user