From 13c7ba45ff201423eb8dba8a40cfb66ebb531439 Mon Sep 17 00:00:00 2001 From: Xander Date: Tue, 25 Apr 2023 16:28:41 +0100 Subject: Architectury! (#61) --- .../dev/isxander/yacl/api/utils/Dimension.java | 33 ---- .../isxander/yacl/api/utils/MutableDimension.java | 11 -- .../java/dev/isxander/yacl/config/ConfigEntry.java | 11 -- .../dev/isxander/yacl/config/ConfigInstance.java | 48 ----- .../isxander/yacl/config/GsonConfigInstance.java | 212 --------------------- .../yacl/impl/utils/DimensionIntegerImpl.java | 115 ----------- .../isxander/yacl/impl/utils/YACLConstants.java | 8 - 7 files changed, 438 deletions(-) delete mode 100644 src/main/java/dev/isxander/yacl/api/utils/Dimension.java delete mode 100644 src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java delete mode 100644 src/main/java/dev/isxander/yacl/config/ConfigEntry.java delete mode 100644 src/main/java/dev/isxander/yacl/config/ConfigInstance.java delete mode 100644 src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java delete mode 100644 src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java delete mode 100644 src/main/java/dev/isxander/yacl/impl/utils/YACLConstants.java (limited to 'src/main/java') diff --git a/src/main/java/dev/isxander/yacl/api/utils/Dimension.java b/src/main/java/dev/isxander/yacl/api/utils/Dimension.java deleted file mode 100644 index 0de0a58..0000000 --- a/src/main/java/dev/isxander/yacl/api/utils/Dimension.java +++ /dev/null @@ -1,33 +0,0 @@ -package dev.isxander.yacl.api.utils; - -import dev.isxander.yacl.impl.utils.DimensionIntegerImpl; - -public interface Dimension { - T x(); - T y(); - - T width(); - T height(); - - T xLimit(); - T yLimit(); - - T centerX(); - T centerY(); - - boolean isPointInside(T x, T y); - - MutableDimension clone(); - - Dimension withX(T x); - Dimension withY(T y); - Dimension withWidth(T width); - Dimension withHeight(T height); - - Dimension moved(T x, T y); - Dimension expanded(T width, T height); - - static MutableDimension ofInt(int x, int y, int width, int height) { - return new DimensionIntegerImpl(x, y, width, height); - } -} diff --git a/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java b/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java deleted file mode 100644 index eff0186..0000000 --- a/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java +++ /dev/null @@ -1,11 +0,0 @@ -package dev.isxander.yacl.api.utils; - -public interface MutableDimension extends Dimension { - MutableDimension setX(T x); - MutableDimension setY(T y); - MutableDimension setWidth(T width); - MutableDimension setHeight(T height); - - MutableDimension move(T x, T y); - MutableDimension expand(T width, T height); -} diff --git a/src/main/java/dev/isxander/yacl/config/ConfigEntry.java b/src/main/java/dev/isxander/yacl/config/ConfigEntry.java deleted file mode 100644 index 7f04c33..0000000 --- a/src/main/java/dev/isxander/yacl/config/ConfigEntry.java +++ /dev/null @@ -1,11 +0,0 @@ -package dev.isxander.yacl.config; - -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 ConfigEntry { -} diff --git a/src/main/java/dev/isxander/yacl/config/ConfigInstance.java b/src/main/java/dev/isxander/yacl/config/ConfigInstance.java deleted file mode 100644 index c207161..0000000 --- a/src/main/java/dev/isxander/yacl/config/ConfigInstance.java +++ /dev/null @@ -1,48 +0,0 @@ -package dev.isxander.yacl.config; - -import java.lang.reflect.InvocationTargetException; - -/** - * Responsible for handing the actual config data type. - * Holds the instance along with a final default instance - * to reference default values for options and should not be changed. - * - * Abstract methods to save and load the class, implementations are responsible for - * how it saves and load. - * - * @param config data type - */ -public abstract class ConfigInstance { - private final Class configClass; - private final T defaultInstance; - private T instance; - - public ConfigInstance(Class configClass) { - this.configClass = configClass; - - try { - this.defaultInstance = this.instance = configClass.getConstructor().newInstance(); - } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { - throw new IllegalStateException(String.format("Could not create default instance of config for %s. Make sure there is a default constructor!", this.configClass.getSimpleName())); - } - } - - public abstract void save(); - public abstract void load(); - - public T getConfig() { - return this.instance; - } - - protected void setConfig(T instance) { - this.instance = instance; - } - - public T getDefaults() { - return this.defaultInstance; - } - - public Class getConfigClass() { - return this.configClass; - } -} diff --git a/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java b/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java deleted file mode 100644 index ad7f550..0000000 --- a/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java +++ /dev/null @@ -1,212 +0,0 @@ -package dev.isxander.yacl.config; - -import com.google.gson.*; -import dev.isxander.yacl.impl.utils.YACLConstants; -import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.Style; - -import java.awt.*; -import java.io.IOException; -import java.lang.reflect.Type; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardOpenOption; -import java.util.function.UnaryOperator; - -/** - * Uses GSON to serialize and deserialize config data from JSON to a file. - * - * Only fields annotated with {@link ConfigEntry} are included in the JSON. - * {@link Component}, {@link Style} and {@link Color} have default type adapters, so there is no need to provide them in your GSON instance. - * GSON is automatically configured to format fields as {@code lower_camel_case}. - * - * @param config data type - */ -public class GsonConfigInstance extends ConfigInstance { - private final Gson gson; - private final Path path; - - @Deprecated - public GsonConfigInstance(Class configClass, Path path) { - this(configClass, path, new GsonBuilder()); - } - - @Deprecated - public GsonConfigInstance(Class configClass, Path path, Gson gson) { - this(configClass, path, gson.newBuilder()); - } - - @Deprecated - public GsonConfigInstance(Class configClass, Path path, UnaryOperator builder) { - this(configClass, path, builder.apply(new GsonBuilder())); - } - - @Deprecated - public GsonConfigInstance(Class configClass, Path path, GsonBuilder builder) { - super(configClass); - this.path = path; - this.gson = builder - .setExclusionStrategies(new ConfigExclusionStrategy()) - .registerTypeHierarchyAdapter(Component.class, new Component.Serializer()) - .registerTypeHierarchyAdapter(Style.class, new Style.Serializer()) - .registerTypeHierarchyAdapter(Color.class, new ColorTypeAdapter()) - .serializeNulls() - .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) - .create(); - } - - private GsonConfigInstance(Class configClass, Path path, Gson gson, boolean fromBuilder) { - super(configClass); - this.path = path; - this.gson = gson; - } - - @Override - public void save() { - try { - YACLConstants.LOGGER.info("Saving {}...", getConfigClass().getSimpleName()); - Files.writeString(path, gson.toJson(getConfig()), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Override - public void load() { - try { - if (Files.notExists(path)) { - save(); - return; - } - - YACLConstants.LOGGER.info("Loading {}...", getConfigClass().getSimpleName()); - setConfig(gson.fromJson(Files.readString(path), getConfigClass())); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public Path getPath() { - return this.path; - } - - private static class ConfigExclusionStrategy implements ExclusionStrategy { - @Override - public boolean shouldSkipField(FieldAttributes fieldAttributes) { - return fieldAttributes.getAnnotation(ConfigEntry.class) == null; - } - - @Override - public boolean shouldSkipClass(Class aClass) { - return false; - } - } - - public static class ColorTypeAdapter implements JsonSerializer, JsonDeserializer { - @Override - public Color deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { - return new Color(jsonElement.getAsInt(), true); - } - - @Override - public JsonElement serialize(Color color, Type type, JsonSerializationContext jsonSerializationContext) { - return new JsonPrimitive(color.getRGB()); - } - } - - /** - * Creates a builder for a GSON config instance. - * @param configClass the config class - * @return a new builder - * @param the config type - */ - public static Builder createBuilder(Class configClass) { - return new Builder<>(configClass); - } - - public static class Builder { - private final Class configClass; - private Path path; - private UnaryOperator gsonBuilder = builder -> builder - .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) - .serializeNulls() - .registerTypeHierarchyAdapter(Component.class, new Component.Serializer()) - .registerTypeHierarchyAdapter(Style.class, new Style.Serializer()) - .registerTypeHierarchyAdapter(Color.class, new ColorTypeAdapter()); - - private Builder(Class configClass) { - this.configClass = configClass; - } - - /** - * Sets the file path to save and load the config from. - */ - public Builder setPath(Path path) { - this.path = path; - return this; - } - - /** - * Sets the GSON instance to use. Overrides all YACL defaults such as: - *
    - *
  • lower_camel_case field naming policy
  • - *
  • null serialization
  • - *
  • {@link Component}, {@link Style} and {@link Color} type adapters
  • - *
- * Still respects the exclusion strategy to only serialize {@link ConfigEntry} - * but these can be added to with setExclusionStrategies. - * - * @param gsonBuilder gson builder to use - */ - public Builder overrideGsonBuilder(GsonBuilder gsonBuilder) { - this.gsonBuilder = builder -> gsonBuilder; - return this; - } - - /** - * Sets the GSON instance to use. Overrides all YACL defaults such as: - *
    - *
  • lower_camel_case field naming policy
  • - *
  • null serialization
  • - *
  • {@link Component}, {@link Style} and {@link Color} type adapters
  • - *
- * Still respects the exclusion strategy to only serialize {@link ConfigEntry} - * but these can be added to with setExclusionStrategies. - * - * @param gson gson instance to be converted to a builder - */ - public Builder overrideGsonBuilder(Gson gson) { - return this.overrideGsonBuilder(gson.newBuilder()); - } - - /** - * Appends extra configuration to a GSON builder. - * This is the intended way to add functionality to the GSON instance. - *

- * By default, YACL sets the GSON with the following options: - *

    - *
  • lower_camel_case field naming policy
  • - *
  • null serialization
  • - *
  • {@link Component}, {@link Style} and {@link Color} type adapters
  • - *
- * - * @param gsonBuilder the function to apply to the builder - */ - public Builder appendGsonBuilder(UnaryOperator gsonBuilder) { - this.gsonBuilder = builder -> gsonBuilder.apply(this.gsonBuilder.apply(builder)); - return this; - } - - /** - * Builds the config instance. - * @return the built config instance - */ - public GsonConfigInstance build() { - UnaryOperator gsonBuilder = builder -> this.gsonBuilder.apply(builder) - .addSerializationExclusionStrategy(new ConfigExclusionStrategy()) - .addDeserializationExclusionStrategy(new ConfigExclusionStrategy()); - - return new GsonConfigInstance<>(configClass, path, gsonBuilder.apply(new GsonBuilder()).create(), true); - } - } -} diff --git a/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java b/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java deleted file mode 100644 index 6c7508d..0000000 --- a/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java +++ /dev/null @@ -1,115 +0,0 @@ -package dev.isxander.yacl.impl.utils; - -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.api.utils.MutableDimension; - -public class DimensionIntegerImpl implements MutableDimension { - private int x, y; - private int width, height; - - public DimensionIntegerImpl(int x, int y, int width, int height) { - this.x = x; - this.y = y; - this.width = width; - this.height = height; - } - - @Override - public Integer x() { - return x; - } - - @Override - public Integer y() { - return y; - } - - @Override - public Integer width() { - return width; - } - - @Override - public Integer height() { - return height; - } - - @Override - public Integer xLimit() { - return x + width; - } - - @Override - public Integer yLimit() { - return y + height; - } - - @Override - public Integer centerX() { - return x + width / 2; - } - - @Override - public Integer centerY() { - return y + height / 2; - } - - @Override - public boolean isPointInside(Integer x, Integer y) { - return x >= x() && x <= xLimit() && y >= y() && y <= yLimit(); - } - - @Override - public MutableDimension clone() { - return new DimensionIntegerImpl(x, y, width, height); - } - - @Override public MutableDimension setX(Integer x) { this.x = x; return this; } - @Override public MutableDimension setY(Integer y) { this.y = y; return this; } - @Override public MutableDimension setWidth(Integer width) { this.width = width; return this; } - @Override public MutableDimension setHeight(Integer height) { this.height = height; return this; } - - @Override - public Dimension withX(Integer x) { - return clone().setX(x); - } - - @Override - public Dimension withY(Integer y) { - return clone().setY(y); - } - - @Override - public Dimension withWidth(Integer width) { - return clone().setWidth(width); - } - - @Override - public Dimension withHeight(Integer height) { - return clone().setHeight(height); - } - - @Override - public MutableDimension move(Integer x, Integer y) { - this.x += x; - this.y += y; - return this; - } - - @Override - public MutableDimension expand(Integer width, Integer height) { - this.width += width; - this.height += height; - return this; - } - - @Override - public Dimension moved(Integer x, Integer y) { - return clone().move(x, y); - } - - @Override - public Dimension expanded(Integer width, Integer height) { - return clone().expand(width, height); - } -} diff --git a/src/main/java/dev/isxander/yacl/impl/utils/YACLConstants.java b/src/main/java/dev/isxander/yacl/impl/utils/YACLConstants.java deleted file mode 100644 index 3d382d4..0000000 --- a/src/main/java/dev/isxander/yacl/impl/utils/YACLConstants.java +++ /dev/null @@ -1,8 +0,0 @@ -package dev.isxander.yacl.impl.utils; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class YACLConstants { - public static final Logger LOGGER = LoggerFactory.getLogger("YetAnotherConfigLib"); -} -- cgit