hand-config -> sitting-config

This commit is contained in:
Oth3r 2024-07-23 11:27:06 -05:00
commit e105f36dc8
4 changed files with 65 additions and 61 deletions

View file

@ -20,28 +20,28 @@ public class Data {
} }
/** /**
* The default hand config for all new players * The default sitting config for all new players
*/ */
private static HandConfig handConfig = new HandConfig(); private static SittingConfig sittingConfig = new SittingConfig();
public static HandConfig getHandConfig() { public static SittingConfig getSittingConfig() {
return new HandConfig(handConfig); return new SittingConfig(sittingConfig);
} }
public static void setHandConfig(HandConfig newHandConfig) { public static void setSittingConfig(SittingConfig newSittingConfig) {
handConfig = new HandConfig(newHandConfig); sittingConfig = new SittingConfig(newSittingConfig);
} }
/** /**
* the hand config stored per player on the server * the sitting config stored per player on the server
*/ */
private static final HashMap<ServerPlayerEntity, HandConfig> playerSettings = new HashMap<>(); private static final HashMap<ServerPlayerEntity, SittingConfig> playerSettings = new HashMap<>();
public static void clearPlayerSettings() { public static void clearPlayerSettings() {
playerSettings.clear(); playerSettings.clear();
} }
public static void setPlayerSetting(ServerPlayerEntity player, HandConfig config) { public static void setPlayerSetting(ServerPlayerEntity player, SittingConfig config) {
playerSettings.put(player, config); playerSettings.put(player, config);
} }
@ -49,8 +49,8 @@ public class Data {
playerSettings.remove(player); playerSettings.remove(player);
} }
public static HandConfig getPlayerSetting(ServerPlayerEntity player) { public static SittingConfig getPlayerSetting(ServerPlayerEntity player) {
return playerSettings.getOrDefault(player,handConfig); return playerSettings.getOrDefault(player, sittingConfig);
} }
/** /**
@ -97,6 +97,6 @@ public class Data {
*/ */
public static void loadFiles(boolean tryLegacy) { public static void loadFiles(boolean tryLegacy) {
ServerConfig.load(tryLegacy); ServerConfig.load(tryLegacy);
HandConfig.load(); SittingConfig.load();
} }
} }

View file

@ -12,32 +12,36 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
public class HandConfig { public class SittingConfig {
@SerializedName("version") @SerializedName("version")
private Double version = 1.0; private Double version = 1.0;
@SerializedName("enabled")
private Boolean enabled = true;
@SerializedName("hand-sitting") @SerializedName("hand-sitting")
private boolean handSitting = true; private Boolean handSitting = true;
@SerializedName("main-hand") @SerializedName("main-hand")
private HandSetting mainHand = new HandSetting(HandSetting.SittingRequirement.EMPTY, new HandSetting.Filter()); private HandSetting mainHand = new HandSetting(HandSetting.SittingRequirement.EMPTY, new HandSetting.Filter());
@SerializedName("off-hand") @SerializedName("off-hand")
private HandSetting offHand = new HandSetting(HandSetting.SittingRequirement.FILTER, private HandSetting offHand = new HandSetting(HandSetting.SittingRequirement.FILTER,
new HandSetting.Filter(false,true,false,new ArrayList<>(),new ArrayList<>())); // todo fill out some fox examples sake new HandSetting.Filter(false,true,false,new ArrayList<>(),new ArrayList<>())); // todo fill out some fox examples sake
public HandConfig() {} public SittingConfig() {}
public HandConfig(double version, boolean handSitting, HandSetting mainHand, HandSetting offHand) { public SittingConfig(double version, boolean enabled, boolean handSitting, HandSetting mainHand, HandSetting offHand) {
this.version = version; this.version = version;
this.enabled = enabled;
this.handSitting = handSitting; this.handSitting = handSitting;
this.mainHand = mainHand; this.mainHand = mainHand;
this.offHand = offHand; this.offHand = offHand;
} }
public HandConfig(HandConfig handConfig) { public SittingConfig(SittingConfig sittingConfig) {
this.version = handConfig.version; this.version = sittingConfig.version;
this.handSitting = handConfig.handSitting; this.enabled = sittingConfig.enabled;
this.mainHand = handConfig.mainHand; this.handSitting = sittingConfig.handSitting;
this.offHand = handConfig.offHand; this.mainHand = sittingConfig.mainHand;
this.offHand = sittingConfig.offHand;
} }
public Double getVersion() { public Double getVersion() {
@ -61,7 +65,7 @@ public class HandConfig {
} }
public static File getFile() { public static File getFile() {
return new File(Sit.CONFIG_DIR+"hand-config.json"); return new File(Sit.CONFIG_DIR+"sitting-config.json");
} }
/** /**
@ -73,7 +77,7 @@ public class HandConfig {
if (!file.exists()) save(); if (!file.exists()) save();
// try reading the file // try reading the file
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) { try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
Updater.HandConfigFile.run(reader); Updater.SittingConfigFile.run(reader);
} catch (Exception e) { } catch (Exception e) {
Sit.LOGGER.error(String.format("ERROR LOADING '%s`: %s", file.getName(),e.getMessage())); Sit.LOGGER.error(String.format("ERROR LOADING '%s`: %s", file.getName(),e.getMessage()));
} }
@ -89,7 +93,7 @@ public class HandConfig {
Sit.LOGGER.info(String.format("Creating new `%s`", getFile().getName())); Sit.LOGGER.info(String.format("Creating new `%s`", getFile().getName()));
} }
try (BufferedWriter writer = Files.newBufferedWriter(getFile().toPath(), StandardCharsets.UTF_8)) { try (BufferedWriter writer = Files.newBufferedWriter(getFile().toPath(), StandardCharsets.UTF_8)) {
writer.write(Utl.getGson().toJson(Data.getHandConfig())); writer.write(Utl.getGson().toJson(Data.getSittingConfig()));
} catch (Exception e) { } catch (Exception e) {
Sit.LOGGER.error(String.format("ERROR SAVING '%s`: %s", getFile().getName(), e.getMessage())); Sit.LOGGER.error(String.format("ERROR SAVING '%s`: %s", getFile().getName(), e.getMessage()));
} }

View file

@ -17,7 +17,7 @@ import java.util.*;
public class Updater { public class Updater {
public static class HandConfigFile { public static class SittingConfigFile {
/** /**
* runs the updater from the file reader and sets the loaded settings when finished * runs the updater from the file reader and sets the loaded settings when finished
@ -27,34 +27,34 @@ public class Updater {
public static void run(BufferedReader reader) public static void run(BufferedReader reader)
throws NullPointerException { throws NullPointerException {
// try to read the json // try to read the json
HandConfig handConfig; SittingConfig sittingConfig;
try { try {
handConfig = Utl.getGson().fromJson(reader, HandConfig.class); sittingConfig = Utl.getGson().fromJson(reader, SittingConfig.class);
} catch (Exception e) { } catch (Exception e) {
throw new NullPointerException(); throw new NullPointerException();
} }
// throw null if the fileData is null or version is null // throw null if the fileData is null or version is null
if (handConfig == null) throw new NullPointerException(); if (sittingConfig == null) throw new NullPointerException();
// get the file version // get the file version
Double version = handConfig.getVersion(); Double version = sittingConfig.getVersion();
// if there's no version, throw // if there's no version, throw
if (version == null) throw new NullPointerException(); if (version == null) throw new NullPointerException();
// update the config (using the non-null version) // update the config (using the non-null version)
handConfig = update(handConfig); sittingConfig = update(sittingConfig);
// set the config in the mod data // set the config in the mod data
Data.setHandConfig(handConfig); Data.setSittingConfig(sittingConfig);
} }
/** /**
* updates the file * updates the file
*/ */
public static HandConfig update(HandConfig old) { public static SittingConfig update(SittingConfig old) {
HandConfig serverConfig = new HandConfig(old); SittingConfig serverConfig = new SittingConfig(old);
return serverConfig; return serverConfig;
} }
} }
@ -202,19 +202,19 @@ public class Updater {
new ArrayList<>() new ArrayList<>()
); );
HandConfig defaultHandConfig = new HandConfig(); SittingConfig defaultSittingConfig = new SittingConfig();
HandConfig handConfig = null; SittingConfig sittingConfig = null;
// * filters are flipped because the way they work are flipped // * filters are flipped because the way they work are flipped
try { try {
handConfig = new HandConfig( sittingConfig = new SittingConfig(
1.0, Boolean.parseBoolean((String) properties.computeIfAbsent("hand.sitting", a -> String.valueOf(defaultHandConfig.canSitWithHand()))), 1.0, true, Boolean.parseBoolean((String) properties.computeIfAbsent("hand.sitting", a -> String.valueOf(defaultSittingConfig.canSitWithHand()))),
new HandSetting( new HandSetting(
Utl.Enum.get(properties.computeIfAbsent("hand.main.requirement", a -> String.valueOf(defaultHandConfig.getHand(Hand.MAIN_HAND).getSittingRequirement())),HandSetting.SittingRequirement.class,HandSetting.SittingRequirement.FILTER), Utl.Enum.get(properties.computeIfAbsent("hand.main.requirement", a -> String.valueOf(defaultSittingConfig.getHand(Hand.MAIN_HAND).getSittingRequirement())),HandSetting.SittingRequirement.class,HandSetting.SittingRequirement.FILTER),
new HandSetting.Filter( new HandSetting.Filter(
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.block", a -> String.valueOf(!defaultHandConfig.getHand(Hand.MAIN_HAND).getFilter().isBlock()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isBlock()))),
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.food", a -> String.valueOf(!defaultHandConfig.getHand(Hand.MAIN_HAND).getFilter().isFood()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isFood()))),
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.usable", a -> String.valueOf(!defaultHandConfig.getHand(Hand.MAIN_HAND).getFilter().isUsable()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isUsable()))),
getFilterList( getFilterList(
new Gson().fromJson((String) properties.computeIfAbsent("hand.main.whitelist", a -> "[]"), listType), new Gson().fromJson((String) properties.computeIfAbsent("hand.main.whitelist", a -> "[]"), listType),
new Gson().fromJson((String) properties.computeIfAbsent("hand.main.blacklist", a -> "[]"), listType) new Gson().fromJson((String) properties.computeIfAbsent("hand.main.blacklist", a -> "[]"), listType)
@ -223,11 +223,11 @@ public class Updater {
) )
), ),
new HandSetting( new HandSetting(
Utl.Enum.get(properties.computeIfAbsent("hand.off.requirement", a -> String.valueOf(defaultHandConfig.getHand(Hand.OFF_HAND).getSittingRequirement())),HandSetting.SittingRequirement.class,HandSetting.SittingRequirement.FILTER), Utl.Enum.get(properties.computeIfAbsent("hand.off.requirement", a -> String.valueOf(defaultSittingConfig.getHand(Hand.OFF_HAND).getSittingRequirement())),HandSetting.SittingRequirement.class,HandSetting.SittingRequirement.FILTER),
new HandSetting.Filter( new HandSetting.Filter(
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.block", a -> String.valueOf(!defaultHandConfig.getHand(Hand.OFF_HAND).getFilter().isBlock()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isBlock()))),
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.food", a -> String.valueOf(!defaultHandConfig.getHand(Hand.OFF_HAND).getFilter().isFood()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isFood()))),
!Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.usable", a -> String.valueOf(!defaultHandConfig.getHand(Hand.OFF_HAND).getFilter().isUsable()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isUsable()))),
getFilterList( getFilterList(
new Gson().fromJson((String) properties.computeIfAbsent("hand.off.whitelist", a -> "[]"), listType), new Gson().fromJson((String) properties.computeIfAbsent("hand.off.whitelist", a -> "[]"), listType),
new Gson().fromJson((String) properties.computeIfAbsent("hand.off.blacklist", a -> "[]"), listType) new Gson().fromJson((String) properties.computeIfAbsent("hand.off.blacklist", a -> "[]"), listType)
@ -241,14 +241,14 @@ public class Updater {
// load an older version // load an older version
if (version == 1.0) { if (version == 1.0) {
try { try {
handConfig = new HandConfig( sittingConfig = new SittingConfig(
1.0, defaultHandConfig.canSitWithHand(), 1.0, true, defaultSittingConfig.canSitWithHand(),
new HandSetting( new HandSetting(
Utl.Enum.get(properties.computeIfAbsent("main-hand-requirement", a -> String.valueOf(defaultHandConfig.getHand(Hand.MAIN_HAND).getSittingRequirement())),HandSetting.SittingRequirement.class,HandSetting.SittingRequirement.FILTER), Utl.Enum.get(properties.computeIfAbsent("main-hand-requirement", a -> String.valueOf(defaultSittingConfig.getHand(Hand.MAIN_HAND).getSittingRequirement())),HandSetting.SittingRequirement.class,HandSetting.SittingRequirement.FILTER),
new HandSetting.Filter( new HandSetting.Filter(
!Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-block", a -> String.valueOf(!defaultHandConfig.getHand(Hand.MAIN_HAND).getFilter().isBlock()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isBlock()))),
!Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-food", a -> String.valueOf(!defaultHandConfig.getHand(Hand.MAIN_HAND).getFilter().isFood()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isFood()))),
!Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-usable", a -> String.valueOf(!defaultHandConfig.getHand(Hand.MAIN_HAND).getFilter().isUsable()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isUsable()))),
getFilterList( getFilterList(
new Gson().fromJson((String) properties.computeIfAbsent("main-hand-whitelist", a -> "[]"), listType), new Gson().fromJson((String) properties.computeIfAbsent("main-hand-whitelist", a -> "[]"), listType),
new Gson().fromJson((String) properties.computeIfAbsent("main-hand-blacklist", a -> "[]"), listType) new Gson().fromJson((String) properties.computeIfAbsent("main-hand-blacklist", a -> "[]"), listType)
@ -257,11 +257,11 @@ public class Updater {
) )
), ),
new HandSetting( new HandSetting(
Utl.Enum.get(properties.computeIfAbsent("off-hand-requirement", a -> String.valueOf(defaultHandConfig.getHand(Hand.OFF_HAND).getSittingRequirement())),HandSetting.SittingRequirement.class,HandSetting.SittingRequirement.FILTER), Utl.Enum.get(properties.computeIfAbsent("off-hand-requirement", a -> String.valueOf(defaultSittingConfig.getHand(Hand.OFF_HAND).getSittingRequirement())),HandSetting.SittingRequirement.class,HandSetting.SittingRequirement.FILTER),
new HandSetting.Filter( new HandSetting.Filter(
!Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-block", a -> String.valueOf(!defaultHandConfig.getHand(Hand.OFF_HAND).getFilter().isBlock()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isBlock()))),
!Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-food", a -> String.valueOf(!defaultHandConfig.getHand(Hand.OFF_HAND).getFilter().isFood()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isFood()))),
!Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-usable", a -> String.valueOf(!defaultHandConfig.getHand(Hand.OFF_HAND).getFilter().isUsable()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isUsable()))),
getFilterList( getFilterList(
new Gson().fromJson((String) properties.computeIfAbsent("off-hand-whitelist", a -> "[]"), listType), new Gson().fromJson((String) properties.computeIfAbsent("off-hand-whitelist", a -> "[]"), listType),
new Gson().fromJson((String) properties.computeIfAbsent("off-hand-blacklist", a -> "[]"), listType) new Gson().fromJson((String) properties.computeIfAbsent("off-hand-blacklist", a -> "[]"), listType)
@ -274,9 +274,9 @@ public class Updater {
} }
Data.setServerConfig(serverConfig); Data.setServerConfig(serverConfig);
Data.setHandConfig(handConfig); Data.setSittingConfig(sittingConfig);
ServerConfig.save(); ServerConfig.save();
HandConfig.save(); SittingConfig.save();
} catch (Exception e) { } catch (Exception e) {
Sit.LOGGER.error("Error loading legacy config: {}", e.getMessage()); Sit.LOGGER.error("Error loading legacy config: {}", e.getMessage());
} }

View file

@ -8,7 +8,7 @@ import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import one.oth3r.sit.file.Data; import one.oth3r.sit.file.Data;
import one.oth3r.sit.file.HandConfig; import one.oth3r.sit.file.SittingConfig;
import one.oth3r.sit.file.HandSetting; import one.oth3r.sit.file.HandSetting;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -46,9 +46,9 @@ public class Logic {
* checks the hands of the player and the items in each hand and sees if the player can sit down * checks the hands of the player and the items in each hand and sees if the player can sit down
*/ */
public static boolean checkHands(ServerPlayerEntity player) { public static boolean checkHands(ServerPlayerEntity player) {
HandConfig handConfig = Data.getPlayerSetting(player); SittingConfig sittingConfig = Data.getPlayerSetting(player);
// if can't sit with hand, false // if can't sit with hand, false
if (!handConfig.canSitWithHand()) return false; if (!sittingConfig.canSitWithHand()) return false;
boolean canSit = true; boolean canSit = true;
@ -57,7 +57,7 @@ public class Logic {
// if they can't sit, no need to run extra code // if they can't sit, no need to run extra code
if (!canSit) break; if (!canSit) break;
HandSetting handSetting = handConfig.getHand(hand); HandSetting handSetting = sittingConfig.getHand(hand);
switch (handSetting.getSittingRequirement()) { switch (handSetting.getSittingRequirement()) {
case EMPTY -> canSit = player.getStackInHand(hand).isEmpty(); case EMPTY -> canSit = player.getStackInHand(hand).isEmpty();
case FILTER -> canSit = Utl.checkItem(handSetting.getFilter(), player.getStackInHand(hand)); case FILTER -> canSit = Utl.checkItem(handSetting.getFilter(), player.getStackInHand(hand));