Skip to content

Commit

Permalink
feat: 初步实现方块破坏
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Aug 9, 2023
1 parent abda69f commit a8b54d4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.allay.api.block.property.type.BlockPropertyType;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.protocol.bedrock.data.definitions.BlockDefinition;
import org.cloudburstmc.protocol.bedrock.data.definitions.SimpleBlockDefinition;
import org.jetbrains.annotations.UnmodifiableView;

Expand Down Expand Up @@ -48,4 +49,8 @@ default SimpleBlockDefinition toNetworkBlockDefinition() {
statesBuilder.build()
);
}

default BlockDefinition toNetworkBlockDefinitionRuntime() {
return this::blockStateHash;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,17 @@ protected void handleBlockAction(List<PlayerBlockActionData> blockActions) {
//TODO: checking
switch(action.getAction()) {
case START_BREAK -> {
getWorld().sendLevelEventPacket(pos, LevelEvent.BLOCK_START_BREAK, 65535);
getWorld().sendLevelEventPacket(pos, LevelEvent.BLOCK_START_BREAK, 0);
}
case BLOCK_PREDICT_DESTROY -> {
var oldState = getWorld().getBlockState(pos.getX(), pos.getY(), pos.getZ());
if (oldState == null) {
log.warn("Player " + name + " tried to break block at " + pos + " but it is air");
continue;
}
getWorld().setBlockState(pos.getX(), pos.getY(), pos.getZ(), VanillaBlockTypes.AIR_TYPE.getDefaultState());
getWorld().sendLevelEventPacket(pos, LevelEvent.BLOCK_STOP_BREAK, 1);
getWorld().sendLevelEventPacket(pos, LevelEvent.BLOCK_STOP_BREAK, 0);
getWorld().sendLevelEventPacket(pos, LevelEvent.PARTICLE_DESTROY_BLOCK, oldState.blockStateHash());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void setBlockState(@Range(from = 0, to = 15) int x, @Range(from = -512, t
if (send) {
var updateBlockPacket = new UpdateBlockPacket();
updateBlockPacket.setBlockPosition(Vector3i.from((chunkX << 4) + x, y, (chunkZ << 4) + z));
updateBlockPacket.setDefinition(blockState.toNetworkBlockDefinition());
updateBlockPacket.setDefinition(blockState.toNetworkBlockDefinitionRuntime());
updateBlockPacket.setDataLayer(layer ? 1 : 0);
updateBlockPacket.getFlags().addAll( UpdateBlockPacket.FLAG_ALL_PRIORITY );
sendChunkPacket(updateBlockPacket);
Expand Down

0 comments on commit a8b54d4

Please sign in to comment.