Skip to content

Commit

Permalink
Implement new packets
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 5, 2024
1 parent e8d4aea commit 4cc522e
Show file tree
Hide file tree
Showing 11 changed files with 687 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@

public class Main implements ModInitializer {
@Override
public void onInitialize() {}
public void onInitialize() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ public void packetSent(Session session, Packet packet) {
(posRot.getX() * 32 - lastX * 32) * 128,
(posRot.getY() * 32 - lastY * 32) * 128,
(posRot.getZ() * 32 - lastZ * 32) * 128,
posRot.getYRot(),
posRot.getXRot(),
posRot.getYaw(),
posRot.getPitch(),
clientEntity.onGround()));
lastX = posRot.getX();
lastY = posRot.getY();
Expand All @@ -413,8 +413,8 @@ public void packetSent(Session session, Packet packet) {
case ServerboundMovePlayerRotPacket rot -> povSession.send(
new ClientboundMoveEntityRotPacket(
clientEntity.entityId(),
rot.getYRot(),
rot.getXRot(),
rot.getYaw(),
rot.getPitch(),
clientEntity.onGround()));
default -> {
}
Expand Down Expand Up @@ -454,8 +454,8 @@ public void packetSent(Session session, Packet packet) {
clientEntity.x(posRot.getX());
clientEntity.y(posRot.getY());
clientEntity.z(posRot.getZ());
clientEntity.yRot(posRot.getYRot());
clientEntity.xRot(posRot.getXRot());
clientEntity.yRot(posRot.getYaw());
clientEntity.xRot(posRot.getPitch());
}
case ServerboundMovePlayerPosPacket pos -> {
lastX = pos.getX();
Expand All @@ -467,8 +467,8 @@ public void packetSent(Session session, Packet packet) {
clientEntity.z(pos.getZ());
}
case ServerboundMovePlayerRotPacket rot -> {
clientEntity.yRot(rot.getYRot());
clientEntity.xRot(rot.getXRot());
clientEntity.yRot(rot.getYaw());
clientEntity.xRot(rot.getPitch());
}
case ServerboundAcceptTeleportationPacket teleportationPacket -> {
// This was a forced teleport, the server should not know about it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.soulfiremc.server.protocol.bot.state.registry.DimensionType;
import com.soulfiremc.server.protocol.bot.state.registry.SFChatType;
import com.soulfiremc.server.settings.lib.SettingsSource;
import com.soulfiremc.server.util.EntityMovement;
import com.soulfiremc.server.util.SFHelpers;
import com.soulfiremc.server.util.VectorHelper;
import com.soulfiremc.server.util.structs.TickTimer;
Expand Down Expand Up @@ -93,6 +94,7 @@
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.ServerboundClientCommandPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.level.ServerboundAcceptTeleportationPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundMovePlayerPosRotPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundMovePlayerRotPacket;
import org.geysermc.mcprotocollib.protocol.packet.login.clientbound.ClientboundLoginDisconnectPacket;
import org.geysermc.mcprotocollib.protocol.packet.login.clientbound.ClientboundLoginFinishedPacket;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -301,32 +303,13 @@ public void onTickingStep(ClientboundTickingStepPacket packet) {

@EventHandler
public void onPosition(ClientboundPlayerPositionPacket packet) {
var relative = packet.getRelatives();
var x = relative.contains(PositionElement.X) ? clientEntity.x() + packet.getPosition().getX() : packet.getPosition().getX();
var y = relative.contains(PositionElement.Y) ? clientEntity.y() + packet.getPosition().getY() : packet.getPosition().getY();
var z = relative.contains(PositionElement.Z) ? clientEntity.z() + packet.getPosition().getZ() : packet.getPosition().getZ();
var yRot =
relative.contains(PositionElement.Y_ROT)
? clientEntity.yRot() + packet.getYRot()
: packet.getYRot();
var xRot =
relative.contains(PositionElement.X_ROT)
? clientEntity.xRot() + packet.getXRot()
: packet.getXRot();
var deltaMovement = packet.getDeltaMovement();
if (relative.contains(PositionElement.ROTATE_DELTA)) {
var k = clientEntity.yRot() - yRot;
var l = clientEntity.xRot() - xRot;
deltaMovement = VectorHelper.xRot(deltaMovement, (float) Math.toRadians(l));
deltaMovement = VectorHelper.yRot(deltaMovement, (float) Math.toRadians(k));
}

clientEntity.setPosition(x, y, z);
clientEntity.setRotation(yRot, xRot);
clientEntity.setMotion(
relative.contains(PositionElement.DELTA_X) ? clientEntity.motionX() + deltaMovement.getX() : deltaMovement.getX(),
relative.contains(PositionElement.DELTA_Y) ? clientEntity.motionY() + deltaMovement.getY() : deltaMovement.getY(),
relative.contains(PositionElement.DELTA_Z) ? clientEntity.motionZ() + deltaMovement.getZ() : deltaMovement.getZ());
var newMovement = EntityMovement.toAbsolute(clientEntity.toMovement(), new EntityMovement(
packet.getPosition(),
packet.getDeltaMovement(),
packet.getYRot(),
packet.getXRot()
), packet.getRelatives());
clientEntity.setFrom(newMovement);

var position = clientEntity.blockPos();
if (!joinedWorld) {
Expand All @@ -344,10 +327,32 @@ public void onPosition(ClientboundPlayerPositionPacket packet) {
"Position updated: X {} Y {} Z {}", position.getX(), position.getY(), position.getZ());
}

connection.sendPacket(new ServerboundMovePlayerPosRotPacket(false, false, x, y, z, yRot, xRot));
connection.sendPacket(new ServerboundMovePlayerPosRotPacket(
false,
false,
packet.getPosition().getX(),
packet.getPosition().getY(),
packet.getPosition().getZ(),
packet.getYRot(),
packet.getXRot()
));
connection.sendPacket(new ServerboundAcceptTeleportationPacket(packet.getId()));
}

@EventHandler
public void onROtation(ClientboundPlayerRotationPacket packet) {
clientEntity.setRotation(
packet.getYRot(),
packet.getXRot()
);
connection.sendPacket(new ServerboundMovePlayerRotPacket(
false,
false,
packet.getYRot(),
packet.getXRot()
));
}

@EventHandler
public void onLookAt(ClientboundPlayerLookAtPacket packet) {
var targetPosition = Vector3d.from(packet.getX(), packet.getY(), packet.getZ());
Expand Down Expand Up @@ -601,6 +606,16 @@ public void onSetContainerData(ClientboundContainerSetDataPacket packet) {
container.setProperty(packet.getRawProperty(), packet.getValue());
}

@EventHandler
public void onSetPlayerInventory(ClientboundSetPlayerInventoryPacket packet) {
inventoryManager.playerInventory().setSlot(packet.getSlot(), SFItemStack.from(packet.getContents()));
}

@EventHandler
public void onSetCursor(ClientboundSetCursorItemPacket packet) {
inventoryManager.cursorItem(SFItemStack.from(packet.getContents()));
}

@EventHandler
public void onSetSlot(ClientboundSetHeldSlotPacket packet) {
inventoryManager.heldItemSlot(packet.getSlot());
Expand Down Expand Up @@ -999,15 +1014,37 @@ public void onEntityPosRot(ClientboundMoveEntityPosRotPacket packet) {

@EventHandler
public void onEntityTeleport(ClientboundTeleportEntityPacket packet) {
var state = entityTrackerState.getEntity(packet.getEntityId());
var state = entityTrackerState.getEntity(packet.getId());

if (state == null) {
log.debug("Received entity teleport packet for unknown entity {}", packet.getId());
return;
}

state.setFrom(EntityMovement.toAbsolute(state.toMovement(), new EntityMovement(
packet.getPosition(),
packet.getDeltaMovement(),
packet.getYRot(),
packet.getXRot()
), packet.getRelatives()));
state.onGround(packet.isOnGround());
}

@EventHandler
public void onEntityPositionSync(ClientboundEntityPositionSyncPacket packet) {
var state = entityTrackerState.getEntity(packet.getId());

if (state == null) {
log.debug("Received entity teleport packet for unknown entity {}", packet.getEntityId());
log.debug("Received entity teleport packet for unknown entity {}", packet.getId());
return;
}

state.setPosition(packet.getPosition());
state.setRotation(packet.getYRot(), packet.getXRot());
state.setFrom(new EntityMovement(
packet.getPosition(),
packet.getDeltaMovement(),
packet.getYRot(),
packet.getXRot()
));
state.onGround(packet.isOnGround());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.soulfiremc.server.protocol.bot.state.EntityEffectState;
import com.soulfiremc.server.protocol.bot.state.EntityMetadataState;
import com.soulfiremc.server.protocol.bot.state.Level;
import com.soulfiremc.server.util.EntityMovement;
import com.soulfiremc.server.util.MathHelper;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -80,6 +81,16 @@ public Entity(int entityId, UUID uuid, EntityType entityType,
this.motionZ = motionZ;
}

public EntityMovement toMovement() {
return new EntityMovement(Vector3d.from(x, y, z), Vector3d.from(motionX, motionY, motionZ), yRot, xRot);
}

public void setFrom(EntityMovement entityMovement) {
setPosition(entityMovement.pos());
setMotion(entityMovement.deltaMovement());
setRotation(entityMovement.yRot(), entityMovement.xRot());
}

public void setPosition(Vector3d pos) {
setPosition(pos.getX(), pos.getY(), pos.getZ());
}
Expand All @@ -105,6 +116,10 @@ public void setHeadRotation(float headYRot) {
this.headYRot = headYRot;
}

public void setMotion(Vector3d motion) {
setMotion(motion.getX(), motion.getY(), motion.getZ());
}

public void setMotion(double motionX, double motionY, double motionZ) {
this.motionX = motionX;
this.motionY = motionY;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.soulfiremc.server.util;

import org.cloudburstmc.math.vector.Vector3d;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.PositionElement;

import java.util.List;

public record EntityMovement(Vector3d pos, Vector3d deltaMovement, float yRot, float xRot) {
public static EntityMovement toAbsolute(EntityMovement current, EntityMovement packet, List<PositionElement> relative) {
var x = relative.contains(PositionElement.X) ? current.pos().getX() + packet.pos().getX() : packet.pos().getX();
var y = relative.contains(PositionElement.Y) ? current.pos().getY() + packet.pos().getY() : packet.pos().getY();
var z = relative.contains(PositionElement.Z) ? current.pos().getZ() + packet.pos().getZ() : packet.pos().getZ();
var yRot =
relative.contains(PositionElement.Y_ROT)
? current.yRot() + packet.yRot()
: packet.yRot();
var xRot =
relative.contains(PositionElement.X_ROT)
? current.xRot() + packet.xRot()
: packet.xRot();
var deltaMovement = packet.deltaMovement();
if (relative.contains(PositionElement.ROTATE_DELTA)) {
var k = current.yRot() - yRot;
var l = current.xRot() - xRot;
deltaMovement = VectorHelper.xRot(deltaMovement, (float) Math.toRadians(l));
deltaMovement = VectorHelper.yRot(deltaMovement, (float) Math.toRadians(k));
}

return new EntityMovement(Vector3d.from(x, y, z), Vector3d.from(
relative.contains(PositionElement.DELTA_X) ? current.deltaMovement().getX() + deltaMovement.getX() : deltaMovement.getX(),
relative.contains(PositionElement.DELTA_Y) ? current.deltaMovement().getY() + deltaMovement.getY() : deltaMovement.getY(),
relative.contains(PositionElement.DELTA_Z) ? current.deltaMovement().getZ() + deltaMovement.getZ() : deltaMovement.getZ()
), yRot, xRot);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
*/
package com.soulfiremc.server.util;

import com.soulfiremc.server.util.mcstructs.AABB;
import org.cloudburstmc.math.vector.Vector3d;
import org.cloudburstmc.math.vector.Vector3i;

import java.util.Locale;
import java.util.function.IntPredicate;
Expand Down Expand Up @@ -315,6 +317,12 @@ public static double frac(double number) {
return number - (double) lfloor(number);
}

public static long getSeed(int x, int y, int z) {
var l = (x * 3129871L) ^ (long) z * 116129781L ^ (long) y;
l = l * l * 42317861L + l * 11L;
return l >> 16;
}

public static double inverseLerp(double delta, double start, double end) {
return (delta - start) / (end - start);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.soulfiremc.server.util;

import lombok.extern.slf4j.Slf4j;
import org.cloudburstmc.math.vector.Vector3d;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.soulfiremc.server.util;
package com.soulfiremc.server.util.mcstructs;

import com.soulfiremc.server.util.MathHelper;
import org.cloudburstmc.math.vector.Vector3d;
import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.math.vector.Vector3i;
Expand Down Expand Up @@ -34,17 +35,6 @@ public AABB(Vector3d arg, Vector3d arg2) {
this(arg.getX(), arg.getY(), arg.getZ(), arg2.getX(), arg2.getY(), arg2.getZ());
}

public static AABB of(BoundingBox mutableBox) {
return new AABB(
(double) mutableBox.minX(),
(double) mutableBox.minY(),
(double) mutableBox.minZ(),
(double) (mutableBox.maxX() + 1),
(double) (mutableBox.maxY() + 1),
(double) (mutableBox.maxZ() + 1)
);
}

public static AABB unitCubeFromLowerCorner(Vector3d vector) {
return new AABB(vector.getX(), vector.getY(), vector.getZ(), vector.getX() + 1.0, vector.getY() + 1.0, vector.getZ() + 1.0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.soulfiremc.server.util.mcstructs;

import org.cloudburstmc.math.vector.Vector3d;
import org.cloudburstmc.math.vector.Vector3i;

public class BlockHitResult extends HitResult {
private final Direction direction;
private final Vector3i blockPos;
private final boolean miss;
private final boolean inside;
private final boolean worldBorderHit;

public static BlockHitResult miss(Vector3d location, Direction direction, Vector3i pos) {
return new BlockHitResult(true, location, direction, pos, false, false);
}

public BlockHitResult(Vector3d arg, Direction arg2, Vector3i arg3, boolean bl) {
this(false, arg, arg2, arg3, bl, false);
}

public BlockHitResult(Vector3d arg, Direction arg2, Vector3i arg3, boolean bl, boolean bl2) {
this(false, arg, arg2, arg3, bl, bl2);
}

private BlockHitResult(boolean bl, Vector3d arg, Direction arg2, Vector3i arg3, boolean bl2, boolean bl3) {
super(arg);
this.miss = bl;
this.direction = arg2;
this.blockPos = arg3;
this.inside = bl2;
this.worldBorderHit = bl3;
}

public BlockHitResult withDirection(Direction newFace) {
return new BlockHitResult(this.miss, this.location, newFace, this.blockPos, this.inside, this.worldBorderHit);
}

public BlockHitResult withPosition(Vector3i pos) {
return new BlockHitResult(this.miss, this.location, this.direction, pos, this.inside, this.worldBorderHit);
}

public BlockHitResult hitBorder() {
return new BlockHitResult(this.miss, this.location, this.direction, this.blockPos, this.inside, true);
}

public Vector3i getVector3i() {
return this.blockPos;
}

public Direction getDirection() {
return this.direction;
}

@Override
public HitResult.Type getType() {
return this.miss ? HitResult.Type.MISS : HitResult.Type.BLOCK;
}

public boolean isInside() {
return this.inside;
}

public boolean isWorldBorderHit() {
return this.worldBorderHit;
}
}
Loading

0 comments on commit 4cc522e

Please sign in to comment.