/* * Decompiled with CFR 0.152. */ package net.minecraft.world.level.chunk; import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; import net.minecraft.core.IdMap; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.VarInt; import net.minecraft.util.CrudeIncrementalIntIdentityHashBiMap; import net.minecraft.world.level.chunk.MissingPaletteEntryException; import net.minecraft.world.level.chunk.Palette; import net.minecraft.world.level.chunk.PaletteResize; public class HashMapPalette implements Palette { private final CrudeIncrementalIntIdentityHashBiMap values; private final int bits; public HashMapPalette(int bits, List values) { this(bits); values.forEach(this.values::add); } public HashMapPalette(int bits) { this(bits, CrudeIncrementalIntIdentityHashBiMap.create(1 << bits)); } private HashMapPalette(int bits, CrudeIncrementalIntIdentityHashBiMap values) { this.bits = bits; this.values = values; } public static Palette create(int bits, List paletteEntries) { return new HashMapPalette(bits, paletteEntries); } @Override public int idFor(T value, PaletteResize resizeHandler) { int id = this.values.getId(value); if (id == -1 && (id = this.values.add(value)) >= 1 << this.bits) { id = resizeHandler.onResize(this.bits + 1, value); } return id; } @Override public boolean maybeHas(Predicate predicate) { for (int i = 0; i < this.getSize(); ++i) { if (!predicate.test(this.values.byId(i))) continue; return true; } return false; } @Override public T valueFor(int index) { T value = this.values.byId(index); if (value == null) { throw new MissingPaletteEntryException(index); } return value; } @Override public void read(FriendlyByteBuf buffer, IdMap globalMap) { this.values.clear(); int size = buffer.readVarInt(); for (int i = 0; i < size; ++i) { this.values.add(globalMap.byIdOrThrow(buffer.readVarInt())); } } @Override public void write(FriendlyByteBuf buffer, IdMap globalMap) { int size = this.getSize(); buffer.writeVarInt(size); for (int i = 0; i < size; ++i) { buffer.writeVarInt(globalMap.getId(this.values.byId(i))); } } @Override public int getSerializedSize(IdMap globalMap) { int size = VarInt.getByteSize(this.getSize()); for (int i = 0; i < this.getSize(); ++i) { size += VarInt.getByteSize(globalMap.getId(this.values.byId(i))); } return size; } public List getEntries() { ArrayList list = new ArrayList(); this.values.iterator().forEachRemaining(list::add); return list; } @Override public int getSize() { return this.values.size(); } @Override public Palette copy() { return new HashMapPalette(this.bits, this.values.copy()); } }