From 1d687f30845b0ef617d2428c98d1dfdef5d5040e Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Wed, 2 Jul 2014 00:05:21 -0500 Subject: [PATCH] Graceful saving/loading and breaking of CPUs. --- block/crafting/BlockCraftingUnit.java | 13 ++++ .../implementations/CraftingCPUCluster.java | 73 +++++++++++++------ tile/crafting/TileCraftingTile.java | 67 +++++++++++++++++ tile/qnb/TileQuantumBridge.java | 30 +++++--- 4 files changed, 149 insertions(+), 34 deletions(-) diff --git a/block/crafting/BlockCraftingUnit.java b/block/crafting/BlockCraftingUnit.java index 92cb511e0..b7213c772 100644 --- a/block/crafting/BlockCraftingUnit.java +++ b/block/crafting/BlockCraftingUnit.java @@ -41,6 +41,9 @@ public class BlockCraftingUnit extends AEBaseBlock @Override public boolean onActivated(World w, int x, int y, int z, EntityPlayer p, int side, float hitX, float hitY, float hitZ) { + if ( Platform.isClient() ) + return true; + TileCraftingTile tg = getTileEntity( w, x, y, z ); if ( tg != null && !p.isSneaking() && tg.isFormed() && tg.isActive() ) { @@ -94,6 +97,16 @@ public class BlockCraftingUnit extends AEBaseBlock return RenderBlockCrafting.class; } + @Override + public void breakBlock(World w, int x, int y, int z, Block a, int b) + { + TileCraftingTile cp = getTileEntity( w, x, y, z ); + if ( cp != null ) + cp.breakCluster(); + + super.breakBlock( w, x, y, z, a, b ); + } + @Override public void onNeighborBlockChange(World w, int x, int y, int z, Block junk) { diff --git a/me/cluster/implementations/CraftingCPUCluster.java b/me/cluster/implementations/CraftingCPUCluster.java index 1126639db..8cbe1a7d3 100644 --- a/me/cluster/implementations/CraftingCPUCluster.java +++ b/me/cluster/implementations/CraftingCPUCluster.java @@ -64,6 +64,7 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor tasks = new HashMap(); IItemList waitingFor = AEApi.instance().storage().createItemList(); @@ -78,7 +79,6 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor, Object> listeners = new HashMap, Object>(); @@ -168,6 +168,11 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor getInventory() + { + return inventory; + } + public CraftingCPUCluster(WorldCoord _min, WorldCoord _max) { min = _min; max = _max; @@ -302,9 +307,17 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor ii = sg.getItemInventory(); - - for (IAEItemStack is : inventory.getItemList()) - { - is = inventory.extractItems( is.copy(), Actionable.MODULATE, machineSrc ); - - if ( is != null ) - is = ii.injectItems( is, Actionable.MODULATE, machineSrc ); - - if ( is != null ) - inventory.injectItems( is, Actionable.MODULATE, machineSrc ); - } - - if ( inventory.getItemList().isEmpty() ) - inventory = new MECraftingInventory(); - - markDirty(); + storeItems(); return; } @@ -539,6 +535,32 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor ii = sg.getItemInventory(); + + for (IAEItemStack is : inventory.getItemList()) + { + is = inventory.extractItems( is.copy(), Actionable.MODULATE, machineSrc ); + + if ( is != null ) + is = ii.injectItems( is, Actionable.MODULATE, machineSrc ); + + if ( is != null ) + inventory.injectItems( is, Actionable.MODULATE, machineSrc ); + } + + if ( inventory.getItemList().isEmpty() ) + inventory = new MECraftingInventory(); + + markDirty(); + } + private World getWorld() { return null; @@ -657,6 +679,7 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor e : tasks.entrySet()) @@ -744,4 +768,9 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor inv = clust.getInventory(); + + LinkedList places = new LinkedList(); + + Iterator i = clust.getTiles(); + while (i.hasNext()) + { + IGridHost h = i.next(); + if ( h == this ) + places.add( new WorldCoord( this ) ); + else + { + TileEntity te = (TileEntity) h; + + for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) + { + WorldCoord wc = new WorldCoord( te ); + wc.add( d, 1 ); + if ( worldObj.isAirBlock( wc.x, wc.y, wc.z ) ) + places.add( wc ); + } + + } + } + + Collections.shuffle( places ); + + if ( places.isEmpty() ) + throw new RuntimeException( "No air or even the tile hat was destroyed?!?!" ); + + for (IAEItemStack ais : inv.getAvailableItems( new ItemList( IAEItemStack.class ) )) + { + ais = ais.copy(); + ais.setStackSize( ais.getItemStack().getMaxStackSize() ); + while (true) + { + IAEItemStack g = inv.extractItems( ais.copy(), Actionable.MODULATE, clust.getActionSource() ); + if ( g == null ) + break; + + WorldCoord wc = places.poll(); + places.add( wc ); + + Platform.spawnDrops( worldObj, wc.x, wc.y, wc.z, Arrays.asList( g.getItemStack() ) ); + } + + } + + clust.destroy(); + } + } } diff --git a/tile/qnb/TileQuantumBridge.java b/tile/qnb/TileQuantumBridge.java index 6e9e59cbe..9e97f2a6f 100644 --- a/tile/qnb/TileQuantumBridge.java +++ b/tile/qnb/TileQuantumBridge.java @@ -31,8 +31,8 @@ import appeng.util.Platform; public class TileQuantumBridge extends AENetworkInvTile implements IAEMultiBlock { - final private static ItemStack ring = AEApi.instance().blocks().blockQuantumRing.stack(1); - + final private static ItemStack ring = AEApi.instance().blocks().blockQuantumRing.stack( 1 ); + final int sidesRing[] = new int[] {}; final int sidesLink[] = new int[] { 0 }; @@ -134,14 +134,14 @@ public class TileQuantumBridge extends AENetworkInvTile implements IAEMultiBlock { if ( clust != null ) { - if ( ! affectWorld ) + if ( !affectWorld ) clust.updateStatus = false; - + clust.destroy(); } - + clust = null; - + if ( affectWorld ) gridProxy.setValidSides( EnumSet.noneOf( ForgeDirection.class ) ); } @@ -157,26 +157,26 @@ public class TileQuantumBridge extends AENetworkInvTile implements IAEMultiBlock { return !isInvalid(); } - + @Override public void onReady() { super.onReady(); - if ( worldObj.getBlock( xCoord, yCoord, zCoord) == AEApi.instance().blocks().blockQuantumRing.block() ) + if ( worldObj.getBlock( xCoord, yCoord, zCoord ) == AEApi.instance().blocks().blockQuantumRing.block() ) gridProxy.setVisualRepresentation( ring ); } @Override public void invalidate() { - disconnect(false); + disconnect( false ); super.invalidate(); } @Override public void onChunkUnload() { - disconnect(false); + disconnect( false ); super.onChunkUnload(); } @@ -189,9 +189,9 @@ public class TileQuantumBridge extends AENetworkInvTile implements IAEMultiBlock if ( xdex != flags ) { xdex = flags; - markForUpdate(); + markForUpdate(); } - + if ( isCorner() || isCenter() ) { gridProxy.setValidSides( getConnections() ); @@ -283,4 +283,10 @@ public class TileQuantumBridge extends AENetworkInvTile implements IAEMultiBlock return (xdex & hasSingularity) == hasSingularity; } + public void breakCluster() + { + if ( clust != null ) + clust.destroy(); + } + }