use the otterlib customfile interface

This commit is contained in:
Oth3r 2025-05-04 13:39:34 -05:00
commit 3944881f22
6 changed files with 217 additions and 28 deletions

View file

@ -9,6 +9,7 @@ import net.minecraft.util.Identifier;
import one.oth3r.sit.utl.Utl; import one.oth3r.sit.utl.Utl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects;
public class CustomBlock { 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 // 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; 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);
}
} }

View file

@ -7,6 +7,7 @@ import net.minecraft.registry.tag.TagKey;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects;
public class CustomItem { public class CustomItem {
@SerializedName("item-ids") @SerializedName("item-ids")
@ -21,6 +22,11 @@ public class CustomItem {
this.itemTags = itemTags; this.itemTags = itemTags;
} }
public CustomItem(CustomItem customItem) {
this.itemIDs = new ArrayList<>(customItem.itemIDs);
this.itemTags = new ArrayList<>(customItem.itemTags);
}
public ArrayList<String> getItemIDs() { public ArrayList<String> getItemIDs() {
return itemIDs; 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 // 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; 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);
}
} }

View file

@ -3,6 +3,7 @@ package one.oth3r.sit.file;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class HandSetting { public class HandSetting {
@ -21,6 +22,11 @@ public class HandSetting {
this.filter = filter; this.filter = filter;
} }
public HandSetting(HandSetting handSetting) {
this.sittingRequirement = handSetting.sittingRequirement;
this.filter = new Filter(handSetting.filter);
}
public SittingRequirement getSittingRequirement() { public SittingRequirement getSittingRequirement() {
return sittingRequirement; return sittingRequirement;
} }
@ -52,6 +58,12 @@ public class HandSetting {
this.customItems = customItems; 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() { public Boolean isInverted() {
return invert; return invert;
} }
@ -80,6 +92,12 @@ public class HandSetting {
this.usable = usable; this.usable = usable;
} }
public Presets(Presets presets) {
this.block = presets.block;
this.food = presets.food;
this.usable = presets.usable;
}
public boolean isBlock() { public boolean isBlock() {
return block; return block;
} }
@ -91,6 +109,42 @@ public class HandSetting {
public boolean isUsable() { public boolean isUsable() {
return usable; 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);
}
} }

View file

@ -1,10 +1,13 @@
package one.oth3r.sit.file; package one.oth3r.sit.file;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import net.minecraft.util.Hand; 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.Data;
import one.oth3r.sit.utl.Utl; import one.oth3r.sit.utl.Utl;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -13,8 +16,11 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -51,7 +57,7 @@ public class ServerConfig implements CustomFile<ServerConfig> {
public ServerConfig() {} public ServerConfig() {}
public ServerConfig(ServerConfig serverConfig) { public ServerConfig(ServerConfig serverConfig) {
loadFileData(serverConfig); copyFileData(serverConfig);
} }
public ServerConfig(Double version, String lang, boolean keepActive, boolean sitWhileSeated, public ServerConfig(Double version, String lang, boolean keepActive, boolean sitWhileSeated,
@ -151,6 +157,18 @@ public class ServerConfig implements CustomFile<ServerConfig> {
public boolean isFullBlocks() { public boolean isFullBlocks() {
return fullBlocks; 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 { public static class YDifferenceLimit {
@ -187,11 +205,36 @@ public class ServerConfig implements CustomFile<ServerConfig> {
public void setBelow(Double below) { public void setBelow(Double below) {
this.below = 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 @Override
public void reset() { public void reset() {
loadFileData(new ServerConfig()); copyFileData(new ServerConfig());
} }
@Override @Override
@ -199,8 +242,13 @@ public class ServerConfig implements CustomFile<ServerConfig> {
return ServerConfig.class; 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 @Override
public void loadFileData(ServerConfig newFile) { public void copyFileData(ServerConfig newFile) {
this.version = newFile.version; this.version = newFile.version;
this.lang = newFile.lang; this.lang = newFile.lang;
this.keepActive = newFile.keepActive; this.keepActive = newFile.keepActive;
@ -216,8 +264,13 @@ public class ServerConfig implements CustomFile<ServerConfig> {
this.interactionBlocks = newFile.interactionBlocks.stream().map(CustomBlock::new).collect(Collectors.toCollection(ArrayList::new)); 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 @Override
public void update() { public void update(JsonElement json) {
/// update to 2.1, just a new list, nothing to change /// update to 2.1, just a new list, nothing to change
/// update to 2.2, new settings, no changes /// update to 2.2, new settings, no changes
if (version >= 2.0 && version <= 2.1) { if (version >= 2.0 && version <= 2.1) {
@ -225,16 +278,6 @@ public class ServerConfig implements CustomFile<ServerConfig> {
} }
} }
@Override
public String getFileName() {
return "server-config.json";
}
@Override
public String getDirectory() {
return Data.CONFIG_DIR;
}
@Override @Override
public void fileNotExist() { public void fileNotExist() {
CustomFile.super.fileNotExist(); CustomFile.super.fileNotExist();
@ -245,6 +288,25 @@ public class ServerConfig implements CustomFile<ServerConfig> {
} }
} }
@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 { protected static class Legacy {
/** /**
* gets the legacy file, from the old directory for fabric, and the same one for spigot * gets the legacy file, from the old directory for fabric, and the same one for spigot

View file

@ -3,6 +3,7 @@ package one.oth3r.sit.file;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects;
public class SittingBlock extends CustomBlock { public class SittingBlock extends CustomBlock {
@SerializedName("sitting-height") @SerializedName("sitting-height")
@ -27,4 +28,17 @@ public class SittingBlock extends CustomBlock {
super(sittingBlock); super(sittingBlock);
this.sittingHeight = sittingBlock.sittingHeight; 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);
}
} }

View file

@ -1,10 +1,17 @@
package one.oth3r.sit.file; package one.oth3r.sit.file;
import com.google.common.base.Objects;
import com.google.gson.JsonElement;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import net.minecraft.util.Hand; 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.Data;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.nio.file.Path;
import java.nio.file.Paths;
public class SittingConfig implements CustomFile<SittingConfig> { public class SittingConfig implements CustomFile<SittingConfig> {
@SerializedName("version") @SerializedName("version")
@ -29,7 +36,7 @@ public class SittingConfig implements CustomFile<SittingConfig> {
} }
public SittingConfig(SittingConfig sittingConfig) { public SittingConfig(SittingConfig sittingConfig) {
loadFileData(sittingConfig); copyFileData(sittingConfig);
} }
public Double getVersion() { public Double getVersion() {
@ -64,9 +71,19 @@ public class SittingConfig implements CustomFile<SittingConfig> {
return offHand; return offHand;
} }
@Override
public FileSettings getFileSettings() {
return new FileSettings();
}
@Override
public Path getFilePath() {
return Paths.get(Data.CONFIG_DIR, "sitting-config.json");
}
@Override @Override
public void reset() { public void reset() {
loadFileData(new SittingConfig()); copyFileData(new SittingConfig());
} }
@Override @Override
@ -75,24 +92,35 @@ public class SittingConfig implements CustomFile<SittingConfig> {
} }
@Override @Override
public void loadFileData(SittingConfig newFile) { public void copyFileData(SittingConfig sittingConfig) {
this.version = newFile.version; this.version = sittingConfig.version;
this.enabled = newFile.enabled; this.enabled = sittingConfig.enabled;
this.handSitting = newFile.handSitting; this.handSitting = sittingConfig.handSitting;
this.mainHand = newFile.mainHand; this.mainHand = new HandSetting(sittingConfig.mainHand);
this.offHand = newFile.offHand; this.offHand = new HandSetting(sittingConfig.offHand);
} }
@Override @Override
public void update() {} public void update(JsonElement jsonElement) {
@Override
public String getFileName() {
return "sitting-config.json";
} }
@Override @Override
public String getDirectory() { public SittingConfig clone() {
return Data.CONFIG_DIR; 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);
} }
} }