mirror of
https://github.com/LegacyModdingMC/ChunkAPI.git
synced 2026-11-18 19:29:12 +00:00
Cubic Chunks Compatibility
This commit is contained in:
1 parent
b94617d502
commit
023d9c96ee
4 files changed
+158
-4
No files matched your search
@@ -59,12 +59,14 @@ public class DataRegistryImpl {
|
||||
private static final Map<String, OrderedManager> managersUnordered = new HashMap<>();
|
||||
private static final SortedSet<OrderedManager> managers = new TreeSet<>();
|
||||
private static final DualMap<PacketManagerInfo> packetManagers = new DualMap<>();
|
||||
private static final DualMap<CubicPacketManagerInfo> cubicPacketManagers = new DualMap<>();
|
||||
private static final DualMap<DataManager.BlockPacketDataManager> blockPacketManagers = new DualMap<>();
|
||||
private static final DualMap<DataManager.StorageDataManager> NBTManagers = new DualMap<>();
|
||||
private static final SortedMap<OrderedManager, DataManager.ChunkDataManager> chunkNBTManagers = new TreeMap<>();
|
||||
private static final SortedMap<OrderedManager, DataManager.SubChunkDataManager> subChunkNBTManagers = new TreeMap<>();
|
||||
private static final Set<String> disabledManagers = new HashSet<>();
|
||||
private static int maxPacketSize = 4;
|
||||
private static int maxPacketSizeCubic = 4;
|
||||
|
||||
@Data
|
||||
private static class PacketManagerInfo {
|
||||
@@ -72,6 +74,12 @@ public class DataRegistryImpl {
|
||||
public final DataManager.PacketDataManager manager;
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class CubicPacketManagerInfo {
|
||||
public final int maxPacketSize;
|
||||
public final DataManager.CubicPacketDataManager manager;
|
||||
}
|
||||
|
||||
public static void registerDataManager(DataManager manager, int ordering) throws IllegalStateException, IllegalArgumentException {
|
||||
if (Loader.instance().getLoaderState() != LoaderState.INITIALIZATION) {
|
||||
throw new IllegalStateException("ChunkDataManager registration is not allowed at this time! Please register your ChunkDataManager in the init phase.");
|
||||
@@ -100,6 +108,12 @@ public class DataRegistryImpl {
|
||||
val blockPacketManager = (DataManager.BlockPacketDataManager) manager;
|
||||
blockPacketManagers.put(ord, blockPacketManager);
|
||||
}
|
||||
if (manager instanceof DataManager.CubicPacketDataManager) {
|
||||
val cubicPacketManager = (DataManager.CubicPacketDataManager) manager;
|
||||
val maxSize = cubicPacketManager.maxPacketSizeCubic();
|
||||
maxPacketSizeCubic += 4 + id.getBytes(StandardCharsets.UTF_8).length + 4 + maxSize;
|
||||
cubicPacketManagers.put(ord, new CubicPacketManagerInfo(maxSize, cubicPacketManager));
|
||||
}
|
||||
if (manager instanceof DataManager.StorageDataManager) {
|
||||
NBTManagers.put(ord, (DataManager.StorageDataManager) manager);
|
||||
if (manager instanceof DataManager.ChunkDataManager) {
|
||||
@@ -127,6 +141,10 @@ public class DataRegistryImpl {
|
||||
val removed = packetManagers.remove(ord);
|
||||
maxPacketSize -= 4 + id.getBytes(StandardCharsets.UTF_8).length + 4 + removed.maxPacketSize;
|
||||
}
|
||||
if (cubicPacketManagers.containsKey(ord)) {
|
||||
val removed = cubicPacketManagers.remove(ord);
|
||||
maxPacketSizeCubic -= 4 + id.getBytes(StandardCharsets.UTF_8).length + 4 + removed.maxPacketSize;
|
||||
}
|
||||
blockPacketManagers.remove(ord);
|
||||
chunkNBTManagers.remove(ord);
|
||||
subChunkNBTManagers.remove(ord);
|
||||
@@ -141,6 +159,10 @@ public class DataRegistryImpl {
|
||||
return maxPacketSize;
|
||||
}
|
||||
|
||||
public static int maxPacketSizeCubic() {
|
||||
return maxPacketSizeCubic;
|
||||
}
|
||||
|
||||
private static void writeString(ByteBuffer buffer, String string) {
|
||||
val bytes = string.getBytes();
|
||||
buffer.putInt(bytes.length);
|
||||
@@ -177,6 +199,29 @@ public class DataRegistryImpl {
|
||||
}
|
||||
}
|
||||
|
||||
public static void readFromBufferCubic(Chunk chunk, ExtendedBlockStorage blockStorage, byte[] data) {
|
||||
val buf = ByteBuffer.wrap(data);
|
||||
buf.order(ByteOrder.LITTLE_ENDIAN);
|
||||
int count = buf.getInt();
|
||||
for (int i = 0; i < count; i++) {
|
||||
val id = readString(buf);
|
||||
val length = buf.getInt();
|
||||
val managerInfo = cubicPacketManagers.get(id);
|
||||
if (managerInfo == null) {
|
||||
Common.LOG.error("Received data for unknown CubicPacketDataManager " + id + ". Skipping.");
|
||||
buf.position(buf.position() + length);
|
||||
continue;
|
||||
}
|
||||
if (length > managerInfo.maxPacketSize) {
|
||||
Common.LOG.error("Received packet larger than max size for CubicPacketDataManager " + id + "! Continuing anyways, things might break!");
|
||||
}
|
||||
int start = buf.position();
|
||||
val slice = createSlice(buf, start, length);
|
||||
managerInfo.manager.readFromBuffer(chunk, blockStorage, slice);
|
||||
buf.position(start + length);
|
||||
}
|
||||
}
|
||||
|
||||
public static int writeToBuffer(Chunk chunk, int subChunkMask, boolean forceUpdate, byte[] data) {
|
||||
val buf = ByteBuffer.wrap(data);
|
||||
buf.order(ByteOrder.LITTLE_ENDIAN);
|
||||
@@ -195,6 +240,24 @@ public class DataRegistryImpl {
|
||||
return buf.position();
|
||||
}
|
||||
|
||||
public static int writeToBufferCubic(Chunk chunk, ExtendedBlockStorage blockStorage, byte[] data) {
|
||||
val buf = ByteBuffer.wrap(data);
|
||||
buf.order(ByteOrder.LITTLE_ENDIAN);
|
||||
buf.putInt(cubicPacketManagers.size());
|
||||
for (val pair : cubicPacketManagers.entrySet()) {
|
||||
val ord = pair.getKey();
|
||||
val managerInfo = pair.getValue();
|
||||
writeString(buf, ord.id);
|
||||
int start = buf.position() + 4;
|
||||
val slice = createSlice(buf, start, managerInfo.maxPacketSize);
|
||||
managerInfo.manager.writeToBuffer(chunk, blockStorage, slice);
|
||||
int length = slice.position();
|
||||
buf.putInt(length);
|
||||
buf.position(start + length);
|
||||
}
|
||||
return buf.position();
|
||||
}
|
||||
|
||||
public static void writeBlockToPacket(Chunk chunk, int x, int y, int z, S23PacketBlockChange packet) {
|
||||
for (val manager : blockPacketManagers.values()) {
|
||||
manager.writeBlockToPacket(chunk, x, y, z, packet);
|
||||
|
||||
@@ -40,7 +40,7 @@ import java.nio.ByteBuffer;
|
||||
import static com.falsepattern.chunk.internal.Common.BLOCKS_PER_SUBCHUNK;
|
||||
import static com.falsepattern.chunk.internal.Common.SUBCHUNKS_PER_CHUNK;
|
||||
|
||||
public class BlockIDManager extends VanillaManager implements DataManager.PacketDataManager, DataManager.BlockPacketDataManager, DataManager.SubChunkDataManager {
|
||||
public class BlockIDManager extends VanillaManager implements DataManager.PacketDataManager, DataManager.CubicPacketDataManager, DataManager.BlockPacketDataManager, DataManager.SubChunkDataManager {
|
||||
private static final int LSB_BYTES_PER_SUBCHUNK = BLOCKS_PER_SUBCHUNK;
|
||||
private static final int MSB_BYTES_PER_SUBCHUNK = BLOCKS_PER_SUBCHUNK / 2;
|
||||
private static final int HEADER_SIZE = 2;
|
||||
@@ -100,6 +100,36 @@ public class BlockIDManager extends VanillaManager implements DataManager.Packet
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int maxPacketSizeCubic() {
|
||||
return HEADER_SIZE + LSB_BYTES_PER_SUBCHUNK + MSB_BYTES_PER_SUBCHUNK;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToBuffer(Chunk chunk, ExtendedBlockStorage blockStorage, ByteBuffer buffer) {
|
||||
buffer.put(blockStorage.getBlockLSBArray());
|
||||
|
||||
if (blockStorage.getBlockMSBArray() != null) {
|
||||
buffer.put((byte) 1);
|
||||
buffer.put(blockStorage.getBlockMSBArray().data);
|
||||
} else {
|
||||
buffer.put((byte) 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromBuffer(Chunk chunk, ExtendedBlockStorage blockStorage, ByteBuffer buffer) {
|
||||
buffer.get(blockStorage.getBlockLSBArray());
|
||||
|
||||
if (buffer.get() != 0) {
|
||||
if (blockStorage.getBlockMSBArray() == null) {
|
||||
blockStorage.createBlockMSBArray();
|
||||
}
|
||||
|
||||
buffer.get(blockStorage.getBlockMSBArray().data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean subChunkPrivilegedAccess() {
|
||||
return true;
|
||||
|
||||
@@ -32,7 +32,7 @@ import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public abstract class NibbleManager extends VanillaManager implements DataManager.PacketDataManager {
|
||||
public abstract class NibbleManager extends VanillaManager implements DataManager.PacketDataManager, DataManager.CubicPacketDataManager {
|
||||
public static final int BYTES_PER_SUBCHUNK = Common.BLOCKS_PER_SUBCHUNK / 2;
|
||||
|
||||
protected abstract NibbleArray getNibbleArray(ExtendedBlockStorage subChunk);
|
||||
@@ -67,4 +67,19 @@ public abstract class NibbleManager extends VanillaManager implements DataManage
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int maxPacketSizeCubic() {
|
||||
return BYTES_PER_SUBCHUNK;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToBuffer(Chunk chunk, ExtendedBlockStorage blockStorage, ByteBuffer buffer) {
|
||||
buffer.put(getNibbleArray(blockStorage).data, 0, BYTES_PER_SUBCHUNK);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromBuffer(Chunk chunk, ExtendedBlockStorage blockStorage, ByteBuffer buffer) {
|
||||
buffer.get(getNibbleArray(blockStorage).data, 0, BYTES_PER_SUBCHUNK);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user