Skip to content

Commit

Permalink
Initial work on block breaking translation
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Aug 13, 2024
1 parent 1d671c2 commit 6e88177
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.raphimc.viabedrock.protocol.BedrockProtocol;
import net.raphimc.viabedrock.protocol.ServerboundBedrockPackets;
import net.raphimc.viabedrock.protocol.data.ProtocolConstants;
import net.raphimc.viabedrock.protocol.data.enums.Direction;
import net.raphimc.viabedrock.protocol.data.enums.bedrock.*;
import net.raphimc.viabedrock.protocol.data.enums.java.AbilitiesFlag;
import net.raphimc.viabedrock.protocol.data.enums.java.GameMode;
Expand All @@ -43,10 +44,7 @@
import net.raphimc.viabedrock.protocol.storage.PlayerListStorage;
import net.raphimc.viabedrock.protocol.types.BedrockTypes;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;

Expand All @@ -68,12 +66,15 @@ public class ClientPlayerEntity extends PlayerEntity {
// Server Authoritative Movement
private Position3f prevPosition;
private final EnumSet<PlayerAuthInputPacket_InputData> authInputData = EnumSet.noneOf(PlayerAuthInputPacket_InputData.class);
private final List<AuthInputBlockAction> authInputBlockActions = new ArrayList<>();
private boolean sneaking;
private boolean sprinting;

// Misc data
private GameType gameType;
private GameMode javaGameMode;
private boolean cancelNextSwingPacket;
private BlockBreakingInfo blockBreakingInfo;

public ClientPlayerEntity(final UserConnection user, final long runtimeId, final UUID javaUuid, final PlayerAbilities abilities) {
super(user, runtimeId, 0, javaUuid, abilities);
Expand Down Expand Up @@ -168,23 +169,51 @@ public void sendPlayerAuthInputPacketToServer(final ClientPlayMode playMode) {
playerAuthInput.write(BedrockTypes.UNSIGNED_VAR_INT, NewInteractionModel.Touch.getValue()); // interaction mode
playerAuthInput.write(BedrockTypes.UNSIGNED_VAR_LONG, (long) this.age); // tick
playerAuthInput.write(BedrockTypes.POSITION_3F, gravityAffectedPositionDelta); // position delta
if (this.authInputData.contains(PlayerAuthInputPacket_InputData.PerformBlockActions)) {
playerAuthInput.write(BedrockTypes.VAR_INT, this.authInputBlockActions.size()); // player block actions count
for (AuthInputBlockAction blockAction : this.authInputBlockActions) {
playerAuthInput.write(BedrockTypes.VAR_INT, blockAction.action.getValue()); // action
switch (blockAction.action) {
case StartDestroyBlock, AbortDestroyBlock, StopDestroyBlock, CrackBlock, PredictDestroyBlock, ContinueDestroyBlock -> {
playerAuthInput.write(BedrockTypes.POSITION_3I, blockAction.position); // position
playerAuthInput.write(BedrockTypes.VAR_INT, blockAction.direction); // facing
}
}
}
}
playerAuthInput.write(BedrockTypes.POSITION_2F, new Position2f(0F, 0F)); // analog move vector
playerAuthInput.sendToServer(BedrockProtocol.class);

this.prevPosition = this.position;
this.authInputData.clear();
this.authInputBlockActions.clear();
}

public void sendPlayerActionPacketToServer(final PlayerActionType action) {
this.sendPlayerActionPacketToServer(action, 0);
}

public void sendPlayerActionPacketToServer(final PlayerActionType action, final int direction) {
this.sendPlayerActionPacketToServer(action, new BlockPosition(0, 0, 0), direction);
}

public void sendPlayerActionPacketToServer(final PlayerActionType action, final int face) {
public void sendPlayerActionPacketToServer(final PlayerActionType action, final BlockPosition blockPosition, final int direction) {
final PacketWrapper playerAction = PacketWrapper.create(ServerboundBedrockPackets.PLAYER_ACTION, this.user);
playerAction.write(BedrockTypes.UNSIGNED_VAR_LONG, this.runtimeId); // runtime entity id
playerAction.write(BedrockTypes.VAR_INT, action.getValue()); // action
playerAction.write(BedrockTypes.BLOCK_POSITION, new BlockPosition(0, 0, 0)); // block position
playerAction.write(BedrockTypes.BLOCK_POSITION, blockPosition); // block position
playerAction.write(BedrockTypes.BLOCK_POSITION, new BlockPosition(0, 0, 0)); // result position
playerAction.write(BedrockTypes.VAR_INT, face); // face
playerAction.write(BedrockTypes.VAR_INT, direction); // facing
playerAction.sendToServer(BedrockProtocol.class);
}

public void sendSwingPacketToServer() {
final PacketWrapper animate = PacketWrapper.create(ServerboundBedrockPackets.ANIMATE, this.user);
animate.write(BedrockTypes.VAR_INT, AnimatePacket_Action.Swing.getValue()); // action
animate.write(BedrockTypes.UNSIGNED_VAR_LONG, this.runtimeId); // runtime entity id
animate.sendToServer(BedrockProtocol.class);
}

public void updatePlayerPosition(final PacketWrapper wrapper, final boolean onGround) {
if (!this.preMove(null, null, onGround)) {
wrapper.cancel();
Expand All @@ -209,7 +238,7 @@ public void updatePlayerPosition(final PacketWrapper wrapper, final double x, fi
}

if (this.gameSession.getMovementMode() == ServerAuthMovementMode.ClientAuthoritative && MathUtil.roughlyEquals(newPosition.y() - this.position.y(), ProtocolConstants.PLAYER_JUMP_HEIGHT)) {
this.sendPlayerActionPacketToServer(PlayerActionType.StartJump, 0);
this.sendPlayerActionPacketToServer(PlayerActionType.StartJump);
}

this.position = newPosition;
Expand All @@ -232,7 +261,7 @@ public void updatePlayerPosition(final PacketWrapper wrapper, final double x, fi
}

if (this.gameSession.getMovementMode() == ServerAuthMovementMode.ClientAuthoritative && MathUtil.roughlyEquals(newPosition.y() - this.position.y(), ProtocolConstants.PLAYER_JUMP_HEIGHT)) {
this.sendPlayerActionPacketToServer(PlayerActionType.StartJump, 0);
this.sendPlayerActionPacketToServer(PlayerActionType.StartJump);
}

this.position = newPosition;
Expand Down Expand Up @@ -275,7 +304,7 @@ public void confirmTeleport(final int teleportId) {
ViaBedrock.getPlatform().getLogger().log(Level.WARNING, "Received teleport confirm for teleport id " + teleportId + " but player is not spawned yet");
}
if (this.gameSession.getMovementMode() == ServerAuthMovementMode.ClientAuthoritative) {
this.sendPlayerActionPacketToServer(PlayerActionType.HandledTeleport, 0);
this.sendPlayerActionPacketToServer(PlayerActionType.HandledTeleport);
} else {
this.authInputData.add(PlayerAuthInputPacket_InputData.HandledTeleport);
}
Expand All @@ -290,6 +319,11 @@ public void addAuthInputData(final PlayerAuthInputPacket_InputData... data) {
this.authInputData.addAll(Arrays.asList(data));
}

public void addAuthInputBlockAction(final AuthInputBlockAction blockAction) {
this.authInputData.add(PlayerAuthInputPacket_InputData.PerformBlockActions);
this.authInputBlockActions.add(blockAction);
}

@Override
public void setPosition(final Position3f position) {
this.prevPosition = null;
Expand Down Expand Up @@ -407,6 +441,24 @@ public void updateJavaGameMode() {
}
}

public boolean checkCancelSwingPacket() {
final boolean cancel = this.cancelNextSwingPacket;
this.cancelNextSwingPacket = false;
return cancel;
}

public void cancelNextSwingPacket() {
this.cancelNextSwingPacket = true;
}

public BlockBreakingInfo blockBreakingInfo() {
return this.blockBreakingInfo;
}

public void setBlockBreakingInfo(final BlockBreakingInfo blockBreakingInfo) {
this.blockBreakingInfo = blockBreakingInfo;
}

@Override
protected boolean translateAttribute(final EntityAttribute attribute, final PacketWrapper javaAttributes, final AtomicInteger attributeCount, final List<EntityData> javaEntityData) {
return switch (attribute.name()) {
Expand Down Expand Up @@ -501,4 +553,10 @@ private boolean preMove(final Position3f newPosition, final Position3f newRotati
return false;
}

public record BlockBreakingInfo(BlockPosition position, Direction direction) {
}

public record AuthInputBlockAction(PlayerActionType action, BlockPosition position, int direction) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock
* Copyright (C) 2023-2024 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viabedrock.protocol.data.enums.java;

public enum PlayerActionAction {

START_DESTROY_BLOCK,
ABORT_DESTROY_BLOCK,
STOP_DESTROY_BLOCK,
DROP_ALL_ITEMS,
DROP_ITEM,
RELEASE_USE_ITEM,
SWAP_ITEM_WITH_OFFHAND,

}
Loading

0 comments on commit 6e88177

Please sign in to comment.