Skip to content

Commit

Permalink
Working palette Mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
bundabrg committed Sep 13, 2020
1 parent 409406c commit 4c7bff9
Showing 1 changed file with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,38 @@

import java.io.InputStream;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/*
Geyser re-arranged the block palette which will cause issues with the new baked in palette format.
What we do here is to re-arrange it back based upon the supported bedrock palette in Geyser and keep a record
of the mapping so we translate it in other packets
*/
public class StartGameHandler_Geyser_v408 extends StartGameHandler_Bedrock {
protected boolean initialized = false;

public StartGameHandler_Geyser_v408(BedrockTranslator translator) {
super(translator);
}

@Override
public boolean fromServer(StartGamePacket packet) {
public boolean fromDownstream(StartGamePacket packet) {
// Only grab runtime ID's for the first player
Map<Integer, Integer> runtimeIdMap = getTranslator().getRegisteredTranslator().getBlockMapper().getRuntimeIdDownstreamToUpstreamMap();
if (runtimeIdMap.size() == 0) {
if (!initialized) {
initialized = true;

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

NbtList<NbtMap> originalTags;
Map<Integer, NbtMap> originalTags;
try (NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(stream)) {
//noinspection unchecked
originalTags = (NbtList<NbtMap>) nbtInputStream.readTag();
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);
}
Expand All @@ -64,23 +71,28 @@ public boolean fromServer(StartGamePacket packet) {
for (int geyserId = 0; geyserId < geyserTags.size(); geyserId++) {
NbtMap geyserTag = geyserTags.get(geyserId);
boolean found = false;
for (int originalId = 0; originalId < originalTags.size(); originalId++) {
NbtMap originalTag = originalTags.get(originalId);
if (geyserTag.equals(originalTag)) {
runtimeIdMap.put(geyserId, originalId);
for (Map.Entry<Integer, NbtMap> entry : originalTags.entrySet()) {
if (geyserTag.equals(entry.getValue())) {
getTranslator().getRegisteredTranslator().getBlockMapper().registerRuntimeIdMapping(geyserId, entry.getKey());
originalTags.remove(entry.getKey());
found = true;
break;
}
}
if (!found) {
GeyserReversionExtension.getInstance().getLogger().error("Unable to find palette entry: " + geyserTag);
runtimeIdMap.put(geyserId, 0); // Set to 0 for now
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) {
break;
}
}
}

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

return super.fromServer(packet);
return super.fromDownstream(packet);
}
}

0 comments on commit 4c7bff9

Please sign in to comment.