Improve und fix various things

BlockActions: Use unsigned byte (short)
BlockActions: Add BellAction and ignore all other actions
ChatComponent: Fix mishandling, wrong order
Rename some GameStates
Fix Respawn Packet Handling in 1.16.2+
This commit is contained in:
Bixilon 2020-11-22 12:50:56 +01:00
parent 996ad522a4
commit 492dd7a0ce
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
14 changed files with 81 additions and 46 deletions

View File

@ -15,7 +15,7 @@ package de.bixilon.minosoft.data.mappings.blocks.actions;
public class BeaconAction implements BlockAction {
public BeaconAction(byte status, byte ignored) {
public BeaconAction(short status, short ignored) {
// only 1 action (id 1)
}

View File

@ -0,0 +1,33 @@
/*
* 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.data.mappings.blocks.actions;
import com.sun.javafx.scene.traversal.Direction;
public class BellAction implements BlockAction {
private final Direction direction;
public BellAction(short unused, short direction) {
this.direction = Direction.values()[direction];
}
public Direction getDirection() {
return direction;
}
@Override
public String toString() {
return String.format("BELL_HIT_%s", direction);
}
}

View File

@ -14,9 +14,9 @@
package de.bixilon.minosoft.data.mappings.blocks.actions;
public class ChestAction implements BlockAction {
final byte playersLookingInChest;
final short playersLookingInChest;
public ChestAction(byte unused, byte playersLookingInChest) {
public ChestAction(short unused, short playersLookingInChest) {
this.playersLookingInChest = playersLookingInChest;
}

View File

@ -15,7 +15,7 @@ package de.bixilon.minosoft.data.mappings.blocks.actions;
public class EndGatewayAction implements BlockAction {
public EndGatewayAction(byte status, byte ignored) {
public EndGatewayAction(short status, short ignored) {
// only 1 action (id 1)
}

View File

@ -15,7 +15,7 @@ package de.bixilon.minosoft.data.mappings.blocks.actions;
public class MobSpawnerAction implements BlockAction {
public MobSpawnerAction(byte status, byte ignored) {
public MobSpawnerAction(short status, short ignored) {
// only 1 action (id 1)
}

View File

@ -15,9 +15,9 @@ package de.bixilon.minosoft.data.mappings.blocks.actions;
public class NoteBlockAction implements BlockAction {
final Instruments instrument;
final byte pitch;
final short pitch;
public NoteBlockAction(byte instrument, byte pitch) {
public NoteBlockAction(short instrument, short pitch) {
this.instrument = Instruments.byId(instrument);
this.pitch = pitch;
}

View File

@ -19,7 +19,7 @@ public class PistonAction implements BlockAction {
final PistonStates status;
final Directions direction;
public PistonAction(byte status, byte direction) {
public PistonAction(short status, short direction) {
this.status = PistonStates.byId(status);
this.direction = Directions.byId(direction);
}

View File

@ -157,6 +157,10 @@ public class BaseComponent implements ChatComponent {
thisTextComponent = new TextComponent(text, color, formattingCodes);
}
if (thisTextComponent != null) {
parts.add(thisTextComponent);
}
if (json.has("extra")) {
JsonArray extras = json.getAsJsonArray("extra");
TextComponent finalThisChatPart = thisTextComponent;
@ -166,10 +170,6 @@ public class BaseComponent implements ChatComponent {
if (json.has("translate")) {
parts.add(new TranslatableComponent(json.get("translate").getAsString(), json.getAsJsonArray("with")));
}
if (thisTextComponent != null) {
parts.add(thisTextComponent);
}
}
@Override

View File

@ -51,7 +51,11 @@ public final class ChatColors {
}
public static RGBColor getColorByFormattingChar(char c) {
try {
return colors[Character.digit(c, 16)];
} catch (IndexOutOfBoundsException e) {
return null;
}
}
public static RGBColor getColorById(int id) {

View File

@ -37,6 +37,9 @@ public class Chunk {
throw new IllegalArgumentException(String.format("Invalid chunk location %s %s %s", x, y, z));
}
byte section = (byte) (y / 16);
if (!sections.containsKey(section)) {
return null;
}
return sections.get(section).getBlock(x, y % 16, z);
}

View File

@ -21,8 +21,6 @@ import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
import java.lang.reflect.InvocationTargetException;
public class PacketBlockAction implements ClientboundPacket {
BlockPosition position;
BlockAction data;
@ -35,26 +33,19 @@ public class PacketBlockAction implements ClientboundPacket {
} else {
position = buffer.readPosition();
}
byte byte1 = buffer.readByte();
byte byte2 = buffer.readByte();
Class<? extends BlockAction> clazz;
short byte1 = buffer.readUnsignedByte();
short byte2 = buffer.readUnsignedByte();
BlockId blockId = buffer.getConnection().getMapping().getBlockIdById(buffer.readVarInt());
// beacon
// end gateway
clazz = switch (blockId.getIdentifier()) {
case "noteblock" -> NoteBlockAction.class; // ToDo: was replaced in 17w47a (346) with the block id
case "sticky_piston", "piston" -> PistonAction.class;
case "chest", "ender_chest", "trapped_chest", "white_shulker_box", "shulker_box", "orange_shulker_box", "magenta_shulker_box", "light_blue_shulker_box", "yellow_shulker_box", "lime_shulker_box", "pink_shulker_box", "gray_shulker_box", "silver_shulker_box", "cyan_shulker_box", "purple_shulker_box", "blue_shulker_box", "brown_shulker_box", "green_shulker_box", "red_shulker_box", "black_shulker_box" -> ChestAction.class;
case "beacon" -> BeaconAction.class;
case "mob_spawner" -> MobSpawnerAction.class;
case "end_gateway" -> EndGatewayAction.class;
default -> throw new IllegalStateException(String.format("Unexpected block action (blockId=%s)", blockId));
data = switch (blockId.getIdentifier()) {
case "noteblock" -> new NoteBlockAction(byte1, byte2); // ToDo: was replaced in 17w47a (346) with the block id
case "sticky_piston", "piston" -> new PistonAction(byte1, byte2);
case "chest", "ender_chest", "trapped_chest", "white_shulker_box", "shulker_box", "orange_shulker_box", "magenta_shulker_box", "light_blue_shulker_box", "yellow_shulker_box", "lime_shulker_box", "pink_shulker_box", "gray_shulker_box", "silver_shulker_box", "cyan_shulker_box", "purple_shulker_box", "blue_shulker_box", "brown_shulker_box", "green_shulker_box", "red_shulker_box", "black_shulker_box" -> new ChestAction(byte1, byte2);
case "beacon" -> new BeaconAction(byte1, byte2);
case "mob_spawner" -> new MobSpawnerAction(byte1, byte2);
case "end_gateway" -> new EndGatewayAction(byte1, byte2);
default -> null;
};
try {
data = clazz.getConstructor(byte.class, byte.class).newInstance(byte1, byte2);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
return true;
}

View File

@ -50,18 +50,18 @@ public class PacketChangeGameState implements ClientboundPacket {
}
public enum Reason {
INVALID_BED(new MapSet[]{new MapSet<>(0, 0)}),
END_RAIN(new MapSet[]{new MapSet<>(0, 1), new MapSet<>(498, 2), new MapSet<>(578, 1)}), // ToDo: when exactly did these 2 switch?
START_RAIN(new MapSet[]{new MapSet<>(0, 2), new MapSet<>(498, 1), new MapSet<>(578, 2)}),
NO_RESPAWN_BLOCK_AVAILABLE(new MapSet[]{new MapSet<>(0, 0)}),
START_RAINING(new MapSet[]{new MapSet<>(0, 1), new MapSet<>(498, 2), new MapSet<>(578, 1)}), // ToDo: when exactly did these 2 switch?
STOP_RAINING(new MapSet[]{new MapSet<>(0, 2), new MapSet<>(498, 1), new MapSet<>(578, 2)}),
CHANGE_GAMEMODE(new MapSet[]{new MapSet<>(0, 3)}),
ENTER_CREDITS(new MapSet[]{new MapSet<>(0, 4)}),
DEMO_MESSAGES(new MapSet[]{new MapSet<>(0, 5)}),
ARROW_HITTING_PLAYER(new MapSet[]{new MapSet<>(0, 6)}),
FADE_VALUE(new MapSet[]{new MapSet<>(0, 7)}),
FADE_TIME(new MapSet[]{new MapSet<>(0, 8)}),
PLAY_PUFFERFISH_STING_SOUND(new MapSet[]{new MapSet<>(0, 9)}),
PLAY_ELDER_GUARDIAN_MOB_APPEARANCE(new MapSet[]{new MapSet<>(0, 10)}),
ENABLE_RESPAWN_SCREEN(new MapSet[]{new MapSet<>(552, 11)});
RAIN_LEVEL_CHANGE(new MapSet[]{new MapSet<>(0, 7)}),
THUNDER_LEVEL_CHANGE(new MapSet[]{new MapSet<>(0, 8)}),
PUFFERFISH_STING(new MapSet[]{new MapSet<>(0, 9)}),
GUARDIAN_ELDER_EFFECT(new MapSet[]{new MapSet<>(0, 10)}),
IMMEDIATE_RESPAWN(new MapSet[]{new MapSet<>(552, 11)});
final VersionValueMap<Integer> valueMap;

View File

@ -21,6 +21,7 @@ import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
import de.bixilon.minosoft.util.nbt.tag.CompoundTag;
public class PacketRespawn implements ClientboundPacket {
Dimension dimension;
@ -40,8 +41,11 @@ public class PacketRespawn implements ClientboundPacket {
} else {
dimension = buffer.getConnection().getMapping().getDimensionById(buffer.readInt());
}
} else {
} else if (buffer.getVersionId() < 748) {
dimension = buffer.getConnection().getMapping().getDimensionByIdentifier(buffer.readString());
} else {
CompoundTag tag = (CompoundTag) buffer.readNBT();
dimension = buffer.getConnection().getMapping().getDimensionByIdentifier(tag.getStringTag("effects").getValue()); //ToDo
}
if (buffer.getVersionId() < 464) {
difficulty = Difficulties.byId(buffer.readByte());

View File

@ -277,15 +277,15 @@ public class PacketHandler {
}
Log.game(switch (pkg.getReason()) {
case START_RAIN -> "Received weather packet: Starting rain...";
case END_RAIN -> "Received weather packet: Stopping rain...";
case STOP_RAINING -> "Received weather packet: Starting rain...";
case START_RAINING -> "Received weather packet: Stopping rain...";
case CHANGE_GAMEMODE -> String.format("Received game mode change: Now in %s", GameModes.byId(pkg.getValue().intValue()));
default -> "";
});
switch (pkg.getReason()) {
case START_RAIN -> connection.getPlayer().getWorld().setRaining(true);
case END_RAIN -> connection.getPlayer().getWorld().setRaining(false);
case STOP_RAINING -> connection.getPlayer().getWorld().setRaining(true);
case START_RAINING -> connection.getPlayer().getWorld().setRaining(false);
case CHANGE_GAMEMODE -> connection.getPlayer().setGameMode(GameModes.byId(pkg.getValue().intValue()));
}
}