Skip to content

Commit

Permalink
0.8.14: Better wildness support.
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed Jan 2, 2025
1 parent c8787a9 commit b88a25f
Show file tree
Hide file tree
Showing 14 changed files with 18 additions and 120 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod_name=Area Control
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=BSD-3-Clause
# The mod version. See https://semver.org/
mod_version=0.8.13
mod_version=0.8.14
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/teacon/areacontrol/AreaControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.config.ModConfig;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.fml.loading.FMLPaths;
import net.neoforged.neoforge.common.ModConfigSpec;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.neoforge.event.level.LevelEvent;
Expand Down Expand Up @@ -67,8 +66,7 @@ public static void onServerStart(ServerAboutToStartEvent event) {
AreaControlAPI.groupProvider = VanillaScoreboardTeamGroupProvider.INSTANCE;
final MinecraftServer server = event.getServer();
final Path dataDir = server.getWorldPath(SERVER_CONFIG).resolve("area_control");
final Path globalConfigDir = FMLPaths.CONFIGDIR.get();
final var repo = AreaRepositoryManager.INSTANCE.create(AreaControlConfig.persistenceMode.get(), dataDir, globalConfigDir);
final var repo = AreaRepositoryManager.INSTANCE.create(AreaControlConfig.persistenceMode.get(), dataDir);
AreaManager.INSTANCE.init(repo);
if (Files.isDirectory(dataDir)) {
try {
Expand Down
24 changes: 6 additions & 18 deletions src/main/java/org/teacon/areacontrol/AreaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.ServerLevelAccessor;
import net.minecraft.world.phys.Vec3;
import net.neoforged.fml.loading.FMLPaths;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
Expand All @@ -27,7 +26,6 @@
import org.teacon.areacontrol.impl.persistence.AreaRepository;

import java.math.BigInteger;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
Expand Down Expand Up @@ -61,9 +59,6 @@ public final class AreaManager {
// Apparently create a ResourceKey involves String.intern(), so here we go...
private final Map<String, ResourceKey<Level>> levelKeyCache = new HashMap<>();

/** A special area instance that allows more fine-grained permission control over wild area */
private Area virtualWild;

private ResourceKey<Level> getOrCreate(String dimKey) {
return this.levelKeyCache.computeIfAbsent(dimKey, k -> ResourceKey.create(Registries.DIMENSION, ResourceLocation.parse(k)));
}
Expand Down Expand Up @@ -96,11 +91,6 @@ public void init(AreaRepository repository) {
}

public void load() throws Exception {
try {
this.virtualWild = this.repository.loadWildness();
} catch (Exception e) {
LOGGER.error("Error occurred while loading virtual wild definition. Proceeding without virtual wild", e);
}
var writeLock = this.lock.writeLock();
try {
writeLock.lock();
Expand Down Expand Up @@ -230,9 +220,12 @@ public boolean add(Area area, ResourceKey<Level> worldIndex, ServerPlayer actor)
if (parent.size() > 1) {
return false;
}
for (var child : children) {
if (!AreaChecks.isACtrlAreaOwner(actor, child)) {
return false;
if (!AreaChecks.isACtrlAdmin(actor)) {
// AC admin should be able to do this.
for (var child : children) {
if (!AreaChecks.isACtrlAreaOwner(actor, child)) {
return false;
}
}
}
// No conflict, single parent - this is a success. Building cache for this new area.
Expand Down Expand Up @@ -590,9 +583,4 @@ public Collection<Area> findAllIn(ResourceKey<Level> dim, ChunkPos from, ChunkPo
.map(this::findBy)
.toList();
}

@ApiStatus.Internal
public Area getVirtualWild() {
return this.virtualWild;
}
}
5 changes: 0 additions & 5 deletions src/main/java/org/teacon/areacontrol/api/AreaControlAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ public Area findBy(String dimKey, double x, double y, double z) {
public Area findBy(String dimKey, int x, int y, int z) {
throw new IllegalStateException("Not initialized yet!");
}

@Override
public @Nullable Area getVirtualWild() {
throw new IllegalStateException("Not initialized yet!");
}
};

public static GroupProvider groupProvider = new GroupProvider() {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/org/teacon/areacontrol/api/AreaLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ public interface AreaLookup {

@Nullable
Area findBy(String dimKey, int x, int y, int z);

@Nullable
Area getVirtualWild();
}
9 changes: 1 addition & 8 deletions src/main/java/org/teacon/areacontrol/api/AreaProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,7 @@ public static Optional<Boolean> getBoolOptional(@Nullable Area area, String key)
}

public static Optional<Boolean> getBoolOptional(@Nullable Area area, String key, boolean recursive) {
if (area == null) {
Area virtualWild = AreaControlAPI.areaLookup.getVirtualWild();
if (virtualWild == null) {
return Optional.empty();
}
area = virtualWild;
recursive = false;
}
if (area == null) return Optional.empty();
Object o = area.properties.get(key);
if (o == null || "null".equals(o)) {
if (recursive) {
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/org/teacon/areacontrol/impl/AreaLookupImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;
import org.teacon.areacontrol.AreaManager;
import org.teacon.areacontrol.api.Area;
import org.teacon.areacontrol.api.AreaLookup;
Expand Down Expand Up @@ -40,10 +39,4 @@ public Area findBy(String dimKey, int x, int y, int z) {
var dimResKey = this.getOrCreate(dimKey);
return AreaManager.INSTANCE.findBy(dimResKey, new BlockPos(x, y, z));
}

@Override
public @Nullable Area getVirtualWild() {
return AreaManager.INSTANCE.getVirtualWild();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ public interface AreaRepository {

Collection<Area> load() throws Exception;

Area loadWildness() throws Exception;

void remove(Area areaToRemove) throws Exception;

void save(Collection<Area> areas) throws Exception;

void saveWildness(Area area) throws Exception;

interface Factory {
AreaRepository createFrom(Path dataRootDir, Path globalConfigDir);
AreaRepository createFrom(Path dataRootDir);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public void registerFactory(String type, AreaRepository.Factory factory) {
}
}

public AreaRepository create(String type, Path dataRootDir, Path globalConfigDir) {
return this.repoFactories.get(type).createFrom(dataRootDir, globalConfigDir);
public AreaRepository create(String type, Path dataRootDir) {
return this.repoFactories.get(type).createFrom(dataRootDir);
}

public static void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class JsonBasedAreaRepository implements AreaRepository {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private final Path dataDirRoot;

public JsonBasedAreaRepository(Path dataDirRoot, Path globalConfigDir) {
public JsonBasedAreaRepository(Path dataDirRoot) {
this.dataDirRoot = dataDirRoot;
}

Expand Down Expand Up @@ -57,11 +57,6 @@ public Collection<Area> load() throws Exception {
return ret;
}

@Override
public Area loadWildness() throws Exception {
throw new UnsupportedOperationException("Unimplemented");
}

@Override
public void remove(Area areaToRemove) throws Exception {
Files.deleteIfExists(this.dataDirRoot.resolve("claim-%s.json".formatted(areaToRemove.uid)));
Expand All @@ -85,8 +80,4 @@ public void save(Collection<Area> areas) throws Exception {
}
}

@Override
public void saveWildness(Area rea) throws Exception {
throw new UnsupportedOperationException("Unimplemented");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import com.google.common.io.MoreFiles;
import it.unimi.dsi.fastutil.objects.ObjectArraySet;
import net.minecraft.core.BlockPos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.teacon.areacontrol.api.Area;

import javax.annotation.Nullable;
Expand All @@ -30,18 +28,13 @@

public class TomlBasedAreaRepository implements AreaRepository {

private static final Logger LOGGER = LoggerFactory.getLogger(TomlBasedAreaRepository.class);

private final Path dataDirRoot;
private final Path globalConfigDirRoot;

private final ObjectSerializer writer;
private final ObjectDeserializer reader;

public TomlBasedAreaRepository(Path dataDirRoot, Path globalConfigDir) {
public TomlBasedAreaRepository(Path dataDirRoot) {
this.dataDirRoot = dataDirRoot;
this.globalConfigDirRoot = globalConfigDir;

var desBuilder = ObjectDeserializer.builder();
desBuilder.withDefaultDeserializerProvider(BlockPosSerDeProvider.INSTANCE);
this.reader = desBuilder.build();
Expand Down Expand Up @@ -78,25 +71,6 @@ public Collection<Area> load() throws Exception {
return Collections.unmodifiableCollection(areas.values());
}

@Override
public Area loadWildness() throws Exception {
LOGGER.info("Loading wildness permission data...");
Path wildDef = this.globalConfigDirRoot.resolve("area_control-wildness.toml");
if (Files.notExists(wildDef)) {
LOGGER.info("No wildness found, will use default one");
return null;
}
try (FileConfig areasData = FileConfig.of(wildDef, TomlFormat.instance())) {
areasData.load();
var model = reader.deserializeFields(areasData, AreaModel::new);
LOGGER.info("Wildness permission data loaded");
return model.toRealArea();
} catch (Exception e) {
LOGGER.error("Error occurred while loading permission data for the wildness", e);
throw e;
}
}

@Override
public void remove(Area areaToRemove) throws Exception {
Files.deleteIfExists(this.dataDirRoot.resolve("claim-%s.toml".formatted(areaToRemove.uid)));
Expand All @@ -123,16 +97,6 @@ public void save(Collection<Area> areas) throws Exception {
}
}

@Override
public void saveWildness(Area wild) throws Exception {
Path wildDef = this.globalConfigDirRoot.resolve("area_control-wildness.toml");
try (FileConfig areasData = FileConfig.builder(wildDef).sync().build()) {
var saved = this.writer.serializeFields(new AreaModel(wild), TomlFormat::newConfig);
areasData.addAll(saved);
areasData.save();
}
}

public static final class AreaModel {
@SerdeAssert(SerdeAssert.AssertThat.NOT_EMPTY)
public UUID uid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,15 @@ public class InMemoryAreaRepository implements AreaRepository {

public final List<Area> areas;

public final Area virtualWild;

public InMemoryAreaRepository(List<Area> areas, Area virtualWild) {
public InMemoryAreaRepository(List<Area> areas) {
this.areas = areas;
this.virtualWild = virtualWild;
}

@Override
public Collection<Area> load() {
return List.copyOf(this.areas);
}

@Override
public Area loadWildness() throws Exception {
return this.virtualWild;
}

@Override
public void remove(Area areaToRemove) {
this.areas.remove(areaToRemove);
Expand All @@ -36,9 +28,4 @@ public void remove(Area areaToRemove) {
public void save(Collection<Area> areas) {
// No-op, this is in-memory
}

@Override
public void saveWildness(Area rea) throws Exception {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static void initAreaManager() {
i.properties.put(AreaProperties.ALLOW_ENTITY_USE_SELECTOR_FROM_PARENT, false);
j.properties.put(AreaProperties.ALLOW_ENTITY_USE_SELECTOR_FROM_PARENT, true);

var areaRepo = new InMemoryAreaRepository(allAreas, new Area());
var areaRepo = new InMemoryAreaRepository(allAreas);
try {
AreaManager.INSTANCE.init(areaRepo);
AreaManager.INSTANCE.load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ public class JsonBasedAreaRepositoryTest {
@BeforeEach
public void setup() {
Path claimStoreRoot = this.fsRoot.getPath("/area-control");
Path globalConfigRoot = this.fsRoot.getPath("/config");
try {
Files.createDirectories(claimStoreRoot);
Files.createDirectories(globalConfigRoot);
} catch (IOException e) {
Assertions.fail(e);
}
Expand All @@ -47,7 +45,6 @@ public void setup() {
@Test
public void testLoad() {
Path claimStoreRoot = this.fsRoot.getPath("/area-control");
Path globalConfigRoot = this.fsRoot.getPath("/config");
String claimData = """
{
"uid": "f6b2a791-d2cd-4992-b4d6-4b0ecd242f92",
Expand Down Expand Up @@ -76,7 +73,7 @@ public void testLoad() {
Assertions.fail(e);
}

JsonBasedAreaRepository repo = new JsonBasedAreaRepository(claimStoreRoot, globalConfigRoot);
JsonBasedAreaRepository repo = new JsonBasedAreaRepository(claimStoreRoot);
Collection<Area> areas = null;
try {
areas = repo.load();
Expand Down Expand Up @@ -114,8 +111,7 @@ public void testSave() {
Mockito.when(this.mockPlayer.getGameProfile()).thenReturn(dummyProfile);

Path claimStoreRoot = this.fsRoot.getPath("/area-control");
Path globalConfigRoot = this.fsRoot.getPath("/config");
JsonBasedAreaRepository repo = new JsonBasedAreaRepository(claimStoreRoot, globalConfigRoot);
JsonBasedAreaRepository repo = new JsonBasedAreaRepository(claimStoreRoot);

Area area = Util.createArea(new BlockPos(-1, -1, -1), new BlockPos(1, 1,1 ), this.mockPlayer);
area.uid = UUID.fromString("5d9e9b0d-f438-4e5a-826e-7d42ab76385a");
Expand Down

0 comments on commit b88a25f

Please sign in to comment.