block actions

This commit is contained in:
bixilon 2020-06-16 21:59:26 +02:00
parent d6370aff07
commit bad0b12652
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 241 additions and 0 deletions

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.game.datatypes.blocks.actions;
public interface BlockAction {
}

View File

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

View File

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

View File

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

View File

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

View File

@ -298,4 +298,8 @@ public class PacketHandler {
public void handle(PacketBlockBreakAnimation pkg) { public void handle(PacketBlockBreakAnimation pkg) {
// ToDo // ToDo
} }
public void handle(PacketBlockAction pkg) {
// ToDo
}
} }

View File

@ -85,6 +85,7 @@ public interface Protocol {
packetClassMapping.put(Packets.Clientbound.PLAY_USE_BED, PacketUseBed.class); 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_ENTITY_DATA, PacketBlockEntityMetadata.class);
packetClassMapping.put(Packets.Clientbound.PLAY_BLOCK_BREAK_ANIMATION, PacketBlockBreakAnimation.class); packetClassMapping.put(Packets.Clientbound.PLAY_BLOCK_BREAK_ANIMATION, PacketBlockBreakAnimation.class);
packetClassMapping.put(Packets.Clientbound.PLAY_BLOCK_ACTION, PacketBlockAction.class);
} }
int getProtocolVersion(); int getProtocolVersion();