From 6a8fafa58dca20e64c1d486c6e79fb1eaf737fa7 Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Tue, 13 May 2014 21:42:14 -0500 Subject: [PATCH] Working Towards Crafing CPU MultiBlock --- block/crafting/BlockCraftingUnit.java | 24 ++++- block/crafting/BlockMolecularAssembler.java | 4 +- .../render/blocks/RenderBlockAssembler.java | 34 +++++++ client/render/blocks/RenderBlockCrafting.java | 48 ++++++++++ .../CraftingCPUCalculator.java | 26 +++++- .../implementations/CraftingCPUCluster.java | 30 +++++- tile/crafting/TileCraftingTile.java | 91 +++++++++++++++++-- tile/crafting/TileMolecularAssembler.java | 65 ++++++++++++- 8 files changed, 305 insertions(+), 17 deletions(-) create mode 100644 client/render/blocks/RenderBlockAssembler.java create mode 100644 client/render/blocks/RenderBlockCrafting.java diff --git a/block/crafting/BlockCraftingUnit.java b/block/crafting/BlockCraftingUnit.java index 3d0084e5..eda5b12a 100644 --- a/block/crafting/BlockCraftingUnit.java +++ b/block/crafting/BlockCraftingUnit.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.EnumSet; import java.util.List; +import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.Item; @@ -12,6 +13,8 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.MovingObjectPosition; import net.minecraft.world.World; import appeng.block.AEBaseBlock; +import appeng.client.render.BaseBlockRender; +import appeng.client.render.blocks.RenderBlockCrafting; import appeng.core.features.AEFeature; import appeng.tile.crafting.TileCraftingTile; @@ -23,6 +26,11 @@ public class BlockCraftingUnit extends AEBaseBlock public final static int BASE_STORAGE = 2; public final static int BASE_ACCELERATOR = 3; + static public boolean checkType(int meta, int type) + { + return (meta & 7) == type; + } + public BlockCraftingUnit() { super( BlockCraftingUnit.class, Material.iron ); hasSubtypes = true; @@ -30,11 +38,25 @@ public class BlockCraftingUnit extends AEBaseBlock setTileEntiy( TileCraftingTile.class ); } + @Override + protected Class getRenderer() + { + return RenderBlockCrafting.class; + } + + @Override + public void onNeighborBlockChange(World w, int x, int y, int z, Block junk) + { + TileCraftingTile cp = getTileEntity( w, x, y, z ); + if ( cp != null ) + cp.updateMultiBlock(); + } + public ItemStack getItemStack(World world, int x, int y, int z) { TileCraftingTile ct = getTileEntity( world, x, y, z ); - int meta = world.getBlockMetadata( x, y, z ); + int meta = world.getBlockMetadata( x, y, z ) & 7; if ( ct != null && meta == BASE_STORAGE ) { return createStackForBytes( ct.getStorageBytes() ); diff --git a/block/crafting/BlockMolecularAssembler.java b/block/crafting/BlockMolecularAssembler.java index a2c8e9f9..fb6ca5a5 100644 --- a/block/crafting/BlockMolecularAssembler.java +++ b/block/crafting/BlockMolecularAssembler.java @@ -9,7 +9,7 @@ import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; import appeng.block.AEBaseBlock; import appeng.client.render.BaseBlockRender; -import appeng.client.render.blocks.RenderQuartzGlass; +import appeng.client.render.blocks.RenderBlockAssembler; import appeng.core.features.AEFeature; import appeng.core.sync.GuiBridge; import appeng.tile.crafting.TileMolecularAssembler; @@ -32,7 +32,7 @@ public class BlockMolecularAssembler extends AEBaseBlock @SideOnly(Side.CLIENT) public Class getRenderer() { - return RenderQuartzGlass.class; + return RenderBlockAssembler.class; } @Override diff --git a/client/render/blocks/RenderBlockAssembler.java b/client/render/blocks/RenderBlockAssembler.java new file mode 100644 index 00000000..b7c053de --- /dev/null +++ b/client/render/blocks/RenderBlockAssembler.java @@ -0,0 +1,34 @@ +package appeng.client.render.blocks; + +import net.minecraft.client.renderer.RenderBlocks; +import net.minecraft.item.ItemStack; +import net.minecraft.world.IBlockAccess; +import net.minecraftforge.client.IItemRenderer.ItemRenderType; +import appeng.block.AEBaseBlock; +import appeng.client.render.BaseBlockRender; + +public class RenderBlockAssembler extends BaseBlockRender +{ + + public RenderBlockAssembler() { + super( false, 20 ); + } + + @Override + public void renderInventory(AEBaseBlock blk, ItemStack is, RenderBlocks renderer, ItemRenderType type, Object[] obj) + { + renderer.setOverrideBlockTexture( blk.getIcon( 0, 0 ) ); + super.renderInventory( blk, is, renderer, type, obj ); + renderer.setOverrideBlockTexture( null ); + } + + @Override + public boolean renderInWorld(AEBaseBlock blk, IBlockAccess world, int x, int y, int z, RenderBlocks renderer) + { + renderer.overrideBlockTexture = blk.getIcon( 0, 0 ); + boolean out = renderer.renderStandardBlock( blk, x, y, z ); + renderer.overrideBlockTexture = null; + + return out; + } +} diff --git a/client/render/blocks/RenderBlockCrafting.java b/client/render/blocks/RenderBlockCrafting.java new file mode 100644 index 00000000..e3b367d6 --- /dev/null +++ b/client/render/blocks/RenderBlockCrafting.java @@ -0,0 +1,48 @@ +package appeng.client.render.blocks; + +import net.minecraft.client.renderer.RenderBlocks; +import net.minecraft.item.ItemStack; +import net.minecraft.world.IBlockAccess; +import net.minecraftforge.client.IItemRenderer.ItemRenderType; +import appeng.block.AEBaseBlock; +import appeng.client.render.BaseBlockRender; +import appeng.client.texture.ExtraTextures; +import appeng.tile.crafting.TileCraftingTile; + +public class RenderBlockCrafting extends BaseBlockRender +{ + + public RenderBlockCrafting() { + super( false, 20 ); + } + + @Override + public void renderInventory(AEBaseBlock blk, ItemStack is, RenderBlocks renderer, ItemRenderType type, Object[] obj) + { + renderer.setOverrideBlockTexture( blk.getIcon( 0, 0 ) ); + super.renderInventory( blk, is, renderer, type, obj ); + renderer.setOverrideBlockTexture( null ); + } + + @Override + public boolean renderInWorld(AEBaseBlock blk, IBlockAccess world, int x, int y, int z, RenderBlocks renderer) + { + TileCraftingTile ct = blk.getTileEntity( world, x, y, z ); + if ( ct != null && ct.isFormed() ) + { + renderer.overrideBlockTexture = ExtraTextures.BlockControllerConflict.getIcon(); + boolean out = renderer.renderStandardBlock( blk, x, y, z ); + renderer.overrideBlockTexture = null; + + return out; + } + else + { + renderer.overrideBlockTexture = blk.getIcon( 0, 0 ); + boolean out = renderer.renderStandardBlock( blk, x, y, z ); + renderer.overrideBlockTexture = null; + + return out; + } + } +} diff --git a/me/cluster/implementations/CraftingCPUCalculator.java b/me/cluster/implementations/CraftingCPUCalculator.java index c4367234..bcdaea94 100644 --- a/me/cluster/implementations/CraftingCPUCalculator.java +++ b/me/cluster/implementations/CraftingCPUCalculator.java @@ -7,7 +7,6 @@ import appeng.me.cluster.IAECluster; import appeng.me.cluster.IAEMultiBlock; import appeng.me.cluster.MBCalculator; import appeng.tile.crafting.TileCraftingTile; -import appeng.tile.qnb.TileQuantumBridge; public class CraftingCPUCalculator extends MBCalculator { @@ -22,19 +21,41 @@ public class CraftingCPUCalculator extends MBCalculator @Override public boolean isValidTile(TileEntity te) { - return te instanceof TileQuantumBridge; + return te instanceof TileCraftingTile; } @Override public boolean checkMultiblockScale(WorldCoord min, WorldCoord max) { + if ( max.x - min.x > 8 ) + return false; + + if ( max.y - min.y > 8 ) + return false; + + if ( max.z - min.z > 8 ) + return false; + return true; } @Override public void updateTiles(IAECluster cl, World w, WorldCoord min, WorldCoord max) { + CraftingCPUCluster c = (CraftingCPUCluster) cl; + for (int x = min.x; x <= max.x; x++) + { + for (int y = min.y; y <= max.y; y++) + { + for (int z = min.z; z <= max.z; z++) + { + TileCraftingTile te = (TileCraftingTile) w.getTileEntity( x, y, z ); + te.updateStatus( c ); + c.addTile( te ); + } + } + } } @Override @@ -52,7 +73,6 @@ public class CraftingCPUCalculator extends MBCalculator @Override public boolean verifyInternalStructure(World w, WorldCoord min, WorldCoord max) { - for (int x = min.x; x <= max.x; x++) { for (int y = min.y; y <= max.y; y++) diff --git a/me/cluster/implementations/CraftingCPUCluster.java b/me/cluster/implementations/CraftingCPUCluster.java index 2fc0200e..9e83ae15 100644 --- a/me/cluster/implementations/CraftingCPUCluster.java +++ b/me/cluster/implementations/CraftingCPUCluster.java @@ -6,6 +6,7 @@ import java.util.LinkedList; import appeng.api.networking.IGridHost; import appeng.api.util.WorldCoord; import appeng.me.cluster.IAECluster; +import appeng.tile.crafting.TileCraftingTile; public class CraftingCPUCluster implements IAECluster { @@ -14,12 +15,16 @@ public class CraftingCPUCluster implements IAECluster public WorldCoord max; public boolean isDestroyed = false; - private LinkedList tiles = new LinkedList(); + private LinkedList tiles = new LinkedList(); + + int accelerator = 0; + private LinkedList storage = new LinkedList(); + private LinkedList status = new LinkedList(); @Override public Iterator getTiles() { - return tiles.iterator(); + return (Iterator) tiles.iterator(); } public CraftingCPUCluster(WorldCoord _min, WorldCoord _max) { @@ -30,7 +35,10 @@ public class CraftingCPUCluster implements IAECluster @Override public void updateStatus(boolean updateGrid) { - + for (TileCraftingTile r : tiles) + { + r.updateMeta(); + } } @Override @@ -40,6 +48,22 @@ public class CraftingCPUCluster implements IAECluster return; isDestroyed = true; + for (TileCraftingTile r : tiles) + { + r.updateStatus( null ); + } + } + + public void addTile(TileCraftingTile te) + { + tiles.add( te ); + + if ( te.isStorage() ) + storage.add( te ); + else if ( te.isStatus() ) + status.add( te ); + else if ( te.isAccelerator() ) + accelerator++; } } diff --git a/tile/crafting/TileCraftingTile.java b/tile/crafting/TileCraftingTile.java index 2a861ceb..72a18aae 100644 --- a/tile/crafting/TileCraftingTile.java +++ b/tile/crafting/TileCraftingTile.java @@ -1,21 +1,51 @@ package appeng.tile.crafting; +import java.util.EnumSet; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.common.util.ForgeDirection; import appeng.api.AEApi; +import appeng.api.networking.GridFlags; +import appeng.block.crafting.BlockCraftingUnit; import appeng.me.cluster.IAECluster; import appeng.me.cluster.IAEMultiBlock; -import appeng.tile.AEBaseTile; +import appeng.me.cluster.implementations.CraftingCPUCalculator; +import appeng.me.cluster.implementations.CraftingCPUCluster; +import appeng.me.helpers.AENetworkProxy; +import appeng.me.helpers.AENetworkProxyMultiblock; import appeng.tile.events.AETileEventHandler; import appeng.tile.events.TileEventType; +import appeng.tile.grid.AENetworkTile; -public class TileCraftingTile extends AEBaseTile implements IAEMultiBlock +public class TileCraftingTile extends AENetworkTile implements IAEMultiBlock { private long storageBytes = 0; + CraftingCPUCluster clust; + final CraftingCPUCalculator calc = new CraftingCPUCalculator( this ); + + @Override + protected AENetworkProxy createProxy() + { + return new AENetworkProxyMultiblock( this, "proxy", getItemFromTile( this ), true ); + } + + public void updateStatus(CraftingCPUCluster c) + { + clust = c; + updateMeta(); + } + + public void updateMultiBlock() + { + calc.calculateMultiblock( worldObj, getLocation() ); + } public TileCraftingTile() { + gridProxy.setFlags( GridFlags.MULTIBLOCK, GridFlags.REQUIRE_CHANNEL ); + gridProxy.setValidSides( EnumSet.noneOf( ForgeDirection.class ) ); addNewHandler( new AETileEventHandler( TileEventType.NETWORK, TileEventType.WORLD_NBT ) { public void writeToNBT(NBTTagCompound data) @@ -43,6 +73,13 @@ public class TileCraftingTile extends AEBaseTile implements IAEMultiBlock } ); } + @Override + public void onReady() + { + super.onReady(); + updateMultiBlock(); + } + @Override public void onPlacement(ItemStack stack, EntityPlayer player, int side) { @@ -55,6 +92,30 @@ public class TileCraftingTile extends AEBaseTile implements IAEMultiBlock @Override public void disconnect() + { + if ( clust != null ) + { + clust.destroy(); + updateMeta(); + } + } + + public void updateMeta() + { + boolean formed = clust != null; + int current = worldObj.getBlockMetadata( xCoord, yCoord, zCoord ); + int newmeta = (current & 7) | (formed ? 8 : 0); + + if ( current != newmeta ) + worldObj.setBlockMetadataWithNotify( xCoord, yCoord, zCoord, newmeta, 3 ); + + if ( isFormed() ) + gridProxy.setValidSides( EnumSet.allOf( ForgeDirection.class ) ); + else + gridProxy.setValidSides( EnumSet.noneOf( ForgeDirection.class ) ); + } + + private void dropAndBreak() { // TODO Auto-generated method stub @@ -63,15 +124,13 @@ public class TileCraftingTile extends AEBaseTile implements IAEMultiBlock @Override public IAECluster getCluster() { - // TODO Auto-generated method stub - return null; + return clust; } @Override public boolean isValid() { - // TODO Auto-generated method stub - return false; + return true; } public long getStorageBytes() @@ -79,4 +138,24 @@ public class TileCraftingTile extends AEBaseTile implements IAEMultiBlock return storageBytes; } + public boolean isFormed() + { + return (worldObj.getBlockMetadata( xCoord, yCoord, zCoord ) & 8) == 8; + } + + public boolean isStorage() + { + return BlockCraftingUnit.checkType( worldObj.getBlockMetadata( xCoord, yCoord, zCoord ), BlockCraftingUnit.BASE_STORAGE ); + } + + public boolean isStatus() + { + return BlockCraftingUnit.checkType( worldObj.getBlockMetadata( xCoord, yCoord, zCoord ), BlockCraftingUnit.BASE_MONITOR ); + } + + public boolean isAccelerator() + { + return BlockCraftingUnit.checkType( worldObj.getBlockMetadata( xCoord, yCoord, zCoord ), BlockCraftingUnit.BASE_ACCELERATOR ); + } + } diff --git a/tile/crafting/TileMolecularAssembler.java b/tile/crafting/TileMolecularAssembler.java index dd79c045..4add6f7a 100644 --- a/tile/crafting/TileMolecularAssembler.java +++ b/tile/crafting/TileMolecularAssembler.java @@ -56,18 +56,39 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IAEAppEn private ICraftingPatternDetails myPlan = null; private double progress = 0; private boolean isAwake = false; + private boolean forcePlan = false; @Override public boolean pushPattern(ICraftingPatternDetails patternDetails, InventoryCrafting table, ForgeDirection where) { + if ( myPattern == null ) + { + boolean isEmpty = true; + for (int x = 0; x < inv.getSizeInventory(); x++) + isEmpty = inv.getStackInSlot( x ) == null && isEmpty; + + if ( isEmpty ) + { + forcePlan = true; + myPlan = patternDetails; + pushDirection = where; + + for (int x = 0; x < table.getSizeInventory(); x++) + inv.setInventorySlotContents( x, table.getStackInSlot( x ) ); + + updateSleepyness(); + return true; + } + } return false; } private void recalculatePlan() { - ItemStack is = inv.getStackInSlot( 10 ); + if ( forcePlan ) + return; - boolean wasEnabled = isAwake; + ItemStack is = inv.getStackInSlot( 10 ); if ( is != null && is.getItem() instanceof ItemEncodedPattern ) { @@ -89,6 +110,12 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IAEAppEn myPattern = null; } + updateSleepyness(); + } + + private void updateSleepyness() + { + boolean wasEnabled = isAwake; isAwake = myPlan != null && hasMats() || canPush(); if ( wasEnabled != isAwake ) { @@ -132,6 +159,17 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IAEAppEn @Override public void writeToNBT(NBTTagCompound data) { + if ( forcePlan ) + { + ItemStack pattern = myPlan.getPattern(); + if ( pattern != null ) + { + NBTTagCompound pdata = new NBTTagCompound(); + pattern.writeToNBT( pdata ); + data.setTag( "myPlan", pdata ); + } + } + upgrades.writeToNBT( data, "upgrades" ); inv.writeToNBT( data, "inv" ); settings.writeToNBT( data ); @@ -140,6 +178,23 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IAEAppEn @Override public void readFromNBT(NBTTagCompound data) { + if ( data.hasKey( "myPlan" ) ) + { + ItemStack myPat = ItemStack.loadItemStackFromNBT( data.getCompoundTag( "myPlan" ) ); + + if ( myPat != null && myPat.getItem() instanceof ItemEncodedPattern ) + { + World w = getWorldObj(); + ItemEncodedPattern iep = (ItemEncodedPattern) is.getItem(); + ICraftingPatternDetails ph = iep.getPatternForItem( is, w ); + if ( ph != null ) + { + forcePlan = true; + myPlan = ph; + } + } + } + upgrades.readFromNBT( data, "upgrades" ); inv.readFromNBT( data, "inv" ); settings.readFromNBT( data ); @@ -340,6 +395,12 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IAEAppEn else output = pushTo( output, pushDirection ); + if ( output == null && forcePlan ) + { + forcePlan = false; + recalculatePlan(); + } + inv.setInventorySlotContents( 9, output ); }