From d6a5bf1d333586c267a5b156eca6b576529fce74 Mon Sep 17 00:00:00 2001 From: isXander Date: Sun, 27 Nov 2022 18:43:24 +0000 Subject: replacement of ConfigInstance#buildConfig in YetAnotherConfigLib.java move some things to main that can be and fix testmod --- .../dev/isxander/yacl/api/utils/Dimension.java | 33 ++++++ .../isxander/yacl/api/utils/MutableDimension.java | 11 ++ .../dev/isxander/yacl/config/ConfigInstance.java | 2 - .../isxander/yacl/config/GsonConfigInstance.java | 5 +- .../isxander/yacl/config/NbtConfigInstance.java | 13 +-- .../yacl/impl/utils/DimensionIntegerImpl.java | 115 +++++++++++++++++++++ .../isxander/yacl/impl/utils/YACLConstants.java | 8 ++ 7 files changed, 177 insertions(+), 10 deletions(-) create mode 100644 src/main/java/dev/isxander/yacl/api/utils/Dimension.java create mode 100644 src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java create mode 100644 src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java create mode 100644 src/main/java/dev/isxander/yacl/impl/utils/YACLConstants.java (limited to 'src/main/java/dev') diff --git a/src/main/java/dev/isxander/yacl/api/utils/Dimension.java b/src/main/java/dev/isxander/yacl/api/utils/Dimension.java new file mode 100644 index 0000000..0de0a58 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/utils/Dimension.java @@ -0,0 +1,33 @@ +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 new file mode 100644 index 0000000..eff0186 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/api/utils/MutableDimension.java @@ -0,0 +1,11 @@ +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/ConfigInstance.java b/src/main/java/dev/isxander/yacl/config/ConfigInstance.java index 18733f3..3ceee6d 100644 --- a/src/main/java/dev/isxander/yacl/config/ConfigInstance.java +++ b/src/main/java/dev/isxander/yacl/config/ConfigInstance.java @@ -16,8 +16,6 @@ import java.lang.reflect.InvocationTargetException; * @param config data type */ public abstract class ConfigInstance { - protected final static Logger logger = LoggerFactory.getLogger("YetAnotherConfigLib"); - private final Class configClass; private final T defaultInstance; private T instance; diff --git a/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java b/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java index 3e075ab..40c2c99 100644 --- a/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java +++ b/src/main/java/dev/isxander/yacl/config/GsonConfigInstance.java @@ -1,6 +1,7 @@ package dev.isxander.yacl.config; import com.google.gson.*; +import dev.isxander.yacl.impl.utils.YACLConstants; import net.minecraft.text.Style; import net.minecraft.text.Text; @@ -53,7 +54,7 @@ public class GsonConfigInstance extends ConfigInstance { @Override public void save() { try { - logger.info("Saving {}...", getConfigClass().getSimpleName()); + YACLConstants.LOGGER.info("Saving {}...", getConfigClass().getSimpleName()); Files.writeString(path, gson.toJson(getConfig()), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); } catch (IOException e) { e.printStackTrace(); @@ -68,7 +69,7 @@ public class GsonConfigInstance extends ConfigInstance { return; } - logger.info("Loading {}...", getConfigClass().getSimpleName()); + YACLConstants.LOGGER.info("Loading {}...", getConfigClass().getSimpleName()); setConfig(gson.fromJson(Files.readString(path), getConfigClass())); } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/dev/isxander/yacl/config/NbtConfigInstance.java b/src/main/java/dev/isxander/yacl/config/NbtConfigInstance.java index 8f817cb..5749695 100644 --- a/src/main/java/dev/isxander/yacl/config/NbtConfigInstance.java +++ b/src/main/java/dev/isxander/yacl/config/NbtConfigInstance.java @@ -1,5 +1,6 @@ package dev.isxander.yacl.config; +import dev.isxander.yacl.impl.utils.YACLConstants; import net.minecraft.nbt.*; import java.awt.*; @@ -57,13 +58,13 @@ public class NbtConfigInstance extends ConfigInstance { @Override public void save() { - logger.info("Saving {}...", getConfigClass().getSimpleName()); + YACLConstants.LOGGER.info("Saving {}...", getConfigClass().getSimpleName()); NbtCompound nbt; try { nbt = (NbtCompound) serializeObject(getConfig(), nbtSerializerHolder, field -> field.isAnnotationPresent(ConfigEntry.class)); } catch (IllegalAccessException e) { - logger.error("Failed to convert '{}' -> NBT", getConfigClass().getName(), e); + YACLConstants.LOGGER.error("Failed to convert '{}' -> NBT", getConfigClass().getName(), e); return; } @@ -76,7 +77,7 @@ public class NbtConfigInstance extends ConfigInstance { else NbtIo.write(nbt, new DataOutputStream(fos)); } catch (IOException e) { - logger.error("Failed to write NBT to '{}'", path, e); + YACLConstants.LOGGER.error("Failed to write NBT to '{}'", path, e); } } @@ -87,19 +88,19 @@ public class NbtConfigInstance extends ConfigInstance { return; } - logger.info("Loading {}...", getConfigClass().getSimpleName()); + YACLConstants.LOGGER.info("Loading {}...", getConfigClass().getSimpleName()); NbtCompound nbt; try { nbt = compressed ? NbtIo.readCompressed(path.toFile()) : NbtIo.read(path.toFile()); } catch (IOException e) { - logger.error("Failed to read NBT file '{}'", path, e); + YACLConstants.LOGGER.error("Failed to read NBT file '{}'", path, e); return; } try { setConfig(deserializeObject(nbt, getConfigClass(), nbtSerializerHolder, field -> field.isAnnotationPresent(ConfigEntry.class))); } catch (InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException e) { - logger.error("Failed to convert NBT -> '{}'", getConfigClass().getName(), e); + YACLConstants.LOGGER.error("Failed to convert NBT -> '{}'", getConfigClass().getName(), e); } } diff --git a/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java b/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java new file mode 100644 index 0000000..6c7508d --- /dev/null +++ b/src/main/java/dev/isxander/yacl/impl/utils/DimensionIntegerImpl.java @@ -0,0 +1,115 @@ +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 new file mode 100644 index 0000000..3d382d4 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/impl/utils/YACLConstants.java @@ -0,0 +1,8 @@ +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