Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert chunks from NotEnoughIDs format to JEID format #15

Merged
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
3 changes: 3 additions & 0 deletions src/main/java/org/dimdev/jeid/INewBlockStateContainer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.dimdev.jeid;

import net.minecraft.world.chunk.NibbleArray;

public interface INewBlockStateContainer {
void setTemporaryPalette(int[] temporaryPalette);
int[] getTemporaryPalette();
void setLegacyAdd2(NibbleArray add2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ private void readPaletteNBT(World world, NBTTagCompound nbt, CallbackInfoReturna
NBTTagCompound storageNBT, int y, ExtendedBlockStorage extendedBlockStorage) {
int[] palette = storageNBT.hasKey("Palette", 11) ? storageNBT.getIntArray("Palette") : null;
((INewBlockStateContainer) extendedBlockStorage.getData()).setTemporaryPalette(palette);
NibbleArray add2 = storageNBT.hasKey("Add2", 7) ? new NibbleArray(storageNBT.getByteArray("Add2")) : null;
((INewBlockStateContainer) extendedBlockStorage.getData()).setLegacyAdd2(add2);
}

/** @reason Write palette to NBT for JustEnoughIDs BlockStateContainers. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.BitArray;
import net.minecraft.world.chunk.BlockStateContainer;
import net.minecraft.world.chunk.IBlockStatePalette;
Expand All @@ -26,6 +27,7 @@ public abstract class MixinBlockStateContainer implements INewBlockStateContaine
@Shadow @SuppressWarnings("unused") protected abstract void setBits(int bitsIn);

private int[] temporaryPalette; // index -> state id
private NibbleArray add2; // NEID format

@Override
public int[] getTemporaryPalette() {
Expand All @@ -37,6 +39,11 @@ public void setTemporaryPalette(int[] temporaryPalette) {
this.temporaryPalette = temporaryPalette;
}

@Override
public void setLegacyAdd2(NibbleArray add2) {
this.add2 = add2;
}

/**
* @reason If this BlockStateContainer should be saved in JustEnoughIDs format,
* store palette IDs rather than block IDs in the container's "Blocks" and
Expand Down Expand Up @@ -79,18 +86,31 @@ private void newGetDataForNBT(byte[] blockIds, NibbleArray data, CallbackInfoRet
@SuppressWarnings("deprecation")
@Inject(method = "setDataFromNBT", at = @At("HEAD"), cancellable = true)
private void newSetDataFromNBT(byte[] blockIds, NibbleArray data, NibbleArray blockIdExtension, CallbackInfo ci) {
if (temporaryPalette == null) return; // Read containers in in pallette format only if the container has a palette (has a palette)
if (temporaryPalette == null) { // Read containers in in pallette format only if the container has a palette (has a palette)
for (int index = 0; index < 4096; ++index) {
int x = index & 15;
int y = index >> 8 & 15;
int z = index >> 4 & 15;
int toAdd = (blockIdExtension == null) ? 0 : blockIdExtension.get(x, y, z);
if (add2 != null) {
toAdd = ((toAdd & 0xF) | add2.get(x, y, z) << 4);
}
final int id = toAdd << 12 | (blockIds[index] & 0xFF) << 4 | (data.get(x, y, z) & 0xF);
IBlockState bs = (id == 0) ? Blocks.AIR.getDefaultState() : Block.BLOCK_STATE_IDS.getByValue(id);
set(index, bs);
}
} else {
for (int index = 0; index < 4096; ++index) {
int x = index & 15;
int y = index >> 8 & 15;
int z = index >> 4 & 15;
int paletteID = (blockIds[index] & 255) << 4 | data.get(x, y, z);

for (int index = 0; index < 4096; ++index) {
int x = index & 15;
int y = index >> 8 & 15;
int z = index >> 4 & 15;
int paletteID = (blockIds[index] & 255) << 4 | data.get(x, y, z);
set(index, Block.BLOCK_STATE_IDS.getByValue(temporaryPalette[paletteID]));
}

set(index, Block.BLOCK_STATE_IDS.getByValue(temporaryPalette[paletteID]));
temporaryPalette = null;
}

temporaryPalette = null;
ci.cancel();
}
}