Skip to content

Commit

Permalink
Migrate from sharing_1.10 to 1.12 and continue project.
Browse files Browse the repository at this point in the history
This is a fresh commit rather then a merge because it is clearer to see how the changes are reflected against the modern code base.

Blueprints are survival usable at this point, though you still have to use a command to dump them out, they aren't super efficient either. I know there are missing localization and art, and a lot of testing needs to get done.
  • Loading branch information
AlgorithmX2 committed Dec 3, 2017
1 parent 68ce8ae commit 64fa3fa
Show file tree
Hide file tree
Showing 92 changed files with 5,655 additions and 220 deletions.
19 changes: 17 additions & 2 deletions src/main/java/mod/chiselsandbits/api/IChiselAndBitsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
Expand Down Expand Up @@ -71,8 +72,22 @@ IBitAccess getBitAccess(
BlockPos pos ) throws CannotBeChiseled;

/**
* Create a bit access from an ItemStack, passing an empty ItemStack creates
* an empty bit access, passing an invalid item returns null.
* Read-Only access to bits for a given block.
*
* @param access
* @param pos
* @return A {@link IBitAccess} for the specified location.
* @throws CannotBeChiseled
* when the location cannot support bits, or if the parameters
* are invalid.
*/
IBitAccess getBitAccess(
IBlockAccess access,
BlockPos pos ) throws CannotBeChiseled;

/**
* Create a bit access from an item, passing null creates an empty item,
* passing an invalid item returns null.
*
* @return a {@link IBitAccess} for an ItemStack.
*/
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/mod/chiselsandbits/bitbag/BagInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,20 @@ public boolean func_191420_l()

return true;
}

public int countItems(
int blk )
{
int count = 0;

for ( int x = 0; x < stackSlots.length; ++x )
{
if ( inv.contents[x * ItemBitBag.INTS_PER_BIT_TYPE + ItemBitBag.OFFSET_STATE_ID] == blk )
{
count += inv.contents[x * ItemBitBag.INTS_PER_BIT_TYPE + ItemBitBag.OFFSET_QUANTITY] = 0;
}
}

return count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package mod.chiselsandbits.blueprints;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class BlueprintContainer extends Container
{
final EntityPlayer thePlayer;
protected int bluePrintSlot;

public BlueprintContainer(
final EntityPlayer player,
final World world,
final int x,
final int y,
final int z )
{
thePlayer = player;
bluePrintSlot = player.inventory.currentItem;
}

@Override
public boolean canInteractWith(
final EntityPlayer playerIn )
{
return playerIn == thePlayer && hasBlueprintInHand( thePlayer );
}

private boolean hasBlueprintInHand(
final EntityPlayer player )
{
final ItemStack inHand = player.getHeldItem( EnumHand.MAIN_HAND );

if ( inHand != null && inHand.getItem() instanceof ItemBlueprint )
{
return true;
}

return false;
}

@SideOnly( Side.CLIENT )
public static Object getGuiClass()
{
return BlueprintGui.class;
}

}
243 changes: 243 additions & 0 deletions src/main/java/mod/chiselsandbits/blueprints/BlueprintData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
package mod.chiselsandbits.blueprints;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.imageio.ImageIO;

import mod.chiselsandbits.chiseledblock.TileEntityBlockChiseled;
import mod.chiselsandbits.chiseledblock.data.IVoxelAccess;
import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.core.ClientSide;
import mod.chiselsandbits.core.Log;
import mod.chiselsandbits.share.ShareWorldData;
import mod.chiselsandbits.voxelspace.IVoxelProvider;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;

public class BlueprintData implements Runnable, IVoxelProvider
{
public static enum EnumLoadState
{
LOADING,
LOADED,
FAILED;

public boolean readyOrWaiting()
{
return this != FAILED;
}
};

private long lastNeeded = getCurrentTime();
private EnumLoadState state = EnumLoadState.LOADING;
private URL url = null;

private ShareWorldData data;

public BlueprintData(
final String url )
{
try
{
if ( url != null )
{
this.url = new URL( url );
final Thread t = new Thread( this );
t.setName( "Blueprint-" + url );
t.start();
}
}
catch ( final MalformedURLException e )
{
state = EnumLoadState.FAILED;
Log.logError( "Blueprint URL is invalid.", e );
}
}

private long getCurrentTime()
{
return System.currentTimeMillis();
}

synchronized public void updateTime()
{
lastNeeded = getCurrentTime();
}

synchronized public boolean isExpired()
{
return getCurrentTime() - lastNeeded > ChiselsAndBits.getConfig().blueprintExpireTime;
}

synchronized public EnumLoadState getState()
{
updateTime();
return state;
}

public void setLocalSource(
final String string ) throws MalformedURLException, UnsupportedEncodingException
{
url = new URL( "file", ClientSide.instance.getLocalName(), 0, "/" + URLEncoder.encode( string, "UTF-8" ) );
}

public void setURLSource(
final URL url2 )
{
url = url2;
}

public void loadData(
InputStream is ) throws IOException
{
is = new BufferedInputStream( is );
EnumLoadState result = EnumLoadState.FAILED;

try
{
final byte[] peek = new byte[4];
is.mark( peek.length );
is.read( peek );
is.reset();

// load png? or is it text?
if ( peek[0] == 0x89 && peek[1] == 0x50 && peek[2] == 0x4E && peek[3] == 0x47 )
{
data = new ShareWorldData( ImageIO.read( is ) );
}
else
{
final StringBuilder builder = new StringBuilder();
final byte[] buffer = new byte[2048];
int read = 0;

do
{
read = is.read( buffer );

// C&B data should be visible as ascii if utf8 or various
// iso
// formats, probably the best approach to prevent utf-8 from
// being confused from other charsets
if ( read > 0 )
{
builder.append( new String( buffer, 0, read, "ASCII" ) );
}
}
while ( read > 0 );

data = new ShareWorldData( builder.toString() );
}

result = EnumLoadState.LOADED;
}
finally
{
state = result;
}
}

public void loadData(
final byte[] bs ) throws IOException
{
EnumLoadState result = EnumLoadState.FAILED;
try
{
data = new ShareWorldData( bs );
result = EnumLoadState.LOADED;
}
finally
{
state = result;
}
}

@Override
public void run()
{
EnumLoadState result = EnumLoadState.FAILED;
try
{
if ( url.getProtocol().equals( "file" ) )
{
if ( url.getHost().equals( ClientSide.instance.getLocalName() ) )
{
loadData( new FileInputStream( new File( URLDecoder.decode( url.getFile().substring( 1 ), "UTF-8" ) ) ) );
result = EnumLoadState.LOADED;
return;
}

return;
}

final URLConnection src = url.openConnection();
loadData( src.getInputStream() );
result = EnumLoadState.LOADED;
}
catch ( final IOException e )
{
Log.logError( "Unload to download Blueprint.", e );
}
finally
{
state = result;
}
}

public byte[] getStuctureData() throws IOException
{
return data.getStuctureData();
}

public int getXSize()
{
return data.getXSize();
}

public int getYSize()
{
return data.getYSize();
}

public int getZSize()
{
return data.getZSize();
}

public String getURL()
{
return url == null ? "" : url.toString();
}

@Override
public IVoxelAccess get(
final int x,
final int y,
final int z )
{
return data.getBlob( x, y, z );
}

public IBlockState getStateAt(
final BlockPos p )
{
return data.getStateAt( p );
}

public TileEntityBlockChiseled getTileAt(
final BlockPos p )
{
return data.getTileAt( p );
}

}

0 comments on commit 64fa3fa

Please sign in to comment.