Skip to content

Commit

Permalink
Add saveConfig and fix acceptedPlayers list
Browse files Browse the repository at this point in the history
  • Loading branch information
Libreh committed Feb 8, 2025
1 parent 02041af commit 6a2fa01
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.21.4+build.1
loader_version=0.16.9

# Mod Properties
mod_version=0.0.4
mod_version=0.0.5
maven_group=me.libreh
archives_base_name=rulebook

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/me/libreh/rulebook/Rulebook.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.server.command.ServerCommandSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -19,12 +21,14 @@
import static net.minecraft.server.command.CommandManager.literal;

public class Rulebook implements ModInitializer {
public static final String MOD_ID = "rulebook";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final Set<UUID> JOIN_LIST = new HashSet<>();
public static final Set<UUID> RULEBOOK_LIST = new HashSet<>();

@Override
public void onInitialize() {
Config.load();
Config.loadConfig();

CommandRegistrationCallback.EVENT.register((dispatcher, access, environment) -> {
Commands.register(dispatcher);
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/me/libreh/rulebook/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;

import java.util.UUID;

import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;

Expand All @@ -22,7 +24,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
hasPermission(source.getPlayer(), "rulebook.reload")) || (!source.isExecutedByPlayer())
)
.executes(context -> {
Config.load();
Config.saveConfig();

return Command.SINGLE_SUCCESS;
})
Expand All @@ -36,8 +38,12 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
Config.unaccept(player);
}

Config.getConfig().acceptedPlayers.removeIf(uuid -> context.getSource().getServer().getPlayerManager().getPlayer(uuid) == null);
Config.load();
for (UUID uuid : Config.getConfig().acceptedPlayers) {
if (context.getSource().getServer().getPlayerManager().getPlayer(uuid) == null) {
Config.getConfig().acceptedPlayers.remove(uuid);
Config.saveConfig();
}
}

return Command.SINGLE_SUCCESS;
})
Expand All @@ -46,7 +52,6 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
for (ServerPlayerEntity player : EntityArgumentType.getPlayers(context, "players")) {
Config.unaccept(player);
}
Config.load();

return Command.SINGLE_SUCCESS;
})
Expand All @@ -56,8 +61,12 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
hasPermission(source.getPlayer(), "rulebook.reload")) || (!source.isExecutedByPlayer())
)
.executes(context -> {
Config.getConfig().acceptedPlayers.removeIf(uuid -> context.getSource().getServer().getPlayerManager().getPlayer(uuid) == null);
Config.load();
for (UUID uuid : Config.getConfig().acceptedPlayers) {
if (context.getSource().getServer().getPlayerManager().getPlayer(uuid) == null) {
Config.getConfig().acceptedPlayers.remove(uuid);
Config.saveConfig();
}
}

return Command.SINGLE_SUCCESS;
})
Expand All @@ -68,7 +77,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
.requires(Permissions.require("rulebook.main", true))
.executes(context -> {
Config.accept(context.getSource().getPlayer());
Config.load();
Config.saveConfig();

return Command.SINGLE_SUCCESS;
})
Expand Down
41 changes: 32 additions & 9 deletions src/main/java/me/libreh/rulebook/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import eu.pb4.placeholders.api.PlaceholderContext;
import eu.pb4.placeholders.api.Placeholders;
import eu.pb4.placeholders.api.TextParserUtils;
import me.libreh.rulebook.Rulebook;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.network.ServerPlayerEntity;

Expand Down Expand Up @@ -51,37 +52,59 @@ public static class Rule {

public List<UUID> acceptedPlayers = new ArrayList<>();

public static void load() {
public static boolean loadConfig() {
Config oldConfig = CONFIG;
boolean success;

CONFIG = null;
try {
File configFile = new File(FabricLoader.getInstance().getConfigDir().toFile(), "rulebook.json");
File configFile = getConfigFile();

Config config = configFile.exists() ? GSON.fromJson(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8), Config.class) : new Config();

CONFIG = config;
CONFIG = configFile.exists() ? GSON.fromJson(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8), Config.class) : new Config();

{
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), StandardCharsets.UTF_8));
writer.write(GSON.toJson(config));
writer.close();

}
saveConfig();
success = true;
} catch (IOException exception) {
success = false;
CONFIG = oldConfig;
Rulebook.LOGGER.error("Something went wrong while reading config!");
exception.printStackTrace();
}

return success;
}

public static void saveConfig() {
try {
File configFile = getConfigFile();

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), StandardCharsets.UTF_8));
writer.write(GSON.toJson(CONFIG));
writer.close();
} catch (Exception exception) {
Rulebook.LOGGER.error("Something went wrong while saving config!", exception);
}
}

private static File getConfigFile() {
return new File(FabricLoader.getInstance().getConfigDir().toFile(), "rulebook.json");
}

public static boolean hasAccepted(ServerPlayerEntity player) {
return getConfig().acceptedPlayers.contains(player.getUuid());
}

public static void accept(ServerPlayerEntity player) {
getConfig().acceptedPlayers.add(player.getUuid());
Config.saveConfig();
}

public static void unaccept(ServerPlayerEntity player) {
getConfig().acceptedPlayers.remove(player.getUuid());
player.networkHandler.disconnect(Placeholders.parseText(TextParserUtils.formatText(Config.getConfig().kickMessages.updatedRules), PlaceholderContext.of(player)));
Config.saveConfig();
player.networkHandler.disconnect(Placeholders.parseText(TextParserUtils.formatText(getConfig().kickMessages.updatedRules), PlaceholderContext.of(player)));
}
}

0 comments on commit 6a2fa01

Please sign in to comment.