diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/actions/BlockAction.java b/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/actions/BlockAction.java new file mode 100644 index 000000000..0334e47a3 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/actions/BlockAction.java @@ -0,0 +1,17 @@ +/* + * 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.game.datatypes.blocks.actions; + +public interface BlockAction { +} diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/actions/ChestAction.java b/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/actions/ChestAction.java new file mode 100644 index 000000000..258d909a3 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/actions/ChestAction.java @@ -0,0 +1,27 @@ +/* + * 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.game.datatypes.blocks.actions; + +public class ChestAction implements BlockAction { + final byte playersLookingInChest; + + public ChestAction(byte unused, byte playersLookingInChest) { + this.playersLookingInChest = playersLookingInChest; + } + + @Override + public String toString() { + return playersLookingInChest > 0 ? String.format("CHEST_OPEN (%d)", playersLookingInChest) : "CHEST_CLOSE"; + } +} diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/actions/NoteBlockAction.java b/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/actions/NoteBlockAction.java new file mode 100644 index 000000000..593eb2abd --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/actions/NoteBlockAction.java @@ -0,0 +1,56 @@ +/* + * 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.game.datatypes.blocks.actions; + +public class NoteBlockAction implements BlockAction { + final Instruments instrument; + final byte pitch; + + public NoteBlockAction(byte instrument, byte pitch) { + this.instrument = Instruments.byId(instrument); + this.pitch = pitch; + } + + @Override + public String toString() { + return String.format("NOTEBLOCK_%s:%d", instrument.name(), pitch); + } + + public enum Instruments { + HARP(0), + DOUBLE_BASS(1), + SNARE_DRUM(2), + CLICKS_STICKS(3), + BASS_DRUM(4); + + final byte id; + + Instruments(int id) { + this.id = (byte) id; + } + + public static Instruments byId(int id) { + for (Instruments i : values()) { + if (i.getId() == id) { + return i; + } + } + return null; + } + + public byte getId() { + return id; + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/actions/PistonAction.java b/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/actions/PistonAction.java new file mode 100644 index 000000000..afdb2b174 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/blocks/actions/PistonAction.java @@ -0,0 +1,55 @@ +/* + * 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.game.datatypes.blocks.actions; + +import de.bixilon.minosoft.game.datatypes.Direction; + +public class PistonAction implements BlockAction { + final PistonStates status; + final Direction direction; + + public PistonAction(byte status, byte direction) { + this.status = PistonStates.byId(status); + this.direction = Direction.byId(direction); + } + + @Override + public String toString() { + return String.format("PISTON_%s:%s", status.name(), direction.name()); + } + + public enum PistonStates { + PUSH(0), + PULL(1); + + final byte id; + + PistonStates(int id) { + this.id = (byte) id; + } + + public static PistonStates byId(int id) { + for (PistonStates s : values()) { + if (s.getId() == id) { + return s; + } + } + return null; + } + + public byte getId() { + return id; + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketBlockAction.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketBlockAction.java new file mode 100644 index 000000000..d1152f37c --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketBlockAction.java @@ -0,0 +1,81 @@ +/* + * 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.clientbound.play; + +import de.bixilon.minosoft.game.datatypes.blocks.actions.BlockAction; +import de.bixilon.minosoft.game.datatypes.blocks.actions.ChestAction; +import de.bixilon.minosoft.game.datatypes.blocks.actions.NoteBlockAction; +import de.bixilon.minosoft.game.datatypes.blocks.actions.PistonAction; +import de.bixilon.minosoft.game.datatypes.world.BlockPosition; +import de.bixilon.minosoft.logging.Log; +import de.bixilon.minosoft.protocol.packets.ClientboundPacket; +import de.bixilon.minosoft.protocol.protocol.InPacketBuffer; +import de.bixilon.minosoft.protocol.protocol.PacketHandler; +import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; + +import java.lang.reflect.InvocationTargetException; + +public class PacketBlockAction implements ClientboundPacket { + BlockPosition position; + BlockAction data; + + + @Override + public void read(InPacketBuffer buffer, ProtocolVersion v) { + switch (v) { + case VERSION_1_7_10: + position = buffer.readBlockPositionShort(); + byte byte1 = buffer.readByte(); + byte byte2 = buffer.readByte(); + Class clazz; + switch (buffer.readVarInt()) { + case 25: + // noteblock + clazz = NoteBlockAction.class; + break; + case 29: + case 33: + // piston + clazz = PistonAction.class; + break; + case 54: + case 130: + case 146: + // chest + clazz = ChestAction.class; + break; + default: + throw new IllegalStateException("Unexpected value: " + buffer.readVarInt()); + } + try { + data = clazz.getConstructor(byte.class, byte.class).newInstance(byte1, byte2); + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + e.printStackTrace(); + } + + break; + } + } + + @Override + public void log() { + Log.protocol(String.format("Block action received %s at %s", data.toString(), position.toString())); + } + + @Override + public void handle(PacketHandler h) { + h.handle(this); + } + +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java index 8f704fb0d..0af4937c1 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java @@ -298,4 +298,8 @@ public class PacketHandler { public void handle(PacketBlockBreakAnimation pkg) { // ToDo } + + public void handle(PacketBlockAction pkg) { + // ToDo + } } diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/Protocol.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/Protocol.java index 77f5b8008..11e0dd75d 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/Protocol.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/Protocol.java @@ -85,6 +85,7 @@ public interface Protocol { packetClassMapping.put(Packets.Clientbound.PLAY_USE_BED, PacketUseBed.class); packetClassMapping.put(Packets.Clientbound.PLAY_BLOCK_ENTITY_DATA, PacketBlockEntityMetadata.class); packetClassMapping.put(Packets.Clientbound.PLAY_BLOCK_BREAK_ANIMATION, PacketBlockBreakAnimation.class); + packetClassMapping.put(Packets.Clientbound.PLAY_BLOCK_ACTION, PacketBlockAction.class); } int getProtocolVersion();