forked from SteamWar/SteamWar
Start rebuilt
This commit is contained in:
+158
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user