Skip to content

Commit

Permalink
feat: converter unfinished
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolLoong committed Aug 7, 2023
1 parent ef0139c commit 5c23c2e
Show file tree
Hide file tree
Showing 25 changed files with 402 additions and 69 deletions.
8 changes: 2 additions & 6 deletions Allay-API/src/main/java/cn/allay/api/client/skin/Skin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import cn.allay.api.utils.Utils;
import it.unimi.dsi.fastutil.io.FastByteArrayOutputStream;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.cloudburstmc.protocol.bedrock.data.skin.*;
import org.jose4j.json.internal.json_simple.JSONObject;
Expand All @@ -26,9 +24,7 @@
*
* @author LucGamesYT | daoge_cmd
*/
@ToString
@Getter
@Setter
@Data
@Slf4j
public class Skin {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package cn.allay.api.datastruct.aabbtree;

import lombok.Getter;

import java.util.Objects;

@Getter
public record CollisionPair<T extends HasAABB & HasLongId>(T objectA, T objectB) {

@Override
Expand Down
4 changes: 2 additions & 2 deletions Allay-API/src/main/java/cn/allay/api/world/World.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cn.allay.api.world;

import cn.allay.api.block.type.BlockState;
import cn.allay.api.client.Client;
import cn.allay.api.entity.Entity;
import cn.allay.api.math.Position3ic;
import cn.allay.api.client.Client;
import cn.allay.api.scheduler.Scheduler;
import cn.allay.api.server.Server;
import cn.allay.api.world.chunk.ChunkAccessible;
Expand All @@ -17,7 +17,6 @@
import org.joml.primitives.AABBdc;

import java.util.Collection;
import java.util.List;

/**
* Describe a world
Expand Down Expand Up @@ -189,5 +188,6 @@ default BlockState[][][] getCollidingBlocks(AABBdc aabb, boolean layer, boolean
}
}
}
return blockStates;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.allay.api.world.palette.bitarray;
package cn.allay.api.world.bitarray;

import io.netty.buffer.ByteBuf;
import org.cloudburstmc.protocol.common.util.VarInts;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.allay.api.world.palette.bitarray;
package cn.allay.api.world.bitarray;

import org.cloudburstmc.math.GenericMath;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.allay.api.world.palette.bitarray;
package cn.allay.api.world.bitarray;

import lombok.Getter;
import org.cloudburstmc.math.GenericMath;

import java.util.Arrays;
Expand All @@ -10,7 +9,6 @@
*
* @author JukeboxMC | daoge_cmd
*/
@Getter
public record PaddedBitArray(BitArrayVersion version, int size, int[] words) implements BitArray {

public PaddedBitArray(BitArrayVersion version, int size, int[] words) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.allay.api.world.palette.bitarray;
package cn.allay.api.world.bitarray;

import lombok.Getter;
import org.cloudburstmc.math.GenericMath;

import java.util.Arrays;
Expand All @@ -10,7 +9,6 @@
*
* @author JukeboxMC | daoge_cmd
*/
@Getter
public record Pow2BitArray(BitArrayVersion version, int size, int[] words) implements BitArray {

public Pow2BitArray(BitArrayVersion version, int size, int[] words) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.allay.api.world.palette.bitarray;
package cn.allay.api.world.bitarray;

import io.netty.buffer.ByteBuf;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ public interface EntityPhysicsService {
boolean containEntity(Entity entity);

default void offerScheduledMove(Entity entity, Location3dc newLoc) {
offerScheduledMove(entity, newLoc, true);
}

void offerScheduledMove(Entity entity, Location3dc newLoc);

default List<Entity> computeCollidingEntities(Entity entity) {
if (entity.hasCollision())
return computeCollidingEntities(entity.getOffsetAABB());
Expand Down
103 changes: 59 additions & 44 deletions Allay-API/src/main/java/cn/allay/api/world/palette/Palette.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
package cn.allay.api.world.palette;

import cn.allay.api.world.bitarray.BitArray;
import cn.allay.api.world.bitarray.BitArrayVersion;
import cn.allay.api.world.chunk.Chunk;
import cn.allay.api.world.palette.bitarray.BitArray;
import cn.allay.api.world.palette.bitarray.BitArrayVersion;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.cloudburstmc.nbt.NBTInputStream;
import org.cloudburstmc.nbt.NBTOutputStream;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtUtils;
import org.cloudburstmc.nbt.*;
import org.cloudburstmc.protocol.common.util.VarInts;

import java.io.IOException;
import java.util.List;
import java.util.stream.IntStream;

/**
* Allay Project 2023/4/14
*
* @author JukeboxMC | daoge_cmd
*/
@Getter
@EqualsAndHashCode
public final class Palette<V> {
public static final int COPY_LAST_FLAG_HEADER = (0x7F << 1) | 1;// 11111111b (1byte)

private final List<V> palette;
private BitArray bitArray;

Expand All @@ -41,22 +36,6 @@ public Palette(V first, BitArrayVersion version) {
this.palette.add(first);
}

private static int getPaletteHeader(BitArrayVersion version, boolean runtime) {
return (version.bits << 1) | (runtime ? 1 : 0);
}

private static BitArrayVersion getVersionFromPaletteHeader(short header) {
return BitArrayVersion.get(header >> 1, true);
}

private static boolean hasCopyLastFlag(short header) {
return (header >> 1) == 0x7F;
}

private static boolean isPersistent(short header) {
return (header & 1) == 0;
}

public V get(int index) {
return this.palette.get(this.bitArray.get(index));
}
Expand All @@ -66,24 +45,34 @@ public void set(int index, V value) {
this.bitArray.set(index, paletteIndex);
}

/**
* Write the Palette data to the network buffer
*
* @param byteBuf the byte buf
* @param serializer the serializer
*/
//Anvil Level 公用
public void writeToNetwork(ByteBuf byteBuf, RuntimeDataSerializer<V> serializer) {
writeWords(byteBuf, serializer);
}

//Anvil Level 公用
public void writeToNetwork(ByteBuf byteBuf, RuntimeDataSerializer<V> serializer, Palette<V> last) {
if (writeLast(byteBuf, last)) return;
if (writeEmpty(byteBuf, serializer)) return;

writeWords(byteBuf, serializer);
}

//Anvil Level 公用
public void readFromNetwork(ByteBuf byteBuf, RuntimeDataDeserializer<V> deserializer) {
readWords(byteBuf, readBitArrayVersion(byteBuf));

final int size = this.bitArray.readSizeFromNetwork(byteBuf);
for (int i = 0; i < size; i++) this.palette.add(deserializer.deserialize(VarInts.readInt(byteBuf)));
}

//Anvil Level 公用
public void writeToStoragePersistent(ByteBuf byteBuf, PersistentDataSerializer<V> serializer) {
byteBuf.writeByte(Palette.getPaletteHeader(this.bitArray.version(), false));

Expand All @@ -99,6 +88,7 @@ public void writeToStoragePersistent(ByteBuf byteBuf, PersistentDataSerializer<V
}
}

//仅LevelDB使用
public void writeToStorageRuntime(ByteBuf byteBuf, RuntimeDataSerializer<V> serializer, Palette<V> last) {
if (writeLast(byteBuf, last)) return;
if (writeEmpty(byteBuf, serializer)) return;
Expand All @@ -109,6 +99,7 @@ public void writeToStorageRuntime(ByteBuf byteBuf, RuntimeDataSerializer<V> seri
for (V value : this.palette) byteBuf.writeIntLE(serializer.serialize(value));
}

//仅LevelDB使用
public void readFromStoragePersistent(ByteBuf byteBuf, PersistentDataDeserializer<V> deserializer) {
readWords(byteBuf, readBitArrayVersion(byteBuf));

Expand All @@ -122,6 +113,7 @@ public void readFromStoragePersistent(ByteBuf byteBuf, PersistentDataDeserialize
}
}

//仅LevelDB使用
public void readFromStorageRuntime(ByteBuf byteBuf, RuntimeDataDeserializer<V> deserializer, Palette<V> last) {
final short header = byteBuf.readUnsignedByte();

Expand All @@ -146,27 +138,11 @@ public void readFromStorageRuntime(ByteBuf byteBuf, RuntimeDataDeserializer<V> d
for (int i = 0; i < paletteSize; i++) this.palette.add(deserializer.deserialize(byteBuf.readIntLE()));
}

private BitArrayVersion readBitArrayVersion(ByteBuf byteBuf) {
short header = byteBuf.readUnsignedByte();
return Palette.getVersionFromPaletteHeader(header);
}

private void readWords(ByteBuf byteBuf, BitArrayVersion version) {
final int wordCount = version.getWordsForSize(Chunk.SECTION_SIZE);
final int[] words = new int[wordCount];
for (int i = 0; i < wordCount; i++) words[i] = byteBuf.readIntLE();

this.bitArray = version.createArray(Chunk.SECTION_SIZE, words);
this.palette.clear();
//仅Anvil使用
public NbtList<Integer> toNBT() {
return new NbtList<>(NbtType.INT, IntStream.of(this.bitArray.words()).boxed().toList());
}

private void onResize(BitArrayVersion version) {
final BitArray newBitArray = version.createArray(Chunk.SECTION_SIZE);
for (int i = 0; i < Chunk.SECTION_SIZE; i++)
newBitArray.set(i, this.bitArray.get(i));

this.bitArray = newBitArray;
}

public int paletteIndexFor(V value) {
int index = this.palette.indexOf(value);
Expand Down Expand Up @@ -225,4 +201,43 @@ private void writeWords(ByteBuf byteBuf, RuntimeDataSerializer<V> serializer) {
this.bitArray.writeSizeToNetwork(byteBuf, this.palette.size());
for (V value : this.palette) VarInts.writeInt(byteBuf, serializer.serialize(value));
}


private BitArrayVersion readBitArrayVersion(ByteBuf byteBuf) {
short header = byteBuf.readUnsignedByte();
return Palette.getVersionFromPaletteHeader(header);
}

private void readWords(ByteBuf byteBuf, BitArrayVersion version) {
final int wordCount = version.getWordsForSize(Chunk.SECTION_SIZE);
final int[] words = new int[wordCount];
for (int i = 0; i < wordCount; i++) words[i] = byteBuf.readIntLE();

this.bitArray = version.createArray(Chunk.SECTION_SIZE, words);
this.palette.clear();
}

private void onResize(BitArrayVersion version) {
final BitArray newBitArray = version.createArray(Chunk.SECTION_SIZE);
for (int i = 0; i < Chunk.SECTION_SIZE; i++)
newBitArray.set(i, this.bitArray.get(i));

this.bitArray = newBitArray;
}

private static int getPaletteHeader(BitArrayVersion version, boolean runtime) {
return (version.bits << 1) | (runtime ? 1 : 0);
}

private static BitArrayVersion getVersionFromPaletteHeader(short header) {
return BitArrayVersion.get(header >> 1, true);
}

private static boolean hasCopyLastFlag(short header) {
return (header >> 1) == 0x7F;
}

private static boolean isPersistent(short header) {
return (header & 1) == 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
* @author JukeboxMC | daoge_cmd
*/
@FunctionalInterface
public interface PersistentDataDeserializer<V> {
V deserialize(NbtMap nbtMap);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
* @author JukeboxMC | daoge_cmd
*/
@FunctionalInterface
public interface PersistentDataSerializer<V> {
NbtMap serialize(V value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
* @author JukeboxMC | daoge_cmd
*/
@FunctionalInterface
public interface RuntimeDataDeserializer<V> {
V deserialize(int id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
* @author JukeboxMC | daoge_cmd
*/
@FunctionalInterface
public interface RuntimeDataSerializer<V> {
int serialize(V value);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.allay.api.utils;
package cn.allay.server.utils;

import cn.allay.server.utils.GameLoop;
import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicInteger;
Expand Down
Loading

0 comments on commit 5c23c2e

Please sign in to comment.