Skip to content

Commit

Permalink
Fix Geyser Remapper
Browse files Browse the repository at this point in the history
  • Loading branch information
bundabrg committed Sep 13, 2020
1 parent 4c7bff9 commit bba505b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
import au.com.grieve.reversion.editions.bedrock.BedrockRegisteredTranslator;
import au.com.grieve.reversion.editions.bedrock.BedrockTranslator;
import au.com.grieve.reversion.editions.bedrock.handlers.LevelChunkHandler_Bedrock;
import au.com.grieve.reversion.editions.bedrock.handlers.UpdateBlockHandler_Bedrock;
import au.com.grieve.reversion.editions.bedrock.mappers.BlockMapper;
import au.com.grieve.reversion.editions.bedrock.mappers.EntityMapper;
import au.com.grieve.reversion.editions.bedrock.mappers.ItemMapper;
import com.nukkitx.protocol.bedrock.packet.LevelChunkPacket;
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
import com.nukkitx.protocol.bedrock.v408.Bedrock_v408;

public class Register_Geyser_v408 {
Expand All @@ -35,8 +39,14 @@ public class Register_Geyser_v408 {
.toProtocolVersion(408)
.codec(Bedrock_v408.V408_CODEC)
.translator(BedrockTranslator.class)
.blockMapper(BlockMapper.DEFAULT)
.blockMapper(BlockMapper.builder()
.palette(() -> Register_Geyser_v408.class.getResourceAsStream("/protocol/bedrock-v408/blockpalette.nbt"))
.build()
)
.itemMapper(ItemMapper.DEFAULT)
.entityMapper(EntityMapper.DEFAULT)
.registerPacketHandler(LevelChunkPacket.class, LevelChunkHandler_Bedrock.class)
.registerPacketHandler(StartGamePacket.class, StartGameHandler_Geyser_v408.class)
.registerPacketHandler(UpdateBlockPacket.class, UpdateBlockHandler_Bedrock.class)
.build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@
import au.com.grieve.geyser.reversion.GeyserReversionExtension;
import au.com.grieve.reversion.editions.bedrock.BedrockTranslator;
import au.com.grieve.reversion.editions.bedrock.handlers.StartGameHandler_Bedrock;
import com.nukkitx.nbt.NBTInputStream;
import com.nukkitx.nbt.NbtList;
import com.nukkitx.nbt.NbtMap;
import com.nukkitx.nbt.NbtUtils;
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
import org.geysermc.connector.utils.FileUtils;

import java.io.InputStream;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand All @@ -51,45 +47,34 @@ public boolean fromDownstream(StartGamePacket packet) {
if (!initialized) {
initialized = true;

/* Load block palette */
InputStream stream = FileUtils.getResource("bedrock/runtime_block_states.dat");

Map<Integer, NbtMap> originalTags;
try (NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(stream)) {
//noinspection unchecked
NbtList<NbtMap> nbt = (NbtList<NbtMap>) nbtInputStream.readTag();

originalTags = IntStream.range(0, nbt.size())
.boxed()
.collect(Collectors.toMap(i -> i, nbt::get));
} catch (Exception e) {
throw new AssertionError("Unable to get blocks from runtime block states", e);
}

NbtList<NbtMap> geyserTags = packet.getBlockPalette();

Map<Integer, NbtMap> translatedTags = IntStream.range(0, getTranslator().getRegisteredTranslator().getBlockMapper().getUpstreamPalette().size())
.boxed()
.collect(Collectors.toMap(i -> i, i -> getTranslator().getRegisteredTranslator().getBlockMapper().getUpstreamPalette().get(i).getCompound("block")));

for (int geyserId = 0; geyserId < geyserTags.size(); geyserId++) {
NbtMap geyserTag = geyserTags.get(geyserId);
NbtMap geyserTag = geyserTags.get(geyserId).getCompound("block");
boolean found = false;
for (Map.Entry<Integer, NbtMap> entry : originalTags.entrySet()) {
for (Map.Entry<Integer, NbtMap> entry : translatedTags.entrySet()) {
if (geyserTag.equals(entry.getValue())) {
getTranslator().getRegisteredTranslator().getBlockMapper().registerRuntimeIdMapping(geyserId, entry.getKey());
originalTags.remove(entry.getKey());
found = true;
translatedTags.remove(entry.getKey());
break;
}
}
if (!found) {
GeyserReversionExtension.getInstance().getLogger().error("Unable to find upstream palette entry: " + geyserTag);
getTranslator().getRegisteredTranslator().getBlockMapper().registerRuntimeIdMapping(geyserId, 0); // Set to 0 for now
}
if (originalTags.size() == 0) {
if (translatedTags.size() == 0) {
break;
}
}

if (originalTags.size() > 0) {
GeyserReversionExtension.getInstance().getLogger().error("Extra upstream unmatched palette entries: \n" + originalTags);
if (translatedTags.size() > 0) {
GeyserReversionExtension.getInstance().getLogger().error("Extra upstream unmatched palette entries: \n" + translatedTags);
}
}

Expand Down

0 comments on commit bba505b

Please sign in to comment.