2014-06-18 08:23:37 +02:00
|
|
|
package appeng.container.implementations;
|
|
|
|
|
2014-06-28 22:18:36 +02:00
|
|
|
import java.io.IOException;
|
2014-07-11 06:46:51 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
2014-06-18 08:23:37 +02:00
|
|
|
import java.util.concurrent.Future;
|
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2014-06-28 22:18:36 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2014-06-18 08:23:37 +02:00
|
|
|
import net.minecraft.entity.player.InventoryPlayer;
|
|
|
|
import net.minecraft.inventory.ICrafting;
|
2014-07-13 08:45:16 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-06-18 08:23:37 +02:00
|
|
|
import net.minecraft.util.ChatComponentText;
|
|
|
|
import net.minecraft.world.World;
|
2014-06-28 22:18:36 +02:00
|
|
|
import appeng.api.AEApi;
|
2014-06-29 11:06:07 +02:00
|
|
|
import appeng.api.config.Actionable;
|
2014-06-18 08:23:37 +02:00
|
|
|
import appeng.api.config.SecurityPermissions;
|
|
|
|
import appeng.api.networking.IGrid;
|
2014-07-11 06:46:51 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingCPU;
|
2014-07-03 06:48:18 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingGrid;
|
|
|
|
import appeng.api.networking.crafting.ICraftingJob;
|
2014-07-13 08:45:16 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingLink;
|
2014-06-18 08:23:37 +02:00
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
|
|
|
import appeng.api.networking.security.IActionHost;
|
|
|
|
import appeng.api.networking.security.PlayerSource;
|
2014-06-29 11:06:07 +02:00
|
|
|
import appeng.api.networking.storage.IStorageGrid;
|
|
|
|
import appeng.api.storage.IMEInventory;
|
2014-06-18 08:23:37 +02:00
|
|
|
import appeng.api.storage.ITerminalHost;
|
2014-06-28 22:18:36 +02:00
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
2014-06-18 08:23:37 +02:00
|
|
|
import appeng.container.AEBaseContainer;
|
2014-06-28 22:18:36 +02:00
|
|
|
import appeng.container.guisync.GuiSync;
|
2014-06-18 08:23:37 +02:00
|
|
|
import appeng.core.AELog;
|
2014-07-13 08:45:16 +02:00
|
|
|
import appeng.core.sync.GuiBridge;
|
2014-06-28 22:18:36 +02:00
|
|
|
import appeng.core.sync.network.NetworkHandler;
|
|
|
|
import appeng.core.sync.packets.PacketMEInventoryUpdate;
|
2014-07-13 08:45:16 +02:00
|
|
|
import appeng.helpers.WirelessTerminalGuiObject;
|
|
|
|
import appeng.parts.reporting.PartCraftingTerminal;
|
|
|
|
import appeng.parts.reporting.PartPatternTerminal;
|
|
|
|
import appeng.parts.reporting.PartTerminal;
|
2014-07-11 06:46:51 +02:00
|
|
|
import appeng.util.ItemSorters;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableSet;
|
2014-06-18 08:23:37 +02:00
|
|
|
|
2014-07-03 06:48:18 +02:00
|
|
|
public class ContainerCraftConfirm extends AEBaseContainer
|
2014-06-18 08:23:37 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
ITerminalHost priHost;
|
2014-07-03 06:48:18 +02:00
|
|
|
public Future<ICraftingJob> job;
|
|
|
|
public ICraftingJob result;
|
2014-06-18 08:23:37 +02:00
|
|
|
|
2014-06-28 22:18:36 +02:00
|
|
|
@GuiSync(0)
|
|
|
|
public long bytesUsed;
|
|
|
|
|
|
|
|
@GuiSync(1)
|
|
|
|
public long cpuBytesAvail;
|
|
|
|
|
|
|
|
@GuiSync(2)
|
|
|
|
public int cpuCoProcessors;
|
|
|
|
|
2014-06-29 11:06:07 +02:00
|
|
|
@GuiSync(3)
|
|
|
|
public boolean autoStart = false;
|
|
|
|
|
|
|
|
@GuiSync(4)
|
|
|
|
public boolean simulation = true;
|
|
|
|
|
2014-07-11 06:46:51 +02:00
|
|
|
@GuiSync(5)
|
|
|
|
public int selectedCpu = -1;
|
|
|
|
|
|
|
|
@GuiSync(6)
|
|
|
|
public boolean noCPU = true;
|
|
|
|
|
|
|
|
protected long cpuIdx = Long.MIN_VALUE;
|
|
|
|
|
|
|
|
public class CraftingCPURecord implements Comparable<CraftingCPURecord>
|
|
|
|
{
|
|
|
|
|
|
|
|
ICraftingCPU cpu;
|
|
|
|
|
|
|
|
long id = cpuIdx++;
|
|
|
|
long size;
|
|
|
|
int processors;
|
|
|
|
|
|
|
|
CraftingCPURecord(long size, int proc, ICraftingCPU server) {
|
|
|
|
this.size = size;
|
|
|
|
this.processors = proc;
|
|
|
|
this.cpu = server;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compareTo(CraftingCPURecord o)
|
|
|
|
{
|
|
|
|
int a = ItemSorters.compareLong( o.processors, processors );
|
|
|
|
if ( a != 0 )
|
|
|
|
return a;
|
|
|
|
return ItemSorters.compareLong( o.size, size );
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
public ArrayList<CraftingCPURecord> cpus = new ArrayList();
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
public ContainerCraftConfirm(InventoryPlayer ip, ITerminalHost te) {
|
|
|
|
super( ip, te );
|
|
|
|
priHost = te;
|
|
|
|
}
|
|
|
|
|
2014-07-11 06:46:51 +02:00
|
|
|
private void sendCPUs()
|
|
|
|
{
|
|
|
|
Collections.sort( cpus );
|
|
|
|
|
|
|
|
if ( selectedCpu >= cpus.size() )
|
|
|
|
{
|
|
|
|
selectedCpu = -1;
|
|
|
|
cpuBytesAvail = 0;
|
|
|
|
cpuCoProcessors = 0;
|
|
|
|
}
|
|
|
|
else if ( selectedCpu != -1 )
|
|
|
|
{
|
|
|
|
cpuBytesAvail = cpus.get( selectedCpu ).size;
|
|
|
|
cpuCoProcessors = cpus.get( selectedCpu ).processors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cycleCpu(boolean next)
|
|
|
|
{
|
|
|
|
if ( next )
|
|
|
|
selectedCpu++;
|
|
|
|
else
|
|
|
|
selectedCpu--;
|
|
|
|
|
|
|
|
if ( selectedCpu < -1 )
|
|
|
|
selectedCpu = cpus.size() - 1;
|
|
|
|
else if ( selectedCpu >= cpus.size() )
|
|
|
|
selectedCpu = -1;
|
|
|
|
|
|
|
|
if ( selectedCpu == -1 )
|
|
|
|
{
|
|
|
|
cpuBytesAvail = 0;
|
|
|
|
cpuCoProcessors = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cpuBytesAvail = cpus.get( selectedCpu ).size;
|
|
|
|
cpuCoProcessors = cpus.get( selectedCpu ).processors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
@Override
|
|
|
|
public void detectAndSendChanges()
|
|
|
|
{
|
2014-07-11 06:46:51 +02:00
|
|
|
if ( Platform.isClient() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
ICraftingGrid cc = getGrid().getCache( ICraftingGrid.class );
|
|
|
|
ImmutableSet<ICraftingCPU> cpuSet = cc.getCpus();
|
|
|
|
|
|
|
|
int matches = 0;
|
|
|
|
boolean changed = false;
|
|
|
|
for (ICraftingCPU c : cpuSet)
|
|
|
|
{
|
|
|
|
boolean found = false;
|
|
|
|
for (CraftingCPURecord ccr : cpus)
|
|
|
|
if ( ccr.cpu == c )
|
|
|
|
found = true;
|
|
|
|
|
|
|
|
boolean matched = cpuMatches( c );
|
|
|
|
|
|
|
|
if ( matched )
|
|
|
|
matches++;
|
|
|
|
|
|
|
|
if ( !found != matched )
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( changed || cpus.size() != matches )
|
|
|
|
{
|
|
|
|
cpus.clear();
|
|
|
|
for (ICraftingCPU c : cpuSet)
|
|
|
|
{
|
|
|
|
if ( cpuMatches( c ) )
|
|
|
|
cpus.add( new CraftingCPURecord( c.getAvailableStorage(), c.getCoProcessors(), c ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
sendCPUs();
|
|
|
|
}
|
|
|
|
|
|
|
|
noCPU = cpus.size() == 0;
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
super.detectAndSendChanges();
|
2014-07-11 06:46:51 +02:00
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
if ( job != null && job.isDone() )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
result = job.get();
|
2014-06-29 11:06:07 +02:00
|
|
|
|
2014-06-22 09:00:38 +02:00
|
|
|
if ( !result.isSimulation() )
|
|
|
|
{
|
2014-06-29 11:06:07 +02:00
|
|
|
simulation = false;
|
|
|
|
if ( autoStart )
|
2014-06-28 22:18:36 +02:00
|
|
|
{
|
2014-06-29 11:06:07 +02:00
|
|
|
startJob();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
simulation = true;
|
2014-06-28 22:18:36 +02:00
|
|
|
|
2014-06-29 11:06:07 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
PacketMEInventoryUpdate a = new PacketMEInventoryUpdate( (byte) 0 );
|
|
|
|
PacketMEInventoryUpdate b = new PacketMEInventoryUpdate( (byte) 1 );
|
|
|
|
PacketMEInventoryUpdate c = result.isSimulation() ? new PacketMEInventoryUpdate( (byte) 2 ) : null;
|
2014-06-28 22:18:36 +02:00
|
|
|
|
2014-06-29 11:06:07 +02:00
|
|
|
IItemList<IAEItemStack> plan = AEApi.instance().storage().createItemList();
|
2014-07-03 06:48:18 +02:00
|
|
|
result.populatePlan( plan );
|
2014-06-28 22:18:36 +02:00
|
|
|
|
2014-06-29 11:06:07 +02:00
|
|
|
bytesUsed = result.getByteTotal();
|
2014-06-28 22:18:36 +02:00
|
|
|
|
2014-06-29 11:06:07 +02:00
|
|
|
for (IAEItemStack out : plan)
|
|
|
|
{
|
|
|
|
IAEItemStack m = null;
|
2014-06-28 22:18:36 +02:00
|
|
|
|
2014-06-29 11:06:07 +02:00
|
|
|
IAEItemStack o = out.copy();
|
|
|
|
o.reset();
|
|
|
|
o.setStackSize( out.getStackSize() );
|
2014-06-28 22:18:36 +02:00
|
|
|
|
2014-06-29 11:06:07 +02:00
|
|
|
IAEItemStack p = out.copy();
|
|
|
|
p.reset();
|
|
|
|
p.setStackSize( out.getCountRequestable() );
|
|
|
|
|
|
|
|
IStorageGrid sg = getGrid().getCache( IStorageGrid.class );
|
|
|
|
IMEInventory<IAEItemStack> itemsg = sg.getItemInventory();
|
2014-06-28 22:18:36 +02:00
|
|
|
|
2014-06-29 11:06:07 +02:00
|
|
|
if ( c != null && result.isSimulation() )
|
2014-06-28 22:18:36 +02:00
|
|
|
{
|
2014-06-29 11:06:07 +02:00
|
|
|
m = o.copy();
|
|
|
|
o = itemsg.extractItems( o, Actionable.SIMULATE, mySrc );
|
|
|
|
|
|
|
|
if ( o == null )
|
2014-06-28 22:18:36 +02:00
|
|
|
{
|
2014-06-29 11:06:07 +02:00
|
|
|
o = m.copy();
|
|
|
|
o.setStackSize( 0 );
|
2014-06-28 22:18:36 +02:00
|
|
|
}
|
2014-06-29 11:06:07 +02:00
|
|
|
|
|
|
|
m.setStackSize( m.getStackSize() - o.getStackSize() );
|
2014-06-28 22:18:36 +02:00
|
|
|
}
|
2014-06-29 11:06:07 +02:00
|
|
|
|
|
|
|
if ( o.getStackSize() > 0 )
|
|
|
|
a.appendItem( o );
|
|
|
|
|
|
|
|
if ( p.getStackSize() > 0 )
|
|
|
|
b.appendItem( p );
|
|
|
|
|
|
|
|
if ( c != null && m != null && m.getStackSize() > 0 )
|
|
|
|
c.appendItem( m );
|
2014-06-28 22:18:36 +02:00
|
|
|
}
|
2014-06-29 11:06:07 +02:00
|
|
|
|
|
|
|
for (Object g : this.crafters)
|
2014-06-28 22:18:36 +02:00
|
|
|
{
|
2014-06-29 11:06:07 +02:00
|
|
|
if ( g instanceof EntityPlayer )
|
|
|
|
{
|
|
|
|
NetworkHandler.instance.sendTo( a, (EntityPlayerMP) g );
|
|
|
|
NetworkHandler.instance.sendTo( b, (EntityPlayerMP) g );
|
|
|
|
if ( c != null )
|
|
|
|
NetworkHandler.instance.sendTo( c, (EntityPlayerMP) g );
|
|
|
|
}
|
2014-06-28 22:18:36 +02:00
|
|
|
}
|
2014-06-29 11:06:07 +02:00
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
// :P
|
2014-06-22 09:00:38 +02:00
|
|
|
}
|
2014-06-18 08:23:37 +02:00
|
|
|
}
|
|
|
|
catch (Throwable e)
|
|
|
|
{
|
|
|
|
getPlayerInv().player.addChatMessage( new ChatComponentText( "Error: " + e.toString() ) );
|
|
|
|
AELog.error( e );
|
|
|
|
this.isContainerValid = false;
|
|
|
|
result = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
job = null;
|
|
|
|
}
|
|
|
|
verifyPermissions( SecurityPermissions.CRAFT, false );
|
|
|
|
}
|
|
|
|
|
2014-07-11 06:46:51 +02:00
|
|
|
private boolean cpuMatches(ICraftingCPU c)
|
|
|
|
{
|
|
|
|
return c.getAvailableStorage() >= bytesUsed && !c.isBusy();
|
|
|
|
}
|
|
|
|
|
2014-06-29 11:06:07 +02:00
|
|
|
public void startJob()
|
|
|
|
{
|
2014-07-13 08:45:16 +02:00
|
|
|
GuiBridge OriginalGui = null;
|
|
|
|
|
|
|
|
IActionHost ah = getActionHost();
|
|
|
|
if ( ah instanceof WirelessTerminalGuiObject )
|
|
|
|
OriginalGui = GuiBridge.GUI_WIRELESS_TERM;
|
|
|
|
|
|
|
|
if ( ah instanceof PartTerminal )
|
|
|
|
OriginalGui = GuiBridge.GUI_ME;
|
|
|
|
|
|
|
|
if ( ah instanceof PartCraftingTerminal )
|
|
|
|
OriginalGui = GuiBridge.GUI_CRAFTING_TERMINAL;
|
|
|
|
|
|
|
|
if ( ah instanceof PartPatternTerminal )
|
|
|
|
OriginalGui = GuiBridge.GUI_PATTERN_TERMINAL;
|
|
|
|
|
2014-06-29 11:06:07 +02:00
|
|
|
if ( result != null && simulation == false )
|
|
|
|
{
|
2014-07-03 06:48:18 +02:00
|
|
|
ICraftingGrid cc = getGrid().getCache( ICraftingGrid.class );
|
2014-07-13 08:45:16 +02:00
|
|
|
ICraftingLink g = cc.submitJob( result, null, selectedCpu == -1 ? null : cpus.get( selectedCpu ).cpu, getActionSrc() );
|
|
|
|
autoStart = false;
|
|
|
|
if ( g != null && OriginalGui != null && openContext != null )
|
|
|
|
{
|
|
|
|
TileEntity te = openContext.w.getTileEntity( openContext.x, openContext.y, openContext.z );
|
|
|
|
Platform.openGUI( invPlayer.player, te, openContext.side, OriginalGui );
|
|
|
|
}
|
2014-06-29 11:06:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
@Override
|
|
|
|
public void onContainerClosed(EntityPlayer par1EntityPlayer)
|
|
|
|
{
|
|
|
|
super.onContainerClosed( par1EntityPlayer );
|
|
|
|
if ( job != null )
|
|
|
|
{
|
|
|
|
job.cancel( true );
|
|
|
|
job = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeCraftingFromCrafters(ICrafting c)
|
|
|
|
{
|
|
|
|
super.removeCraftingFromCrafters( c );
|
|
|
|
if ( job != null )
|
|
|
|
{
|
|
|
|
job.cancel( true );
|
|
|
|
job = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IGrid getGrid()
|
|
|
|
{
|
|
|
|
IActionHost h = ((IActionHost) this.getTarget());
|
|
|
|
return h.getActionableNode().getGrid();
|
|
|
|
}
|
|
|
|
|
|
|
|
public World getWorld()
|
|
|
|
{
|
|
|
|
return getPlayerInv().player.worldObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
public BaseActionSource getActionSrc()
|
|
|
|
{
|
|
|
|
return new PlayerSource( getPlayerInv().player, (IActionHost) getTarget() );
|
|
|
|
}
|
|
|
|
}
|