Start rebuilt

This commit is contained in:
2025-12-18 18:09:15 +01:00
parent 6d1b969af5
commit 3062621233
43 changed files with 415 additions and 74 deletions
@@ -19,22 +19,13 @@
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.RegionDataRepository;
import de.steamwar.bausystem.region.dynamic.global.GlobalRegion;
import de.steamwar.bausystem.region.dynamic.path.PathRegion;
import de.steamwar.bausystem.region.dynamic.spawn.SpawnPathRegion;
import de.steamwar.bausystem.region.dynamic.spawn.SpawnRegion;
import de.steamwar.bausystem.region.dynamic.spawn.SpawnResetter;
import de.steamwar.bausystem.region.dynamic.Tile;
import lombok.NonNull;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DynamicRegionSystem implements RegionSystem {
@@ -42,67 +33,55 @@ public class DynamicRegionSystem implements RegionSystem {
public static DynamicRegionSystem INSTANCE;
private static final World WORLD = Bukkit.getWorlds().get(0);
private static Map<UUID, Region> regionMap = new HashMap<>();
private static final Map<Long, Region> regionCache = new LinkedHashMap<>(16, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<Long, Region> eldest) {
return size() > 8192; // Tweak this number if needed!
}
}; // Will be cleared on region add/delete/remove!
private static final Map<UUID, Region> regionMap = new HashMap<>();
private static final Map<RegionType, Set<Region>> regionTypeMap = new EnumMap<>(RegionType.class);
@Override
public void load() {
INSTANCE = this;
new DynamicRegionCommand();
RegionDataRepository.loadRegions();
Bukkit.getPluginManager().registerEvents(new MovementListener(), BauSystem.getInstance());
if (regionMap.isEmpty()) { // TODO: Implement this in default region!
new SpawnRegion(-9, -9);
new SpawnPathRegion(-9, -28);
new SpawnPathRegion(-9, 10);
new SpawnPathRegion(-28, -9);
new SpawnPathRegion(10, -9);
new PathRegion(-28, -28);
new PathRegion(-28, 10);
new PathRegion(10, -28);
new PathRegion(10, 10);
}
}
public void add(DynamicRegion region) {
regionMap.put(region.getID(), region);
}
public void delete(DynamicRegion region) {
regionMap.remove(region.getID());
}
@Override
public void save() {
}
@Override
public @NonNull Location getWorldSpawn() {
if (SpawnResetter.isBigSpawn()) {
return SpawnResetter.BIG_WORLD_SPAWN;
} else {
return SpawnResetter.SMALL_WORLD_SPAWN;
}
return null;
}
@Override
public @NonNull Region getGlobalRegion() {
return GlobalRegion.INSTANCE;
return null;
}
// TODO: Optimize later on!
private Region get(Location location, Collection<Region> regions) {
return regions.stream()
.filter(region -> region.getArea().inRegion(location, false))
private Region get(int x, int z, boolean fastCache, Collection<Region> regions) {
Tile tile = Tile.fromXZ(x, z).orElse(null);
if (tile == null) {
return getGlobalRegion();
}
if (regionCache.containsKey(tile.getId())) {
Region region = regionCache.get(tile.getId());
if (fastCache || regions.contains(region)) return region;
}
Region region = regions.stream()
.filter(rg -> rg.getArea().inRegion(x, z, false))
.findFirst()
.orElse(GlobalRegion.INSTANCE);
.orElseGet(this::getGlobalRegion);
regionCache.put(tile.getId(), region);
return region;
}
@Override
public @NonNull Region get(@NonNull Location location) {
return get(location, regionMap.values());
return get(location.getBlockX(), location.getBlockZ(), true, regionMap.values());
}
@Override
@@ -115,44 +94,46 @@ public class DynamicRegionSystem implements RegionSystem {
return regionMap.values().stream();
}
private Stream<DynamicRegion> getNeighbours(Region region, boolean noCorners, Collection<Region> regions) {
private Stream<Region> getNeighbours(Region region, boolean noCorners, boolean fastCache, Collection<Region> regions) {
Point minPoint = region.getArea().getMinPoint(false).subtract(18, 0, 18);
Point maxPoint = region.getArea().getMaxPoint(false).add(19, 0, 19);
Set<Region> neighbours = new HashSet<>();
for (int x = minPoint.getX() + (noCorners ? 18 : 0); x <= maxPoint.getX() - (noCorners ? 19 : 0); x += 19) {
int minZ = minPoint.getZ();
int maxZ = maxPoint.getZ();
neighbours.add(get(new Location(WORLD, x, 0, minZ), regions));
neighbours.add(get(new Location(WORLD, x, 0, maxZ), regions));
neighbours.add(get(x, minZ, fastCache, regions));
neighbours.add(get(x, maxZ, fastCache, regions));
}
for (int z = minPoint.getZ() + 18; z <= maxPoint.getZ() - 19; z += 19) {
int minX = minPoint.getX();
int maxX = maxPoint.getX();
neighbours.add(get(new Location(WORLD, minX, 0, z), regions));
neighbours.add(get(new Location(WORLD, maxX, 0, z), regions));
neighbours.add(get(minX, z, fastCache, regions));
neighbours.add(get(maxX, z, fastCache, regions));
}
neighbours.remove(GlobalRegion.INSTANCE);
return ((Set<DynamicRegion>) (Set) neighbours).stream();
neighbours.remove(getGlobalRegion());
return neighbours.stream();
}
public Stream<DynamicRegion> getNeighbours(Region region) {
return getNeighbours(region, false, regionMap.values());
public Stream<Region> getNeighbours(Region region) {
return getNeighbours(region, false, true, regionMap.values());
}
public Stream<DynamicRegion> getConnectedRegions(DynamicRegion region) {
Set<Region> regions = regionMap.values()
.stream()
.filter(r -> r.getType() == region.getType())
.collect(Collectors.toSet());
public Stream<Region> getConnectedRegions(Region region) {
Set<Region> regions = regionTypeMap.get(region.getType());
Set<DynamicRegion> connectedRegions = new HashSet<>();
LinkedHashSet<DynamicRegion> current = new LinkedHashSet<>();
Set<Region> connected = new HashSet<>();
LinkedHashSet<Region> current = new LinkedHashSet<>();
current.add(region);
while (!current.isEmpty()) {
DynamicRegion r = current.removeFirst();
if (!connectedRegions.add(r)) continue;
getNeighbours(r, true, regions).forEach(current::add);
Region r = current.removeFirst();
if (!connected.add(r)) continue;
getNeighbours(r, true, false, regions).forEach(current::add);
}
return connectedRegions.stream();
return connected.stream();
}
}