Use methods for palette rewriting, fix "value already present"

This commit is contained in:
Gerrygames 2018-11-12 16:08:01 +01:00
parent 9a87adf51b
commit 3b14dd3b04
5 changed files with 27 additions and 12 deletions

View File

@ -78,6 +78,24 @@ public class ChunkSection {
return blocks[idx]; 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 * Set a block state in the chunk
* This method will not update non-air blocks count * This method will not update non-air blocks count

View File

@ -1,6 +1,5 @@
package us.myles.ViaVersion.protocols.protocol1_13_1to1_13.packets; 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.PacketWrapper;
import us.myles.ViaVersion.api.minecraft.BlockChangeRecord; import us.myles.ViaVersion.api.minecraft.BlockChangeRecord;
import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.Chunk;
@ -29,9 +28,8 @@ public class WorldPackets {
for (ChunkSection section : chunk.getSections()) { for (ChunkSection section : chunk.getSections()) {
if (section == null) continue; if (section == null) continue;
BiMap<Integer, Integer> inverse = section.getPalette().inverse(); for (int i = 0; i < section.getPaletteSize(); i++) {
for (int i = 0; i < inverse.size(); i++) { section.setPaletteEntry(i, Protocol1_13_1To1_13.getNewBlockStateId(section.getPaletteEntry(i)));
inverse.put(i, Protocol1_13_1To1_13.getNewBlockStateId(inverse.get(i)));
} }
} }
} }

View File

@ -242,14 +242,13 @@ public class WorldPackets {
boolean willStoreAnyBlock = false; boolean willStoreAnyBlock = false;
BiMap<Integer, Integer> inverse = section.getPalette().inverse(); for (int p = 0; p < section.getPaletteSize(); p++) {
for (int p = 0; p < inverse.size(); p++) { int old = section.getPaletteEntry(p);
int old = inverse.get(p);
int newId = toNewId(old); int newId = toNewId(old);
if (storage.isWelcome(newId)) { if (storage.isWelcome(newId)) {
willStoreAnyBlock = true; willStoreAnyBlock = true;
} }
inverse.put(p, newId); section.setPaletteEntry(p, newId);
} }
if (willStoreAnyBlock) { if (willStoreAnyBlock) {

View File

@ -54,8 +54,8 @@ public class Chunk1_9_1_2Type extends PartialType<Chunk, ClientWorld> {
if (world.getEnvironment() == Environment.NORMAL) { if (world.getEnvironment() == Environment.NORMAL) {
section.readSkyLight(input); section.readSkyLight(input);
} }
if (replacePistons && section.getPalette().containsKey(36)) { if (replacePistons) {
section.getPalette().put(replacementId, section.getPalette().remove(36)); section.replacePaletteEntry(36, replacementId);
} }
} }

View File

@ -89,8 +89,8 @@ public class Chunk1_9to1_8Type extends PartialType<Chunk, ClientChunks> {
ChunkSection section = Types1_8.CHUNK_SECTION.read(input); ChunkSection section = Types1_8.CHUNK_SECTION.read(input);
sections[i] = section; sections[i] = section;
if (replacePistons && section.getPalette().containsKey(36)) { if (replacePistons) {
section.getPalette().put(replacementId, section.getPalette().remove(36)); section.replacePaletteEntry(36, replacementId);
} }
} }