| 1 | package net.minecraft.world.level.chunk;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.function.Predicate;
|
| 6 | import net.minecraft.core.IdMap;
|
| 7 | import net.minecraft.network.FriendlyByteBuf;
|
| 8 | import net.minecraft.network.VarInt;
|
| 9 | import net.minecraft.util.CrudeIncrementalIntIdentityHashBiMap;
|
| 10 |
|
| 11 | public class HashMapPalette<T> implements Palette<T> {
|
| 12 | private final CrudeIncrementalIntIdentityHashBiMap<T> values;
|
| 13 | private final int bits;
|
| 14 |
|
| 15 | public HashMapPalette(final int bits, final List<T> values) {
|
| 16 | this(bits);
|
| 17 | values.forEach(this.values::add);
|
| 18 | }
|
| 19 |
|
| 20 | public HashMapPalette(final int bits) {
|
| 21 | this(bits, CrudeIncrementalIntIdentityHashBiMap.create(1 << bits));
|
| 22 | }
|
| 23 |
|
| 24 | private HashMapPalette(final int bits, final CrudeIncrementalIntIdentityHashBiMap<T> values) {
|
| 25 | this.bits = bits;
|
| 26 | this.values = values;
|
| 27 | }
|
| 28 |
|
| 29 | public static <A> Palette<A> create(final int bits, final List<A> paletteEntries) {
|
| 30 | return new HashMapPalette<>(bits, paletteEntries);
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public int idFor(final T value, final PaletteResize<T> resizeHandler) {
|
| 35 | int id = this.values.getId(value);
|
| 36 | if (id == -1) {
|
| 37 | id = this.values.add(value);
|
| 38 | if (id >= 1 << this.bits) {
|
| 39 | id = resizeHandler.onResize(this.bits + 1, value);
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | return id;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public boolean maybeHas(final Predicate<T> predicate) {
|
| 48 | for (int i = 0; i < this.getSize(); i++) {
|
| 49 | if (predicate.test(this.values.byId(i))) {
|
| 50 | return true;
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | return false;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public T valueFor(final int index) {
|
| 59 | T value = this.values.byId(index);
|
| 60 | if (value == null) {
|
| 61 | throw new MissingPaletteEntryException(index);
|
| 62 | } else {
|
| 63 | return value;
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public void read(final FriendlyByteBuf buffer, final IdMap<T> globalMap) {
|
| 69 | this.values.clear();
|
| 70 | int size = buffer.readVarInt();
|
| 71 |
|
| 72 | for (int i = 0; i < size; i++) {
|
| 73 | this.values.add(globalMap.byIdOrThrow(buffer.readVarInt()));
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public void write(final FriendlyByteBuf buffer, final IdMap<T> globalMap) {
|
| 79 | int size = this.getSize();
|
| 80 | buffer.writeVarInt(size);
|
| 81 |
|
| 82 | for (int i = 0; i < size; i++) {
|
| 83 | buffer.writeVarInt(globalMap.getId(this.values.byId(i)));
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public int getSerializedSize(final IdMap<T> globalMap) {
|
| 89 | int size = VarInt.getByteSize(this.getSize());
|
| 90 |
|
| 91 | for (int i = 0; i < this.getSize(); i++) {
|
| 92 | size += VarInt.getByteSize(globalMap.getId(this.values.byId(i)));
|
| 93 | }
|
| 94 |
|
| 95 | return size;
|
| 96 | }
|
| 97 |
|
| 98 | public List<T> getEntries() {
|
| 99 | ArrayList<T> list = new ArrayList<>();
|
| 100 | this.values.iterator().forEachRemaining(list::add);
|
| 101 | return list;
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public int getSize() {
|
| 106 | return this.values.size();
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public Palette<T> copy() {
|
| 111 | return new HashMapPalette<>(this.bits, this.values.copy());
|
| 112 | }
|
| 113 | }
|
| 114 |
|