From 04d246a5bff03db7b9660cf8f5a0828c141ea5ac Mon Sep 17 00:00:00 2001 From: Oth3r Date: Tue, 11 Feb 2025 14:51:32 -0600 Subject: [PATCH] new y sitting limiter - #16 --- .../java/one/oth3r/sit/file/ServerConfig.java | 73 +++++++++++++++++-- src/main/java/one/oth3r/sit/utl/Logic.java | 23 ++++++ 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/src/main/java/one/oth3r/sit/file/ServerConfig.java b/src/main/java/one/oth3r/sit/file/ServerConfig.java index ed84611..dad103f 100644 --- a/src/main/java/one/oth3r/sit/file/ServerConfig.java +++ b/src/main/java/one/oth3r/sit/file/ServerConfig.java @@ -16,25 +16,33 @@ import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.Properties; +import java.util.stream.Collectors; public class ServerConfig implements CustomFile { @SerializedName("version") - private Double version = 2.1; + private Double version = 2.2; + @SerializedName("lang") private String lang = "en_us"; @SerializedName("lang-options") private final String langOptions = "en_us, it_it, pt_br, tr_tr, zh_tw"; + @SerializedName("keep-active") private Boolean keepActive = true; @SerializedName("sit-while-seated") private Boolean sitWhileSeated = false; @SerializedName("preset-blocks") private PresetBlocks presetBlocks = new PresetBlocks(); + + @SerializedName("sitting-eye-y-bounds") + private YBounds yBounds = new YBounds(); + @SerializedName("custom-enabled") private Boolean customEnabled = false; @SerializedName("custom-blocks") private ArrayList sittingBlocks = FileData.Defaults.SITTING_BLOCKS; + @SerializedName("blacklisted-blocks") private ArrayList blacklistedBlocks = FileData.Defaults.BLACKLISTED_BLOCKS; @SerializedName("interaction-blocks") @@ -81,6 +89,10 @@ public class ServerConfig implements CustomFile { return presetBlocks; } + public YBounds getYBounds() { + return yBounds; + } + public Boolean isCustomEnabled() { return customEnabled; } @@ -117,6 +129,13 @@ public class ServerConfig implements CustomFile { this.fullBlocks = fullBlocks; } + public PresetBlocks(PresetBlocks presetBlocks) { + this.stairs = presetBlocks.stairs; + this.slabs = presetBlocks.slabs; + this.carpets = presetBlocks.carpets; + this.fullBlocks = presetBlocks.fullBlocks; + } + public boolean isStairs() { return stairs; } @@ -134,6 +153,42 @@ public class ServerConfig implements CustomFile { } } + public static class YBounds { + @SerializedName("above") + private Double above = 0.1; + @SerializedName("below") + private Double below = 3.0; + + public YBounds() { + } + + public YBounds(Double above, Double below) { + this.above = above; + this.below = below; + } + + public YBounds(YBounds yBounds) { + this.above = yBounds.above; + this.below = yBounds.below; + } + + public Double getAbove() { + return above; + } + + public void setAbove(Double above) { + this.above = above; + } + + public Double getBelow() { + return below; + } + + public void setBelow(Double below) { + this.below = below; + } + } + @Override public void reset() { loadFileData(new ServerConfig()); @@ -150,17 +205,23 @@ public class ServerConfig implements CustomFile { this.lang = newFile.lang; this.keepActive = newFile.keepActive; this.sitWhileSeated = newFile.sitWhileSeated; - this.presetBlocks = newFile.presetBlocks; + + this.presetBlocks = new PresetBlocks(newFile.presetBlocks); + + this.yBounds = new YBounds(newFile.yBounds); + this.customEnabled = newFile.customEnabled; - this.sittingBlocks = newFile.sittingBlocks; - this.blacklistedBlocks = newFile.blacklistedBlocks; + this.sittingBlocks = newFile.sittingBlocks.stream().map(SittingBlock::new).collect(Collectors.toCollection(ArrayList::new)); + this.blacklistedBlocks = newFile.blacklistedBlocks.stream().map(CustomBlock::new).collect(Collectors.toCollection(ArrayList::new)); + this.interactionBlocks = newFile.interactionBlocks.stream().map(CustomBlock::new).collect(Collectors.toCollection(ArrayList::new)); } @Override public void update() { /// update to 2.1, just a new list, nothing to change - if (version == 2.0) { - version = 2.1; + /// update to 2.2, new settings, no changes + if (version >= 2.0 && version <= 2.1) { + version = 2.2; } } diff --git a/src/main/java/one/oth3r/sit/utl/Logic.java b/src/main/java/one/oth3r/sit/utl/Logic.java index 62556b4..a7d792e 100644 --- a/src/main/java/one/oth3r/sit/utl/Logic.java +++ b/src/main/java/one/oth3r/sit/utl/Logic.java @@ -10,6 +10,7 @@ import net.minecraft.util.Hand; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import one.oth3r.sit.file.FileData; +import one.oth3r.sit.file.ServerConfig; import one.oth3r.sit.file.SittingConfig; import one.oth3r.sit.file.HandSetting; import org.jetbrains.annotations.Nullable; @@ -27,6 +28,9 @@ public class Logic { if (!checkHands(player)) return false; } + // check if the block is in the right y level limits from the config + if (!checkYLimits(player, blockPos)) return false; + ServerWorld serverWorld = player.getServerWorld(); BlockState blockState = serverWorld.getBlockState(blockPos); @@ -74,6 +78,25 @@ public class Logic { return canSit; } + /** + * check if the Y-level of the block is within the limits of the player, bounds are set in the {@link ServerConfig} + */ + public static boolean checkYLimits(ServerPlayerEntity player, BlockPos blockPos) { + double playerEyeY = player.getEyeY(); + double blockY = blockPos.getY(); + // if the block is above the eye height + boolean isAbove = playerEyeY < blockY; + // get the height difference (positive) + double heightDifference = Math.abs(playerEyeY - blockY); + // get the config limits + ServerConfig.YBounds yBounds = FileData.getServerConfig().getYBounds(); + + // debug + Data.LOGGER.info("{} {} can sit? {}", heightDifference, isAbove ? "Above" : "Below", (isAbove? yBounds.getAbove(): yBounds.getBelow()) >= heightDifference); + + return (isAbove? yBounds.getAbove(): yBounds.getBelow()) >= heightDifference; + } + /** * removes the entity bound to the player from the game, using the player */