Applied-Energistics-2-tiler.../src/main/java/appeng/container/implementations/ContainerPatternTerm.java

443 lines
12 KiB
Java
Raw Normal View History

2014-04-08 02:43:57 +02:00
package appeng.container.implementations;
import java.util.ArrayList;
import java.util.List;
2014-04-17 06:38:43 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
2014-04-08 02:43:57 +02:00
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ICrafting;
2014-04-08 02:43:57 +02:00
import net.minecraft.inventory.IInventory;
2014-04-17 06:38:43 +02:00
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.Slot;
2014-04-17 06:38:43 +02:00
import net.minecraft.inventory.SlotCrafting;
2014-04-08 02:43:57 +02:00
import net.minecraft.item.ItemStack;
2014-04-17 06:38:43 +02:00
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
2014-04-17 06:38:43 +02:00
import net.minecraftforge.common.util.ForgeDirection;
import appeng.api.AEApi;
2014-04-17 06:38:43 +02:00
import appeng.api.config.Actionable;
import appeng.api.networking.security.MachineSource;
import appeng.api.storage.IMEMonitor;
2014-04-08 02:43:57 +02:00
import appeng.api.storage.ITerminalHost;
2014-04-17 06:38:43 +02:00
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.container.ContainerNull;
import appeng.container.guisync.GuiSync;
2014-04-10 06:51:08 +02:00
import appeng.container.slot.IOptionalSlotHost;
import appeng.container.slot.OptionalSlotFake;
import appeng.container.slot.SlotFakeCraftingMatrix;
import appeng.container.slot.SlotPatternOutputs;
2014-04-08 02:43:57 +02:00
import appeng.container.slot.SlotPatternTerm;
2014-04-08 04:25:41 +02:00
import appeng.container.slot.SlotRestrictedInput;
2014-04-17 06:38:43 +02:00
import appeng.core.sync.packets.PacketPatternSlot;
import appeng.helpers.IContainerCraftingPacket;
import appeng.items.storage.ItemViewCell;
2014-04-08 02:43:57 +02:00
import appeng.parts.reporting.PartPatternTerminal;
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.IAEAppEngInventory;
import appeng.tile.inventory.InvOperation;
2014-04-17 06:38:43 +02:00
import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
import appeng.util.inv.AdaptorPlayerHand;
import appeng.util.item.AEItemStack;
2014-04-08 02:43:57 +02:00
public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEAppEngInventory, IOptionalSlotHost, IContainerCraftingPacket
2014-04-08 02:43:57 +02:00
{
2014-04-17 06:38:43 +02:00
AppEngInternalInventory cOut = new AppEngInternalInventory( null, 1 );
IInventory crafting;
2014-04-08 02:43:57 +02:00
SlotFakeCraftingMatrix craftingSlots[] = new SlotFakeCraftingMatrix[9];
2014-04-10 06:51:08 +02:00
OptionalSlotFake outputSlots[] = new OptionalSlotFake[3];
2014-04-08 02:43:57 +02:00
SlotPatternTerm craftSlot;
2014-04-10 06:51:08 +02:00
SlotRestrictedInput patternSlotIN;
SlotRestrictedInput patternSlotOUT;
public PartPatternTerminal ct;
2014-04-08 02:43:57 +02:00
2014-09-21 01:06:22 +02:00
public ContainerPatternTerm(InventoryPlayer ip, ITerminalHost monitorable)
{
2014-09-21 01:06:22 +02:00
super( ip, monitorable, false );
ct = (PartPatternTerminal) monitorable;
2014-04-08 02:43:57 +02:00
2014-04-08 04:25:41 +02:00
IInventory patternInv = ct.getInventoryByName( "pattern" );
2014-04-10 06:51:08 +02:00
IInventory output = ct.getInventoryByName( "output" );
2014-04-17 06:38:43 +02:00
crafting = ct.getInventoryByName( "crafting" );
2014-04-08 02:43:57 +02:00
for (int y = 0; y < 3; y++)
for (int x = 0; x < 3; x++)
addSlotToContainer( craftingSlots[x + y * 3] = new SlotFakeCraftingMatrix( crafting, x + y * 3, 18 + x * 18, -76 + y * 18 ) );
2014-04-10 06:51:08 +02:00
2014-09-21 01:06:22 +02:00
addSlotToContainer( craftSlot = new SlotPatternTerm( ip.player, mySrc, powerSrc, monitorable, crafting, patternInv, cOut, 110, -76 + 18, this, 2, this ) );
2014-04-10 06:51:08 +02:00
craftSlot.IIcon = -1;
2014-04-08 02:43:57 +02:00
for (int y = 0; y < 3; y++)
2014-04-10 06:51:08 +02:00
{
addSlotToContainer( outputSlots[y] = new SlotPatternOutputs( output, this, y, 110, -76 + y * 18, 0, 0, 1 ) );
2014-04-10 06:51:08 +02:00
outputSlots[y].renderDisabled = false;
outputSlots[y].IIcon = -1;
}
2014-04-17 06:38:43 +02:00
2014-09-21 02:38:51 +02:00
addSlotToContainer( patternSlotIN = new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.BLANK_PATTERN, patternInv, 0, 147, -72 - 9, invPlayer ) );
addSlotToContainer( patternSlotOUT = new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.ENCODED_PATTERN, patternInv, 1, 147, -72 + 34, invPlayer ) );
2014-04-08 02:43:57 +02:00
2014-04-10 06:51:08 +02:00
patternSlotOUT.setStackLimit( 1 );
2014-04-08 02:43:57 +02:00
bindPlayerInventory( ip, 0, 0 );
2014-04-10 06:51:08 +02:00
updateOrderOfOutputSlots();
}
private void updateOrderOfOutputSlots()
{
if ( !craftingMode )
{
craftSlot.xDisplayPosition = -9000;
2014-04-10 06:51:08 +02:00
for (int y = 0; y < 3; y++)
outputSlots[y].xDisplayPosition = outputSlots[y].defX;
}
else
{
craftSlot.xDisplayPosition = craftSlot.defX;
for (int y = 0; y < 3; y++)
outputSlots[y].xDisplayPosition = -9000;
2014-04-10 06:51:08 +02:00
}
}
2014-04-17 06:38:43 +02:00
@Override
public void putStackInSlot(int par1, ItemStack par2ItemStack)
{
super.putStackInSlot( par1, par2ItemStack );
getAndUpdateOutput();
2014-04-17 06:38:43 +02:00
}
@Override
public void putStacksInSlots(ItemStack[] par1ArrayOfItemStack)
{
super.putStacksInSlots( par1ArrayOfItemStack );
getAndUpdateOutput();
2014-04-17 06:38:43 +02:00
}
public ItemStack getAndUpdateOutput()
2014-04-17 06:38:43 +02:00
{
InventoryCrafting ic = new InventoryCrafting( this, 3, 3 );
for (int x = 0; x < ic.getSizeInventory(); x++)
ic.setInventorySlotContents( x, crafting.getStackInSlot( x ) );
ItemStack is = CraftingManager.getInstance().findMatchingRecipe( ic, this.getPlayerInv().player.worldObj );
cOut.setInventorySlotContents( 0, is );
return is;
2014-04-17 06:38:43 +02:00
}
@GuiSync(97)
public boolean craftingMode = true;
2014-04-10 06:51:08 +02:00
@Override
public void detectAndSendChanges()
{
super.detectAndSendChanges();
2014-04-17 06:38:43 +02:00
if ( Platform.isServer() )
2014-04-10 06:51:08 +02:00
{
2014-07-02 03:43:08 +02:00
if ( craftingMode != ct.isCraftingRecipe() )
2014-04-10 06:51:08 +02:00
{
2014-07-02 03:43:08 +02:00
craftingMode = ct.isCraftingRecipe();
2014-04-17 06:38:43 +02:00
updateOrderOfOutputSlots();
2014-04-10 06:51:08 +02:00
}
}
}
@Override
public void onUpdate(String field, Object oldValue, Object newValue)
2014-04-10 06:51:08 +02:00
{
super.onUpdate( field, oldValue, newValue );
if ( field.equals( "craftingMode" ) )
2014-04-10 06:51:08 +02:00
{
getAndUpdateOutput();
2014-04-10 06:51:08 +02:00
updateOrderOfOutputSlots();
}
2014-04-08 02:43:57 +02:00
}
@Override
public void saveChanges()
{
}
2014-04-10 06:51:08 +02:00
public void encode()
{
ItemStack output = patternSlotOUT.getStack();
ItemStack[] in = getInputs();
ItemStack[] out = getOutputs();
// if there is no input, this would be silly.
if ( in == null || out == null )
return;
// first check the output slots, should either be null, or a pattern
if ( output != null && !isPattern( output ) )
return;
// if nothing is there we should snag a new pattern.
else if ( output == null )
{
output = patternSlotIN.getStack();
if ( output == null || !isPattern( output ) )
return; // no blanks.
// remove one, and clear the input slot.
output.stackSize--;
if ( output.stackSize == 0 )
patternSlotIN.putStack( null );
// add a new encoded pattern.
patternSlotOUT.putStack( output = AEApi.instance().items().itemEncodedPattern.stack( 1 ) );
}
// encode the slot.
NBTTagCompound encodedValue = new NBTTagCompound();
NBTTagList tagIn = new NBTTagList();
NBTTagList tagOut = new NBTTagList();
for (ItemStack i : in)
tagIn.appendTag( createItemTag( i ) );
for (ItemStack i : out)
tagOut.appendTag( createItemTag( i ) );
encodedValue.setTag( "in", tagIn );
encodedValue.setTag( "out", tagOut );
encodedValue.setBoolean( "crafting", craftingMode );
output.setTagCompound( encodedValue );
}
private NBTBase createItemTag(ItemStack i)
{
NBTTagCompound c = new NBTTagCompound();
if ( i != null )
i.writeToNBT( c );
return c;
}
private ItemStack[] getInputs()
{
ItemStack[] input = new ItemStack[9];
boolean hasValue = false;
for (int x = 0; x < craftingSlots.length; x++)
{
input[x] = craftingSlots[x].getStack();
if ( input[x] != null )
hasValue = true;
}
if ( hasValue )
return input;
return null;
}
private ItemStack[] getOutputs()
{
if ( craftingMode )
{
ItemStack out = getAndUpdateOutput();
if ( out != null && out.stackSize > 0 )
return new ItemStack[] { out };
}
else
{
List<ItemStack> list = new ArrayList( 3 );
boolean hasValue = false;
2014-04-10 06:51:08 +02:00
for (int x = 0; x < outputSlots.length; x++)
{
ItemStack out = outputSlots[x].getStack();
if ( out != null && out.stackSize > 0 )
{
list.add( out );
hasValue = true;
}
}
if ( hasValue )
return list.toArray( new ItemStack[list.size()] );
}
return null;
}
private boolean isPattern(ItemStack output)
{
if ( output == null )
return false;
return AEApi.instance().items().itemEncodedPattern.sameAsStack( output ) || AEApi.instance().materials().materialBlankPattern.sameAsStack( output );
2014-04-10 06:51:08 +02:00
}
@Override
public boolean isSlotEnabled(int idx)
{
if ( idx == 1 )
2014-07-02 03:43:08 +02:00
return Platform.isServer() ? ct.isCraftingRecipe() == false : craftingMode == false;
2014-04-10 06:51:08 +02:00
if ( idx == 2 )
2014-07-02 03:43:08 +02:00
return Platform.isServer() ? ct.isCraftingRecipe() == true : craftingMode == true;
2014-04-10 06:51:08 +02:00
return false;
}
2014-04-17 06:38:43 +02:00
@Override
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack)
{
}
public void craftOrGetItem(PacketPatternSlot packetPatternSlot)
{
if ( packetPatternSlot.slotItem != null && cellInv != null )
{
IAEItemStack out = packetPatternSlot.slotItem.copy();
InventoryAdaptor inv = new AdaptorPlayerHand( getPlayerInv().player );
InventoryAdaptor playerInv = InventoryAdaptor.getAdaptor( getPlayerInv().player, ForgeDirection.UNKNOWN );
if ( packetPatternSlot.shift )
inv = playerInv;
if ( inv.simulateAdd( out.getItemStack() ) != null )
return;
IAEItemStack extracted = Platform.poweredExtraction( powerSrc, cellInv, out, mySrc );
EntityPlayer p = getPlayerInv().player;
2014-04-17 06:38:43 +02:00
if ( extracted != null )
{
inv.addItems( extracted.getItemStack() );
if ( p instanceof EntityPlayerMP )
updateHeld( (EntityPlayerMP) p );
2014-04-17 06:38:43 +02:00
detectAndSendChanges();
return;
}
InventoryCrafting ic = new InventoryCrafting( new ContainerNull(), 3, 3 );
InventoryCrafting real = new InventoryCrafting( new ContainerNull(), 3, 3 );
for (int x = 0; x < 9; x++)
{
ic.setInventorySlotContents( x, packetPatternSlot.pattern[x] == null ? null : packetPatternSlot.pattern[x].getItemStack() );
}
IRecipe r = Platform.findMatchingRecipe( ic, p.worldObj );
if ( r == null )
return;
IMEMonitor<IAEItemStack> storage = ct.getItemInventory();
IItemList<IAEItemStack> all = storage.getStorageList();
ItemStack is = r.getCraftingResult( ic );
if ( r != null && inv != null )
{
for (int x = 0; x < ic.getSizeInventory(); x++)
{
if ( ic.getStackInSlot( x ) != null )
{
ItemStack pulled = Platform.extractItemsByRecipe( powerSrc, mySrc, storage, p.worldObj, r, is, ic, ic.getStackInSlot( x ), x, all,
Actionable.MODULATE, ItemViewCell.createFilter( getViewCells() ) );
2014-04-17 06:38:43 +02:00
real.setInventorySlotContents( x, pulled );
}
}
}
IRecipe rr = Platform.findMatchingRecipe( real, p.worldObj );
if ( rr == r && Platform.isSameItemPrecise( rr.getCraftingResult( real ), is ) )
{
SlotCrafting sc = new SlotCrafting( p, real, cOut, 0, 0, 0 );
sc.onPickupFromSlot( p, is );
for (int x = 0; x < real.getSizeInventory(); x++)
{
ItemStack failed = playerInv.addItems( real.getStackInSlot( x ) );
if ( failed != null )
p.dropPlayerItemWithRandomChoice( failed, false );
}
inv.addItems( is );
if ( p instanceof EntityPlayerMP )
updateHeld( (EntityPlayerMP) p );
2014-04-17 06:38:43 +02:00
detectAndSendChanges();
}
else
{
for (int x = 0; x < real.getSizeInventory(); x++)
{
ItemStack failed = real.getStackInSlot( x );
if ( failed != null )
{
cellInv.injectItems( AEItemStack.create( failed ), Actionable.MODULATE, new MachineSource( ct ) );
}
}
}
}
}
2014-07-02 03:43:08 +02:00
@Override
public void onSlotChange(Slot s)
2014-07-02 03:43:08 +02:00
{
if ( s == patternSlotOUT && Platform.isServer() )
2014-07-02 03:43:08 +02:00
{
for (int i = 0; i < this.crafters.size(); ++i)
2014-07-02 03:43:08 +02:00
{
ICrafting icrafting = (ICrafting) this.crafters.get( i );
2014-07-02 03:43:08 +02:00
for (Object g : inventorySlots)
{
if ( g instanceof OptionalSlotFake || g instanceof SlotFakeCraftingMatrix )
{
Slot sri = (Slot) g;
icrafting.sendSlotContents( this, sri.slotNumber, sri.getStack() );
}
}
((EntityPlayerMP) icrafting).isChangingQuantityOnly = false;
}
2014-07-02 03:43:08 +02:00
detectAndSendChanges();
}
2014-07-02 03:43:08 +02:00
}
public void clear()
{
for (Slot s : craftingSlots)
s.putStack( null );
for (Slot s : outputSlots)
s.putStack( null );
detectAndSendChanges();
getAndUpdateOutput();
}
@Override
public IInventory getInventoryByName(String name)
{
return ct.getInventoryByName( name );
}
@Override
public boolean useRealItems()
{
return false;
}
2014-04-08 02:43:57 +02:00
}