Skip to content

Commit

Permalink
Re-implement serverside config using toml4j
Browse files Browse the repository at this point in the history
  • Loading branch information
sammko committed Jun 8, 2021
1 parent 984533f commit e56b6db
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
15 changes: 4 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ dependencies {
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

implementation "com.moandjiezana.toml:toml4j:${project.toml4j_version}"
include "com.moandjiezana.toml:toml4j:${project.toml4j_version}"
}

processResources {
Expand All @@ -35,7 +38,7 @@ tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"

// Minecraft 1.17 (21w19a) upwards uses Java 16.
it.options.release = 16
it.options.release.set(16)
}

java {
Expand All @@ -62,14 +65,4 @@ publishing {
}
}
}
//
// repositories {
// maven {
// url = mavenUrl
// credentials {
// username mavenUser
// password mavenPassword
// }
// }
// }
}
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ fabric_version=0.34.9+1.17
# Mod Properties
mod_version = 1.5
maven_group = net.cavoj
archives_base_name = servertick
archives_base_name = servertick

toml4j_version = 0.7.2
33 changes: 33 additions & 0 deletions src/main/java/net/cavoj/servertick/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.cavoj.servertick;

import com.moandjiezana.toml.Toml;
import com.moandjiezana.toml.TomlWriter;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

public class Config {
public final boolean requireOP;

public Config(Path configPath) {
if (!Files.exists(configPath)) {
createDefault(configPath);
}
Toml toml = new Toml().read(configPath.toFile());
this.requireOP = toml.getBoolean("requireOP");
}

private static void createDefault(Path configPath) {
TomlWriter tw = new TomlWriter();
Map<String, Object> defaults = new HashMap<>();
defaults.put("requireOP", true);
try {
tw.write(defaults, configPath.toFile());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
9 changes: 7 additions & 2 deletions src/main/java/net/cavoj/servertick/ServerTick.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,26 @@
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;

import java.nio.file.Path;

public class ServerTick implements ModInitializer {
private Config config;

@Override
public void onInitialize() {
ServerPlayNetworking.registerGlobalReceiver(NetworkC2S.PACKET_ENABLED, this::processTogglePacket);
ServerTickEvents.END_SERVER_TICK.register((minecraftServer -> {
((MinecraftServerWithST)minecraftServer).tickST();
}));
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER) {
// TODO Load server config
Path configFilePath = FabricLoader.getInstance().getConfigDir().resolve("servertick.toml");
this.config = new Config(configFilePath);
}
}

private boolean checkPlayerPrivilege(PlayerEntity player) {
// TODO check server config
return (player.getServer() != null && !player.getServer().isDedicated()) ||
(this.config != null && !this.config.requireOP) ||
player.hasPermissionLevel(4);
}

Expand Down

0 comments on commit e56b6db

Please sign in to comment.