PacketPlayerDigging

This commit is contained in:
Bixilon 2020-06-19 17:00:17 +02:00
parent 715579bb38
commit c4dc27a9b0
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 101 additions and 2 deletions

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* 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;
}
}
}

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.protocol.protocol; package de.bixilon.minosoft.protocol.protocol;
import de.bixilon.minosoft.game.datatypes.TextComponent; 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.inventory.Slot;
import de.bixilon.minosoft.game.datatypes.world.BlockPosition; import de.bixilon.minosoft.game.datatypes.world.BlockPosition;
import de.bixilon.minosoft.nbt.tag.CompoundTag; import de.bixilon.minosoft.nbt.tag.CompoundTag;
@ -153,8 +154,8 @@ public class OutByteBuffer {
writeString(j.toString()); writeString(j.toString());
} }
public void writeBlockPosition(BlockPosition pos) { public void writePosition(Location location) {
writeLong((((long) pos.getX() & 0x3FFFFFF) << 38) | (((long) pos.getZ() & 0x3FFFFFF) << 12) | ((long) pos.getY() & 0xFFF)); writeLong((((long) location.getX() & 0x3FFFFFF) << 38) | (((long) location.getZ() & 0x3FFFFFF) << 12) | ((long) location.getY() & 0xFFF));
} }
public void writeChatComponent(TextComponent c) { public void writeChatComponent(TextComponent c) {
@ -181,4 +182,22 @@ public class OutByteBuffer {
bytes.add(b); 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());
}
} }