aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/io/polyfrost/oneconfig/OneConfig.java7
-rw-r--r--src/main/java/io/polyfrost/oneconfig/annotations/Button.java14
-rw-r--r--src/main/java/io/polyfrost/oneconfig/annotations/Category.java12
-rw-r--r--src/main/java/io/polyfrost/oneconfig/annotations/ColorPicker.java14
-rw-r--r--src/main/java/io/polyfrost/oneconfig/annotations/Selector.java15
-rw-r--r--src/main/java/io/polyfrost/oneconfig/annotations/Slider.java15
-rw-r--r--src/main/java/io/polyfrost/oneconfig/annotations/Switch.java13
-rw-r--r--src/main/java/io/polyfrost/oneconfig/annotations/TextField.java15
-rw-r--r--src/main/java/io/polyfrost/oneconfig/interfaces/Config.java63
-rw-r--r--src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java53
-rw-r--r--src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapterFactory.java30
-rw-r--r--src/main/java/io/polyfrost/oneconfig/test/TestConfig.java24
12 files changed, 271 insertions, 4 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/OneConfig.java b/src/main/java/io/polyfrost/oneconfig/OneConfig.java
index 1816427..a025984 100644
--- a/src/main/java/io/polyfrost/oneconfig/OneConfig.java
+++ b/src/main/java/io/polyfrost/oneconfig/OneConfig.java
@@ -1,7 +1,7 @@
package io.polyfrost.oneconfig;
import io.polyfrost.oneconfig.command.OneConfigCommand;
-import io.polyfrost.oneconfig.themes.Themes;
+import io.polyfrost.oneconfig.test.TestConfig;
import net.minecraft.client.Minecraft;
import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.common.MinecraftForge;
@@ -13,11 +13,11 @@ import java.io.File;
@Mod(modid = "oneconfig", name = "OneConfig", version = "${version}")
public class OneConfig {
- private static Minecraft mc = Minecraft.getMinecraft();
+ private static final Minecraft mc = Minecraft.getMinecraft();
public static File jarFile;
public static File oneConfigDir = new File(mc.mcDataDir, "OneConfig/");
public static File themesDir = new File(oneConfigDir, "themes/");
-
+ public static TestConfig config = new TestConfig();
@Mod.EventHandler
public void onPreFMLInit(FMLPreInitializationEvent event) {
@@ -30,6 +30,5 @@ public class OneConfig {
public void onFMLInitialization(FMLInitializationEvent event) {
ClientCommandHandler.instance.registerCommand(new OneConfigCommand());
MinecraftForge.EVENT_BUS.register(this);
- Themes.openTheme(new File("C:\\Users\\Harry\\Documents\\Coding\\Minecraft\\Forge1.8.9\\OneConfig\\run\\OneConfig\\Themes\\one.zip"));
}
}
diff --git a/src/main/java/io/polyfrost/oneconfig/annotations/Button.java b/src/main/java/io/polyfrost/oneconfig/annotations/Button.java
new file mode 100644
index 0000000..98b735f
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/annotations/Button.java
@@ -0,0 +1,14 @@
+package io.polyfrost.oneconfig.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface Button {
+ String name();
+ String description() default "";
+ String text() default "Button";
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/annotations/Category.java b/src/main/java/io/polyfrost/oneconfig/annotations/Category.java
new file mode 100644
index 0000000..05449dc
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/annotations/Category.java
@@ -0,0 +1,12 @@
+package io.polyfrost.oneconfig.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface Category {
+ String name();
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/annotations/ColorPicker.java b/src/main/java/io/polyfrost/oneconfig/annotations/ColorPicker.java
new file mode 100644
index 0000000..bf997a6
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/annotations/ColorPicker.java
@@ -0,0 +1,14 @@
+package io.polyfrost.oneconfig.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface ColorPicker {
+ String name();
+ String description() default "";
+ boolean allowAlpha() default true;
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/annotations/Selector.java b/src/main/java/io/polyfrost/oneconfig/annotations/Selector.java
new file mode 100644
index 0000000..39d238c
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/annotations/Selector.java
@@ -0,0 +1,15 @@
+package io.polyfrost.oneconfig.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface Selector {
+ String name();
+ String description() default "";
+ String[] options();
+ int defaultSelection() default 0;
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/annotations/Slider.java b/src/main/java/io/polyfrost/oneconfig/annotations/Slider.java
new file mode 100644
index 0000000..990c70b
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/annotations/Slider.java
@@ -0,0 +1,15 @@
+package io.polyfrost.oneconfig.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface Slider {
+ String name();
+ String description() default "";
+ int min();
+ int max();
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/annotations/Switch.java b/src/main/java/io/polyfrost/oneconfig/annotations/Switch.java
new file mode 100644
index 0000000..fffb490
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/annotations/Switch.java
@@ -0,0 +1,13 @@
+package io.polyfrost.oneconfig.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface Switch {
+ String name();
+ String description() default "";
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/annotations/TextField.java b/src/main/java/io/polyfrost/oneconfig/annotations/TextField.java
new file mode 100644
index 0000000..5761dd6
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/annotations/TextField.java
@@ -0,0 +1,15 @@
+package io.polyfrost.oneconfig.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface TextField {
+ String name();
+ String description() default "";
+ String placeholder() default "";
+ boolean hideText() default false;
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java b/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java
new file mode 100644
index 0000000..48abf1c
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java
@@ -0,0 +1,63 @@
+package io.polyfrost.oneconfig.interfaces;
+
+import com.google.gson.*;
+
+import java.io.*;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+
+public class Config {
+ private final File configFile;
+
+ public Config(File configFile) {
+ this.configFile = configFile;
+ if (configFile.exists())
+ load();
+ else
+ save();
+ }
+
+ Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting()
+ .registerTypeAdapterFactory(OneConfigTypeAdapterFactory.getStaticTypeAdapterFactory()).create();
+
+ public void save() {
+ try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), StandardCharsets.UTF_8))) {
+ writer.write(gson.toJson(this.getClass()));
+ } catch (IOException ignored) {
+ }
+ }
+
+ public void load() {
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8))) {
+ processPart(new JsonParser().parse(reader).getAsJsonObject(), this.getClass());
+ } catch (IOException ignored) {
+ }
+ }
+
+ private void processPart(JsonObject json, Class<?> clazz) {
+ for (Map.Entry<String, JsonElement> element : json.entrySet()) {
+ String name = element.getKey();
+ JsonElement value = element.getValue();
+ if (value.isJsonObject()) {
+ for (Class<?> innerClass : clazz.getClasses()) {
+ if (innerClass.getSimpleName().equals(name)) {
+ processPart(value.getAsJsonObject(), innerClass);
+ break;
+ }
+ }
+ } else {
+ try {
+ Field field = clazz.getField(name);
+ TypeAdapter<?> adapter = gson.getAdapter(field.getType());
+ Object object = adapter.fromJsonTree(value);
+ field.setAccessible(true);
+ field.set(null, object);
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java
new file mode 100644
index 0000000..95a558d
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java
@@ -0,0 +1,53 @@
+package io.polyfrost.oneconfig.interfaces;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+
+final class OneConfigTypeAdapter<T> extends TypeAdapter<Class<T>> {
+
+ private final Gson gson;
+ private final JsonParser parser = new JsonParser();
+
+ private OneConfigTypeAdapter(final Gson gson) {
+ this.gson = gson;
+ }
+
+ static <T> TypeAdapter<Class<T>> getStaticTypeAdapter(final Gson gson) {
+ return new OneConfigTypeAdapter<>(gson);
+ }
+
+ @Override
+ public void write(final JsonWriter out, final Class<T> value) throws IOException {
+ try {
+ out.beginObject();
+ for (Field field : value.getFields()) {
+ out.name(field.getName());
+ field.setAccessible(true);
+ final Object fieldValue = field.get(null);
+ final TypeAdapter<Object> adapter = (TypeAdapter) gson.getAdapter(field.getType());
+ adapter.write(out, fieldValue);
+ }
+ for (Class<?> clazz : value.getClasses()) {
+ System.out.println(clazz.toString());
+ out.name(clazz.getSimpleName());
+ final TypeAdapter<JsonElement> adapter = gson.getAdapter(JsonElement.class);
+ adapter.write(out, parser.parse(gson.toJson(clazz)));
+ }
+ out.endObject();
+ } catch (final IllegalAccessException ex) {
+ throw new IOException(ex);
+ }
+ }
+
+ @Override
+ public Class<T> read(final JsonReader in) throws IOException {
+ return null;
+ }
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapterFactory.java b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapterFactory.java
new file mode 100644
index 0000000..6491ba2
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapterFactory.java
@@ -0,0 +1,30 @@
+package io.polyfrost.oneconfig.interfaces;
+
+import com.google.gson.Gson;
+import com.google.gson.TypeAdapter;
+import com.google.gson.TypeAdapterFactory;
+import com.google.gson.reflect.TypeToken;
+
+import java.lang.reflect.Type;
+
+import static io.polyfrost.oneconfig.interfaces.OneConfigTypeAdapter.getStaticTypeAdapter;
+
+public class OneConfigTypeAdapterFactory implements TypeAdapterFactory {
+
+ private static final TypeAdapterFactory staticTypeAdapterFactory = new OneConfigTypeAdapterFactory();
+
+ public static TypeAdapterFactory getStaticTypeAdapterFactory() {
+ return staticTypeAdapterFactory;
+ }
+
+ @Override
+ public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) {
+ final Type type = typeToken.getType();
+ if (type.equals(Class.class)) {
+ @SuppressWarnings("unchecked") final TypeAdapter<T> castStaticTypeAdapter = (TypeAdapter<T>) getStaticTypeAdapter(gson);
+ return castStaticTypeAdapter;
+ }
+ return null;
+ }
+
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/test/TestConfig.java b/src/main/java/io/polyfrost/oneconfig/test/TestConfig.java
new file mode 100644
index 0000000..21259ac
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/test/TestConfig.java
@@ -0,0 +1,24 @@
+package io.polyfrost.oneconfig.test;
+
+import io.polyfrost.oneconfig.annotations.Category;
+import io.polyfrost.oneconfig.annotations.Switch;
+import io.polyfrost.oneconfig.annotations.TextField;
+import io.polyfrost.oneconfig.interfaces.Config;
+
+import java.io.File;
+
+public class TestConfig extends Config {
+
+ @Switch(name = "Cool Switch")
+ public static boolean toggle = false;
+
+ @Category(name = "Cool Category")
+ public static class category {
+ @TextField(name = "Cool text field")
+ public static String text = "e";
+ }
+
+ public TestConfig() {
+ super(new File("./config/testConfig.json"));
+ }
+}