diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketPlayerDigging.java b/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketPlayerDigging.java new file mode 100644 index 000000000..8c5291489 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketPlayerDigging.java @@ -0,0 +1,80 @@ +/* + * Codename Minosoft + * Copyright (C) 2020 Moritz Zwerger + * + * 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 . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.protocol.packets.serverbound.play; + +import de.bixilon.minosoft.game.datatypes.world.BlockPosition; +import de.bixilon.minosoft.logging.Log; +import de.bixilon.minosoft.protocol.packets.ServerboundPacket; +import de.bixilon.minosoft.protocol.protocol.OutPacketBuffer; +import de.bixilon.minosoft.protocol.protocol.Packets; +import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; + +public class PacketPlayerDigging implements ServerboundPacket { + final DiggingStatus status; + final BlockPosition position; + final byte face; + + + public PacketPlayerDigging(DiggingStatus status, BlockPosition position, byte face) { + this.status = status; + this.position = position; + this.face = face; + log(); + } + + + @Override + public OutPacketBuffer write(ProtocolVersion v) { + OutPacketBuffer buffer = new OutPacketBuffer(v.getPacketCommand(Packets.Serverbound.PLAY_PLAYER_DIGGING)); + switch (v) { + case VERSION_1_7_10: + buffer.writeByte((byte) status.getId()); + if (position == null) { + buffer.writeInteger(0); + buffer.writeByte((byte) 0); + buffer.writeInteger(0); + } else { + buffer.writePositionByte(position); + } + buffer.writeByte(face); + + break; + } + return buffer; + } + + @Override + public void log() { + Log.protocol(String.format("Send player digging packet (status=%s, position=%s, face=%d)", status.name(), position.toString(), face)); + } + + public enum DiggingStatus { + START_DIGGING(0), + CANCELLED_DIGGING(1), + FINISHED_DIGGING(2), + DROP_ITEM_STACK(3), + DROP_ITEM(4), + SHOOT_ARROW__FINISH_EATING(5); + + final int id; + + DiggingStatus(int id) { + this.id = id; + } + + public int getId() { + return id; + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java index a6b5cc931..2e46ceadc 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java @@ -14,6 +14,7 @@ package de.bixilon.minosoft.protocol.protocol; import de.bixilon.minosoft.game.datatypes.TextComponent; +import de.bixilon.minosoft.game.datatypes.entities.Location; import de.bixilon.minosoft.game.datatypes.inventory.Slot; import de.bixilon.minosoft.game.datatypes.world.BlockPosition; import de.bixilon.minosoft.nbt.tag.CompoundTag; @@ -153,8 +154,8 @@ public class OutByteBuffer { writeString(j.toString()); } - public void writeBlockPosition(BlockPosition pos) { - writeLong((((long) pos.getX() & 0x3FFFFFF) << 38) | (((long) pos.getZ() & 0x3FFFFFF) << 12) | ((long) pos.getY() & 0xFFF)); + public void writePosition(Location location) { + writeLong((((long) location.getX() & 0x3FFFFFF) << 38) | (((long) location.getZ() & 0x3FFFFFF) << 12) | ((long) location.getY() & 0xFFF)); } public void writeChatComponent(TextComponent c) { @@ -181,4 +182,22 @@ public class OutByteBuffer { bytes.add(b); } } + + public void writePositionInteger(BlockPosition pos) { + writeInteger(pos.getX()); + writeInteger(pos.getY()); + writeInteger(pos.getZ()); + } + + public void writePositionShort(BlockPosition pos) { + writeInteger(pos.getX()); + writeShort((short) pos.getY()); + writeInteger(pos.getZ()); + } + + public void writePositionByte(BlockPosition pos) { + writeInteger(pos.getX()); + writeByte((byte) pos.getY()); + writeInteger(pos.getZ()); + } }