Handle Crafting CPU Gui Networking and background monitoring.

This commit is contained in:
AlgorithmX2 2014-06-27 00:45:29 -05:00
parent 20de41caca
commit ad0151d086
3 changed files with 206 additions and 32 deletions

View file

@ -116,7 +116,8 @@ public class GuiCraftingCPU extends AEBaseGui implements ISortSource
if ( l.getStackSize() <= 0 )
{
a.reset();
if ( a != null )
a.reset();
}
else
{
@ -126,6 +127,8 @@ public class GuiCraftingCPU extends AEBaseGui implements ISortSource
a = s.findPrecise( l );
}
if ( a != null )
a.setStackSize( l.getStackSize() );
}
}

View file

@ -5,27 +5,34 @@ import java.io.IOException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.inventory.ICrafting;
import net.minecraftforge.common.util.ForgeDirection;
import appeng.api.AEApi;
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridBlock;
import appeng.api.networking.IGridHost;
import appeng.api.networking.IGridNode;
import appeng.api.networking.crafting.CraftingItemList;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.storage.IBaseMonitor;
import appeng.api.storage.IMEMonitorHandlerReceiver;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.container.AEBaseContainer;
import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketMEInventoryUpdate;
import appeng.me.cluster.IAECluster;
import appeng.me.cluster.implementations.CraftingCPUCluster;
import appeng.tile.crafting.TileCraftingTile;
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
public class ContainerCraftingCPU extends AEBaseContainer
public class ContainerCraftingCPU extends AEBaseContainer implements IMEMonitorHandlerReceiver<IAEItemStack>
{
CraftingCPUCluster monitor = null;
IGrid network;
IItemList<IAEItemStack> list = AEApi.instance().storage().createItemList();
public ContainerCraftingCPU(InventoryPlayer ip, TileCraftingTile te) {
super( ip, null, null );
IGridHost host = te;// .getGridHost();
@ -37,6 +44,20 @@ public class ContainerCraftingCPU extends AEBaseContainer
findNode( host, d );
}
if ( te instanceof TileCraftingTile )
{
IAECluster c = ((TileCraftingTile) te).getCluster();
if ( c instanceof CraftingCPUCluster )
{
monitor = (CraftingCPUCluster) c;
if ( monitor != null )
{
monitor.getListOfItem( list, CraftingItemList.ALL );
monitor.addListener( this, null );
}
}
}
if ( network == null && Platform.isServer() )
isContainerValid = false;
}
@ -53,43 +74,49 @@ public class ContainerCraftingCPU extends AEBaseContainer
int delay = 40;
@Override
public void onContainerClosed(EntityPlayer player)
{
super.onContainerClosed( player );
if ( monitor != null )
monitor.removeListener( this );
}
@Override
public void removeCraftingFromCrafters(ICrafting c)
{
super.removeCraftingFromCrafters( c );
if ( this.crafters.isEmpty() && monitor != null )
monitor.removeListener( this );
}
@Override
public void detectAndSendChanges()
{
delay++;
if ( Platform.isServer() && delay > 15 && network != null )
if ( Platform.isServer() )
{
delay = 0;
PacketMEInventoryUpdate piu;
try
{
piu = new PacketMEInventoryUpdate();
PacketMEInventoryUpdate a = new PacketMEInventoryUpdate( (byte) 0 );
PacketMEInventoryUpdate b = new PacketMEInventoryUpdate( (byte) 1 );
PacketMEInventoryUpdate c = new PacketMEInventoryUpdate( (byte) 2 );
for (Class<? extends IGridHost> machineClass : network.getMachinesClasses())
for (IAEItemStack out : list)
{
IItemList<IAEItemStack> list = AEApi.instance().storage().createItemList();
for (IGridNode machine : network.getMachines( machineClass ))
{
IGridBlock blk = machine.getGridBlock();
ItemStack is = blk.getMachineRepresentation();
if ( is != null && is.getItem() != null )
{
IAEItemStack ais = AEItemStack.create( is );
ais.setStackSize( 1 );
ais.setCountRequestable( (long) (blk.getIdlePowerUsage() * 100.0) );
list.add( ais );
}
}
for (IAEItemStack ais : list)
piu.appendItem( ais );
a.appendItem( monitor.getItemStack( out, CraftingItemList.STORAGE ) );
b.appendItem( monitor.getItemStack( out, CraftingItemList.ACTIVE ) );
c.appendItem( monitor.getItemStack( out, CraftingItemList.PENDING ) );
}
for (Object c : this.crafters)
for (Object g : this.crafters)
{
if ( c instanceof EntityPlayer )
NetworkHandler.instance.sendTo( piu, (EntityPlayerMP) c );
if ( g instanceof EntityPlayer )
{
NetworkHandler.instance.sendTo( a, (EntityPlayerMP) g );
NetworkHandler.instance.sendTo( b, (EntityPlayerMP) g );
NetworkHandler.instance.sendTo( c, (EntityPlayerMP) g );
}
}
}
catch (IOException e)
@ -100,4 +127,24 @@ public class ContainerCraftingCPU extends AEBaseContainer
}
super.detectAndSendChanges();
}
@Override
public boolean isValid(Object verificationToken)
{
return true;
}
@Override
public void postChange(IBaseMonitor<IAEItemStack> monitor, IAEItemStack change, BaseActionSource actionSource)
{
change = change.copy();
change.setStackSize( 1 );
list.add( change );
}
@Override
public void onListUpdate()
{
}
}

View file

@ -16,12 +16,15 @@ import appeng.api.config.FuzzyMode;
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridHost;
import appeng.api.networking.IGridNode;
import appeng.api.networking.crafting.CraftingItemList;
import appeng.api.networking.crafting.ICraftingMedium;
import appeng.api.networking.crafting.ICraftingPatternDetails;
import appeng.api.networking.events.MENetworkCraftingCpuChange;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.storage.IBaseMonitor;
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEMonitorHandlerReceiver;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
@ -38,9 +41,48 @@ import appeng.util.Platform;
import appeng.util.item.AEItemStack;
import cpw.mods.fml.common.FMLCommonHandler;
public class CraftingCPUCluster implements IAECluster
public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack>
{
private final HashMap<IMEMonitorHandlerReceiver<IAEItemStack>, Object> listeners = new HashMap<IMEMonitorHandlerReceiver<IAEItemStack>, Object>();
protected Iterator<Entry<IMEMonitorHandlerReceiver<IAEItemStack>, Object>> getListeners()
{
return listeners.entrySet().iterator();
}
protected void postChange(IAEItemStack diff, BaseActionSource src)
{
Iterator<Entry<IMEMonitorHandlerReceiver<IAEItemStack>, Object>> i = getListeners();
while (i.hasNext())
{
Entry<IMEMonitorHandlerReceiver<IAEItemStack>, Object> o = i.next();
IMEMonitorHandlerReceiver<IAEItemStack> recv = o.getKey();
if ( recv.isValid( o.getValue() ) )
recv.postChange( null, diff, src );
else
i.remove();
}
}
/**
* add a new Listener to the monitor, be sure to properly remove yourself when your done.
*/
@Override
public void addListener(IMEMonitorHandlerReceiver<IAEItemStack> l, Object verificationToken)
{
listeners.put( l, verificationToken );
}
/**
* remove a Listener to the monitor.
*/
@Override
public void removeListener(IMEMonitorHandlerReceiver<IAEItemStack> l)
{
listeners.remove( l );
}
public WorldCoord min;
public WorldCoord max;
public boolean isDestroyed = false;
@ -58,6 +100,49 @@ public class CraftingCPUCluster implements IAECluster
private LinkedList<TileCraftingTile> storage = new LinkedList<TileCraftingTile>();
private LinkedList<TileCraftingTile> status = new LinkedList<TileCraftingTile>();
public void getListOfItem(IItemList<IAEItemStack> list, CraftingItemList whichList)
{
switch (whichList)
{
case ACTIVE:
for (IAEItemStack ais : waitingFor)
list.add( ais );
break;
case PENDING:
for (Entry<ICraftingPatternDetails, TaskProgress> t : tasks.entrySet())
{
for (IAEItemStack ais : t.getKey().getCondencedOutputs())
{
ais = ais.copy();
ais.setStackSize( ais.getStackSize() * t.getValue().value );
list.add( ais );
}
}
break;
case STORAGE:
inventory.getAvailableItems( list );
break;
default:
case ALL:
inventory.getAvailableItems( list );
for (IAEItemStack ais : waitingFor)
list.add( ais );
for (Entry<ICraftingPatternDetails, TaskProgress> t : tasks.entrySet())
{
for (IAEItemStack ais : t.getKey().getCondencedOutputs())
{
ais = ais.copy();
ais.setStackSize( ais.getStackSize() * t.getValue().value );
list.add( ais );
}
}
break;
}
}
/**
* crafting job info
*/
@ -485,4 +570,43 @@ public class CraftingCPUCluster implements IAECluster
return !tasks.isEmpty() || !waitingFor.isEmpty();
}
public IAEItemStack getItemStack(IAEItemStack what, CraftingItemList storage2)
{
IAEItemStack is = null;
switch (storage2)
{
case STORAGE:
is = inventory.getItemList().findPrecise( what );
break;
case ACTIVE:
is = waitingFor.findPrecise( what );
break;
case PENDING:
is = what.copy();
is.setStackSize( 0 );
for (Entry<ICraftingPatternDetails, TaskProgress> t : tasks.entrySet())
{
for (IAEItemStack ais : t.getKey().getCondencedOutputs())
{
if ( ais.equals( is ) )
is.setStackSize( is.getStackSize() + ais.getStackSize() * t.getValue().value );
}
}
break;
default:
case ALL:
throw new RuntimeException( "Invalid Operation" );
}
if ( is != null )
return is.copy();
is = what.copy();
is.setStackSize( 0 );
return is;
}
}