From 3b14dd3b04c55ceebc974af6c006254d9798b5d3 Mon Sep 17 00:00:00 2001 From: Gerrygames Date: Mon, 12 Nov 2018 16:08:01 +0100 Subject: [PATCH] Use methods for palette rewriting, fix "value already present" --- .../api/minecraft/chunks/ChunkSection.java | 18 ++++++++++++++++++ .../packets/WorldPackets.java | 6 ++---- .../packets/WorldPackets.java | 7 +++---- .../types/Chunk1_9_1_2Type.java | 4 ++-- .../types/Chunk1_9to1_8Type.java | 4 ++-- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/minecraft/chunks/ChunkSection.java b/common/src/main/java/us/myles/ViaVersion/api/minecraft/chunks/ChunkSection.java index c08a89eb5..88b69da0e 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/minecraft/chunks/ChunkSection.java +++ b/common/src/main/java/us/myles/ViaVersion/api/minecraft/chunks/ChunkSection.java @@ -78,6 +78,24 @@ public class ChunkSection { return blocks[idx]; } + public int getPaletteSize() { + return palette.size(); + } + + public int getPaletteEntry(int index) { + if (index < 0 || index >= palette.size()) throw new IndexOutOfBoundsException(); + return palette.inverse().get(index); + } + + public void setPaletteEntry(int index, int id) { + if (index < 0 || index >= palette.size()) throw new IndexOutOfBoundsException(); + palette.forcePut(id, index); + } + + public void replacePaletteEntry(int oldId, int newId) { + if (palette.containsKey(oldId)) palette.put(newId, palette.remove(oldId)); + } + /** * Set a block state in the chunk * This method will not update non-air blocks count diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java index edae54c15..f643d423f 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/packets/WorldPackets.java @@ -1,6 +1,5 @@ package us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets; -import com.google.common.collect.BiMap; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.BlockChangeRecord; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; @@ -29,9 +28,8 @@ public class WorldPackets { for (ChunkSection section : chunk.getSections()) { if (section == null) continue; - BiMap inverse = section.getPalette().inverse(); - for (int i = 0; i < inverse.size(); i++) { - inverse.put(i, Protocol1_13_1To1_13.getNewBlockStateId(inverse.get(i))); + for (int i = 0; i < section.getPaletteSize(); i++) { + section.setPaletteEntry(i, Protocol1_13_1To1_13.getNewBlockStateId(section.getPaletteEntry(i))); } } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java index 9dd01c2de..813732bc6 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/packets/WorldPackets.java @@ -242,14 +242,13 @@ public class WorldPackets { boolean willStoreAnyBlock = false; - BiMap inverse = section.getPalette().inverse(); - for (int p = 0; p < inverse.size(); p++) { - int old = inverse.get(p); + for (int p = 0; p < section.getPaletteSize(); p++) { + int old = section.getPaletteEntry(p); int newId = toNewId(old); if (storage.isWelcome(newId)) { willStoreAnyBlock = true; } - inverse.put(p, newId); + section.setPaletteEntry(p, newId); } if (willStoreAnyBlock) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java index 6f3d1f6ee..64a0bc949 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java @@ -54,8 +54,8 @@ public class Chunk1_9_1_2Type extends PartialType { if (world.getEnvironment() == Environment.NORMAL) { section.readSkyLight(input); } - if (replacePistons && section.getPalette().containsKey(36)) { - section.getPalette().put(replacementId, section.getPalette().remove(36)); + if (replacePistons) { + section.replacePaletteEntry(36, replacementId); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/Chunk1_9to1_8Type.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/Chunk1_9to1_8Type.java index eae734f1b..4f2d47b18 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/Chunk1_9to1_8Type.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/Chunk1_9to1_8Type.java @@ -89,8 +89,8 @@ public class Chunk1_9to1_8Type extends PartialType { ChunkSection section = Types1_8.CHUNK_SECTION.read(input); sections[i] = section; - if (replacePistons && section.getPalette().containsKey(36)) { - section.getPalette().put(replacementId, section.getPalette().remove(36)); + if (replacePistons) { + section.replacePaletteEntry(36, replacementId); } }