Skip to content

Commit

Permalink
json config
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkGoosik committed Jan 10, 2023
1 parent 9b4ed1e commit bc34b57
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.parallel = true
# Mod Properties
maven_group = ru.pinkgoosik
archives_base_name = skylands
mod_version = 0.1.1
mod_version = 0.2.0

# Dependencies | Check these on https://fabricmc.net/develop
minecraft_version = 1.19.2
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/skylands/config/SkylandsConfig.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package skylands.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.Vec3d;
import skylands.SkylandsMod;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;

public class SkylandsConfig {
public static final Gson GSON = new GsonBuilder().setLenient().setPrettyPrinting().create();
@SuppressWarnings("unused")
public String readDocs = "https://github.com/tyap-lyap/skylands/wiki";
public String configFormat = "json";
public Vec3d defaultSpawnPos = new Vec3d(0.5D, 75D, 0.5D);
public Vec3d defaultVisitsPos = new Vec3d(0.5D, 75D, 0.5D);

Expand Down Expand Up @@ -56,4 +69,40 @@ public void writeToNbt(NbtCompound nbt) {

nbt.put("config", configNbt);
}

public static SkylandsConfig read() {
// String filePath = path.isBlank() ? this.file : this.path + "/" + this.file;

String filePath = FabricLoader.getInstance().getConfigDir().resolve("skylands.json").toString();
try {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
return GSON.fromJson(reader, SkylandsConfig.class);
}
catch(FileNotFoundException e) {
SkylandsMod.LOGGER.info("File " + filePath + " is not found! Setting to default.");
var conf = new SkylandsConfig();
conf.save();
return conf;
}
catch(Exception e) {
SkylandsMod.LOGGER.info("Failed to read skylands config due to an exception. " +
"Please delete skylands.json to regenerate config or fix the issue:\n" + e);
e.printStackTrace();
System.exit(0);
return new SkylandsConfig();
}
}

public void save() {
try {
String filePath = FabricLoader.getInstance().getConfigDir().resolve("skylands.json").toString();
try(FileWriter writer = new FileWriter(filePath)) {
writer.write(GSON.toJson(this));
}
}
catch(Exception e) {
SkylandsMod.LOGGER.info("Failed to save skylands config due to an exception:\n" + e);
}
}

}
10 changes: 10 additions & 0 deletions src/main/java/skylands/config/SkylandsConfigCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,50 @@ public static void init(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(literal("force-sl").requires(source -> source.hasPermissionLevel(4)).then(literal("config").then(literal("default-spawn-pos").then(argument("position", blockPos()).executes(context -> {
var pos = BlockPosArgumentType.getBlockPos(context, "position");
Skylands.instance.config.defaultSpawnPos = new Vec3d(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5);
Skylands.instance.config.save();
String posText = pos.getX() + " " + pos.getY() + " " + pos.getZ();
context.getSource().sendFeedback(Text.of("config.defaultSpawnPos has changed to: " + posText), true);
return 1;

}))).then(literal("default-visits-pos").then(argument("position", blockPos()).executes(context -> {
var pos = BlockPosArgumentType.getBlockPos(context, "position");
Skylands.instance.config.defaultVisitsPos = new Vec3d(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5);
Skylands.instance.config.save();
String posText = pos.getX() + " " + pos.getY() + " " + pos.getZ();
context.getSource().sendFeedback(Text.of("config.defaultVisitsPos has changed to: " + posText), true);
return 1;

}))).then(literal("teleport-after-island-creation").executes(context -> {
var config = Skylands.instance.config;
config.teleportAfterIslandCreation = !config.teleportAfterIslandCreation;
config.save();
context.getSource().sendFeedback(Text.of("config.teleportAfterIslandCreation has changed to: " + config.teleportAfterIslandCreation), true);
return 1;

})).then(literal("create-island-on-player-join").executes(context -> {
var config = Skylands.instance.config;
config.createIslandOnPlayerJoin = !config.createIslandOnPlayerJoin;
config.save();
context.getSource().sendFeedback(Text.of("config.createIslandOnPlayerJoin has changed to: " + config.createIslandOnPlayerJoin), true);
return 1;

})).then(literal("toggle-update-checker").executes(context -> {
var config = Skylands.instance.config;
config.updateCheckerEnabled = !config.updateCheckerEnabled;
config.save();
context.getSource().sendFeedback(Text.of("config.updateCheckerEnabled has changed to: " + config.updateCheckerEnabled), true);
return 1;

})).then(literal("toggle-right-click-harvest").executes(context -> {
var config = Skylands.instance.config;
config.rightClickHarvestEnabled = !config.rightClickHarvestEnabled;
config.save();
context.getSource().sendFeedback(Text.of("config.rightClickHarvestEnabled has changed to: " + config.rightClickHarvestEnabled), true);
return 1;
})).then(literal("reload").executes(context -> {
Skylands.instance.config = SkylandsConfig.read();
context.getSource().sendFeedback(Text.of("Config successfully reloaded!"), true);
return 1;
}))));

}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/skylands/logic/Skylands.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Skylands(MinecraftServer server) {
this.islands = new IslandStuck();
this.hub = new Hub();
this.invites = new Invites();
this.config = new SkylandsConfig();
this.config = SkylandsConfig.read();
}

public void readFromNbt(NbtCompound nbt) {
Expand All @@ -32,7 +32,7 @@ public void readFromNbt(NbtCompound nbt) {
NbtMigrator.update(skylandsNbt);

this.format = skylandsNbt.getInt("format");
this.config.readFromNbt(skylandsNbt);
// this.config.readFromNbt(skylandsNbt);
this.islands.readFromNbt(skylandsNbt);
this.hub.readFromNbt(skylandsNbt);
}
Expand All @@ -41,7 +41,7 @@ public void writeToNbt(NbtCompound nbt) {
NbtCompound skylandsNbt = new NbtCompound();

skylandsNbt.putInt("format", this.format);
this.config.writeToNbt(skylandsNbt);
// this.config.writeToNbt(skylandsNbt);
this.islands.writeToNbt(skylandsNbt);
this.hub.writeToNbt(skylandsNbt);

Expand Down

0 comments on commit bc34b57

Please sign in to comment.