Skip to content

Commit

Permalink
Can now Capture designs using blueprints in game.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlgorithmX2 committed Dec 8, 2017
1 parent e54da0e commit 2ae9750
Show file tree
Hide file tree
Showing 19 changed files with 530 additions and 71 deletions.
Expand Up @@ -196,7 +196,12 @@ public void run()

public byte[] getStuctureData() throws IOException
{
return data.getStuctureData();
if ( getState() == EnumLoadState.LOADED )
{
return data.getStuctureData();
}

throw new IOException();
}

public int getXSize()
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/mod/chiselsandbits/blueprints/BlueprintPosition.java
@@ -0,0 +1,39 @@
package mod.chiselsandbits.blueprints;

import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;

public class BlueprintPosition
{

public final BlockPos center;
public final BlockPos min;
public final BlockPos max;

public final BlockPos bitOffset;
public final EnumFacing axisX;
public final EnumFacing axisY;
public final EnumFacing axisZ;
public final BlockPos afterOffset;

public BlueprintPosition(
BlockPos center,
BlockPos min,
BlockPos max,
BlockPos bitOffset,
EnumFacing axisX,
EnumFacing axisY,
EnumFacing axisZ,
BlockPos afterOffset )
{
this.center = center;
this.min = min;
this.max = max;
this.bitOffset = bitOffset;
this.axisX = axisX;
this.axisY = axisY;
this.axisZ = axisZ;
this.afterOffset = afterOffset;
}

}
151 changes: 139 additions & 12 deletions src/main/java/mod/chiselsandbits/blueprints/EntityBlueprint.java
@@ -1,5 +1,11 @@
package mod.chiselsandbits.blueprints;

import java.io.File;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -18,11 +24,19 @@
import mod.chiselsandbits.network.packets.PacketCompleteBlueprint;
import mod.chiselsandbits.network.packets.PacketShiftBluePrint;
import mod.chiselsandbits.network.packets.PacketUndo;
import mod.chiselsandbits.network.packets.WriteBlueprintPacket;
import mod.chiselsandbits.share.ShareGenerator;
import mod.chiselsandbits.share.output.ClipBoardText;
import mod.chiselsandbits.share.output.ClipboardImage;
import mod.chiselsandbits.share.output.IShareOutput;
import mod.chiselsandbits.share.output.LocalPNGFile;
import mod.chiselsandbits.share.output.LocalTextFile;
import mod.chiselsandbits.voxelspace.IVoxelSrc;
import mod.chiselsandbits.voxelspace.VoxelCompressedProviderWorld;
import mod.chiselsandbits.voxelspace.VoxelOffsetRegion;
import mod.chiselsandbits.voxelspace.VoxelRegionSrc;
import mod.chiselsandbits.voxelspace.VoxelTransformedRegion;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
Expand Down Expand Up @@ -94,7 +108,7 @@ public boolean hitByEntity(
@Override
public boolean canBeCollidedWith()
{
return false;
return true;
}

@Override
Expand Down Expand Up @@ -212,7 +226,11 @@ else if ( handStack.getItem() == ChiselsAndBits.getItems().itemWrench )

if ( getDataManager().get( BLUEPRINT_PLACING ) == true )
{
player.addChatMessage( new TextComponentTranslation( LocalStrings.BlueprintCannotMove.toString() ) );
if ( player.getEntityWorld().isRemote )
{
player.addChatMessage( new TextComponentTranslation( LocalStrings.BlueprintCannotMove.toString() ) );
}

return EnumActionResult.FAIL;
}

Expand All @@ -239,6 +257,15 @@ else if ( handStack.getItem() == ChiselsAndBits.getItems().itemWrench )
{
direction *= 16;
}
else
{
if ( player.getEntityWorld().isRemote )
{
player.addChatMessage( new TextComponentTranslation( LocalStrings.BlueprintBlockOnly.toString() ) );
}

return EnumActionResult.FAIL;
}

switch ( rtr.sideHit )
{
Expand Down Expand Up @@ -368,11 +395,89 @@ private void beginCapture(
{
if ( player.getEntityWorld().isRemote )
{
getDataManager().set( BLUEPRINT_PLACING, true );
sendUpdate();
player.addChatMessage( new TextComponentTranslation( LocalStrings.BlueprintBeginCapture.toString() ) );

final World clientWorld = Minecraft.getMinecraft().theWorld;

IShareOutput out = null;

try
{
switch ( ChiselsAndBits.getConfig().shareOutput )
{
case IMAGE_CLIPBOARD:
out = new ClipboardImage();
break;
case IMAGE_FILE_WITH_SCREENSHOT:
out = new LocalPNGFile( newFileName( ChiselsAndBits.getConfig().getShareFileOutputFolder(), ".png" ) );
break;
default:
case TEXT_CLIPBOARD:
out = new ClipBoardText();
break;
case TEXT_FILE:
out = new LocalTextFile( newFileName( ChiselsAndBits.getConfig().getShareFileOutputFolder(), ".txt" ) );
break;
}

BlueprintPosition bpos = getBlueprintPos();

final IShareOutput sout = out;
final ShareGenerator sg = new ShareGenerator( clientWorld, bpos.min, bpos.max, out );

sg.start( new Runnable() {

@Override
public void run()
{
try
{
WriteBlueprintPacket wp = new WriteBlueprintPacket();

BlueprintData data = sout.getData();
wp.setFrom( getEntityId(), data );

NetworkRouter.instance.sendToServer( wp );
player.addChatMessage( new TextComponentTranslation( sg.message ) );
}
catch ( IOException ioe )
{
player.addChatMessage( new TextComponentTranslation( LocalStrings.BlueprintFailedToWrite.toString() ) );
}
}

}, null );
}
catch ( NoSuchFileException e )
{
player.addChatMessage( new TextComponentTranslation( LocalStrings.BlueprintNoSuchPath.toString() ) );
}
}
}

private File newFileName(
String shareFileOutputFolder,
String type ) throws NoSuchFileException
{
DateFormat dateFormat = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" );
Calendar cal = Calendar.getInstance();
String extra = "";

int loops = 0;
while ( loops++ < 1000 )
{
File o = new File( shareFileOutputFolder, dateFormat.format( cal ) + extra + type );
if ( !o.exists() )
return o;

extra = "" + loops;
}

throw new NoSuchFileException( shareFileOutputFolder );
}

int completed = 0;
int passNumber = 1;

Expand Down Expand Up @@ -496,11 +601,8 @@ public QueuedChanges(
IVoxelSrc application;
Map<BlockPos, QueuedChanges> calculatedSpace = null;

private void calculateSpace(
final BlueprintData bd )
BlueprintPosition getBlueprintPos()
{
calculatedSpace = new HashMap<BlockPos, QueuedChanges>();

final int bitsPerBlock = 16;
final int bitsPerBlock_Minus1 = bitsPerBlock - 1;

Expand Down Expand Up @@ -531,15 +633,24 @@ private void calculateSpace(
final BlockPos min = center.add( -minX, -minY, -minZ );
final BlockPos max = center.add( maxX, maxY, maxZ );

source = new VoxelRegionSrc( new VoxelCompressedProviderWorld( worldObj ), min, max, min );
return new BlueprintPosition( center, min, max, bitOffset, axisX, axisY, axisZ, afterOffset );
}

private void calculateSpace(
final BlueprintData bd )
{
calculatedSpace = new HashMap<BlockPos, QueuedChanges>();

BlueprintPosition bpos = getBlueprintPos();
source = new VoxelRegionSrc( new VoxelCompressedProviderWorld( worldObj ), bpos.min, bpos.max, bpos.min );

final IVoxelSrc data = new VoxelRegionSrc( bd, BlockPos.ORIGIN, new BlockPos( bd.getXSize(), bd.getYSize(), bd.getZSize() ), BlockPos.ORIGIN );
final IVoxelSrc offset = new VoxelTransformedRegion( data, axisX, axisY, axisZ, afterOffset );
application = new VoxelOffsetRegion( offset, bitOffset );
final IVoxelSrc offset = new VoxelTransformedRegion( data, bpos.axisX, bpos.axisY, bpos.axisZ, bpos.afterOffset );
application = new VoxelOffsetRegion( offset, bpos.bitOffset );

for ( final BlockPos p : BlockPos.getAllInBox( min, max ) )
for ( final BlockPos p : BlockPos.getAllInBox( bpos.min, bpos.max ) )
{
calculatedSpace.put( p.toImmutable(), new QueuedChanges( p.subtract( min ) ) );
calculatedSpace.put( p.toImmutable(), new QueuedChanges( p.subtract( bpos.min ) ) );
}
}

Expand Down Expand Up @@ -571,7 +682,14 @@ private void resize(
final DataParameter<Integer> param,
final int direction )
{
getDataManager().set( param, getDataManager().get( param ) + direction );
int newValue = getDataManager().get( param ) + direction;

if ( newValue < 0 )
{
newValue = 0;
}

getDataManager().set( param, newValue );
}

@Override
Expand Down Expand Up @@ -770,4 +888,13 @@ public void setConfiguration(
getDataManager().set( BLUEPRINT_MAX_Z, p.max_z );
}

public void dropItem(
NBTTagCompound tag )
{
ItemStack stack = getItem();
stack.setTagCompound( tag );
worldObj.spawnEntityInWorld( new EntityItem( worldObj, posX, posY, posZ, stack ) );
setDead();
}

}
53 changes: 28 additions & 25 deletions src/main/java/mod/chiselsandbits/blueprints/ItemBlueprint.java
Expand Up @@ -85,32 +85,32 @@ public EnumActionResult onItemUse(
{
ItemStack stack = playerIn.getHeldItem( hand );

if ( isWritten( stack ) )
if ( !worldIn.isRemote )
{
if ( !worldIn.isRemote )
final IBlockState state = worldIn.getBlockState( pos );
if ( !state.getBlock().isReplaceable( worldIn, pos ) )
{
final IBlockState state = worldIn.getBlockState( pos );
if ( !state.getBlock().isReplaceable( worldIn, pos ) )
{
pos = pos.offset( facing );
}
pos = pos.offset( facing );
}

final EntityBlueprint e = new EntityBlueprint( worldIn );
e.posX = pos.getX() + 0.5;
e.posY = pos.getY() + 0.5;
e.posZ = pos.getZ() + 0.5;
e.setItemStack( stack.copy() );
final EntityBlueprint e = new EntityBlueprint( worldIn );
e.setPosition( pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5 );
e.setItemStack( stack.copy() );

if ( stack.hasTagCompound() )
{
final NBTTagCompound tag = stack.getTagCompound();
e.setSize( tag.getInteger( NBT_SIZE_X ), tag.getInteger( NBT_SIZE_Y ), tag.getInteger( NBT_SIZE_Z ) );
if ( !worldIn.spawnEntityInWorld( e ) )
return EnumActionResult.FAIL;
}
else
e.setSize( 1, 1, 1 );

ModUtil.adjustStackSize( stack, -1 );
return EnumActionResult.SUCCESS;
if ( !worldIn.spawnEntityInWorld( e ) )
return EnumActionResult.FAIL;
}

return EnumActionResult.FAIL;
ModUtil.adjustStackSize( stack, -1 );
return EnumActionResult.SUCCESS;
}

public boolean isWritten(
Expand Down Expand Up @@ -235,16 +235,19 @@ synchronized protected BlueprintData getItemData(
protected BlueprintData getStackData(
final ItemStack data )
{
final NBTTagCompound tagCompound = data.getTagCompound();

if ( tagCompound.hasKey( "data" ) )
if ( data.hasTagCompound() )
{
return getItemData( tagCompound.getByteArray( "data" ), data );
}
final NBTTagCompound tagCompound = data.getTagCompound();

if ( tagCompound.hasKey( "url" ) )
{
return getURLData( tagCompound.getString( "url" ) );
if ( tagCompound.hasKey( "data" ) )
{
return getItemData( tagCompound.getByteArray( "data" ), data );
}

if ( tagCompound.hasKey( "url" ) )
{
return getURLData( tagCompound.getString( "url" ) );
}
}

return null;
Expand Down
Expand Up @@ -145,7 +145,7 @@ public void doRender(
if ( entity.renderData == null )
{
final BlueprintData data = ChiselsAndBits.getItems().itemBlueprint.getStackData( entity.getItemStack() );
if ( data.getState() == EnumLoadState.LOADED )
if ( data != null && data.getState() == EnumLoadState.LOADED )
{
entity.renderData = new BlueprintRenderData( data );
}
Expand Down

0 comments on commit 2ae9750

Please sign in to comment.