Use the correct way of sending block breaking particles (#4825)

This commit is contained in:
LetsGoAway 2024-07-20 18:09:22 +08:00 committed by GitHub
parent 9fe3acc81c
commit 8fd99e1e1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 113 additions and 84 deletions

View File

@ -32,7 +32,12 @@ import org.cloudburstmc.protocol.bedrock.data.PlayerActionType;
import org.cloudburstmc.protocol.bedrock.data.definitions.ItemDefinition; import org.cloudburstmc.protocol.bedrock.data.definitions.ItemDefinition;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityEventType; import org.cloudburstmc.protocol.bedrock.data.entity.EntityEventType;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag; import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
import org.cloudburstmc.protocol.bedrock.packet.*; import org.cloudburstmc.protocol.bedrock.packet.AnimatePacket;
import org.cloudburstmc.protocol.bedrock.packet.EntityEventPacket;
import org.cloudburstmc.protocol.bedrock.packet.LevelEventPacket;
import org.cloudburstmc.protocol.bedrock.packet.PlayStatusPacket;
import org.cloudburstmc.protocol.bedrock.packet.PlayerActionPacket;
import org.cloudburstmc.protocol.bedrock.packet.UpdateAttributesPacket;
import org.geysermc.geyser.api.block.custom.CustomBlockState; import org.geysermc.geyser.api.block.custom.CustomBlockState;
import org.geysermc.geyser.entity.type.Entity; import org.geysermc.geyser.entity.type.Entity;
import org.geysermc.geyser.entity.type.ItemFrameEntity; import org.geysermc.geyser.entity.type.ItemFrameEntity;
@ -52,8 +57,17 @@ import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.util.BlockUtils; import org.geysermc.geyser.util.BlockUtils;
import org.geysermc.geyser.util.CooldownUtils; import org.geysermc.geyser.util.CooldownUtils;
import org.geysermc.mcprotocollib.protocol.data.game.entity.object.Direction; import org.geysermc.mcprotocollib.protocol.data.game.entity.object.Direction;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.*; import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.*; import org.geysermc.mcprotocollib.protocol.data.game.entity.player.Hand;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.InteractAction;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.PlayerAction;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.PlayerState;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundInteractPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundPlayerAbilitiesPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundPlayerActionPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundPlayerCommandPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundSwingPacket;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundUseItemOnPacket;
@Translator(packet = PlayerActionPacket.class) @Translator(packet = PlayerActionPacket.class)
public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket> { public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket> {
@ -70,7 +84,7 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
Vector3i vector = packet.getBlockPosition(); Vector3i vector = packet.getBlockPosition();
switch (packet.getAction()) { switch (packet.getAction()) {
case RESPAWN: case RESPAWN -> {
// Respawn process is finished and the server and client are both OK with respawning. // Respawn process is finished and the server and client are both OK with respawning.
EntityEventPacket eventPacket = new EntityEventPacket(); EntityEventPacket eventPacket = new EntityEventPacket();
eventPacket.setRuntimeEntityId(entity.getGeyserId()); eventPacket.setRuntimeEntityId(entity.getGeyserId());
@ -88,16 +102,16 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
// Needed here since 1.19.81 for dimension switching // Needed here since 1.19.81 for dimension switching
session.getEntityCache().updateBossBars(); session.getEntityCache().updateBossBars();
break; }
case START_SWIMMING: case START_SWIMMING -> {
if (!entity.getFlag(EntityFlag.SWIMMING)) { if (!entity.getFlag(EntityFlag.SWIMMING)) {
ServerboundPlayerCommandPacket startSwimPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.START_SPRINTING); ServerboundPlayerCommandPacket startSwimPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.START_SPRINTING);
session.sendDownstreamGamePacket(startSwimPacket); session.sendDownstreamGamePacket(startSwimPacket);
session.setSwimming(true); session.setSwimming(true);
} }
break; }
case STOP_SWIMMING: case STOP_SWIMMING -> {
// Prevent packet spam when Bedrock players are crawling near the edge of a block // Prevent packet spam when Bedrock players are crawling near the edge of a block
if (!session.getCollisionManager().mustPlayerCrawlHere()) { if (!session.getCollisionManager().mustPlayerCrawlHere()) {
ServerboundPlayerCommandPacket stopSwimPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.STOP_SPRINTING); ServerboundPlayerCommandPacket stopSwimPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.STOP_SPRINTING);
@ -105,51 +119,50 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
session.setSwimming(false); session.setSwimming(false);
} }
break; }
case START_GLIDE: case START_GLIDE -> {
// Otherwise gliding will not work in creative // Otherwise gliding will not work in creative
ServerboundPlayerAbilitiesPacket playerAbilitiesPacket = new ServerboundPlayerAbilitiesPacket(false); ServerboundPlayerAbilitiesPacket playerAbilitiesPacket = new ServerboundPlayerAbilitiesPacket(false);
session.sendDownstreamGamePacket(playerAbilitiesPacket); session.sendDownstreamGamePacket(playerAbilitiesPacket);
case STOP_GLIDE: sendPlayerGlideToggle(session, entity);
ServerboundPlayerCommandPacket glidePacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.START_ELYTRA_FLYING); }
session.sendDownstreamGamePacket(glidePacket); case STOP_GLIDE -> sendPlayerGlideToggle(session, entity);
break; case START_SNEAK -> {
case START_SNEAK:
ServerboundPlayerCommandPacket startSneakPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.START_SNEAKING); ServerboundPlayerCommandPacket startSneakPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.START_SNEAKING);
session.sendDownstreamGamePacket(startSneakPacket); session.sendDownstreamGamePacket(startSneakPacket);
session.startSneaking(); session.startSneaking();
break; }
case STOP_SNEAK: case STOP_SNEAK -> {
ServerboundPlayerCommandPacket stopSneakPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.STOP_SNEAKING); ServerboundPlayerCommandPacket stopSneakPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.STOP_SNEAKING);
session.sendDownstreamGamePacket(stopSneakPacket); session.sendDownstreamGamePacket(stopSneakPacket);
session.stopSneaking(); session.stopSneaking();
break; }
case START_SPRINT: case START_SPRINT -> {
if (!entity.getFlag(EntityFlag.SWIMMING)) { if (!entity.getFlag(EntityFlag.SWIMMING)) {
ServerboundPlayerCommandPacket startSprintPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.START_SPRINTING); ServerboundPlayerCommandPacket startSprintPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.START_SPRINTING);
session.sendDownstreamGamePacket(startSprintPacket); session.sendDownstreamGamePacket(startSprintPacket);
session.setSprinting(true); session.setSprinting(true);
} }
break; }
case STOP_SPRINT: case STOP_SPRINT -> {
if (!entity.getFlag(EntityFlag.SWIMMING)) { if (!entity.getFlag(EntityFlag.SWIMMING)) {
ServerboundPlayerCommandPacket stopSprintPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.STOP_SPRINTING); ServerboundPlayerCommandPacket stopSprintPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.STOP_SPRINTING);
session.sendDownstreamGamePacket(stopSprintPacket); session.sendDownstreamGamePacket(stopSprintPacket);
} }
session.setSprinting(false); session.setSprinting(false);
break; }
case DROP_ITEM: case DROP_ITEM -> {
ServerboundPlayerActionPacket dropItemPacket = new ServerboundPlayerActionPacket(PlayerAction.DROP_ITEM, ServerboundPlayerActionPacket dropItemPacket = new ServerboundPlayerActionPacket(PlayerAction.DROP_ITEM,
vector, Direction.VALUES[packet.getFace()], 0); vector, Direction.VALUES[packet.getFace()], 0);
session.sendDownstreamGamePacket(dropItemPacket); session.sendDownstreamGamePacket(dropItemPacket);
break; }
case STOP_SLEEP: case STOP_SLEEP -> {
ServerboundPlayerCommandPacket stopSleepingPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.LEAVE_BED); ServerboundPlayerCommandPacket stopSleepingPacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.LEAVE_BED);
session.sendDownstreamGamePacket(stopSleepingPacket); session.sendDownstreamGamePacket(stopSleepingPacket);
break; }
case START_BREAK: { case START_BREAK -> {
// Ignore START_BREAK when the player is CREATIVE to avoid Spigot receiving 2 packets it interpets as block breaking. https://github.com/GeyserMC/Geyser/issues/4021 // Ignore START_BREAK when the player is CREATIVE to avoid Spigot receiving 2 packets it interpets as block breaking. https://github.com/GeyserMC/Geyser/issues/4021
if (session.getGameMode() == GameMode.CREATIVE) { if (session.getGameMode() == GameMode.CREATIVE) {
break; break;
@ -180,18 +193,20 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
// Account for fire - the client likes to hit the block behind. // Account for fire - the client likes to hit the block behind.
Vector3i fireBlockPos = BlockUtils.getBlockPosition(vector, packet.getFace()); Vector3i fireBlockPos = BlockUtils.getBlockPosition(vector, packet.getFace());
Block block = session.getGeyser().getWorldManager().blockAt(session, fireBlockPos).block(); Block block = session.getGeyser().getWorldManager().blockAt(session, fireBlockPos).block();
Direction direction = Direction.VALUES[packet.getFace()];
if (block == Blocks.FIRE || block == Blocks.SOUL_FIRE) { if (block == Blocks.FIRE || block == Blocks.SOUL_FIRE) {
ServerboundPlayerActionPacket startBreakingPacket = new ServerboundPlayerActionPacket(PlayerAction.START_DIGGING, fireBlockPos, ServerboundPlayerActionPacket startBreakingPacket = new ServerboundPlayerActionPacket(PlayerAction.START_DIGGING, fireBlockPos,
Direction.VALUES[packet.getFace()], session.getWorldCache().nextPredictionSequence()); direction, session.getWorldCache().nextPredictionSequence());
session.sendDownstreamGamePacket(startBreakingPacket); session.sendDownstreamGamePacket(startBreakingPacket);
} }
ServerboundPlayerActionPacket startBreakingPacket = new ServerboundPlayerActionPacket(PlayerAction.START_DIGGING, ServerboundPlayerActionPacket startBreakingPacket = new ServerboundPlayerActionPacket(PlayerAction.START_DIGGING,
vector, Direction.VALUES[packet.getFace()], session.getWorldCache().nextPredictionSequence()); vector, direction, session.getWorldCache().nextPredictionSequence());
session.sendDownstreamGamePacket(startBreakingPacket); session.sendDownstreamGamePacket(startBreakingPacket);
break;
spawnBlockBreakParticles(session, direction, vector, BlockState.of(blockState));
} }
case CONTINUE_BREAK: case CONTINUE_BREAK -> {
if (session.getGameMode() == GameMode.CREATIVE) { if (session.getGameMode() == GameMode.CREATIVE) {
break; break;
} }
@ -201,19 +216,12 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
} }
Vector3f vectorFloat = vector.toFloat(); Vector3f vectorFloat = vector.toFloat();
LevelEventPacket continueBreakPacket = new LevelEventPacket();
continueBreakPacket.setType(LevelEvent.PARTICLE_CRACK_BLOCK);
continueBreakPacket.setData((session.getBlockMappings().getBedrockBlockId(breakingBlock)) | (packet.getFace() << 24));
continueBreakPacket.setPosition(vectorFloat);
session.sendUpstreamPacket(continueBreakPacket);
// Update the break time in the event that player conditions changed (jumping, effects applied)
LevelEventPacket updateBreak = new LevelEventPacket();
updateBreak.setType(LevelEvent.BLOCK_UPDATE_BREAK);
updateBreak.setPosition(vectorFloat);
double breakTime = BlockUtils.getSessionBreakTime(session, BlockState.of(breakingBlock).block()) * 20;
BlockState breakingBlockState = BlockState.of(breakingBlock);
Direction direction = Direction.VALUES[packet.getFace()];
spawnBlockBreakParticles(session, direction, vector, breakingBlockState);
double breakTime = BlockUtils.getSessionBreakTime(session, breakingBlockState.block()) * 20;
// If the block is custom, we must keep track of when it should break ourselves // If the block is custom, we must keep track of when it should break ourselves
long blockBreakStartTime = session.getBlockBreakStartTime(); long blockBreakStartTime = session.getBlockBreakStartTime();
if (blockBreakStartTime != 0) { if (blockBreakStartTime != 0) {
@ -229,17 +237,20 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
// Break the block // Break the block
ServerboundPlayerActionPacket finishBreakingPacket = new ServerboundPlayerActionPacket(PlayerAction.FINISH_DIGGING, ServerboundPlayerActionPacket finishBreakingPacket = new ServerboundPlayerActionPacket(PlayerAction.FINISH_DIGGING,
vector, Direction.VALUES[packet.getFace()], session.getWorldCache().nextPredictionSequence()); vector, direction, session.getWorldCache().nextPredictionSequence());
session.sendDownstreamGamePacket(finishBreakingPacket); session.sendDownstreamGamePacket(finishBreakingPacket);
session.setBlockBreakStartTime(0); session.setBlockBreakStartTime(0);
break; break;
} }
} }
// Update the break time in the event that player conditions changed (jumping, effects applied)
LevelEventPacket updateBreak = new LevelEventPacket();
updateBreak.setType(LevelEvent.BLOCK_UPDATE_BREAK);
updateBreak.setPosition(vectorFloat);
updateBreak.setData((int) (65535 / breakTime)); updateBreak.setData((int) (65535 / breakTime));
session.sendUpstreamPacket(updateBreak); session.sendUpstreamPacket(updateBreak);
break; }
case ABORT_BREAK: case ABORT_BREAK -> {
if (session.getGameMode() != GameMode.CREATIVE) { if (session.getGameMode() != GameMode.CREATIVE) {
// As of 1.16.210: item frame items are taken out here. // As of 1.16.210: item frame items are taken out here.
// Survival also sends START_BREAK, but by attaching our process here adventure mode also works // Survival also sends START_BREAK, but by attaching our process here adventure mode also works
@ -260,25 +271,23 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
stopBreak.setData(0); stopBreak.setData(0);
session.setBreakingBlock(-1); session.setBreakingBlock(-1);
session.sendUpstreamPacket(stopBreak); session.sendUpstreamPacket(stopBreak);
break; }
case STOP_BREAK:
// Handled in BedrockInventoryTransactionTranslator // Handled in BedrockInventoryTransactionTranslator
break; case STOP_BREAK -> {
case DIMENSION_CHANGE_SUCCESS: }
case DIMENSION_CHANGE_SUCCESS -> {
//sometimes the client doesn't feel like loading //sometimes the client doesn't feel like loading
PlayStatusPacket spawnPacket = new PlayStatusPacket(); PlayStatusPacket spawnPacket = new PlayStatusPacket();
spawnPacket.setStatus(PlayStatusPacket.Status.PLAYER_SPAWN); spawnPacket.setStatus(PlayStatusPacket.Status.PLAYER_SPAWN);
session.sendUpstreamPacket(spawnPacket); session.sendUpstreamPacket(spawnPacket);
attributesPacket = new UpdateAttributesPacket(); UpdateAttributesPacket attributesPacket = new UpdateAttributesPacket();
attributesPacket.setRuntimeEntityId(entity.getGeyserId()); attributesPacket.setRuntimeEntityId(entity.getGeyserId());
attributesPacket.getAttributes().addAll(entity.getAttributes().values()); attributesPacket.getAttributes().addAll(entity.getAttributes().values());
session.sendUpstreamPacket(attributesPacket); session.sendUpstreamPacket(attributesPacket);
break; }
case JUMP: case JUMP -> entity.setOnGround(false); // Increase block break time while jumping
entity.setOnGround(false); // Increase block break time while jumping case MISSED_SWING -> {
break;
case MISSED_SWING:
// Java edition sends a cooldown when hitting air. // Java edition sends a cooldown when hitting air.
// Normally handled by BedrockLevelSoundEventTranslator, but there is no sound on Java for this. // Normally handled by BedrockLevelSoundEventTranslator, but there is no sound on Java for this.
CooldownUtils.sendCooldown(session); CooldownUtils.sendCooldown(session);
@ -294,8 +303,8 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
animatePacket.setAction(AnimatePacket.Action.SWING_ARM); animatePacket.setAction(AnimatePacket.Action.SWING_ARM);
session.sendUpstreamPacket(animatePacket); session.sendUpstreamPacket(animatePacket);
} }
break; }
case START_FLYING: // Since 1.20.30 case START_FLYING -> { // Since 1.20.30
if (session.isCanFly()) { if (session.isCanFly()) {
if (session.getGameMode() == GameMode.SPECTATOR) { if (session.getGameMode() == GameMode.SPECTATOR) {
// should already be flying // should already be flying
@ -324,12 +333,12 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
stopFlyingPacket.setFace(0); stopFlyingPacket.setFace(0);
session.sendUpstreamPacket(stopFlyingPacket); session.sendUpstreamPacket(stopFlyingPacket);
} }
break; }
case STOP_FLYING: case STOP_FLYING -> {
session.setFlying(false); session.setFlying(false);
session.sendDownstreamGamePacket(new ServerboundPlayerAbilitiesPacket(false)); session.sendDownstreamGamePacket(new ServerboundPlayerAbilitiesPacket(false));
break; }
case DIMENSION_CHANGE_REQUEST_OR_CREATIVE_DESTROY_BLOCK: // Used by client to get book from lecterns and items from item frame in creative mode since 1.20.70 case DIMENSION_CHANGE_REQUEST_OR_CREATIVE_DESTROY_BLOCK -> { // Used by client to get book from lecterns and items from item frame in creative mode since 1.20.70
BlockState state = session.getGeyser().getWorldManager().blockAt(session, vector); BlockState state = session.getGeyser().getWorldManager().blockAt(session, vector);
if (state.getValue(Properties.HAS_BOOK, false)) { if (state.getValue(Properties.HAS_BOOK, false)) {
@ -352,7 +361,27 @@ public class BedrockActionTranslator extends PacketTranslator<PlayerActionPacket
InteractAction.ATTACK, Hand.MAIN_HAND, session.isSneaking()); InteractAction.ATTACK, Hand.MAIN_HAND, session.isSneaking());
session.sendDownstreamGamePacket(interactPacket); session.sendDownstreamGamePacket(interactPacket);
} }
break;
} }
} }
} }
private void spawnBlockBreakParticles(GeyserSession session, Direction direction, Vector3i position, BlockState blockState) {
LevelEventPacket levelEventPacket = new LevelEventPacket();
switch (direction) {
case UP -> levelEventPacket.setType(LevelEvent.PARTICLE_BREAK_BLOCK_UP);
case DOWN -> levelEventPacket.setType(LevelEvent.PARTICLE_BREAK_BLOCK_DOWN);
case NORTH -> levelEventPacket.setType(LevelEvent.PARTICLE_BREAK_BLOCK_NORTH);
case EAST -> levelEventPacket.setType(LevelEvent.PARTICLE_BREAK_BLOCK_EAST);
case SOUTH -> levelEventPacket.setType(LevelEvent.PARTICLE_BREAK_BLOCK_SOUTH);
case WEST -> levelEventPacket.setType(LevelEvent.PARTICLE_BREAK_BLOCK_WEST);
}
levelEventPacket.setPosition(position.toFloat());
levelEventPacket.setData(session.getBlockMappings().getBedrockBlock(blockState).getRuntimeId());
session.sendUpstreamPacket(levelEventPacket);
}
private void sendPlayerGlideToggle(GeyserSession session, Entity entity) {
ServerboundPlayerCommandPacket glidePacket = new ServerboundPlayerCommandPacket(entity.getEntityId(), PlayerState.START_ELYTRA_FLYING);
session.sendDownstreamGamePacket(glidePacket);
}
}