diff --git a/src/main/java/one/oth3r/sit/file/CustomBlock.java b/src/main/java/one/oth3r/sit/file/CustomBlock.java index e9b8769..0f17147 100644 --- a/src/main/java/one/oth3r/sit/file/CustomBlock.java +++ b/src/main/java/one/oth3r/sit/file/CustomBlock.java @@ -9,6 +9,7 @@ import net.minecraft.util.Identifier; import one.oth3r.sit.utl.Utl; import java.util.ArrayList; +import java.util.Objects; public class CustomBlock { @@ -104,4 +105,16 @@ public class CustomBlock { // not returning true in the loop because there might be a (!) not tag that the block might fall into, after the block was already in another tag return tagCheck; } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + CustomBlock that = (CustomBlock) o; + return Objects.equals(blockIds, that.blockIds) && Objects.equals(blockTags, that.blockTags) && Objects.equals(blockStates, that.blockStates); + } + + @Override + public int hashCode() { + return Objects.hash(blockIds, blockTags, blockStates); + } } diff --git a/src/main/java/one/oth3r/sit/file/CustomItem.java b/src/main/java/one/oth3r/sit/file/CustomItem.java index ec1ef5f..325fe19 100644 --- a/src/main/java/one/oth3r/sit/file/CustomItem.java +++ b/src/main/java/one/oth3r/sit/file/CustomItem.java @@ -7,6 +7,7 @@ import net.minecraft.registry.tag.TagKey; import net.minecraft.util.Identifier; import java.util.ArrayList; +import java.util.Objects; public class CustomItem { @SerializedName("item-ids") @@ -21,6 +22,11 @@ public class CustomItem { this.itemTags = itemTags; } + public CustomItem(CustomItem customItem) { + this.itemIDs = new ArrayList<>(customItem.itemIDs); + this.itemTags = new ArrayList<>(customItem.itemTags); + } + public ArrayList getItemIDs() { return itemIDs; } @@ -62,4 +68,16 @@ public class CustomItem { // not returning true in the loop because there might be a (!) not tag that the item might fall into, after the item was already in another tag return tagCheck; } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + CustomItem that = (CustomItem) o; + return Objects.equals(itemIDs, that.itemIDs) && Objects.equals(itemTags, that.itemTags); + } + + @Override + public int hashCode() { + return Objects.hash(itemIDs, itemTags); + } } diff --git a/src/main/java/one/oth3r/sit/file/HandSetting.java b/src/main/java/one/oth3r/sit/file/HandSetting.java index b0c5527..86c9214 100644 --- a/src/main/java/one/oth3r/sit/file/HandSetting.java +++ b/src/main/java/one/oth3r/sit/file/HandSetting.java @@ -3,6 +3,7 @@ package one.oth3r.sit.file; import com.google.gson.annotations.SerializedName; import java.util.Arrays; +import java.util.Objects; import java.util.stream.Collectors; public class HandSetting { @@ -21,6 +22,11 @@ public class HandSetting { this.filter = filter; } + public HandSetting(HandSetting handSetting) { + this.sittingRequirement = handSetting.sittingRequirement; + this.filter = new Filter(handSetting.filter); + } + public SittingRequirement getSittingRequirement() { return sittingRequirement; } @@ -52,6 +58,12 @@ public class HandSetting { this.customItems = customItems; } + public Filter(Filter filter) { + this.invert = filter.invert; + this.presets = new Presets(filter.presets); + this.customItems = new CustomItem(filter.customItems); + } + public Boolean isInverted() { return invert; } @@ -80,6 +92,12 @@ public class HandSetting { this.usable = usable; } + public Presets(Presets presets) { + this.block = presets.block; + this.food = presets.food; + this.usable = presets.usable; + } + public boolean isBlock() { return block; } @@ -91,6 +109,42 @@ public class HandSetting { public boolean isUsable() { return usable; } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + Presets presets = (Presets) o; + return block == presets.block && food == presets.food && usable == presets.usable; + } + + @Override + public int hashCode() { + return Objects.hash(block, food, usable); + } + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + Filter filter = (Filter) o; + return Objects.equals(invert, filter.invert) && Objects.equals(presets, filter.presets) && Objects.equals(customItems, filter.customItems); + } + + @Override + public int hashCode() { + return Objects.hash(invert, presets, customItems); } } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + HandSetting that = (HandSetting) o; + return sittingRequirement == that.sittingRequirement && Objects.equals(sittingRequirementOptions, that.sittingRequirementOptions) && Objects.equals(filter, that.filter); + } + + @Override + public int hashCode() { + return Objects.hash(sittingRequirement, sittingRequirementOptions, filter); + } } diff --git a/src/main/java/one/oth3r/sit/file/ServerConfig.java b/src/main/java/one/oth3r/sit/file/ServerConfig.java index ed337fc..48321de 100644 --- a/src/main/java/one/oth3r/sit/file/ServerConfig.java +++ b/src/main/java/one/oth3r/sit/file/ServerConfig.java @@ -1,10 +1,13 @@ package one.oth3r.sit.file; import com.google.gson.Gson; +import com.google.gson.JsonElement; import com.google.gson.JsonSyntaxException; import com.google.gson.annotations.SerializedName; import com.google.gson.reflect.TypeToken; import net.minecraft.util.Hand; +import one.oth3r.otterlib.file.CustomFile; +import one.oth3r.otterlib.file.FileSettings; import one.oth3r.sit.utl.Data; import one.oth3r.sit.utl.Utl; import org.jetbrains.annotations.NotNull; @@ -13,8 +16,11 @@ import java.io.File; import java.io.FileInputStream; import java.lang.reflect.Type; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; +import java.util.Objects; import java.util.Properties; import java.util.stream.Collectors; @@ -51,7 +57,7 @@ public class ServerConfig implements CustomFile { public ServerConfig() {} public ServerConfig(ServerConfig serverConfig) { - loadFileData(serverConfig); + copyFileData(serverConfig); } public ServerConfig(Double version, String lang, boolean keepActive, boolean sitWhileSeated, @@ -151,6 +157,18 @@ public class ServerConfig implements CustomFile { public boolean isFullBlocks() { return fullBlocks; } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + PresetBlocks that = (PresetBlocks) o; + return stairs == that.stairs && slabs == that.slabs && carpets == that.carpets && fullBlocks == that.fullBlocks; + } + + @Override + public int hashCode() { + return Objects.hash(stairs, slabs, carpets, fullBlocks); + } } public static class YDifferenceLimit { @@ -187,11 +205,36 @@ public class ServerConfig implements CustomFile { public void setBelow(Double below) { this.below = below; } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + YDifferenceLimit that = (YDifferenceLimit) o; + return Objects.equals(above, that.above) && Objects.equals(below, that.below); + } + + @Override + public int hashCode() { + return Objects.hash(above, below); + } + } + + @Override + public FileSettings getFileSettings() { + return new FileSettings(); + } + + /** + * the path to the file - including the extension ex. usr/config/custom-file.json + */ + @Override + public Path getFilePath() { + return Paths.get(Data.CONFIG_DIR, "server-config.json"); } @Override public void reset() { - loadFileData(new ServerConfig()); + copyFileData(new ServerConfig()); } @Override @@ -199,8 +242,13 @@ public class ServerConfig implements CustomFile { return ServerConfig.class; } + /** + * loads the data from the file object into the current object - DEEP COPY + * + * @param newFile the file to take the properties from + */ @Override - public void loadFileData(ServerConfig newFile) { + public void copyFileData(ServerConfig newFile) { this.version = newFile.version; this.lang = newFile.lang; this.keepActive = newFile.keepActive; @@ -216,8 +264,13 @@ public class ServerConfig implements CustomFile { this.interactionBlocks = newFile.interactionBlocks.stream().map(CustomBlock::new).collect(Collectors.toCollection(ArrayList::new)); } + /** + * updates the file based on the version number of the current instance + * + * @param json + */ @Override - public void update() { + public void update(JsonElement json) { /// update to 2.1, just a new list, nothing to change /// update to 2.2, new settings, no changes if (version >= 2.0 && version <= 2.1) { @@ -225,16 +278,6 @@ public class ServerConfig implements CustomFile { } } - @Override - public String getFileName() { - return "server-config.json"; - } - - @Override - public String getDirectory() { - return Data.CONFIG_DIR; - } - @Override public void fileNotExist() { CustomFile.super.fileNotExist(); @@ -245,6 +288,25 @@ public class ServerConfig implements CustomFile { } } + @Override + public ServerConfig clone() { + ServerConfig clone = new ServerConfig(); + clone.copyFileData(this); + return clone; + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + ServerConfig that = (ServerConfig) o; + return Objects.equals(version, that.version) && Objects.equals(lang, that.lang) && Objects.equals(keepActive, that.keepActive) && Objects.equals(sitWhileSeated, that.sitWhileSeated) && Objects.equals(presetBlocks, that.presetBlocks) && Objects.equals(yDifferenceLimit, that.yDifferenceLimit) && Objects.equals(customEnabled, that.customEnabled) && Objects.equals(sittingBlocks, that.sittingBlocks) && Objects.equals(blacklistedBlocks, that.blacklistedBlocks) && Objects.equals(interactionBlocks, that.interactionBlocks); + } + + @Override + public int hashCode() { + return Objects.hash(version, lang, langOptions, keepActive, sitWhileSeated, presetBlocks, yDifferenceLimit, customEnabled, sittingBlocks, blacklistedBlocks, interactionBlocks); + } + protected static class Legacy { /** * gets the legacy file, from the old directory for fabric, and the same one for spigot diff --git a/src/main/java/one/oth3r/sit/file/SittingBlock.java b/src/main/java/one/oth3r/sit/file/SittingBlock.java index 149cfc0..0541c93 100644 --- a/src/main/java/one/oth3r/sit/file/SittingBlock.java +++ b/src/main/java/one/oth3r/sit/file/SittingBlock.java @@ -3,6 +3,7 @@ package one.oth3r.sit.file; import com.google.gson.annotations.SerializedName; import java.util.ArrayList; +import java.util.Objects; public class SittingBlock extends CustomBlock { @SerializedName("sitting-height") @@ -27,4 +28,17 @@ public class SittingBlock extends CustomBlock { super(sittingBlock); this.sittingHeight = sittingBlock.sittingHeight; } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + SittingBlock that = (SittingBlock) o; + return Objects.equals(sittingHeight, that.sittingHeight); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), sittingHeight); + } } diff --git a/src/main/java/one/oth3r/sit/file/SittingConfig.java b/src/main/java/one/oth3r/sit/file/SittingConfig.java index d9e65e8..22bb840 100644 --- a/src/main/java/one/oth3r/sit/file/SittingConfig.java +++ b/src/main/java/one/oth3r/sit/file/SittingConfig.java @@ -1,10 +1,17 @@ package one.oth3r.sit.file; +import com.google.common.base.Objects; +import com.google.gson.JsonElement; import com.google.gson.annotations.SerializedName; import net.minecraft.util.Hand; +import one.oth3r.otterlib.file.CustomFile; +import one.oth3r.otterlib.file.FileSettings; import one.oth3r.sit.utl.Data; import org.jetbrains.annotations.NotNull; +import java.nio.file.Path; +import java.nio.file.Paths; + public class SittingConfig implements CustomFile { @SerializedName("version") @@ -29,7 +36,7 @@ public class SittingConfig implements CustomFile { } public SittingConfig(SittingConfig sittingConfig) { - loadFileData(sittingConfig); + copyFileData(sittingConfig); } public Double getVersion() { @@ -64,9 +71,19 @@ public class SittingConfig implements CustomFile { return offHand; } + @Override + public FileSettings getFileSettings() { + return new FileSettings(); + } + + @Override + public Path getFilePath() { + return Paths.get(Data.CONFIG_DIR, "sitting-config.json"); + } + @Override public void reset() { - loadFileData(new SittingConfig()); + copyFileData(new SittingConfig()); } @Override @@ -75,24 +92,35 @@ public class SittingConfig implements CustomFile { } @Override - public void loadFileData(SittingConfig newFile) { - this.version = newFile.version; - this.enabled = newFile.enabled; - this.handSitting = newFile.handSitting; - this.mainHand = newFile.mainHand; - this.offHand = newFile.offHand; + public void copyFileData(SittingConfig sittingConfig) { + this.version = sittingConfig.version; + this.enabled = sittingConfig.enabled; + this.handSitting = sittingConfig.handSitting; + this.mainHand = new HandSetting(sittingConfig.mainHand); + this.offHand = new HandSetting(sittingConfig.offHand); } @Override - public void update() {} + public void update(JsonElement jsonElement) { - @Override - public String getFileName() { - return "sitting-config.json"; } @Override - public String getDirectory() { - return Data.CONFIG_DIR; + public SittingConfig clone() { + SittingConfig clone = new SittingConfig(); + clone.copyFileData(this); + return clone; + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + SittingConfig that = (SittingConfig) o; + return Objects.equal(version, that.version) && Objects.equal(enabled, that.enabled) && Objects.equal(handSitting, that.handSitting) && Objects.equal(mainHand, that.mainHand) && Objects.equal(offHand, that.offHand); + } + + @Override + public int hashCode() { + return Objects.hashCode(version, enabled, handSitting, mainHand, offHand); } }