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
@@ -154,6 +154,14 @@ public interface Region {
return true; return true;
} }
default boolean inRegion(int x, int z, boolean extension) {
Point minPoint = getMinPoint(extension);
Point maxPoint = getMaxPoint(extension);
if (x < minPoint.getX() || x > maxPoint.getX()) return false;
if (z < minPoint.getZ() || z > maxPoint.getZ()) return false;
return true;
}
@Nullable @Nullable
default Clipboard copy(boolean extension) { default Clipboard copy(boolean extension) {
return FlatteningWrapper.impl.copy(getMinPoint(extension), getMaxPoint(extension), getCopyPoint()); return FlatteningWrapper.impl.copy(getMinPoint(extension), getMaxPoint(extension), getCopyPoint());
@@ -32,6 +32,8 @@ public interface RegionSystem {
RegionSystem INSTANCE = init(); RegionSystem INSTANCE = init();
UUID GLOBAL_REGION_ID = new UUID(0, 0);
/** /**
* Loads and initializes the Regions and anything that should be loaded on startup. * Loads and initializes the Regions and anything that should be loaded on startup.
*/ */
@@ -19,22 +19,13 @@
package de.steamwar.bausystem.region; package de.steamwar.bausystem.region;
import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.region.dynamic.Tile;
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 lombok.NonNull; import lombok.NonNull;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
public class DynamicRegionSystem implements RegionSystem { public class DynamicRegionSystem implements RegionSystem {
@@ -42,67 +33,55 @@ public class DynamicRegionSystem implements RegionSystem {
public static DynamicRegionSystem INSTANCE; public static DynamicRegionSystem INSTANCE;
private static final World WORLD = Bukkit.getWorlds().get(0); 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 @Override
public void load() { public void load() {
INSTANCE = this; 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 @Override
public void save() { public void save() {
} }
@Override @Override
public @NonNull Location getWorldSpawn() { public @NonNull Location getWorldSpawn() {
if (SpawnResetter.isBigSpawn()) { return null;
return SpawnResetter.BIG_WORLD_SPAWN;
} else {
return SpawnResetter.SMALL_WORLD_SPAWN;
}
} }
@Override @Override
public @NonNull Region getGlobalRegion() { public @NonNull Region getGlobalRegion() {
return GlobalRegion.INSTANCE; return null;
} }
// TODO: Optimize later on! private Region get(int x, int z, boolean fastCache, Collection<Region> regions) {
private Region get(Location location, Collection<Region> regions) { Tile tile = Tile.fromXZ(x, z).orElse(null);
return regions.stream() if (tile == null) {
.filter(region -> region.getArea().inRegion(location, false)) 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() .findFirst()
.orElse(GlobalRegion.INSTANCE); .orElseGet(this::getGlobalRegion);
regionCache.put(tile.getId(), region);
return region;
} }
@Override @Override
public @NonNull Region get(@NonNull Location location) { public @NonNull Region get(@NonNull Location location) {
return get(location, regionMap.values()); return get(location.getBlockX(), location.getBlockZ(), true, regionMap.values());
} }
@Override @Override
@@ -115,44 +94,46 @@ public class DynamicRegionSystem implements RegionSystem {
return regionMap.values().stream(); 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 minPoint = region.getArea().getMinPoint(false).subtract(18, 0, 18);
Point maxPoint = region.getArea().getMaxPoint(false).add(19, 0, 19); Point maxPoint = region.getArea().getMaxPoint(false).add(19, 0, 19);
Set<Region> neighbours = new HashSet<>(); Set<Region> neighbours = new HashSet<>();
for (int x = minPoint.getX() + (noCorners ? 18 : 0); x <= maxPoint.getX() - (noCorners ? 19 : 0); x += 19) { for (int x = minPoint.getX() + (noCorners ? 18 : 0); x <= maxPoint.getX() - (noCorners ? 19 : 0); x += 19) {
int minZ = minPoint.getZ(); int minZ = minPoint.getZ();
int maxZ = maxPoint.getZ(); int maxZ = maxPoint.getZ();
neighbours.add(get(new Location(WORLD, x, 0, minZ), regions)); neighbours.add(get(x, minZ, fastCache, regions));
neighbours.add(get(new Location(WORLD, x, 0, maxZ), regions)); neighbours.add(get(x, maxZ, fastCache, regions));
} }
for (int z = minPoint.getZ() + 18; z <= maxPoint.getZ() - 19; z += 19) { for (int z = minPoint.getZ() + 18; z <= maxPoint.getZ() - 19; z += 19) {
int minX = minPoint.getX(); int minX = minPoint.getX();
int maxX = maxPoint.getX(); int maxX = maxPoint.getX();
neighbours.add(get(new Location(WORLD, minX, 0, z), regions)); neighbours.add(get(minX, z, fastCache, regions));
neighbours.add(get(new Location(WORLD, maxX, 0, z), regions)); neighbours.add(get(maxX, z, fastCache, regions));
}
neighbours.remove(GlobalRegion.INSTANCE);
return ((Set<DynamicRegion>) (Set) neighbours).stream();
} }
public Stream<DynamicRegion> getNeighbours(Region region) { neighbours.remove(getGlobalRegion());
return getNeighbours(region, false, regionMap.values()); return neighbours.stream();
} }
public Stream<DynamicRegion> getConnectedRegions(DynamicRegion region) { public Stream<Region> getNeighbours(Region region) {
Set<Region> regions = regionMap.values() return getNeighbours(region, false, true, regionMap.values());
.stream() }
.filter(r -> r.getType() == region.getType())
.collect(Collectors.toSet());
Set<DynamicRegion> connectedRegions = new HashSet<>(); public Stream<Region> getConnectedRegions(Region region) {
LinkedHashSet<DynamicRegion> current = new LinkedHashSet<>(); Set<Region> regions = regionTypeMap.get(region.getType());
Set<Region> connected = new HashSet<>();
LinkedHashSet<Region> current = new LinkedHashSet<>();
current.add(region); current.add(region);
while (!current.isEmpty()) { while (!current.isEmpty()) {
DynamicRegion r = current.removeFirst(); Region r = current.removeFirst();
if (!connectedRegions.add(r)) continue; if (!connected.add(r)) continue;
getNeighbours(r, true, regions).forEach(current::add); getNeighbours(r, true, false, regions).forEach(current::add);
} }
return connectedRegions.stream();
return connected.stream();
} }
} }
@@ -0,0 +1,26 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.region;
public abstract class RegionConstructor<T extends Region> {
public abstract int getWidthX();
public abstract int getWidthZ();
public abstract T create(int tileX, int tileZ);
}
@@ -0,0 +1,92 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.region.dynamic;
import org.bukkit.Location;
import java.util.Optional;
public class Tile {
public static final int tileSize = 19;
public static final int maxTile = 1023;
public static final int minTile = -maxTile;
public static final int tilesPerAxis = maxTile * 2 + 1;
private final int tileX;
private final int tileZ;
private Tile(int tileX, int tileZ) {
this.tileX = tileX;
this.tileZ = tileZ;
}
public static Optional<Tile> fromTile(int tileX, int tileZ) {
if (tileX < minTile || tileZ < minTile) return Optional.empty();
if (tileX > maxTile || tileZ > maxTile) return Optional.empty();
return Optional.of(new Tile(tileX, tileZ));
}
public static Optional<Tile> fromLocation(Location location) {
return fromXZ(location.getBlockX(), location.getBlockZ());
}
public static Optional<Tile> fromXZ(int x, int z) {
x = (int) Math.floor((x + 9) / (double) tileSize);
z = (int) Math.floor((z + 9) / (double) tileSize);
return fromTile(x, z);
}
public static int getMinX(int tileX) {
return tileX * tileSize - 9;
}
public int getMinX() {
return getMinX(tileX);
}
public static int getMinZ(int tileZ) {
return tileZ * tileSize - 9;
}
public int getMinZ() {
return getMinZ(tileZ);
}
public static Location getMinLocation(int tileX, int tileZ) {
return new Location(null, getMinX(tileX), 0, getMinZ(tileZ));
}
public Location getMinLocation() {
return getMinLocation(tileX, tileZ);
}
public Optional<Tile> add(int offsetX, int offsetZ) {
return fromTile(tileX + offsetX, tileZ + offsetZ);
}
public static long getID(int tileX, int tileZ) {
return (tileX + maxTile) * tilesPerAxis + tileZ + maxTile;
}
public long getId() {
return getID(tileX, tileZ);
}
}
@@ -0,0 +1,46 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
plugins {
steamwar.java
}
tasks.compileJava {
options.isWarnings = false
}
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
dependencies {
compileOnly(project(":BauSystem:BauSystem_Main", "default"))
compileOnly(project(":SpigotCore", "default"))
compileOnly(libs.spigotapi)
compileOnly(libs.axiom)
compileOnly(libs.authlib)
compileOnly(libs.viaapi)
compileOnly(libs.nms20)
compileOnly(libs.fawe18)
implementation(libs.luaj)
}
@@ -0,0 +1,158 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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 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 {
public static DynamicRegionSystem INSTANCE;
private static final World WORLD = Bukkit.getWorlds().get(0);
private static Map<UUID, Region> regionMap = new HashMap<>();
@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;
}
}
@Override
public @NonNull Region getGlobalRegion() {
return GlobalRegion.INSTANCE;
}
// TODO: Optimize later on!
private Region get(Location location, Collection<Region> regions) {
return regions.stream()
.filter(region -> region.getArea().inRegion(location, false))
.findFirst()
.orElse(GlobalRegion.INSTANCE);
}
@Override
public @NonNull Region get(@NonNull Location location) {
return get(location, regionMap.values());
}
@Override
public Optional<Region> getRegion(@NonNull UUID id) {
return Optional.ofNullable(regionMap.get(id));
}
@Override
public @NonNull Stream<Region> getRegions() {
return regionMap.values().stream();
}
private Stream<DynamicRegion> getNeighbours(Region region, boolean noCorners, 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));
}
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.remove(GlobalRegion.INSTANCE);
return ((Set<DynamicRegion>) (Set) neighbours).stream();
}
public Stream<DynamicRegion> getNeighbours(Region region) {
return getNeighbours(region, false, regionMap.values());
}
public Stream<DynamicRegion> getConnectedRegions(DynamicRegion region) {
Set<Region> regions = regionMap.values()
.stream()
.filter(r -> r.getType() == region.getType())
.collect(Collectors.toSet());
Set<DynamicRegion> connectedRegions = new HashSet<>();
LinkedHashSet<DynamicRegion> 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);
}
return connectedRegions.stream();
}
}
@@ -0,0 +1,30 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.region.dynamic;
import de.steamwar.bausystem.region.RegionData;
import yapion.hierarchy.types.YAPIONObject;
public abstract class DefaultRegionData extends RegionData {
protected DefaultRegionData(YAPIONObject data, Runnable onChange) {
super(data, onChange);
}
}
@@ -25,6 +25,7 @@ import de.steamwar.bausystem.region.dynamic.DefaultFlagStorage;
import de.steamwar.bausystem.region.dynamic.NonNormalFlagStorage; import de.steamwar.bausystem.region.dynamic.NonNormalFlagStorage;
import de.steamwar.bausystem.region.dynamic.RegionDataRepository; import de.steamwar.bausystem.region.dynamic.RegionDataRepository;
import de.steamwar.bausystem.utils.PasteBuilder; import de.steamwar.bausystem.utils.PasteBuilder;
import de.steamwar.sql.GameModeConfig;
import lombok.NonNull; import lombok.NonNull;
import org.bukkit.Location; import org.bukkit.Location;
@@ -40,8 +41,6 @@ public class GlobalRegion implements Region {
private static final Point MIN_POINT = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE); private static final Point MIN_POINT = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE);
private static final Point MAX_POINT = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE); private static final Point MAX_POINT = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
private static final UUID GLOBAL_REGION_ID = new UUID(0, 0);
private static final Region.Area GLOBAL_AREA = new Region.Area() { private static final Region.Area GLOBAL_AREA = new Region.Area() {
@Override @Override
public @NonNull Point getMinPoint(boolean extension) { public @NonNull Point getMinPoint(boolean extension) {
@@ -104,7 +103,7 @@ public class GlobalRegion implements Region {
@Override @Override
public @NonNull UUID getID() { public @NonNull UUID getID() {
return GLOBAL_REGION_ID; return RegionSystem.GLOBAL_REGION_ID;
} }
@Override @Override
@@ -43,8 +43,6 @@ public final class FixedGlobalRegion implements Region {
@Setter @Setter
private static RegionData FLAG_STORAGE; private static RegionData FLAG_STORAGE;
private static final UUID GLOBAL_REGION_ID = new UUID(0, 0);
private static final Area GLOBAL_AREA = new Area() { private static final Area GLOBAL_AREA = new Area() {
@Override @Override
public @NonNull Point getMinPoint(boolean extension) { public @NonNull Point getMinPoint(boolean extension) {
@@ -102,7 +100,7 @@ public final class FixedGlobalRegion implements Region {
@Override @Override
public @NonNull UUID getID() { public @NonNull UUID getID() {
return GLOBAL_REGION_ID; return RegionSystem.GLOBAL_REGION_ID;
} }
@Override @Override
+1
View File
@@ -166,6 +166,7 @@ include(
"BauSystem", "BauSystem",
"BauSystem:BauSystem_Main", "BauSystem:BauSystem_Main",
"BauSystem:BauSystem_RegionDynamic", "BauSystem:BauSystem_RegionDynamic",
"BauSystem:BauSystem_RegionDynamic2",
"BauSystem:BauSystem_RegionFixed" "BauSystem:BauSystem_RegionFixed"
) )