Skip to content

Commit

Permalink
Added NBTTagCompound writeTileEntityToTag( NBTTagCompound tag, boolea…
Browse files Browse the repository at this point in the history
…n crossWorld );
  • Loading branch information
AlgorithmX2 committed Dec 26, 2016
1 parent 19e0f3c commit 777279a
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 14 deletions.
25 changes: 25 additions & 0 deletions src/main/java/mod/chiselsandbits/api/IChiseledBlockTileEntity.java
@@ -0,0 +1,25 @@
package mod.chiselsandbits.api;

import net.minecraft.nbt.NBTTagCompound;

/**
* This interface is implemented by Chiseled Block Tile Entities.
*/
public interface IChiseledBlockTileEntity
{

/**
* Used to write Tile Data into cross world format, can be invoked via
* interface or via reflection on the tile itself.
*
* functions identically to writeToNBT(...)
*
* @param tag
* @param crossWorld
* @return modified input tag.
*/
public NBTTagCompound writeTileEntityToTag(
final NBTTagCompound tag,
final boolean crossWorld );

}
Expand Up @@ -431,7 +431,7 @@ public String getItemStackDisplayName(
if ( BlockEntityTag != null )
{
final NBTBlobConverter c = new NBTBlobConverter();
c.readChisleData( BlockEntityTag );
c.readChisleData( BlockEntityTag, VoxelBlob.VERSION_ANY );

final IBlockState state = c.getPrimaryBlockState();
final Block blk = state.getBlock();
Expand Down
Expand Up @@ -131,7 +131,8 @@ public final void writeChisleData(
}

public final boolean readChisleData(
final NBTTagCompound compound )
final NBTTagCompound compound,
final int preferedFormat )
{
if ( compound == null )
{
Expand Down Expand Up @@ -181,8 +182,25 @@ public final boolean readChisleData(
voxelBlobRef = new VoxelBlobStateReference( v, 0 );
format = voxelBlobRef.getFormat();

boolean formatChanged = false;

if ( preferedFormat != format && preferedFormat != VoxelBlob.VERSION_ANY )
{
formatChanged = true;
v = voxelBlobRef.getVoxelBlob().blobToBytes( preferedFormat );
voxelBlobRef = new VoxelBlobStateReference( v, 0 );
format = voxelBlobRef.getFormat();
}

if ( tile != null )
{
if ( formatChanged )
{
// this only works on already loaded tiles, so i'm not sure
// there is much point in it.
tile.markDirty();
}

return tile.updateBlob( this, triggerUpdates );
}

Expand Down
Expand Up @@ -7,6 +7,7 @@

import mod.chiselsandbits.api.EventBlockBitPostModification;
import mod.chiselsandbits.api.EventFullBlockRestoration;
import mod.chiselsandbits.api.IChiseledBlockTileEntity;
import mod.chiselsandbits.api.ItemType;
import mod.chiselsandbits.chiseledblock.data.VoxelBlob;
import mod.chiselsandbits.chiseledblock.data.VoxelBlob.BlobStats;
Expand Down Expand Up @@ -46,7 +47,7 @@
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class TileEntityBlockChiseled extends TileEntity implements IChiseledTileContainer
public class TileEntityBlockChiseled extends TileEntity implements IChiseledTileContainer, IChiseledBlockTileEntity
{

public static class TileEntityBlockChiseledDummy extends TileEntityBlockChiseled
Expand Down Expand Up @@ -361,7 +362,8 @@ private void triggerDynamicUpdates()
public boolean readChisleData(
final NBTTagCompound tag )
{
final boolean changed = new NBTBlobConverter( false, this ).readChisleData( tag );
final NBTBlobConverter converter = new NBTBlobConverter( false, this );
final boolean changed = converter.readChisleData( tag, VoxelBlob.VERSION_COMPACT );

final VoxelNeighborRenderTracker vns = state.getValue( BlockChiseled.UProperty_VoxelNeighborState );

Expand Down Expand Up @@ -396,6 +398,17 @@ public void readFromNBT(
readChisleData( compound );
}

@Override
public NBTTagCompound writeTileEntityToTag(
final NBTTagCompound tag,
final boolean crossWorld )
{
super.writeToNBT( tag );
new NBTBlobConverter( false, this ).writeChisleData( tag, crossWorld );
tag.setBoolean( "cw", crossWorld );
return tag;
}

@Override
public void mirror(
final Mirror p_189668_1_ )
Expand Down
Expand Up @@ -898,9 +898,10 @@ public boolean filter(
return hasValues;
}

public static int VERSION_COMPACT = 0;
public static int VERSION_CROSSWORLD_LEGACY = 1; // stored meta.
public static int VERSION_CROSSWORLD = 2;
public static final int VERSION_ANY = -1;
public static final int VERSION_COMPACT = 0;
public static final int VERSION_CROSSWORLD_LEGACY = 1; // stored meta.
public static final int VERSION_CROSSWORLD = 2;

public void blobFromBytes(
final byte[] bytes ) throws IOException
Expand Down
Expand Up @@ -7,6 +7,7 @@
import mod.chiselsandbits.api.IBitAccess;
import mod.chiselsandbits.api.ItemType;
import mod.chiselsandbits.chiseledblock.NBTBlobConverter;
import mod.chiselsandbits.chiseledblock.data.VoxelBlob;
import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.helpers.ModUtil;
import mod.chiselsandbits.interfaces.ICacheClearable;
Expand Down Expand Up @@ -108,7 +109,7 @@ public void displayAllRelevantItems(
for ( final ItemStack is : myCrossItems )
{
final NBTBlobConverter c = new NBTBlobConverter();
c.readChisleData( ModUtil.getSubCompound( is, ModUtil.NBT_BLOCKENTITYTAG, true ) );
c.readChisleData( ModUtil.getSubCompound( is, ModUtil.NBT_BLOCKENTITYTAG, true ), VoxelBlob.VERSION_ANY );

// recalculate.
c.updateFromBlob();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mod/chiselsandbits/core/ClientSide.java
Expand Up @@ -1240,7 +1240,7 @@ private void showGhost(
lastPartial = partial;

final NBTBlobConverter c = new NBTBlobConverter();
c.readChisleData( ModUtil.getSubCompound( item, ModUtil.NBT_BLOCKENTITYTAG, false ) );
c.readChisleData( ModUtil.getSubCompound( item, ModUtil.NBT_BLOCKENTITYTAG, false ), VoxelBlob.VERSION_ANY );
VoxelBlob blob = c.getBlob();

while ( rotations-- > 0 )
Expand Down
Expand Up @@ -106,7 +106,7 @@ else if ( !ModUtil.isEmpty( f ) )
}

final NBTBlobConverter tmp = new NBTBlobConverter();
tmp.readChisleData( targetA.getTagCompound() );
tmp.readChisleData( targetA.getTagCompound(), VoxelBlob.VERSION_ANY );

final VoxelBlob bestBlob = tmp.getBlob();

Expand Down
Expand Up @@ -73,7 +73,7 @@ else if ( !ModUtil.isEmpty( f ) )
}

final NBTBlobConverter tmp = new NBTBlobConverter();
tmp.readChisleData( targetA.getTagCompound() );
tmp.readChisleData( targetA.getTagCompound(), VoxelBlob.VERSION_ANY );

final VoxelBlob bestBlob = tmp.getBlob();
bestBlob.binaryReplacement( ModUtil.getStateId( Blocks.STONE.getDefaultState() ), 0 );
Expand Down
Expand Up @@ -85,7 +85,7 @@ private ItemStack getSortedVersion(
final @Nonnull ItemStack stack )
{
final NBTBlobConverter tmp = new NBTBlobConverter();
tmp.readChisleData( ModUtil.getSubCompound( stack, ModUtil.NBT_BLOCKENTITYTAG, false ) );
tmp.readChisleData( ModUtil.getSubCompound( stack, ModUtil.NBT_BLOCKENTITYTAG, false ), VoxelBlob.VERSION_ANY );

VoxelBlob bestBlob = tmp.getBlob();
byte[] bestValue = bestBlob.toLegacyByteArray();
Expand Down
Expand Up @@ -149,7 +149,7 @@ public ItemStack getPatternedItem(

// Detect and provide full blocks if pattern solid full and solid.
final NBTBlobConverter conv = new NBTBlobConverter();
conv.readChisleData( tag );
conv.readChisleData( tag, VoxelBlob.VERSION_ANY );

final IBlockState blk = conv.getPrimaryBlockState();
final ItemStack itemstack = new ItemStack( ChiselsAndBits.getBlocks().getConversionWithDefault( blk ), 1 );
Expand Down
Expand Up @@ -236,7 +236,7 @@ public ItemStack getPatternedItem(

// Detect and provide full blocks if pattern solid full and solid.
final NBTBlobConverter conv = new NBTBlobConverter();
conv.readChisleData( tag );
conv.readChisleData( tag, VoxelBlob.VERSION_ANY );

if ( craftingBlocks && ChiselsAndBits.getConfig().fullBlockCrafting )
{
Expand Down

0 comments on commit 777279a

Please sign in to comment.