new y sitting limiter - #16

This commit is contained in:
Oth3r 2025-02-11 14:51:32 -06:00
commit 04d246a5bf
2 changed files with 90 additions and 6 deletions

View file

@ -16,25 +16,33 @@ import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Collectors;
public class ServerConfig implements CustomFile<ServerConfig> { public class ServerConfig implements CustomFile<ServerConfig> {
@SerializedName("version") @SerializedName("version")
private Double version = 2.1; private Double version = 2.2;
@SerializedName("lang") @SerializedName("lang")
private String lang = "en_us"; private String lang = "en_us";
@SerializedName("lang-options") @SerializedName("lang-options")
private final String langOptions = "en_us, it_it, pt_br, tr_tr, zh_tw"; private final String langOptions = "en_us, it_it, pt_br, tr_tr, zh_tw";
@SerializedName("keep-active") @SerializedName("keep-active")
private Boolean keepActive = true; private Boolean keepActive = true;
@SerializedName("sit-while-seated") @SerializedName("sit-while-seated")
private Boolean sitWhileSeated = false; private Boolean sitWhileSeated = false;
@SerializedName("preset-blocks") @SerializedName("preset-blocks")
private PresetBlocks presetBlocks = new PresetBlocks(); private PresetBlocks presetBlocks = new PresetBlocks();
@SerializedName("sitting-eye-y-bounds")
private YBounds yBounds = new YBounds();
@SerializedName("custom-enabled") @SerializedName("custom-enabled")
private Boolean customEnabled = false; private Boolean customEnabled = false;
@SerializedName("custom-blocks") @SerializedName("custom-blocks")
private ArrayList<SittingBlock> sittingBlocks = FileData.Defaults.SITTING_BLOCKS; private ArrayList<SittingBlock> sittingBlocks = FileData.Defaults.SITTING_BLOCKS;
@SerializedName("blacklisted-blocks") @SerializedName("blacklisted-blocks")
private ArrayList<CustomBlock> blacklistedBlocks = FileData.Defaults.BLACKLISTED_BLOCKS; private ArrayList<CustomBlock> blacklistedBlocks = FileData.Defaults.BLACKLISTED_BLOCKS;
@SerializedName("interaction-blocks") @SerializedName("interaction-blocks")
@ -81,6 +89,10 @@ public class ServerConfig implements CustomFile<ServerConfig> {
return presetBlocks; return presetBlocks;
} }
public YBounds getYBounds() {
return yBounds;
}
public Boolean isCustomEnabled() { public Boolean isCustomEnabled() {
return customEnabled; return customEnabled;
} }
@ -117,6 +129,13 @@ public class ServerConfig implements CustomFile<ServerConfig> {
this.fullBlocks = fullBlocks; 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() { public boolean isStairs() {
return stairs; return stairs;
} }
@ -134,6 +153,42 @@ public class ServerConfig implements CustomFile<ServerConfig> {
} }
} }
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 @Override
public void reset() { public void reset() {
loadFileData(new ServerConfig()); loadFileData(new ServerConfig());
@ -150,17 +205,23 @@ public class ServerConfig implements CustomFile<ServerConfig> {
this.lang = newFile.lang; this.lang = newFile.lang;
this.keepActive = newFile.keepActive; this.keepActive = newFile.keepActive;
this.sitWhileSeated = newFile.sitWhileSeated; 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.customEnabled = newFile.customEnabled;
this.sittingBlocks = newFile.sittingBlocks; this.sittingBlocks = newFile.sittingBlocks.stream().map(SittingBlock::new).collect(Collectors.toCollection(ArrayList::new));
this.blacklistedBlocks = newFile.blacklistedBlocks; 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 @Override
public void update() { public void update() {
/// update to 2.1, just a new list, nothing to change /// update to 2.1, just a new list, nothing to change
if (version == 2.0) { /// update to 2.2, new settings, no changes
version = 2.1; if (version >= 2.0 && version <= 2.1) {
version = 2.2;
} }
} }

View file

@ -10,6 +10,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.FileData; import one.oth3r.sit.file.FileData;
import one.oth3r.sit.file.ServerConfig;
import one.oth3r.sit.file.SittingConfig; 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;
@ -27,6 +28,9 @@ public class Logic {
if (!checkHands(player)) return false; 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(); ServerWorld serverWorld = player.getServerWorld();
BlockState blockState = serverWorld.getBlockState(blockPos); BlockState blockState = serverWorld.getBlockState(blockPos);
@ -74,6 +78,25 @@ public class Logic {
return canSit; 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 * removes the entity bound to the player from the game, using the player
*/ */