Added saved configuration file

This commit is contained in:
hhhzzzsss 2023-06-08 20:58:45 -05:00
parent ecc1ba4271
commit dfa928f255
4 changed files with 307 additions and 238 deletions

View file

@ -385,11 +385,13 @@ public class CommandProcessor {
}
public boolean processCommand(String args) {
if (args.length() > 0) {
SongPlayer.creativeCommand = args;
if (SongPlayer.creativeCommand.startsWith("/")) {
SongPlayer.creativeCommand = SongPlayer.creativeCommand.substring(1);
if (args.startsWith("/")) {
Config.getConfig().creativeCommand = args.substring(1);
} else {
Config.getConfig().creativeCommand = args;
}
SongPlayer.addChatMessage("§6Set creative command to §3/" + SongPlayer.creativeCommand);
SongPlayer.addChatMessage("§6Set creative command to §3/" + Config.getConfig().creativeCommand);
Config.saveConfigWithErrorHandling();
return true;
}
else {
@ -413,11 +415,13 @@ public class CommandProcessor {
}
public boolean processCommand(String args) {
if (args.length() > 0) {
SongPlayer.survivalCommand = args;
if (SongPlayer.survivalCommand.startsWith("/")) {
SongPlayer.survivalCommand = SongPlayer.survivalCommand.substring(1);
if (args.startsWith("/")) {
Config.getConfig().survivalCommand = args.substring(1);
} else {
Config.getConfig().survivalCommand = args;
}
SongPlayer.addChatMessage("§6Set survival command to §3/" + SongPlayer.survivalCommand);
SongPlayer.addChatMessage("§6Set survival command to §3/" + Config.getConfig().survivalCommand);
Config.saveConfigWithErrorHandling();
return true;
}
else {
@ -441,9 +445,10 @@ public class CommandProcessor {
}
public boolean processCommand(String args) {
if (args.length() == 0) {
SongPlayer.creativeCommand = "gmc";
SongPlayer.survivalCommand = "gms";
Config.getConfig().creativeCommand = "gmc";
Config.getConfig().survivalCommand = "gms";
SongPlayer.addChatMessage("§6Now using essentials gamemode commands");
Config.saveConfigWithErrorHandling();
return true;
}
else {
@ -467,9 +472,10 @@ public class CommandProcessor {
}
public boolean processCommand(String args) {
if (args.length() == 0) {
SongPlayer.creativeCommand = "gamemode creative";
SongPlayer.survivalCommand = "gamemode survival";
Config.getConfig().creativeCommand = "gamemode creative";
Config.getConfig().survivalCommand = "gamemode survival";
SongPlayer.addChatMessage("§6Now using vanilla gamemode commands");
Config.saveConfigWithErrorHandling();
return true;
}
else {
@ -493,13 +499,14 @@ public class CommandProcessor {
}
public boolean processCommand(String args) {
if (args.length() == 0) {
SongPlayer.showFakePlayer = !SongPlayer.showFakePlayer;
if (SongPlayer.showFakePlayer) {
Config.getConfig().showFakePlayer = !Config.getConfig().showFakePlayer;
if (Config.getConfig().showFakePlayer) {
SongPlayer.addChatMessage("§6Enabled fake player");
}
else {
SongPlayer.addChatMessage("§6Disabled fake player");
}
Config.saveConfigWithErrorHandling();
return true;
}
else {

View file

@ -0,0 +1,60 @@
package com.github.hhhzzzsss.songplayer;
import com.google.gson.Gson;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class Config {
private static Config config = null;
public static final File CONFIG_FILE = new File(SongPlayer.SONGPLAYER_DIR, "config.json");
private static final Gson gson = new Gson();
public String creativeCommand = "gmc";
public String survivalCommand = "gms";
public boolean showFakePlayer = false;
public static Config getConfig() {
if (config == null) {
config = new Config();
try {
if (CONFIG_FILE.exists()) {
loadConfig();
} else {
saveConfig();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return config;
}
public static void loadConfig() throws IOException {
FileInputStream fis = new FileInputStream(CONFIG_FILE);
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isr);
config = gson.fromJson(reader, Config.class);
reader.close();
}
public static void saveConfig() throws IOException {
FileOutputStream fos = new FileOutputStream(CONFIG_FILE);
OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
BufferedWriter writer = new BufferedWriter(osw);
writer.write(gson.toJson(config));
writer.close();
}
public static void saveConfigWithErrorHandling() {
try {
Config.saveConfig();
} catch (IOException e) {
if (SongPlayer.MC.world != null) {
SongPlayer.addChatMessage("§cFailed to save config file");
}
e.printStackTrace();
}
}
}

View file

@ -15,16 +15,17 @@ public class SongPlayer implements ModInitializer {
public static final int NOTEBLOCK_BASE_ID = Block.getRawIdFromState(Blocks.NOTE_BLOCK.getDefaultState());
public static final File SONG_DIR = new File("songs");
public static boolean showFakePlayer = false;
public static final File SONGPLAYER_DIR = new File("SongPlayer");
public static FakePlayerEntity fakePlayer;
public static String creativeCommand = "gmc";
public static String survivalCommand = "gms";
@Override
public void onInitialize() {
if (!SONG_DIR.exists()) {
SONG_DIR.mkdir();
}
if (!SONGPLAYER_DIR.exists()) {
SONGPLAYER_DIR.mkdir();
}
CommandProcessor.initCommands();
}

View file

@ -1,5 +1,6 @@
package com.github.hhhzzzsss.songplayer.playing;
import com.github.hhhzzzsss.songplayer.Config;
import com.github.hhhzzzsss.songplayer.FakePlayerEntity;
import com.github.hhhzzzsss.songplayer.SongPlayer;
import com.github.hhhzzzsss.songplayer.Util;
@ -69,11 +70,11 @@ public class SongHandler {
stage = new Stage();
stage.movePlayerToStagePosition();
}
if (SongPlayer.showFakePlayer && SongPlayer.fakePlayer == null) {
if (Config.getConfig().showFakePlayer && SongPlayer.fakePlayer == null) {
SongPlayer.fakePlayer = new FakePlayerEntity();
SongPlayer.fakePlayer.copyStagePosAndPlayerLook();
}
if (!SongPlayer.showFakePlayer && SongPlayer.fakePlayer != null) {
if (!Config.getConfig().showFakePlayer && SongPlayer.fakePlayer != null) {
SongPlayer.removeFakePlayer();
}
@ -285,13 +286,13 @@ public class SongHandler {
private void setCreativeIfNeeded() {
cachedCommand = null;
if (SongPlayer.MC.interactionManager.getCurrentGameMode() != GameMode.CREATIVE) {
sendGamemodeCommand(SongPlayer.creativeCommand);
sendGamemodeCommand(Config.getConfig().creativeCommand);
}
}
private void setSurvivalIfNeeded() {
cachedCommand = null;
if (SongPlayer.MC.interactionManager.getCurrentGameMode() != GameMode.SURVIVAL) {
sendGamemodeCommand(SongPlayer.survivalCommand);
sendGamemodeCommand(Config.getConfig().survivalCommand);
}
}