Skip to content

Commit

Permalink
Mixins for block palettes in Cubic Chunks save nbt
Browse files Browse the repository at this point in the history
  • Loading branch information
Exsolutus committed Sep 6, 2022
1 parent 20f36af commit 909a1b6
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ classes
*.iml
.idea

# vscode
.vscode

# gradle
build
.gradle
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ dependencies {
compileOnly fg.deobf( "curse.maven:baubles-227083:2518667" )
compileOnly fg.deobf( "curse.maven:Patchouli-306770:3162874" )
compileOnly fg.deobf( "curse.maven:bewitchment-285439:3256343" )

compileOnly fg.deobf( "curse.maven:CubicChunks-292243:3546640" )
}

// Example for how to get properties into the manifest for reading by the runtime..
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/dimdev/jeid/JEID.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
name = "JustEnoughIDs",
updateJSON = "https://gist.githubusercontent.com/Runemoro/67b1d8d31af58e9d35410ef60b2017c3/raw/1fe08a6c45a1f481a8a2a8c71e52d4245dcb7713/jeid_update.json")
public class JEID {
private static final boolean DEBUG_BLOCK_IDS = false;
private static final boolean DEBUG_BLOCK_IDS = true;
private static final boolean DEBUG_ITEM_IDS = false;
private static final boolean DEBUG_BIOME_IDS = false;
private static final boolean DEBUG_POTION_IDS = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.dimdev.jeid.mixin.modsupport.cubicchunks;

import io.github.opencubicchunks.cubicchunks.api.worldgen.CubePrimer;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.Shadow;

import javax.annotation.Nonnull;

@Pseudo
@Mixin(CubePrimer.class)
@SuppressWarnings("deprecation")
public class MixinCubePrimer {
@Shadow private static int getBlockIndex(int x, int y, int z) { return 0; }
@Shadow @Final private static IBlockState DEFAULT_STATE;
private int[] intData = new int[65536];

/**
* Get the block state at the given location
*
* @param x cube local x
* @param y cube local y
* @param z cube local z
* @return the block state
*/
@Overwrite(remap = false)
public IBlockState getBlockState(int x, int y, int z) {
IBlockState state = Block.BLOCK_STATE_IDS.getByValue(intData[getBlockIndex(x, y, z)]);
return state == null ? DEFAULT_STATE : state;
}

/**
* Set the block state at the given location
*
* @param x cube local x
* @param y cube local y
* @param z cube local z
* @param state the block state
*/
@Overwrite(remap = false)
public void setBlockState(int x, int y, int z, @Nonnull IBlockState state) {
intData[getBlockIndex(x, y, z)] = Block.BLOCK_STATE_IDS.get(state);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.dimdev.jeid.mixin.modsupport.cubicchunks;

import io.github.opencubicchunks.cubicchunks.api.util.Coords;
import io.github.opencubicchunks.cubicchunks.core.server.chunkio.IONbtReader;
import io.github.opencubicchunks.cubicchunks.core.world.cube.Cube;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.chunk.NibbleArray;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import net.minecraft.world.World;
import org.dimdev.jeid.INewBlockStateContainer;
import org.dimdev.jeid.Utils;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Pseudo;

@Pseudo
@Mixin(IONbtReader.class)
public class MixinIONbtReader {

@Overwrite
private static void readBlocks(NBTTagCompound nbt, World world, Cube cube) {
boolean isEmpty = !nbt.hasKey("Sections");// is this an empty cube?
if (!isEmpty) {
NBTTagList sectionList = nbt.getTagList("Sections", 10);
nbt = sectionList.getCompoundTagAt(0);

ExtendedBlockStorage ebs = new ExtendedBlockStorage(Coords.cubeToMinBlock(cube.getY()), cube.getWorld().provider.hasSkyLight());

int[] palette = nbt.hasKey("Palette", 11) ? nbt.getIntArray("Palette") : null;
//Utils.LOGGER.info("cube at {}, {} palette size {}", cube.getCoords().getX(), cube.getCoords().getZ(), palette.length);
((INewBlockStateContainer) ebs.getData()).setTemporaryPalette(palette);

byte[] abyte = nbt.getByteArray("Blocks");
NibbleArray data = new NibbleArray(nbt.getByteArray("Data"));
NibbleArray add = nbt.hasKey("Add", 7) ? new NibbleArray(nbt.getByteArray("Add")) : null;

ebs.getData().setDataFromNBT(abyte, data, add);

ebs.setBlockLight(new NibbleArray(nbt.getByteArray("BlockLight")));

if (world.provider.hasSkyLight()) {
ebs.setSkyLight(new NibbleArray(nbt.getByteArray("SkyLight")));
}

ebs.recalculateRefCounts();
cube.setStorage(ebs);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.dimdev.jeid.mixin.modsupport.cubicchunks;

//import io.github.opencubicchunks.cubicchunks.core.server.chunkio.IONbtWriter;
import io.github.opencubicchunks.cubicchunks.core.world.cube.Cube;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.NibbleArray;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import org.dimdev.jeid.INewBlockStateContainer;
import org.dimdev.jeid.INewChunk;
import org.dimdev.jeid.Utils;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Pseudo;

@Pseudo
@Mixin(targets = "io.github.opencubicchunks.cubicchunks.core.server.chunkio.IONbtWriter")
public class MixinIONbtWriter {

// @Overwrite
// private static void writeBiomes(Chunk column, NBTTagCompound nbt) {// biomes
// INewChunk newChunk = (INewChunk) column;
// nbt.setIntArray("Biomes", newChunk.getIntBiomeArray());
// }

// @Overwrite
// private static void writeBiomes(Cube cube, NBTTagCompound nbt) {// biomes
// byte[] biomes = cube.getBiomeArray();
// if (biomes != null)
// nbt.setByteArray("Biomes", biomes);
// }

@Overwrite
private static void writeBlocks(Cube cube, NBTTagCompound cubeNbt) {
ExtendedBlockStorage ebs = cube.getStorage();
if (ebs == null) {
return; // no data to save anyway
}
NBTTagList sectionList = new NBTTagList();
NBTTagCompound section = new NBTTagCompound();
sectionList.appendTag(section);
cubeNbt.setTag("Sections", sectionList);
byte[] abyte = new byte[Cube.SIZE * Cube.SIZE * Cube.SIZE];
NibbleArray data = new NibbleArray();
NibbleArray add = ebs.getData().getDataForNBT(abyte, data);

int[] palette = ((INewBlockStateContainer) ebs.getData()).getTemporaryPalette();
//Utils.LOGGER.info("cube at {}, {} palette size {}", cube.getCoords().getX(), cube.getCoords().getZ(), palette.length);
if (palette != null) section.setIntArray("Palette", palette);

section.setByteArray("Blocks", abyte);
section.setByteArray("Data", data.getData());

if (add != null) {
section.setByteArray("Add", add.getData());
}

section.setByteArray("BlockLight", ebs.getBlockLight().getData());

if (cube.getWorld().provider.hasSkyLight()) {
section.setByteArray("SkyLight", ebs.getSkyLight().getData());
}
}

}
3 changes: 3 additions & 0 deletions src/main/resources/mixins.jeid.modsupport.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"biometweaker.MixinCommandSetBiome",
"bookshelf.MixinWorldUtils",
"creepingnether.MixinCorruptorAbstract",
"cubicchunks.MixinIONbtReader",
"cubicchunks.MixinIONbtWriter",
"cubicchunks.MixinCubePrimer",
"cyclopscore.MixinWorldHelpers",
"extrautils2.MixinBiomeManip",
"extrautils2.MixinWorldProviderSpecialDim",
Expand Down

0 comments on commit 909a1b6

Please sign in to comment.