Skip to content

Start fixing our block breaking code #5356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public ItemMapping getMapping(ItemData data) {
return lightBlock;
}

boolean isBlock = data.getBlockDefinition() != null;
boolean isBlock = data.getBlockDefinition() != null && data.getBlockDefinition().getRuntimeId() != 0;
boolean hasDamage = data.getDamage() != 0;

for (ItemMapping mapping : this.items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
import org.geysermc.geyser.session.cache.registry.JavaRegistries;
import org.geysermc.geyser.text.GeyserLocale;
import org.geysermc.geyser.translator.inventory.InventoryTranslator;
import org.geysermc.geyser.translator.protocol.bedrock.entity.player.input.BlockBreakHandler;
import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.geyser.util.ChunkUtils;
import org.geysermc.geyser.util.EntityUtils;
Expand Down Expand Up @@ -415,6 +416,7 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
@MonotonicNonNull
@Setter
private JavaDimension dimensionType = null;

/**
* Which dimension Bedrock understands themselves to be in.
* This should only be set after the ChangeDimensionPacket is sent, or
Expand All @@ -423,8 +425,10 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
@Setter
private BedrockDimension bedrockDimension = this.bedrockOverworldDimension;

@Setter
private int breakingBlock;
/**
* The block break handler for this session.
*/
BlockBreakHandler blockBreakHandler = new BlockBreakHandler(this);

@Setter
private Vector3i lastBlockPlacePosition;
Expand Down Expand Up @@ -1633,7 +1637,7 @@ private void startGame() {

startGamePacket.setAuthoritativeMovementMode(AuthoritativeMovementMode.SERVER);
startGamePacket.setRewindHistorySize(0);
startGamePacket.setServerAuthoritativeBlockBreaking(false);
startGamePacket.setServerAuthoritativeBlockBreaking(true);

startGamePacket.setServerId("");
startGamePacket.setWorldId("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,9 @@ public ItemStackResponse translateRequest(GeyserSession session, Type inventory,
case CRAFT_RESULTS_DEPRECATED: // Tends to be called for UI inventories
case CRAFT_RECIPE_OPTIONAL: // Anvils and cartography tables will handle this
case CRAFT_LOOM: // Looms 1.17.40+
case CRAFT_REPAIR_AND_DISENCHANT: { // Grindstones 1.17.40+
case CRAFT_REPAIR_AND_DISENCHANT: // Grindstones 1.17.40+
case MINE_BLOCK: // Server auth block breaking, confirms durability change
break;
}
default:
return rejectRequest(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public void translate(GeyserSession session, InventoryTransactionPacket packet)
break;
case ITEM_USE:
switch (packet.getActionType()) {
// Block placing
case 0 -> {
final Vector3i packetBlockPosition = packet.getBlockPosition();
Vector3i blockPos = BlockUtils.getBlockPosition(packetBlockPosition, packet.getBlockFace());
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,36 @@
import org.cloudburstmc.math.vector.Vector2f;
import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.InputMode;
import org.cloudburstmc.protocol.bedrock.data.LevelEvent;
import org.cloudburstmc.protocol.bedrock.data.PlayerAuthInputData;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
import org.cloudburstmc.protocol.bedrock.data.inventory.transaction.ItemUseTransaction;
import org.cloudburstmc.protocol.bedrock.packet.AnimatePacket;
import org.cloudburstmc.protocol.bedrock.packet.LevelEventPacket;
import org.cloudburstmc.protocol.bedrock.packet.PlayerAuthInputPacket;
import org.cloudburstmc.protocol.bedrock.packet.UpdateAttributesPacket;
import org.geysermc.geyser.entity.EntityDefinitions;
import org.geysermc.geyser.entity.type.BoatEntity;
import org.geysermc.geyser.entity.type.Entity;
import org.geysermc.geyser.entity.type.ItemFrameEntity;
import org.geysermc.geyser.entity.type.living.animal.horse.AbstractHorseEntity;
import org.geysermc.geyser.entity.type.living.animal.horse.LlamaEntity;
import org.geysermc.geyser.entity.type.player.SessionPlayerEntity;
import org.geysermc.geyser.entity.vehicle.ClientVehicle;
import org.geysermc.geyser.level.block.type.Block;
import org.geysermc.geyser.network.GameProtocol;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.translator.protocol.bedrock.BedrockInventoryTransactionTranslator;
import org.geysermc.geyser.util.CooldownUtils;
import org.geysermc.mcprotocollib.protocol.data.game.entity.object.Direction;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
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.level.ServerboundMoveVehiclePacket;
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 java.util.HashSet;
import java.util.List;
import java.util.Set;

@Translator(packet = PlayerAuthInputPacket.class)
Expand All @@ -77,6 +70,7 @@ public void translate(GeyserSession session, PlayerAuthInputPacket packet) {

boolean wasJumping = session.getInputCache().wasJumping();
session.getInputCache().processInputs(entity, packet);
BedrockBlockActions.tickBlockBreaking(session, packet);

ServerboundPlayerCommandPacket sprintPacket = null;

Expand All @@ -88,7 +82,7 @@ public void translate(GeyserSession session, PlayerAuthInputPacket packet) {
leftOverInputData.remove(input);
switch (input) {
case PERFORM_ITEM_INTERACTION -> processItemUseTransaction(session, packet.getItemUseTransaction());
case PERFORM_BLOCK_ACTIONS -> BedrockBlockActions.translate(session, packet.getPlayerActions());
case PERFORM_ITEM_STACK_REQUEST -> session.getPlayerInventoryHolder().translateRequests(List.of(packet.getItemStackRequest()));
case START_SWIMMING -> session.setSwimming(true);
case STOP_SWIMMING -> session.setSwimming(false);
case START_CRAWLING -> session.setCrawling(true);
Expand Down Expand Up @@ -219,51 +213,13 @@ public void translate(GeyserSession session, PlayerAuthInputPacket packet) {

private static void processItemUseTransaction(GeyserSession session, ItemUseTransaction transaction) {
if (transaction.getActionType() == 2) {
int blockState = session.getGameMode() == GameMode.CREATIVE ?
session.getGeyser().getWorldManager().getBlockAt(session, transaction.getBlockPosition()) : session.getBreakingBlock();

session.setLastBlockPlaced(null);
session.setLastBlockPlacePosition(null);

// Same deal with vanilla block placing as above.
if (!session.getWorldBorder().isInsideBorderBoundaries()) {
BedrockInventoryTransactionTranslator.restoreCorrectBlock(session, transaction.getBlockPosition());
return;
}

Vector3f playerPosition = session.getPlayerEntity().getPosition();
playerPosition = playerPosition.down(EntityDefinitions.PLAYER.offset() - session.getEyeHeight());

if (!BedrockInventoryTransactionTranslator.canInteractWithBlock(session, playerPosition, transaction.getBlockPosition())) {
BedrockInventoryTransactionTranslator.restoreCorrectBlock(session, transaction.getBlockPosition());
return;
}

int sequence = session.getWorldCache().nextPredictionSequence();
session.getWorldCache().markPositionInSequence(transaction.getBlockPosition());
// -1 means we don't know what block they're breaking
if (blockState == -1) {
blockState = Block.JAVA_AIR_ID;
}

LevelEventPacket blockBreakPacket = new LevelEventPacket();
blockBreakPacket.setType(LevelEvent.PARTICLE_DESTROY_BLOCK);
blockBreakPacket.setPosition(transaction.getBlockPosition().toFloat());
blockBreakPacket.setData(session.getBlockMappings().getBedrockBlockId(blockState));
session.sendUpstreamPacket(blockBreakPacket);
session.setBreakingBlock(-1);

// TODO test
Entity itemFrameEntity = ItemFrameEntity.getItemFrameEntity(session, transaction.getBlockPosition());
if (itemFrameEntity != null) {
ServerboundInteractPacket attackPacket = new ServerboundInteractPacket(itemFrameEntity.getEntityId(),
InteractAction.ATTACK, session.isSneaking());
session.sendDownstreamGamePacket(attackPacket);
return;
}

PlayerAction action = session.getGameMode() == GameMode.CREATIVE ? PlayerAction.START_DIGGING : PlayerAction.FINISH_DIGGING;
ServerboundPlayerActionPacket breakPacket = new ServerboundPlayerActionPacket(action, transaction.getBlockPosition(), Direction.VALUES[transaction.getBlockFace()], sequence);
session.sendDownstreamGamePacket(breakPacket);
} else {
session.getGeyser().getLogger().error("Unhandled item use transaction type!");
if (session.getGeyser().getLogger().isDebug()) {
Expand Down
Loading