Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.github.steveice10.mc.protocol.data.game.chunk;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.*;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
@EqualsAndHashCode
public class BitStorage {
private static final int[] MAGIC_VALUES = {
Expand Down Expand Up @@ -77,6 +76,11 @@ public BitStorage(int bitsPerEntry, int size, long @Nullable[] data) {
this.divideShift = MAGIC_VALUES[magicIndex + 2];
}

public BitStorage(BitStorage original) {
this(Arrays.copyOf(original.data, original.data.length), original.bitsPerEntry, original.size,
original.maxValue, original.valuesPerLong, original.divideMultiply, original.divideAdd, original.divideShift);
}

public int get(int index) {
if (index < 0 || index > this.size - 1) {
throw new IndexOutOfBoundsException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public ChunkSection() {
this(0, DataPalette.createForChunk(), DataPalette.createForBiome(4));
}

public ChunkSection(ChunkSection original) {
this(original.blockCount, new DataPalette(original.chunkData), new DataPalette(original.biomeData));
}

public int getBlock(int x, int y, int z) {
return this.chunkData.get(x, y, z);
}
Expand All @@ -35,4 +39,4 @@ public void setBlock(int x, int y, int z, int state) {
public boolean isBlockCountEmpty() {
return this.blockCount == 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class DataPalette {
private final PaletteType paletteType;
private final int globalPaletteBits;

public DataPalette(DataPalette original) {
this(original.palette.copy(), original.storage == null ? null : new BitStorage(original.storage), original.paletteType, original.globalPaletteBits);
}

public static DataPalette createForChunk() {
return createForChunk(GLOBAL_PALETTE_BITS_PER_ENTRY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public int stateToId(int state) {
public int idToState(int id) {
return id;
}

@Override
public GlobalPalette copy() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import com.github.steveice10.mc.protocol.codec.MinecraftCodecHelper;
import io.netty.buffer.ByteBuf;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.io.IOException;
import java.util.Arrays;

/**
* A palette backed by a List.
*/
@Getter
@EqualsAndHashCode
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ListPalette implements Palette {
private final int maxId;

Expand Down Expand Up @@ -65,4 +69,9 @@ public int idToState(int id) {
return 0;
}
}

@Override
public ListPalette copy() {
return new ListPalette(this.maxId, Arrays.copyOf(this.data, this.data.length), this.nextId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import io.netty.buffer.ByteBuf;
import io.netty.util.collection.IntObjectHashMap;
import io.netty.util.collection.IntObjectMap;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;

import java.io.IOException;
import java.util.Arrays;

/**
* A palette backed by a map.
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@EqualsAndHashCode
public class MapPalette implements Palette {
private final int maxId;
Expand Down Expand Up @@ -66,4 +70,11 @@ public int idToState(int id) {
return 0;
}
}

@Override
public MapPalette copy() {
MapPalette mapPalette = new MapPalette(this.maxId, Arrays.copyOf(this.idToState, this.idToState.length), this.nextId);
mapPalette.stateToId.putAll(this.stateToId);
return mapPalette;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface Palette {
* @param state Block state to convert.
* @return The resulting storage ID.
*/
public int stateToId(int state);
int stateToId(int state);

/**
* Converts a storage ID to a block state. If the storage ID has no mapping,
Expand All @@ -27,5 +27,12 @@ public interface Palette {
* @param id Storage ID to convert.
* @return The resulting block state.
*/
public int idToState(int id);
int idToState(int id);

/**
* Creates a copy of this palette.
* This performs a deep copy of the palette's internal data.
* @return The palette's copy.
*/
Palette copy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public int idToState(int id) {
}
return 0;
}

@Override
public SingletonPalette copy() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,15 @@ public void testChunkSectionEncoding() throws IOException {
Assert.assertEquals("Decoded packet does not match original: " + section + " vs " + decoded, section, decoded);
}
}

@Test
public void testDeepCopy() {
for (ChunkSection section : chunkSectionsToTest) {
ChunkSection copy = new ChunkSection(section);
Assert.assertEquals("Deep copy does not match original: " + section + " vs " + copy, section, copy);

copy.setBlock(1, 1, 1, 10);
Assert.assertNotEquals("Deep copy is not deep: " + section + " vs " + copy, section, copy);
}
}
}