migrate some spaghetti code to ChunkAPI

replaced netcode/saving logic for blocks, metadata, and biomes
This commit is contained in:
FalsePattern committed 2023-03-30 22:50:11 +02:00
1 parent 984c976b34
commit de5fa2d322
17 files changed
+394 -978

No files matched your search

@@ -0,0 +1,75 @@
package com.falsepattern.endlessids.managers;
import com.falsepattern.chunk.api.ChunkDataManager;
import com.falsepattern.endlessids.Hooks;
import com.falsepattern.endlessids.Tags;
import com.falsepattern.endlessids.mixin.helpers.IChunkMixin;
import lombok.var;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.chunk.Chunk;
import java.nio.ByteBuffer;
public class BiomeManager implements ChunkDataManager, ChunkDataManager.PacketDataManager, ChunkDataManager.ChunkNBTDataManager {
@Override
public String domain() {
return Tags.MODID;
}
@Override
public String id() {
return "biome";
}
@Override
public int maxPacketSize() {
return 2 * 16 * 16;
}
@Override
public void writeToBuffer(Chunk chunk, int ebsMask, boolean forceUpdate, ByteBuffer data) {
data.asShortBuffer().put(((IChunkMixin)chunk).getBiomeShortArray());
data.position(data.position() + maxPacketSize());
}
@Override
public void readFromBuffer(Chunk chunk, int ebsMask, boolean forceUpdate, ByteBuffer buffer) {
var arr = ((IChunkMixin)chunk).getBiomeShortArray();
if (arr == null) {
arr = new short[16 * 16];
((IChunkMixin)chunk).setBiomeShortArray(arr);
}
buffer.asShortBuffer().get(arr);
buffer.position(buffer.position() + maxPacketSize());
}
@Override
public boolean chunkPrivilegedAccess() {
return true;
}
@Override
public void writeChunkToNBT(Chunk chunk, NBTTagCompound nbt) {
byte[] arr = Hooks.shortToByteArray(((IChunkMixin)chunk).getBiomeShortArray());
nbt.setByteArray("Biomes16v2", arr);
}
@Override
public void readChunkFromNBT(Chunk chunk, NBTTagCompound nbt) {
var data = ((IChunkMixin) chunk).getBiomeShortArray();
if (data == null) {
data = new short[16 * 16];
((IChunkMixin)chunk).setBiomeShortArray(data);
}
if (nbt.hasKey("Biomes16v2", 7)) {
Hooks.byteToShortArray(nbt.getByteArray("Biomes16v2"), 0, data, 0, data.length * 2);
} else if (nbt.hasKey("Biomes16", 7)) { //NotEnoughBiomes DFU
Hooks.byteToShortArrayLegacy(nbt.getByteArray("Biomes16"), data);
} else if (nbt.hasKey("Biomes", 7)) { //Vanilla DFU
Hooks.scatter(nbt.getByteArray("Biomes"), data);
}
}
}
@@ -0,0 +1,161 @@
package com.falsepattern.endlessids.managers;
import com.falsepattern.chunk.api.ChunkDataManager;
import com.falsepattern.endlessids.Tags;
import com.falsepattern.endlessids.mixin.helpers.IExtendedBlockStorageMixin;
import lombok.val;
import lombok.var;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.NibbleArray;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import java.nio.ByteBuffer;
public class BlockIDManager implements ChunkDataManager, ChunkDataManager.PacketDataManager, ChunkDataManager.SectionNBTDataManager {
@Override
public String domain() {
return Tags.MODID;
}
@Override
public String id() {
return "blockid";
}
@Override
public int maxPacketSize() {
return 3 * 16 * 16 * 16 * 16 + 4;
}
@Override
public void writeToBuffer(Chunk chunk, int ebsMask, boolean forceUpdate, ByteBuffer data) {
val ebsList = chunk.getBlockStorageArray();
int storageFlags = 0;
val start = data.position() + 4;
data.position(start);
for (int i = 0; i < 16; i++) {
if ((ebsMask & (1 << i)) == 0 || ebsList[i] == null) {
continue;
}
val ebs = (IExtendedBlockStorageMixin) ebsList[i];
storageFlags |= ebs.getEBSMSBMask() << (i * 2);
val b1 = ebs.getB1();
data.put(b1);
val b2Low = ebs.getB2Low();
if (b2Low == null) {
continue;
}
data.put(b2Low.data);
val b2High = ebs.getB2High();
if (b2High == null) {
continue;
}
data.put(b2High.data);
val b3 = ebs.getB3();
if (b3 != null) {
data.put(b3);
}
}
data.putInt(start - 4, storageFlags);
}
@Override
public void readFromBuffer(Chunk chunk, int ebsMask, boolean forceUpdate, ByteBuffer buffer) {
val ebsList = chunk.getBlockStorageArray();
val storageFlags = buffer.getInt();
for (int i = 0; i < 16; i++) {
if ((ebsMask & (1 << i)) == 0 || ebsList[i] == null) {
continue;
}
val ebs = (IExtendedBlockStorageMixin) ebsList[i];
val storageFlag = (storageFlags >>> (i * 2)) & 3;
val b1 = ebs.getB1();
buffer.get(b1);
if (storageFlag == 0b00) {
ebs.clearB2Low();
ebs.clearB2High();
ebs.clearB3();
continue;
}
var b2Low = ebs.getB2Low();
if (b2Low == null) {
b2Low = ebs.createB2Low();
}
buffer.get(b2Low.data);
if (storageFlag == 0b01) {
ebs.clearB2High();
ebs.clearB3();
continue;
}
var b2High = ebs.getB2High();
if (b2High == null) {
b2High = ebs.createB2High();
}
buffer.get(b2High.data);
if (storageFlag == 0b10) {
ebs.clearB3();
continue;
}
var b3 = ebs.getB3();
if (b3 == null) {
b3 = ebs.createB3();
}
buffer.get(b3);
}
}
@Override
public boolean sectionPrivilegedAccess() {
return true;
}
@Override
public void writeSectionToNBT(Chunk chunk, ExtendedBlockStorage ebsVanilla, NBTTagCompound section) {
val ebs = (IExtendedBlockStorageMixin) ebsVanilla;
val b1 = ebs.getB1();
val b2Low = ebs.getB2Low();
val b2High = ebs.getB2High();
val b3 = ebs.getB3();
section.setByteArray("Blocks", b1);
if (b2Low != null) {
section.setByteArray("Add", b2Low.data);
}
if (b2High != null) {
section.setByteArray("BlocksB2Hi", b2High.data);
}
if (b3 != null) {
section.setByteArray("BlocksB3", b3);
}
}
@Override
public void readSectionFromNBT(Chunk chunk, ExtendedBlockStorage ebsVanilla, NBTTagCompound section) {
val ebs = (IExtendedBlockStorageMixin) ebsVanilla;
assert section.hasKey("Blocks");
val b1 = section.getByteArray("Blocks");
final byte[] b2Low = section.hasKey("Add") ? section.getByteArray("Add") : null;
final byte[] b2High = section.hasKey("BlocksB2Hi") ? section.getByteArray("BlocksB2Hi") : null;
final byte[] b3 = section.hasKey("BlocksB3") ? section.getByteArray("BlocksB3") : null;
ebs.setB1(b1);
if (b2Low == null) {
ebs.clearB2Low();
} else {
ebs.setB2Low(new NibbleArray(b2Low, 4));
}
if (b2High == null) {
ebs.clearB2High();
} else {
ebs.setB2High(new NibbleArray(b2High, 4));
}
ebs.setB3(b3);
}
}
@@ -0,0 +1,134 @@
package com.falsepattern.endlessids.managers;
import com.falsepattern.chunk.api.ChunkDataManager;
import com.falsepattern.endlessids.Tags;
import com.falsepattern.endlessids.mixin.helpers.IExtendedBlockStorageMixin;
import lombok.val;
import lombok.var;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.NibbleArray;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import java.nio.ByteBuffer;
public class BlockMetaManager implements ChunkDataManager, ChunkDataManager.PacketDataManager, ChunkDataManager.SectionNBTDataManager {
@Override
public String domain() {
return Tags.MODID;
}
@Override
public String id() {
return "metadata";
}
@Override
public int maxPacketSize() {
return 2 * 16 * 16 * 16 * 16 + 4;
}
@Override
public void writeToBuffer(Chunk chunk, int ebsMask, boolean forceUpdate, ByteBuffer data) {
val ebsList = chunk.getBlockStorageArray();
int storageFlags = 0;
val start = data.position() + 4;
data.position(start);
for (int i = 0; i < 16; i++) {
if ((ebsMask & (1 << i)) == 0 || ebsList[i] == null) {
continue;
}
val ebs = (IExtendedBlockStorageMixin) ebsList[i];
storageFlags |= ebs.getEBSMask() << (i * 2);
val m1Low = ebs.getM1Low();
data.put(m1Low.data);
val m1High = ebs.getM1High();
if (m1High == null) {
continue;
}
data.put(m1High.data);
val m2 = ebs.getM2();
if (m2 != null) {
data.put(m2);
}
}
data.putInt(start - 4, storageFlags);
}
@Override
public void readFromBuffer(Chunk chunk, int ebsMask, boolean forceUpdate, ByteBuffer buffer) {
val ebsList = chunk.getBlockStorageArray();
val storageFlags = buffer.getInt();
for (int i = 0; i < 16; i++) {
if ((ebsMask & (1 << i)) == 0 || ebsList[i] == null) {
continue;
}
val ebs = (IExtendedBlockStorageMixin) ebsList[i];
val storageFlag = (storageFlags >>> (i * 2)) & 3;
val m1Low = ebs.getM1Low();
buffer.get(m1Low.data);
if (storageFlag == 0b01) {
ebs.clearM1High();
ebs.clearM2();
continue;
}
var m1High = ebs.getM1High();
if (m1High == null) {
m1High = ebs.createM1High();
}
buffer.get(m1High.data);
if (storageFlag == 0b10) {
ebs.clearM2();
continue;
}
var m2 = ebs.getM2();
if (m2 == null) {
m2 = ebs.createM2();
}
buffer.get(m2);
}
}
@Override
public boolean sectionPrivilegedAccess() {
return true;
}
@Override
public void writeSectionToNBT(Chunk chunk, ExtendedBlockStorage ebsVanilla, NBTTagCompound section) {
val ebs = (IExtendedBlockStorageMixin) ebsVanilla;
val m1Low = ebs.getM1Low();
val m1High = ebs.getM1High();
val m2 = ebs.getM2();
section.setByteArray("Data", m1Low.data);
if (m1High != null) {
section.setByteArray("Data1High", m1High.data);
}
if (m2 != null) {
section.setByteArray("Data2", m2);
}
}
@Override
public void readSectionFromNBT(Chunk chunk, ExtendedBlockStorage ebsVanilla, NBTTagCompound section) {
val ebs = (IExtendedBlockStorageMixin) ebsVanilla;
assert section.hasKey("Data");
val m1Low = section.getByteArray("Data");
final byte[] m1High = section.hasKey("Data1High") ? section.getByteArray("Data1High") : null;
final byte[] m2 = section.hasKey("Data2") ? section.getByteArray("Data2") : null;
ebs.setM1Low(new NibbleArray(m1Low, 4));
if (m1High == null) {
ebs.clearM1High();
} else {
ebs.setM1High(new NibbleArray(m1High, 4));
}
ebs.setM2(m2);
}
}