Skip to content

Commit

Permalink
Merge pull request #2 from DoctorMacc/custom-skulls
Browse files Browse the repository at this point in the history
Custom skull code simplifying
  • Loading branch information
OnlyBMan authored May 29, 2020
2 parents 97e9de2 + 9099009 commit d8798eb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@

package org.geysermc.connector.network.translators.java.world;

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.packet.ingame.server.world.ServerUpdateTileEntityPacket;

import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
Expand All @@ -47,22 +45,19 @@ public void translate(ServerUpdateTileEntityPacket packet, GeyserSession session
BlockEntityTranslator translator = BlockEntityUtils.getBlockEntityTranslator(id);
// If not null then the BlockState is used in BlockEntityTranslator.translateTag()
if (ChunkUtils.CACHED_BLOCK_ENTITIES.get(packet.getPosition()) != null) {
// Check for custom skulls.
if (packet.getNbt().contains("Owner") && SkullBlockEntityTranslator.allowCustomSkulls) {
CompoundTag owner = packet.getNbt().get("Owner");
if (owner.contains("Properties")) {
BlockState blockState = ChunkUtils.CACHED_BLOCK_ENTITIES.get(packet.getPosition());
SkullBlockEntityTranslator.spawnPlayer(session, packet.getNbt(), blockState);
}
}
BlockEntityUtils.updateBlockEntity(session, translator.getBlockEntityTag(id, packet.getNbt(),
ChunkUtils.CACHED_BLOCK_ENTITIES.get(packet.getPosition())), packet.getPosition());
ChunkUtils.CACHED_BLOCK_ENTITIES.remove(packet.getPosition());
} else {
BlockEntityUtils.updateBlockEntity(session, translator.getBlockEntityTag(id, packet.getNbt(), null), packet.getPosition());
}

//Check for custom skulls. This was the only place I could find that would update the entity right as the block was placed.
if (packet.getNbt().contains("Owner") && SkullBlockEntityTranslator.allowCustomSkulls) {
CompoundTag Owner = packet.getNbt().get("Owner");
if (Owner.contains("Properties")) {
CompoundTag tag = packet.getNbt();
Position position = new Position((int) tag.get("x").getValue(), (int) tag.get("y").getValue(), (int) tag.get("z").getValue());
BlockState blockState = session.getChunkCache().getBlockAt(position);
SkullBlockEntityTranslator.SpawnPlayer(session, tag, blockState);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static SerializedSkin getSkin(com.github.steveice10.opennbt.tag.builtin.C
return null;
}

public static void SpawnPlayer(GeyserSession session, com.github.steveice10.opennbt.tag.builtin.CompoundTag tag, BlockState blockState) {
public static void spawnPlayer(GeyserSession session, com.github.steveice10.opennbt.tag.builtin.CompoundTag tag, BlockState blockState) {
SerializedSkin skin = getSkin(tag);
float x = (int) tag.get("x").getValue() + .5f;
float y = (int) tag.get("y").getValue() - .01f;
Expand Down Expand Up @@ -200,9 +200,7 @@ public static void SpawnPlayer(GeyserSession session, com.github.steveice10.open
}, 500, TimeUnit.MILLISECONDS); //Delay 5 seconds to give the model time to load in
}

public static boolean ContainsCustomSkull(Position position) {
if (CACHED_SKULLS.containsKey(position))
return true;
return false;
public static boolean containsCustomSkull(Position position) {
return CACHED_SKULLS.containsKey(position);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,22 @@ public static ChunkData translateToBedrock(Column column, GeyserSession session)
Position pos = new Position((int) tag.get("x").getValue(), (int) tag.get("y").getValue(), (int) tag.get("z").getValue());
BlockState blockState = blockEntityPositions.get(pos);
bedrockBlockEntities[i] = blockEntityTranslator.getBlockEntityTag(tagName, tag, blockState);

//Check for custom skulls
if (tag.contains("Owner") && SkullBlockEntityTranslator.allowCustomSkulls) {
CompoundTag Owner = tag.get("Owner");
if (Owner.contains("Properties")) {
SkullBlockEntityTranslator.spawnPlayer(session, tag, blockState);
}
}

i++;
}
for (com.nukkitx.nbt.tag.CompoundTag tag : bedrockOnlyBlockEntities) {
bedrockBlockEntities[i] = tag;
i++;
}

//Check for custom skulls
for (CompoundTag compoundTag : blockEntities) {
if (compoundTag.contains("Owner") && SkullBlockEntityTranslator.allowCustomSkulls) {
CompoundTag Owner = compoundTag.get("Owner");
if (Owner.contains("Properties")) {
BlockState blockState = blockEntityPositions.get(new Position((int) compoundTag.get("x").getValue(), (int) compoundTag.get("y").getValue(), (int) compoundTag.get("z").getValue()));
SkullBlockEntityTranslator.SpawnPlayer(session, compoundTag, blockState);
}
}
}
chunkData.blockEntities = bedrockBlockEntities;
return chunkData;
}
Expand Down Expand Up @@ -203,12 +202,12 @@ public static void updateBlock(GeyserSession session, BlockState blockState, Vec
}
}

if (SkullBlockEntityTranslator.ContainsCustomSkull(new Position(position.getX(), position.getY(), position.getZ())) && blockState.equals(AIR)) {
Position position1 = new Position(position.getX(), position.getY(), position.getZ());
if (SkullBlockEntityTranslator.containsCustomSkull(new Position(position.getX(), position.getY(), position.getZ())) && blockState.equals(AIR)) {
Position skullPosition = new Position(position.getX(), position.getY(), position.getZ());
RemoveEntityPacket removeEntityPacket = new RemoveEntityPacket();
removeEntityPacket.setUniqueEntityId(SkullBlockEntityTranslator.CACHED_SKULLS.get(position1).getGeyserId());
removeEntityPacket.setUniqueEntityId(SkullBlockEntityTranslator.CACHED_SKULLS.get(skullPosition).getGeyserId());
session.sendUpstreamPacket(removeEntityPacket);
CACHED_BLOCK_ENTITIES.remove(position1);
SkullBlockEntityTranslator.CACHED_SKULLS.remove(skullPosition);
}

int blockId = BlockTranslator.getBedrockBlockId(blockState);
Expand Down

0 comments on commit d8798eb

Please sign in to comment.