Graceful saving/loading and breaking of CPUs.

This commit is contained in:
AlgorithmX2
2014-07-02 00:05:21 -05:00
parent 64f2acfb43
commit 1d687f3084
4 changed files with 149 additions and 34 deletions
+13
View File
@@ -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)
{
@@ -64,6 +64,7 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
IAEItemStack finalOutput;
boolean waiting = false;
private boolean isComplete = true;
Map<ICraftingPatternDetails, TaskProgress> tasks = new HashMap<ICraftingPatternDetails, TaskProgress>();
IItemList<IAEItemStack> waitingFor = AEApi.instance().storage().createItemList();
@@ -78,7 +79,6 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
public WorldCoord min;
public WorldCoord max;
public boolean isDestroyed = false;
private boolean isComplete = true;
private final HashMap<IMEMonitorHandlerReceiver<IAEItemStack>, Object> listeners = new HashMap<IMEMonitorHandlerReceiver<IAEItemStack>, Object>();
@@ -168,6 +168,11 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
return (Iterator) tiles.iterator();
}
public IMEInventory<IAEItemStack> getInventory()
{
return inventory;
}
public CraftingCPUCluster(WorldCoord _min, WorldCoord _max) {
min = _min;
max = _max;
@@ -302,9 +307,17 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
public IGrid getGrid()
{
for (TileCraftingTile r : tiles)
return r.getActionableNode().getGrid();
{
IGridNode gn = r.getActionableNode();
if ( gn != null )
{
IGrid g = gn.getGrid();
if ( g != null )
return r.getActionableNode().getGrid();
}
}
throw new RuntimeException( "No tiles inside a cpu cluster." );
return null;
}
private void completeJob()
@@ -357,7 +370,7 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
isComplete = true;
tasks.clear();
waitingFor.resetStatus();
markDirty();
storeItems(); // marks dirty
}
public void updateCraftingLogic(IGrid grid, IEnergyGrid eg, CraftingCache cc)
@@ -367,24 +380,7 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
if ( inventory.getItemList().isEmpty() )
return;
IStorageGrid sg = grid.getCache( IStorageGrid.class );
IMEInventory<IAEItemStack> 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<IAEItemStack
waiting = true;
}
private void storeItems()
{
IGrid g = getGrid();
if ( g == null )
return;
IStorageGrid sg = g.getCache( IStorageGrid.class );
IMEInventory<IAEItemStack> 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<IAEItemStack
inventory.injectItems( ais, Actionable.MODULATE, machineSrc );
waiting = data.getBoolean( "waiting" );
isComplete = data.getBoolean( "isComplete" );
NBTTagList list = data.getTagList( "tasks", 10 );
for (int x = 0; x < list.tagCount(); x++)
@@ -684,6 +707,7 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
data.setTag( "finalOutput", writeItem( finalOutput ) );
data.setTag( "inventory", writeList( inventory.getItemList() ) );
data.setBoolean( "waiting", waiting );
data.setBoolean( "isComplete", isComplete );
NBTTagList list = new NBTTagList();
for (Entry<ICraftingPatternDetails, TaskProgress> e : tasks.entrySet())
@@ -744,4 +768,9 @@ public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack
}
}
public BaseActionSource getActionSource()
{
return machineSrc;
}
}
+67
View File
@@ -1,15 +1,25 @@
package appeng.tile.crafting;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.LinkedList;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import appeng.api.config.Actionable;
import appeng.api.implementations.IPowerChannelState;
import appeng.api.networking.GridFlags;
import appeng.api.networking.IGridHost;
import appeng.api.networking.events.MENetworkChannelsChanged;
import appeng.api.networking.events.MENetworkEventSubscribe;
import appeng.api.networking.events.MENetworkPowerStatusChange;
import appeng.api.parts.ISimplifiedBundle;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.util.WorldCoord;
import appeng.me.cluster.IAECluster;
import appeng.me.cluster.IAEMultiBlock;
import appeng.me.cluster.implementations.CraftingCPUCalculator;
@@ -20,6 +30,7 @@ import appeng.tile.events.AETileEventHandler;
import appeng.tile.events.TileEventType;
import appeng.tile.grid.AENetworkTile;
import appeng.util.Platform;
import appeng.util.item.ItemList;
public class TileCraftingTile extends AENetworkTile implements IAEMultiBlock, IPowerChannelState
{
@@ -201,4 +212,60 @@ public class TileCraftingTile extends AENetworkTile implements IAEMultiBlock, IP
return isPowered() && isFormed();
}
public void breakCluster()
{
if ( clust != null )
{
clust.cancel();
IMEInventory<IAEItemStack> inv = clust.getInventory();
LinkedList<WorldCoord> places = new LinkedList<WorldCoord>();
Iterator<IGridHost> 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>( 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();
}
}
}
+18 -12
View File
@@ -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();
}
}