Skip to content

Commit

Permalink
Add chunk debug server config
Browse files Browse the repository at this point in the history
  • Loading branch information
senseiwells committed Sep 21, 2024
1 parent a2e5264 commit af8bf17
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
java
}

val modVersion = "2.0.1+beta.1"
val modVersion = "2.0.1"
val releaseVersion = "${modVersion}+mc${libs.versions.minecraft.get()}"
version = releaseVersion
group = "me.senseiwells"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class GuiMixin {
// We do this instead of adding a new layer for shader compatability reasons
@Inject(
method = "renderEffects",
at = @At("TAIL")
at = @At("RETURN")
)
private void renderChunkDebugMinimap(GuiGraphics graphics, DeltaTracker tracker, CallbackInfo ci) {
ChunkDebugClient.getInstance().onGuiRender(graphics, tracker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import me.senseiwells.chunkdebug.ChunkDebug;
import me.senseiwells.chunkdebug.common.network.*;
import me.senseiwells.chunkdebug.common.utils.ChunkData;
import me.senseiwells.chunkdebug.server.config.ChunkDebugServerConfig;
import me.senseiwells.chunkdebug.server.tracker.ChunkDebugTracker;
import me.senseiwells.chunkdebug.server.tracker.ChunkDebugTrackerHolder;
import net.fabricmc.api.ModInitializer;
Expand All @@ -30,6 +31,7 @@ public class ChunkDebugServer implements ModInitializer {
private static ChunkDebugServer instance;

private final Multimap<ResourceKey<Level>, UUID> watching = HashMultimap.create();
private final ChunkDebugServerConfig config = ChunkDebugServerConfig.read();

public static ChunkDebugServer getInstance() {
return instance;
Expand All @@ -47,7 +49,7 @@ public void onInitialize() {
}

public boolean isPermitted(ServerPlayer player) {
if (player.server.isDedicatedServer()) {
if (player.server.isDedicatedServer() && this.config.requirePermissions()) {
return Permissions.check(player, "chunk-debug", 2);
}
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package me.senseiwells.chunkdebug.server.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import me.senseiwells.chunkdebug.ChunkDebug;
import net.fabricmc.loader.api.FabricLoader;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

public record ChunkDebugServerConfig(
boolean requirePermissions
) {
private static final Path PATH = FabricLoader.getInstance().getConfigDir().resolve("chunk-debug-server.json");
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();

public static final Codec<ChunkDebugServerConfig> CODEC = RecordCodecBuilder.create(instance -> {
return instance.group(
Codec.BOOL.fieldOf("require_permissions").forGetter(ChunkDebugServerConfig::requirePermissions)
).apply(instance, ChunkDebugServerConfig::new);
});

public static ChunkDebugServerConfig read() {
if (!Files.exists(PATH)) {
ChunkDebug.LOGGER.info("Generating default config");
return write(new ChunkDebugServerConfig(true));
}
try (BufferedReader reader = Files.newBufferedReader(PATH)) {
JsonElement element = GSON.fromJson(reader, JsonElement.class);
Optional<ChunkDebugServerConfig> result = CODEC.parse(JsonOps.INSTANCE, element).resultOrPartial();
if (result.isPresent()) {
return result.get();
}
} catch (IOException e) {
ChunkDebug.LOGGER.error("Failed to read chunk-debug-server config", e);
}
return write(new ChunkDebugServerConfig(true));
}

private static ChunkDebugServerConfig write(ChunkDebugServerConfig config) {
try {
Files.createDirectories(PATH.getParent());
Optional<JsonElement> result = CODEC.encodeStart(JsonOps.INSTANCE, config).resultOrPartial();
if (result.isPresent()) {
try (BufferedWriter writer = Files.newBufferedWriter(PATH)) {
GSON.toJson(result.get(), writer);
}
}
} catch (IOException e) {
ChunkDebug.LOGGER.error("Failed to write chunk-debug-server config", e);
}
return config;
}
}

0 comments on commit af8bf17

Please sign in to comment.