From b88a25f6eaf00d9b576c68db018245117f9b5efb Mon Sep 17 00:00:00 2001 From: burningtnt Date: Thu, 2 Jan 2025 15:39:27 +0800 Subject: [PATCH] 0.8.14: Better wildness support. --- gradle.properties | 2 +- .../org/teacon/areacontrol/AreaControl.java | 4 +- .../org/teacon/areacontrol/AreaManager.java | 24 +++--------- .../areacontrol/api/AreaControlAPI.java | 5 --- .../teacon/areacontrol/api/AreaLookup.java | 3 -- .../areacontrol/api/AreaProperties.java | 9 +---- .../areacontrol/impl/AreaLookupImpl.java | 7 ---- .../impl/persistence/AreaRepository.java | 6 +-- .../persistence/AreaRepositoryManager.java | 4 +- .../persistence/JsonBasedAreaRepository.java | 11 +----- .../persistence/TomlBasedAreaRepository.java | 38 +------------------ .../test/InMemoryAreaRepository.java | 15 +------- .../impl/AreaEntitySelectorCheckerTest.java | 2 +- .../JsonBasedAreaRepositoryTest.java | 8 +--- 14 files changed, 18 insertions(+), 120 deletions(-) diff --git a/gradle.properties b/gradle.properties index 09e2011..1a1037c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/main/java/org/teacon/areacontrol/AreaControl.java b/src/main/java/org/teacon/areacontrol/AreaControl.java index dac71b7..5c1ac31 100644 --- a/src/main/java/org/teacon/areacontrol/AreaControl.java +++ b/src/main/java/org/teacon/areacontrol/AreaControl.java @@ -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; @@ -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 { diff --git a/src/main/java/org/teacon/areacontrol/AreaManager.java b/src/main/java/org/teacon/areacontrol/AreaManager.java index bc40805..5088270 100644 --- a/src/main/java/org/teacon/areacontrol/AreaManager.java +++ b/src/main/java/org/teacon/areacontrol/AreaManager.java @@ -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; @@ -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; @@ -61,9 +59,6 @@ public final class AreaManager { // Apparently create a ResourceKey involves String.intern(), so here we go... private final Map> levelKeyCache = new HashMap<>(); - /** A special area instance that allows more fine-grained permission control over wild area */ - private Area virtualWild; - private ResourceKey getOrCreate(String dimKey) { return this.levelKeyCache.computeIfAbsent(dimKey, k -> ResourceKey.create(Registries.DIMENSION, ResourceLocation.parse(k))); } @@ -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(); @@ -230,9 +220,12 @@ public boolean add(Area area, ResourceKey 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. @@ -590,9 +583,4 @@ public Collection findAllIn(ResourceKey dim, ChunkPos from, ChunkPo .map(this::findBy) .toList(); } - - @ApiStatus.Internal - public Area getVirtualWild() { - return this.virtualWild; - } } diff --git a/src/main/java/org/teacon/areacontrol/api/AreaControlAPI.java b/src/main/java/org/teacon/areacontrol/api/AreaControlAPI.java index 142bbc7..c5a4c46 100644 --- a/src/main/java/org/teacon/areacontrol/api/AreaControlAPI.java +++ b/src/main/java/org/teacon/areacontrol/api/AreaControlAPI.java @@ -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() { diff --git a/src/main/java/org/teacon/areacontrol/api/AreaLookup.java b/src/main/java/org/teacon/areacontrol/api/AreaLookup.java index 101531b..7baa9ae 100644 --- a/src/main/java/org/teacon/areacontrol/api/AreaLookup.java +++ b/src/main/java/org/teacon/areacontrol/api/AreaLookup.java @@ -14,7 +14,4 @@ public interface AreaLookup { @Nullable Area findBy(String dimKey, int x, int y, int z); - - @Nullable - Area getVirtualWild(); } diff --git a/src/main/java/org/teacon/areacontrol/api/AreaProperties.java b/src/main/java/org/teacon/areacontrol/api/AreaProperties.java index 0216945..2721768 100644 --- a/src/main/java/org/teacon/areacontrol/api/AreaProperties.java +++ b/src/main/java/org/teacon/areacontrol/api/AreaProperties.java @@ -82,14 +82,7 @@ public static Optional getBoolOptional(@Nullable Area area, String key) } public static Optional 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) { diff --git a/src/main/java/org/teacon/areacontrol/impl/AreaLookupImpl.java b/src/main/java/org/teacon/areacontrol/impl/AreaLookupImpl.java index d4a68ab..7f387c1 100644 --- a/src/main/java/org/teacon/areacontrol/impl/AreaLookupImpl.java +++ b/src/main/java/org/teacon/areacontrol/impl/AreaLookupImpl.java @@ -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; @@ -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(); - } - } diff --git a/src/main/java/org/teacon/areacontrol/impl/persistence/AreaRepository.java b/src/main/java/org/teacon/areacontrol/impl/persistence/AreaRepository.java index 855f3d5..5c5a413 100644 --- a/src/main/java/org/teacon/areacontrol/impl/persistence/AreaRepository.java +++ b/src/main/java/org/teacon/areacontrol/impl/persistence/AreaRepository.java @@ -9,15 +9,11 @@ public interface AreaRepository { Collection load() throws Exception; - Area loadWildness() throws Exception; - void remove(Area areaToRemove) throws Exception; void save(Collection areas) throws Exception; - void saveWildness(Area area) throws Exception; - interface Factory { - AreaRepository createFrom(Path dataRootDir, Path globalConfigDir); + AreaRepository createFrom(Path dataRootDir); } } diff --git a/src/main/java/org/teacon/areacontrol/impl/persistence/AreaRepositoryManager.java b/src/main/java/org/teacon/areacontrol/impl/persistence/AreaRepositoryManager.java index cf9e89d..d68d7f7 100644 --- a/src/main/java/org/teacon/areacontrol/impl/persistence/AreaRepositoryManager.java +++ b/src/main/java/org/teacon/areacontrol/impl/persistence/AreaRepositoryManager.java @@ -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() { diff --git a/src/main/java/org/teacon/areacontrol/impl/persistence/JsonBasedAreaRepository.java b/src/main/java/org/teacon/areacontrol/impl/persistence/JsonBasedAreaRepository.java index dd3ea28..4e8ddc6 100644 --- a/src/main/java/org/teacon/areacontrol/impl/persistence/JsonBasedAreaRepository.java +++ b/src/main/java/org/teacon/areacontrol/impl/persistence/JsonBasedAreaRepository.java @@ -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; } @@ -57,11 +57,6 @@ public Collection 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))); @@ -85,8 +80,4 @@ public void save(Collection areas) throws Exception { } } - @Override - public void saveWildness(Area rea) throws Exception { - throw new UnsupportedOperationException("Unimplemented"); - } } diff --git a/src/main/java/org/teacon/areacontrol/impl/persistence/TomlBasedAreaRepository.java b/src/main/java/org/teacon/areacontrol/impl/persistence/TomlBasedAreaRepository.java index 34d077c..197a8fa 100644 --- a/src/main/java/org/teacon/areacontrol/impl/persistence/TomlBasedAreaRepository.java +++ b/src/main/java/org/teacon/areacontrol/impl/persistence/TomlBasedAreaRepository.java @@ -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; @@ -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(); @@ -78,25 +71,6 @@ public Collection 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))); @@ -123,16 +97,6 @@ public void save(Collection 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; diff --git a/src/test/java/org/teacon/areacontrol/test/InMemoryAreaRepository.java b/src/test/java/org/teacon/areacontrol/test/InMemoryAreaRepository.java index 8c3f0de..5602418 100644 --- a/src/test/java/org/teacon/areacontrol/test/InMemoryAreaRepository.java +++ b/src/test/java/org/teacon/areacontrol/test/InMemoryAreaRepository.java @@ -10,11 +10,8 @@ public class InMemoryAreaRepository implements AreaRepository { public final List areas; - public final Area virtualWild; - - public InMemoryAreaRepository(List areas, Area virtualWild) { + public InMemoryAreaRepository(List areas) { this.areas = areas; - this.virtualWild = virtualWild; } @Override @@ -22,11 +19,6 @@ public Collection 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); @@ -36,9 +28,4 @@ public void remove(Area areaToRemove) { public void save(Collection areas) { // No-op, this is in-memory } - - @Override - public void saveWildness(Area rea) throws Exception { - - } } diff --git a/src/test/java/org/teacon/areacontrol/test/impl/AreaEntitySelectorCheckerTest.java b/src/test/java/org/teacon/areacontrol/test/impl/AreaEntitySelectorCheckerTest.java index a6995ca..f0d36bd 100644 --- a/src/test/java/org/teacon/areacontrol/test/impl/AreaEntitySelectorCheckerTest.java +++ b/src/test/java/org/teacon/areacontrol/test/impl/AreaEntitySelectorCheckerTest.java @@ -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(); diff --git a/src/test/java/org/teacon/areacontrol/test/impl/persistence/JsonBasedAreaRepositoryTest.java b/src/test/java/org/teacon/areacontrol/test/impl/persistence/JsonBasedAreaRepositoryTest.java index c07ed1e..a835122 100644 --- a/src/test/java/org/teacon/areacontrol/test/impl/persistence/JsonBasedAreaRepositoryTest.java +++ b/src/test/java/org/teacon/areacontrol/test/impl/persistence/JsonBasedAreaRepositoryTest.java @@ -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); } @@ -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", @@ -76,7 +73,7 @@ public void testLoad() { Assertions.fail(e); } - JsonBasedAreaRepository repo = new JsonBasedAreaRepository(claimStoreRoot, globalConfigRoot); + JsonBasedAreaRepository repo = new JsonBasedAreaRepository(claimStoreRoot); Collection areas = null; try { areas = repo.load(); @@ -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");