mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
Implement skulls and heads
This commit is contained in:
parent
b1f8ad10f0
commit
08eb99fa94
4 changed files with 241 additions and 0 deletions
|
@ -67,6 +67,8 @@ public class BlockTranslator {
|
||||||
|
|
||||||
private static final Map<BlockState, String> JAVA_ID_TO_BLOCK_ENTITY_MAP = new HashMap<>();
|
private static final Map<BlockState, String> JAVA_ID_TO_BLOCK_ENTITY_MAP = new HashMap<>();
|
||||||
private static final Object2ByteMap<BlockState> BED_COLORS = new Object2ByteOpenHashMap<>();
|
private static final Object2ByteMap<BlockState> BED_COLORS = new Object2ByteOpenHashMap<>();
|
||||||
|
private static final Object2ByteMap<BlockState> SKULL_VARIANTS = new Object2ByteOpenHashMap<>();
|
||||||
|
private static final Object2ByteMap<BlockState> SKULL_ROTATIONS = new Object2ByteOpenHashMap<>();
|
||||||
|
|
||||||
public static final Int2DoubleMap JAVA_RUNTIME_ID_TO_HARDNESS = new Int2DoubleOpenHashMap();
|
public static final Int2DoubleMap JAVA_RUNTIME_ID_TO_HARDNESS = new Int2DoubleOpenHashMap();
|
||||||
public static final Int2BooleanMap JAVA_RUNTIME_ID_TO_CAN_HARVEST_WITH_HAND = new Int2BooleanOpenHashMap();
|
public static final Int2BooleanMap JAVA_RUNTIME_ID_TO_CAN_HARVEST_WITH_HAND = new Int2BooleanOpenHashMap();
|
||||||
|
@ -147,6 +149,170 @@ public class BlockTranslator {
|
||||||
JAVA_ID_TO_BLOCK_ENTITY_MAP.put(javaBlockState, javaId);
|
JAVA_ID_TO_BLOCK_ENTITY_MAP.put(javaBlockState, javaId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int lastIndex = javaId.length() -1;
|
||||||
|
byte rotation = 0;
|
||||||
|
if (javaId.contains("skeleton_skull[")) {
|
||||||
|
if (javaId.indexOf("=") == lastIndex - 2) {
|
||||||
|
rotation = Byte.parseByte(javaId.substring(lastIndex -1, lastIndex));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rotation = Byte.parseByte(javaId.substring(lastIndex -2, lastIndex));
|
||||||
|
}
|
||||||
|
SKULL_VARIANTS.put(javaBlockState, (byte) 0);
|
||||||
|
SKULL_ROTATIONS.put(javaBlockState, rotation);
|
||||||
|
}
|
||||||
|
else if (javaId.contains("skeleton_wall")) {
|
||||||
|
if (javaId.contains("north")) {
|
||||||
|
rotation = 0;
|
||||||
|
}
|
||||||
|
else if (javaId.contains("east")) {
|
||||||
|
rotation = 4;
|
||||||
|
}
|
||||||
|
else if (javaId.contains("south")) {
|
||||||
|
rotation = 8;
|
||||||
|
}
|
||||||
|
else if(javaId.contains("west")) {
|
||||||
|
rotation = 12;
|
||||||
|
}
|
||||||
|
SKULL_VARIANTS.put(javaBlockState, (byte) 0);
|
||||||
|
SKULL_ROTATIONS.put(javaBlockState, rotation);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (javaId.contains("wither_skeleton_skull[")) {
|
||||||
|
if (javaId.indexOf("=") == lastIndex - 2) {
|
||||||
|
rotation = Byte.parseByte(javaId.substring(lastIndex -1, lastIndex));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rotation = Byte.parseByte(javaId.substring(lastIndex -2, lastIndex));
|
||||||
|
}
|
||||||
|
SKULL_VARIANTS.put(javaBlockState, (byte) 1);
|
||||||
|
SKULL_ROTATIONS.put(javaBlockState, rotation);
|
||||||
|
}
|
||||||
|
else if (javaId.contains("wither_skeleton_wall")) {
|
||||||
|
if (javaId.contains("north")) {
|
||||||
|
rotation = 0;
|
||||||
|
}
|
||||||
|
else if (javaId.contains("east")) {
|
||||||
|
rotation = 4;
|
||||||
|
}
|
||||||
|
else if (javaId.contains("south")) {
|
||||||
|
rotation = 8;
|
||||||
|
}
|
||||||
|
else if(javaId.contains("west")) {
|
||||||
|
rotation = 12;
|
||||||
|
}
|
||||||
|
SKULL_VARIANTS.put(javaBlockState, (byte) 1);
|
||||||
|
SKULL_ROTATIONS.put(javaBlockState, rotation);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (javaId.contains("zombie_head[")) {
|
||||||
|
if (javaId.indexOf("=") == lastIndex - 2) {
|
||||||
|
rotation = Byte.parseByte(javaId.substring(lastIndex -1, lastIndex));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rotation = Byte.parseByte(javaId.substring(lastIndex -2, lastIndex));
|
||||||
|
}
|
||||||
|
SKULL_VARIANTS.put(javaBlockState, (byte) 2);
|
||||||
|
SKULL_ROTATIONS.put(javaBlockState, rotation);
|
||||||
|
}
|
||||||
|
else if (javaId.contains("zombie_wall")) {
|
||||||
|
if (javaId.contains("north")) {
|
||||||
|
rotation = 0;
|
||||||
|
}
|
||||||
|
else if (javaId.contains("east")) {
|
||||||
|
rotation = 4;
|
||||||
|
}
|
||||||
|
else if (javaId.contains("south")) {
|
||||||
|
rotation = 8;
|
||||||
|
}
|
||||||
|
else if(javaId.contains("west")) {
|
||||||
|
rotation = 12;
|
||||||
|
}
|
||||||
|
SKULL_VARIANTS.put(javaBlockState, (byte) 2);
|
||||||
|
SKULL_ROTATIONS.put(javaBlockState, rotation);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (javaId.contains("player_head[")) {
|
||||||
|
if (javaId.indexOf("=") == lastIndex - 2) {
|
||||||
|
rotation = Byte.parseByte(javaId.substring(lastIndex -1, lastIndex));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rotation = Byte.parseByte(javaId.substring(lastIndex -2, lastIndex));
|
||||||
|
}
|
||||||
|
SKULL_VARIANTS.put(javaBlockState, (byte) 3);
|
||||||
|
SKULL_ROTATIONS.put(javaBlockState, rotation);
|
||||||
|
}
|
||||||
|
else if (javaId.contains("player_wall")) {
|
||||||
|
if (javaId.contains("north")) {
|
||||||
|
rotation = 0;
|
||||||
|
}
|
||||||
|
else if (javaId.contains("east")) {
|
||||||
|
rotation = 4;
|
||||||
|
}
|
||||||
|
else if (javaId.contains("south")) {
|
||||||
|
rotation = 8;
|
||||||
|
}
|
||||||
|
else if(javaId.contains("west")) {
|
||||||
|
rotation = 12;
|
||||||
|
}
|
||||||
|
SKULL_VARIANTS.put(javaBlockState, (byte) 3);
|
||||||
|
SKULL_ROTATIONS.put(javaBlockState, rotation);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (javaId.contains("creeper_head[")) {
|
||||||
|
if (javaId.indexOf("=") == lastIndex - 2) {
|
||||||
|
rotation = Byte.parseByte(javaId.substring(lastIndex -1, lastIndex));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rotation = Byte.parseByte(javaId.substring(lastIndex -2, lastIndex));
|
||||||
|
}
|
||||||
|
SKULL_VARIANTS.put(javaBlockState, (byte) 4);
|
||||||
|
SKULL_ROTATIONS.put(javaBlockState, rotation);
|
||||||
|
}
|
||||||
|
else if (javaId.contains("creeper_wall")) {
|
||||||
|
if (javaId.contains("north")) {
|
||||||
|
rotation = 0;
|
||||||
|
}
|
||||||
|
else if (javaId.contains("east")) {
|
||||||
|
rotation = 4;
|
||||||
|
}
|
||||||
|
else if (javaId.contains("south")) {
|
||||||
|
rotation = 8;
|
||||||
|
}
|
||||||
|
else if(javaId.contains("west")) {
|
||||||
|
rotation = 12;
|
||||||
|
}
|
||||||
|
SKULL_VARIANTS.put(javaBlockState, (byte) 4);
|
||||||
|
SKULL_ROTATIONS.put(javaBlockState, rotation);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (javaId.contains("dragon_head[")) {
|
||||||
|
if (javaId.indexOf("=") == lastIndex - 2) {
|
||||||
|
rotation = Byte.parseByte(javaId.substring(lastIndex -1, lastIndex));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
rotation = Byte.parseByte(javaId.substring(lastIndex -2, lastIndex));
|
||||||
|
}
|
||||||
|
SKULL_VARIANTS.put(javaBlockState, (byte) 5);
|
||||||
|
SKULL_ROTATIONS.put(javaBlockState, rotation);
|
||||||
|
}
|
||||||
|
else if (javaId.contains("dragon_wall")) {
|
||||||
|
if (javaId.contains("north")) {
|
||||||
|
rotation = 0;
|
||||||
|
}
|
||||||
|
else if (javaId.contains("east")) {
|
||||||
|
rotation = 4;
|
||||||
|
}
|
||||||
|
else if (javaId.contains("south")) {
|
||||||
|
rotation = 8;
|
||||||
|
}
|
||||||
|
else if(javaId.contains("west")) {
|
||||||
|
rotation = 12;
|
||||||
|
}
|
||||||
|
SKULL_VARIANTS.put(javaBlockState, (byte) 5);
|
||||||
|
SKULL_ROTATIONS.put(javaBlockState, rotation);
|
||||||
|
}
|
||||||
|
|
||||||
// If the Java ID is bed, signal that it needs a tag to show color
|
// If the Java ID is bed, signal that it needs a tag to show color
|
||||||
// The color is in the namespace ID in Java Edition but it's a tag in Bedrock.
|
// The color is in the namespace ID in Java Edition but it's a tag in Bedrock.
|
||||||
JsonNode bedColor = entry.getValue().get("bed_color");
|
JsonNode bedColor = entry.getValue().get("bed_color");
|
||||||
|
@ -268,6 +434,20 @@ public class BlockTranslator {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte getSkullVariant(BlockState state) {
|
||||||
|
if (SKULL_VARIANTS.containsKey(state)) {
|
||||||
|
return SKULL_VARIANTS.getByte(state);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte getSkullRotation(BlockState state) {
|
||||||
|
if (SKULL_ROTATIONS.containsKey(state)) {
|
||||||
|
return SKULL_ROTATIONS.getByte(state);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
public static BlockState getJavaWaterloggedState(int bedrockId) {
|
public static BlockState getJavaWaterloggedState(int bedrockId) {
|
||||||
return BEDROCK_TO_JAVA_BLOCK_MAP.get(1 << 31 | bedrockId);
|
return BEDROCK_TO_JAVA_BLOCK_MAP.get(1 << 31 | bedrockId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
package org.geysermc.connector.network.translators.block.entity;
|
||||||
|
|
||||||
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
|
||||||
|
import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
|
||||||
|
import com.github.steveice10.mc.protocol.data.message.Message;
|
||||||
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
||||||
|
import com.nukkitx.math.vector.Vector3i;
|
||||||
|
import com.nukkitx.nbt.CompoundTagBuilder;
|
||||||
|
import com.nukkitx.nbt.tag.StringTag;
|
||||||
|
import com.nukkitx.nbt.tag.Tag;
|
||||||
|
import org.geysermc.connector.network.session.GeyserSession;
|
||||||
|
import org.geysermc.connector.network.translators.block.BlockTranslator;
|
||||||
|
import org.geysermc.connector.utils.BlockEntityUtils;
|
||||||
|
import org.geysermc.connector.utils.MessageUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class SkullBlockEntityTranslator {
|
||||||
|
|
||||||
|
public static void checkForSkullVariant(GeyserSession session, BlockState blockState, Vector3i position) {
|
||||||
|
byte skullVariant = BlockTranslator.getSkullVariant(blockState);
|
||||||
|
byte rotation = BlockTranslator.getSkullRotation(blockState);
|
||||||
|
// If Bed Color is not -1 then it is indeed a bed with a color.
|
||||||
|
if (skullVariant > 0) {
|
||||||
|
Position pos = new Position(position.getX(), position.getY(), position.getZ());
|
||||||
|
com.nukkitx.nbt.tag.CompoundTag finalSkullTag = getSkullTag(skullVariant, pos, rotation);
|
||||||
|
// Delay needed, otherwise newly placed beds will not get their color
|
||||||
|
// Delay is not needed for beds already placed on login
|
||||||
|
session.getConnector().getGeneralThreadPool().schedule(() ->
|
||||||
|
BlockEntityUtils.updateBlockEntity(session, finalSkullTag, pos),
|
||||||
|
500,
|
||||||
|
TimeUnit.MILLISECONDS
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static com.nukkitx.nbt.tag.CompoundTag getSkullTag(byte skullvariant, Position pos, byte rotation) {
|
||||||
|
CompoundTagBuilder tagBuilder = CompoundTagBuilder.builder()
|
||||||
|
.intTag("x", pos.getX())
|
||||||
|
.intTag("y", pos.getY())
|
||||||
|
.intTag("z", pos.getZ())
|
||||||
|
.stringTag("id", "Skull")
|
||||||
|
.floatTag("Rotation", rotation * 22.5f);
|
||||||
|
tagBuilder.byteTag("SkullType", skullvariant);
|
||||||
|
return tagBuilder.buildRootTag();
|
||||||
|
}
|
||||||
|
}
|
|
@ -120,9 +120,13 @@ public class JavaChunkDataTranslator extends PacketTranslator<ServerChunkDataPac
|
||||||
for (Map.Entry<Position, BlockState> blockEntityEntry: chunkData.beds.entrySet()) {
|
for (Map.Entry<Position, BlockState> blockEntityEntry: chunkData.beds.entrySet()) {
|
||||||
ChunkUtils.updateBlock(session, blockEntityEntry.getValue(), blockEntityEntry.getKey());
|
ChunkUtils.updateBlock(session, blockEntityEntry.getValue(), blockEntityEntry.getKey());
|
||||||
}
|
}
|
||||||
|
for (Map.Entry<Position, BlockState> blockEntityEntry: chunkData.skulls.entrySet()) {
|
||||||
|
ChunkUtils.updateBlock(session, blockEntityEntry.getValue(), blockEntityEntry.getKey());
|
||||||
|
}
|
||||||
chunkData.signs.clear();
|
chunkData.signs.clear();
|
||||||
chunkData.gateways.clear();
|
chunkData.gateways.clear();
|
||||||
chunkData.beds.clear();
|
chunkData.beds.clear();
|
||||||
|
chunkData.skulls.clear();
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
||||||
import org.geysermc.connector.GeyserConnector;
|
import org.geysermc.connector.GeyserConnector;
|
||||||
import org.geysermc.connector.network.session.GeyserSession;
|
import org.geysermc.connector.network.session.GeyserSession;
|
||||||
import org.geysermc.connector.network.translators.block.entity.BlockEntityTranslator;
|
import org.geysermc.connector.network.translators.block.entity.BlockEntityTranslator;
|
||||||
|
import org.geysermc.connector.network.translators.block.entity.SkullBlockEntityTranslator;
|
||||||
import org.geysermc.connector.world.chunk.ChunkPosition;
|
import org.geysermc.connector.world.chunk.ChunkPosition;
|
||||||
import org.geysermc.connector.network.translators.Translators;
|
import org.geysermc.connector.network.translators.Translators;
|
||||||
import org.geysermc.connector.network.translators.block.BlockTranslator;
|
import org.geysermc.connector.network.translators.block.BlockTranslator;
|
||||||
|
@ -86,6 +87,11 @@ public class ChunkUtils {
|
||||||
// Beds need to be updated separately to add the bed color tag
|
// Beds need to be updated separately to add the bed color tag
|
||||||
// Previously this was done by matching block state but this resulted in only one bed per color+orientation showing
|
// Previously this was done by matching block state but this resulted in only one bed per color+orientation showing
|
||||||
chunkData.beds.put(pos, blockState);
|
chunkData.beds.put(pos, blockState);
|
||||||
|
} else if (BlockTranslator.getSkullVariant(blockState) > 0) {
|
||||||
|
Position pos = new ChunkPosition(column.getX(), column.getZ()).getBlock(x, (chunkY << 4) + y, z);
|
||||||
|
// Beds need to be updated separately to add the bed color tag
|
||||||
|
// Previously this was done by matching block state but this resulted in only one bed per color+orientation showing
|
||||||
|
chunkData.skulls.put(pos, blockState);
|
||||||
} else {
|
} else {
|
||||||
section.getBlockStorageArray()[0].setFullBlock(ChunkSection.blockPosition(x, y, z), id);
|
section.getBlockStorageArray()[0].setFullBlock(ChunkSection.blockPosition(x, y, z), id);
|
||||||
}
|
}
|
||||||
|
@ -160,6 +166,7 @@ public class ChunkUtils {
|
||||||
// Since Java stores bed colors as part of the namespaced ID and Bedrock stores it as a tag
|
// Since Java stores bed colors as part of the namespaced ID and Bedrock stores it as a tag
|
||||||
// This is the only place I could find that interacts with the Java block state and block updates
|
// This is the only place I could find that interacts with the Java block state and block updates
|
||||||
BedBlockEntityTranslator.checkForBedColor(session, blockState, position);
|
BedBlockEntityTranslator.checkForBedColor(session, blockState, position);
|
||||||
|
SkullBlockEntityTranslator.checkForSkullVariant(session, blockState, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void sendEmptyChunks(GeyserSession session, Vector3i position, int radius, boolean forceUpdate) {
|
public static void sendEmptyChunks(GeyserSession session, Vector3i position, int radius, boolean forceUpdate) {
|
||||||
|
@ -194,5 +201,6 @@ public class ChunkUtils {
|
||||||
public Object2IntMap<com.nukkitx.nbt.tag.CompoundTag> signs = new Object2IntOpenHashMap<>();
|
public Object2IntMap<com.nukkitx.nbt.tag.CompoundTag> signs = new Object2IntOpenHashMap<>();
|
||||||
public Object2IntMap<com.nukkitx.nbt.tag.CompoundTag> gateways = new Object2IntOpenHashMap<>();
|
public Object2IntMap<com.nukkitx.nbt.tag.CompoundTag> gateways = new Object2IntOpenHashMap<>();
|
||||||
public Map<Position, BlockState> beds = new HashMap<>();
|
public Map<Position, BlockState> beds = new HashMap<>();
|
||||||
|
public Map<Position, BlockState> skulls = new HashMap<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue