Added config platform and utils

This commit is contained in:
Aleksey 2020-09-22 15:51:54 +03:00
parent 387655f150
commit 41e0d1e42a
8 changed files with 733 additions and 0 deletions

View file

@ -0,0 +1,89 @@
package ru.betterend.util;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.resource.Resource;
import ru.betterend.BetterEnd;
public class JsonFactory {
public final static Gson GSON = new GsonBuilder().setPrettyPrinting().create();
public static JsonObject getJsonObject(String path) throws IOException {
try (InputStream is = JsonFactory.class.getResourceAsStream(path)) {
Reader reader = new InputStreamReader(is);
JsonObject jsonObject = loadJson(reader).getAsJsonObject();
if (jsonObject == null) {
return new JsonObject();
}
return jsonObject;
}
}
public static JsonObject getJsonObject(Resource jsonSource) {
if (jsonSource != null) {
try (InputStream is = jsonSource.getInputStream()) {
Reader reader = new InputStreamReader(is);
JsonObject jsonObject = loadJson(reader).getAsJsonObject();
if (jsonObject == null) {
return new JsonObject();
}
return jsonObject;
} catch (IOException ex) {
BetterEnd.LOGGER.catching(ex);
return new JsonObject();
}
}
return new JsonObject();
}
public static JsonObject getJsonObject(File jsonFile) {
if (jsonFile.exists()) {
JsonObject jsonObject = loadJson(jsonFile).getAsJsonObject();
if (jsonObject == null) {
return new JsonObject();
}
return jsonObject;
}
return new JsonObject();
}
public static JsonElement loadJson(File jsonFile) {
if (jsonFile.exists()) {
try (Reader reader = new FileReader(jsonFile)) {
return loadJson(reader);
} catch (Exception ex) {
BetterEnd.LOGGER.catching(ex);
}
}
return null;
}
public static JsonElement loadJson(Reader reader) {
return GSON.fromJson(reader, JsonElement.class);
}
public static void storeJson(File jsonFile, JsonElement jsonObject) {
try(FileWriter writer = new FileWriter(jsonFile)) {
String json = GSON.toJson(jsonObject);
writer.write(json);
writer.flush();
} catch (IOException ex) {
BetterEnd.LOGGER.catching(ex);
}
}
}

View file

@ -0,0 +1,37 @@
package ru.betterend.util;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.text.TranslatableText;
import ru.betterend.BetterEnd;
public class LangUtil {
private final static String MODID = BetterEnd.MOD_ID;
public final static String CONFIG_ELEMENT = "configuration";
private String element;
public LangUtil(String element) {
this.element = element;
}
public void setElement(String key) {
this.element = key;
}
public String getString(String key) {
return getString(element, key);
}
public TranslatableText getText(String key) {
return getText(element, key);
}
public static String getString(String element, String key) {
return I18n.translate(String.format("%s.%s.%s", MODID, element, key));
}
public static TranslatableText getText(String element, String key) {
return new TranslatableText(getString(element, key));
}
}

View file

@ -0,0 +1,68 @@
package ru.betterend.util;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import ru.betterend.BetterEnd;
public final class Logger {
private static final org.apache.logging.log4j.Logger LOGGER = LogManager.getLogger();
private String modPref = "[" + BetterEnd.MOD_ID + "] ";
private Logger() {}
public static Logger get() {
return new Logger();
}
public void log(Level level, String message) {
LOGGER.log(level, modPref + message);
}
public void log(Level level, String message, Object... params) {
LOGGER.log(level, modPref + message, params);
}
public void debug(Object message) {
this.log(Level.DEBUG, message.toString());
}
public void debug(Object message, Object... params) {
this.log(Level.DEBUG, message.toString(), params);
}
public void catching(Throwable ex) {
this.error(ex.getLocalizedMessage());
LOGGER.catching(ex);
}
public void info(String message) {
this.log(Level.INFO, message);
}
public void info(String message, Object... params) {
this.log(Level.INFO, message, params);
}
public void warning(String message) {
this.log(Level.WARN, message);
}
public void warning(String message, Object obj, Exception ex) {
LOGGER.warn(modPref + message, obj, ex);
}
public void error(String message) {
this.log(Level.ERROR, message);
}
public void error(String message, Object obj, Exception ex) {
LOGGER.error(modPref + message, obj, ex);
}
public void error(String message, Exception ex) {
LOGGER.error(modPref + message, ex);
}
}