Skip to content

Commit

Permalink
Fix various horse bugs (GeyserMC#2115)
Browse files Browse the repository at this point in the history
* Fix horse health display

* Fix horses warping back when damaged

* Fix horse jumping animation

* Fix horses not taking damage while standing on magma

* Allow mules and donkeys to jump
  • Loading branch information
davchoo committed Apr 15, 2021
1 parent 22c492f commit 70e2860
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import com.nukkitx.protocol.bedrock.data.inventory.ContainerType;
import com.nukkitx.protocol.bedrock.packet.EntityEventPacket;
import org.geysermc.connector.entity.attribute.AttributeType;
import org.geysermc.connector.entity.living.animal.AnimalEntity;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
Expand All @@ -44,6 +45,10 @@ public AbstractHorseEntity(long entityId, long geyserId, EntityType entityType,

// Specifies the size of the entity's inventory. Required to place slots in the entity.
metadata.put(EntityData.CONTAINER_BASE_SIZE, 2);
// Add dummy health attribute since LivingEntity updates the attribute for us
attributes.put(AttributeType.HEALTH, AttributeType.HEALTH.getAttribute(20, 20));
// Add horse jump strength attribute to allow donkeys and mules to jump
attributes.put(AttributeType.HORSE_JUMP_STRENGTH, AttributeType.HORSE_JUMP_STRENGTH.getAttribute(0.5f, 2));
}

@Override
Expand Down Expand Up @@ -85,9 +90,15 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

// Needed to control horses
metadata.getFlags().setFlag(EntityFlag.CAN_POWER_JUMP, true);
boolean canPowerJump = entityType != EntityType.LLAMA && entityType != EntityType.TRADER_LLAMA;
metadata.getFlags().setFlag(EntityFlag.CAN_POWER_JUMP, canPowerJump);
metadata.getFlags().setFlag(EntityFlag.WASD_CONTROLLED, true);

super.updateBedrockMetadata(entityMetadata, session);

if (entityMetadata.getId() == 8) {
// Update the health attribute
updateBedrockAttributes(session);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ public class GeyserSession implements CommandSender {
@Setter
private long lastMovementTimestamp = System.currentTimeMillis();

/**
* Used to send a ClientVehicleMovePacket for every PlayerInputPacket after idling on a boat/horse for more than 100ms
*/
@Setter
private long lastVehicleMoveTimestamp = System.currentTimeMillis();

/**
* Controls whether the daylight cycle gamerule has been sent to the client, so the sun/moon remain motionless.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class BedrockMoveEntityAbsoluteTranslator extends PacketTranslator<MoveEn

@Override
public void translate(MoveEntityAbsolutePacket packet, GeyserSession session) {
session.setLastVehicleMoveTimestamp(System.currentTimeMillis());

float y = packet.getPosition().getY();
if (session.getRidingVehicleEntity() instanceof BoatEntity) {
// Remove some Y position to prevents boats from looking like they're floating in water
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@
package org.geysermc.connector.network.translators.bedrock;

import com.github.steveice10.mc.protocol.packet.ingame.client.world.ClientSteerVehiclePacket;
import com.github.steveice10.mc.protocol.packet.ingame.client.world.ClientVehicleMovePacket;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.entity.EntityData;
import com.nukkitx.protocol.bedrock.packet.PlayerInputPacket;
import org.geysermc.connector.entity.BoatEntity;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.living.animal.horse.AbstractHorseEntity;
import org.geysermc.connector.entity.living.animal.horse.LlamaEntity;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.Translator;
Expand All @@ -44,5 +52,42 @@ public void translate(PlayerInputPacket packet, GeyserSession session) {
);

session.sendDownstreamPacket(clientSteerVehiclePacket);

// Bedrock only sends movement vehicle packets while moving
// This allows horses to take damage while standing on magma
Entity vehicle = session.getRidingVehicleEntity();
boolean sendMovement = false;
if (vehicle instanceof AbstractHorseEntity && !(vehicle instanceof LlamaEntity)) {
sendMovement = vehicle.isOnGround();
} else if (vehicle instanceof BoatEntity) {
if (vehicle.getPassengers().size() == 1) {
// The player is the only rider
sendMovement = true;
} else {
// Check if the player is the front rider
Vector3f seatPos = session.getPlayerEntity().getMetadata().getVector3f(EntityData.RIDER_SEAT_POSITION, null);
if (seatPos != null && seatPos.getX() > 0) {
sendMovement = true;
}
}
}
if (sendMovement) {
long timeSinceVehicleMove = System.currentTimeMillis() - session.getLastVehicleMoveTimestamp();
if (timeSinceVehicleMove >= 100) {
Vector3f vehiclePosition = vehicle.getPosition();
Vector3f vehicleRotation = vehicle.getRotation();

if (vehicle instanceof BoatEntity) {
// Remove some Y position to prevents boats flying up
vehiclePosition = vehiclePosition.down(EntityType.BOAT.getOffset());
}

ClientVehicleMovePacket clientVehicleMovePacket = new ClientVehicleMovePacket(
vehiclePosition.getX(), vehiclePosition.getY(), vehiclePosition.getZ(),
vehicleRotation.getX() - 90, vehicleRotation.getY()
);
session.sendDownstreamPacket(clientVehicleMovePacket);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/

package org.geysermc.connector.network.translators.bedrock.entity.player;

import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerState;
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerStatePacket;
import com.nukkitx.protocol.bedrock.packet.RiderJumpPacket;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.living.animal.horse.AbstractHorseEntity;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.Translator;

@Translator(packet = RiderJumpPacket.class)
public class BedrockRiderJumpTranslator extends PacketTranslator<RiderJumpPacket> {
@Override
public void translate(RiderJumpPacket packet, GeyserSession session) {
Entity vehicle = session.getRidingVehicleEntity();
if (vehicle instanceof AbstractHorseEntity) {
ClientPlayerStatePacket playerStatePacket = new ClientPlayerStatePacket((int) vehicle.getEntityId(), PlayerState.START_HORSE_JUMP, packet.getJumpStrength());
session.sendDownstreamPacket(playerStatePacket);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.geysermc.connector.network.translators.java.entity;

import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.living.animal.horse.AbstractHorseEntity;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.Translator;
Expand All @@ -47,6 +48,12 @@ public void translate(ServerEntityVelocityPacket packet, GeyserSession session)

entity.setMotion(Vector3f.from(packet.getMotionX(), packet.getMotionY(), packet.getMotionZ()));

if (entity == session.getRidingVehicleEntity() && entity instanceof AbstractHorseEntity) {
// Horses for some reason teleport back when a SetEntityMotionPacket is sent while
// a player is riding on them. Java clients seem to ignore it anyways.
return;
}

SetEntityMotionPacket entityMotionPacket = new SetEntityMotionPacket();
entityMotionPacket.setRuntimeEntityId(entity.getGeyserId());
entityMotionPacket.setMotion(entity.getMotion());
Expand Down

0 comments on commit 70e2860

Please sign in to comment.