From 818bef0bdfeddbc7381ec2f4ded054bb1b329487 Mon Sep 17 00:00:00 2001 From: AJ Ferguson Date: Thu, 23 May 2024 05:25:29 -0400 Subject: [PATCH] Clamp user input --- .../entity/type/player/SessionPlayerEntity.java | 15 ++++++++++++--- .../entity/vehicle/CamelVehicleComponent.java | 1 + .../geyser/entity/vehicle/VehicleComponent.java | 5 +++++ .../java/entity/JavaSetPassengersTranslator.java | 4 ++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/entity/type/player/SessionPlayerEntity.java b/core/src/main/java/org/geysermc/geyser/entity/type/player/SessionPlayerEntity.java index 0b0311126..4f9316f3b 100644 --- a/core/src/main/java/org/geysermc/geyser/entity/type/player/SessionPlayerEntity.java +++ b/core/src/main/java/org/geysermc/geyser/entity/type/player/SessionPlayerEntity.java @@ -28,7 +28,6 @@ package org.geysermc.geyser.entity.type.player; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import lombok.Getter; import org.checkerframework.checker.nullness.qual.Nullable; -import lombok.Setter; import org.cloudburstmc.math.vector.Vector2f; import org.cloudburstmc.math.vector.Vector3f; import org.cloudburstmc.protocol.bedrock.data.AttributeData; @@ -39,6 +38,7 @@ import org.geysermc.geyser.item.Items; import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.util.AttributeUtils; import org.geysermc.geyser.util.DimensionUtils; +import org.geysermc.geyser.util.MathUtils; import org.geysermc.mcprotocollib.protocol.data.game.entity.attribute.Attribute; import org.geysermc.mcprotocollib.protocol.data.game.entity.attribute.AttributeType; import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.GlobalPos; @@ -70,13 +70,11 @@ public class SessionPlayerEntity extends PlayerEntity { * Used when emulating client-side vehicles */ @Getter - @Setter private Vector2f vehicleInput = Vector2f.ZERO; /** * Used when emulating client-side vehicles */ @Getter - @Setter private int vehicleJumpStrength; public SessionPlayerEntity(GeyserSession session) { @@ -280,4 +278,15 @@ public class SessionPlayerEntity extends PlayerEntity { public void resetAir() { this.setAirSupply(getMaxAir()); } + + public void setVehicleInput(Vector2f vehicleInput) { + this.vehicleInput = Vector2f.from( + MathUtils.clamp(vehicleInput.getX(), -1.0f, 1.0f), + MathUtils.clamp(vehicleInput.getY(), -1.0f, 1.0f) + ); + } + + public void setVehicleJumpStrength(int vehicleJumpStrength) { + this.vehicleJumpStrength = MathUtils.constrain(vehicleJumpStrength, 0, 100); + } } diff --git a/core/src/main/java/org/geysermc/geyser/entity/vehicle/CamelVehicleComponent.java b/core/src/main/java/org/geysermc/geyser/entity/vehicle/CamelVehicleComponent.java index de9b83a38..13e9de34f 100644 --- a/core/src/main/java/org/geysermc/geyser/entity/vehicle/CamelVehicleComponent.java +++ b/core/src/main/java/org/geysermc/geyser/entity/vehicle/CamelVehicleComponent.java @@ -75,6 +75,7 @@ public class CamelVehicleComponent extends VehicleComponent { public void onDismount() { vehicle.setFlag(EntityFlag.HAS_DASH_COOLDOWN, false); vehicle.updateBedrockMetadata(); + super.onDismount(); } @Override diff --git a/core/src/main/java/org/geysermc/geyser/entity/vehicle/VehicleComponent.java b/core/src/main/java/org/geysermc/geyser/entity/vehicle/VehicleComponent.java index 60da6e3ca..375bbf729 100644 --- a/core/src/main/java/org/geysermc/geyser/entity/vehicle/VehicleComponent.java +++ b/core/src/main/java/org/geysermc/geyser/entity/vehicle/VehicleComponent.java @@ -143,6 +143,11 @@ public class VehicleComponent { this.stepHeight = MathUtils.clamp(stepHeight, 1.0f, 10.0f); } + public void onMount() { + vehicle.getSession().getPlayerEntity().setVehicleInput(Vector2f.ZERO); + vehicle.getSession().getPlayerEntity().setVehicleJumpStrength(0); + } + public void onDismount() { // } diff --git a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/entity/JavaSetPassengersTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/entity/JavaSetPassengersTranslator.java index c35c2b756..865ca0464 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/entity/JavaSetPassengersTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/entity/JavaSetPassengersTranslator.java @@ -56,6 +56,10 @@ public class JavaSetPassengersTranslator extends PacketTranslator