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

523 lines
14 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
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;
2014-12-29 21:59:05 +01:00
import appeng.api.AEApi;
2014-04-17 06:38:43 +02:00
import appeng.api.config.Actionable;
import appeng.api.definitions.IDefinitions;
2014-04-17 06:38:43 +02:00
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
{
public final PartPatternTerminal ct;
final AppEngInternalInventory cOut = new AppEngInternalInventory( null, 1 );
final IInventory crafting;
final SlotFakeCraftingMatrix[] craftingSlots = new SlotFakeCraftingMatrix[9];
final OptionalSlotFake[] outputSlots = new OptionalSlotFake[3];
final SlotPatternTerm craftSlot;
final SlotRestrictedInput patternSlotIN;
final SlotRestrictedInput patternSlotOUT;
@GuiSync( 97 )
public boolean craftingMode = true;
2014-04-10 06:51:08 +02:00
public ContainerPatternTerm( InventoryPlayer ip, ITerminalHost monitorable )
{
2014-09-21 01:06:22 +02:00
super( ip, monitorable, false );
2014-12-29 15:13:47 +01:00
this.ct = (PartPatternTerminal) monitorable;
2014-04-08 02:43:57 +02:00
2014-12-29 15:13:47 +01:00
IInventory patternInv = this.ct.getInventoryByName( "pattern" );
IInventory output = this.ct.getInventoryByName( "output" );
this.crafting = this.ct.getInventoryByName( "crafting" );
2014-04-08 02:43:57 +02:00
for( int y = 0; y < 3; y++ )
2015-04-29 02:30:53 +02:00
{
for( int x = 0; x < 3; x++ )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.addSlotToContainer( this.craftingSlots[x + y * 3] = new SlotFakeCraftingMatrix( this.crafting, x + y * 3, 18 + x * 18, -76 + y * 18 ) );
2015-04-29 02:30:53 +02:00
}
}
2014-04-10 06:51:08 +02:00
2014-12-29 15:13:47 +01:00
this.addSlotToContainer( this.craftSlot = new SlotPatternTerm( ip.player, this.mySrc, this.powerSrc, monitorable, this.crafting, patternInv, this.cOut, 110, -76 + 18, this, 2, this ) );
this.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
{
2014-12-29 15:13:47 +01:00
this.addSlotToContainer( this.outputSlots[y] = new SlotPatternOutputs( output, this, y, 110, -76 + y * 18, 0, 0, 1 ) );
this.outputSlots[y].renderDisabled = false;
this.outputSlots[y].IIcon = -1;
2014-04-10 06:51:08 +02:00
}
2014-04-17 06:38:43 +02:00
2014-12-29 15:13:47 +01:00
this.addSlotToContainer( this.patternSlotIN = new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.BLANK_PATTERN, patternInv, 0, 147, -72 - 9, this.invPlayer ) );
this.addSlotToContainer( this.patternSlotOUT = new SlotRestrictedInput( SlotRestrictedInput.PlacableItemType.ENCODED_PATTERN, patternInv, 1, 147, -72 + 34, this.invPlayer ) );
2014-04-08 02:43:57 +02:00
2014-12-29 15:13:47 +01:00
this.patternSlotOUT.setStackLimit( 1 );
2014-04-08 02:43:57 +02:00
2014-12-29 15:13:47 +01:00
this.bindPlayerInventory( ip, 0, 0 );
this.updateOrderOfOutputSlots();
2014-04-10 06:51:08 +02:00
}
private void updateOrderOfOutputSlots()
{
if( !this.craftingMode )
2014-04-10 06:51:08 +02:00
{
2014-12-29 15:13:47 +01:00
this.craftSlot.xDisplayPosition = -9000;
2014-04-10 06:51:08 +02:00
for( int y = 0; y < 3; y++ )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.outputSlots[y].xDisplayPosition = this.outputSlots[y].defX;
2015-04-29 02:30:53 +02:00
}
2014-04-10 06:51:08 +02:00
}
else
{
2014-12-29 15:13:47 +01:00
this.craftSlot.xDisplayPosition = this.craftSlot.defX;
2014-04-10 06:51:08 +02:00
for( int y = 0; y < 3; y++ )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.outputSlots[y].xDisplayPosition = -9000;
2015-04-29 02:30:53 +02:00
}
2014-04-10 06:51:08 +02:00
}
}
2014-04-17 06:38:43 +02:00
@Override
public void putStackInSlot( int par1, ItemStack par2ItemStack )
2014-04-17 06:38:43 +02:00
{
super.putStackInSlot( par1, par2ItemStack );
2014-12-29 15:13:47 +01:00
this.getAndUpdateOutput();
2014-04-17 06:38:43 +02:00
}
@Override
public void putStacksInSlots( ItemStack[] par1ArrayOfItemStack )
2014-04-17 06:38:43 +02:00
{
super.putStacksInSlots( par1ArrayOfItemStack );
2014-12-29 15:13:47 +01:00
this.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++ )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
ic.setInventorySlotContents( x, this.crafting.getStackInSlot( x ) );
2015-04-29 02:30:53 +02:00
}
2014-04-17 06:38:43 +02:00
ItemStack is = CraftingManager.getInstance().findMatchingRecipe( ic, this.getPlayerInv().player.worldObj );
2014-12-29 15:13:47 +01:00
this.cOut.setInventorySlotContents( 0, is );
return is;
2014-04-17 06:38:43 +02:00
}
2014-04-10 06:51:08 +02:00
@Override
public void saveChanges()
2014-04-10 06:51:08 +02:00
{
2014-04-08 02:43:57 +02:00
}
@Override
public void onChangeInventory( IInventory inv, int slot, InvOperation mc, ItemStack removedStack, ItemStack newStack )
2014-04-08 02:43:57 +02:00
{
}
2014-04-10 06:51:08 +02:00
public void encode()
{
2014-12-29 15:13:47 +01:00
ItemStack output = this.patternSlotOUT.getStack();
2014-12-29 15:13:47 +01:00
ItemStack[] in = this.getInputs();
ItemStack[] out = this.getOutputs();
// if there is no input, this would be silly.
if( in == null || out == null )
2015-04-29 02:30:53 +02:00
{
return;
2015-04-29 02:30:53 +02:00
}
// first check the output slots, should either be null, or a pattern
if( output != null && !this.isPattern( output ) )
2015-04-29 02:30:53 +02:00
{
return;
2015-04-29 02:30:53 +02:00
}// if nothing is there we should snag a new pattern.
else if( output == null )
{
2014-12-29 15:13:47 +01:00
output = this.patternSlotIN.getStack();
if( output == null || !this.isPattern( output ) )
2015-04-29 02:30:53 +02:00
{
return; // no blanks.
2015-04-29 02:30:53 +02:00
}
// remove one, and clear the input slot.
output.stackSize--;
if( output.stackSize == 0 )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.patternSlotIN.putStack( null );
2015-04-29 02:30:53 +02:00
}
// add a new encoded pattern.
for( ItemStack encodedPatternStack : AEApi.instance().definitions().items().encodedPattern().maybeStack( 1 ).asSet() )
{
output = encodedPatternStack;
this.patternSlotOUT.putStack( output );
}
}
// encode the slot.
NBTTagCompound encodedValue = new NBTTagCompound();
NBTTagList tagIn = new NBTTagList();
NBTTagList tagOut = new NBTTagList();
for( ItemStack i : in )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
tagIn.appendTag( this.createItemTag( i ) );
2015-04-29 02:30:53 +02:00
}
for( ItemStack i : out )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
tagOut.appendTag( this.createItemTag( i ) );
2015-04-29 02:30:53 +02:00
}
encodedValue.setTag( "in", tagIn );
encodedValue.setTag( "out", tagOut );
2014-12-29 15:13:47 +01:00
encodedValue.setBoolean( "crafting", this.craftingMode );
output.setTagCompound( encodedValue );
}
private ItemStack[] getInputs()
{
ItemStack[] input = new ItemStack[9];
boolean hasValue = false;
for( int x = 0; x < this.craftingSlots.length; x++ )
{
2014-12-29 15:13:47 +01:00
input[x] = this.craftingSlots[x].getStack();
if( input[x] != null )
2015-04-29 02:30:53 +02:00
{
hasValue = true;
2015-04-29 02:30:53 +02:00
}
}
if( hasValue )
2015-04-29 02:30:53 +02:00
{
return input;
2015-04-29 02:30:53 +02:00
}
return null;
}
private ItemStack[] getOutputs()
{
if( this.craftingMode )
{
2014-12-29 15:13:47 +01:00
ItemStack out = this.getAndUpdateOutput();
if( out != null && out.stackSize > 0 )
2015-04-29 02:30:53 +02:00
{
return new ItemStack[] { out };
2015-04-29 02:30:53 +02:00
}
}
else
{
2014-09-28 22:20:14 +02:00
List<ItemStack> list = new ArrayList<ItemStack>( 3 );
boolean hasValue = false;
2014-04-10 06:51:08 +02:00
for( OptionalSlotFake outputSlot : this.outputSlots )
{
ItemStack out = outputSlot.getStack();
if( out != null && out.stackSize > 0 )
{
list.add( out );
hasValue = true;
}
}
if( hasValue )
2015-04-29 02:30:53 +02:00
{
return list.toArray( new ItemStack[list.size()] );
2015-04-29 02:30:53 +02:00
}
}
return null;
}
private boolean isPattern( ItemStack output )
{
if( output == null )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
final IDefinitions definitions = AEApi.instance().definitions();
boolean isPattern = definitions.items().encodedPattern().isSameAs( output );
isPattern |= definitions.materials().blankPattern().isSameAs( output );
return isPattern;
2014-04-10 06:51:08 +02:00
}
private NBTBase createItemTag( ItemStack i )
{
NBTTagCompound c = new NBTTagCompound();
if( i != null )
2015-04-29 02:30:53 +02:00
{
i.writeToNBT( c );
2015-04-29 02:30:53 +02:00
}
return c;
}
2014-04-10 06:51:08 +02:00
@Override
public boolean isSlotEnabled( int idx )
2014-04-10 06:51:08 +02:00
{
if( idx == 1 )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
return Platform.isServer() ? !this.ct.isCraftingRecipe() : !this.craftingMode;
2015-04-29 02:30:53 +02:00
}
else if( idx == 2 )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
return Platform.isServer() ? this.ct.isCraftingRecipe() : this.craftingMode;
2015-04-29 02:30:53 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
2014-04-10 06:51:08 +02:00
}
2014-04-17 06:38:43 +02:00
public void craftOrGetItem( PacketPatternSlot packetPatternSlot )
2014-04-17 06:38:43 +02:00
{
if( packetPatternSlot.slotItem != null && this.cellInv != null )
2014-04-17 06:38:43 +02:00
{
IAEItemStack out = packetPatternSlot.slotItem.copy();
2014-12-29 15:13:47 +01:00
InventoryAdaptor inv = new AdaptorPlayerHand( this.getPlayerInv().player );
InventoryAdaptor playerInv = InventoryAdaptor.getAdaptor( this.getPlayerInv().player, ForgeDirection.UNKNOWN );
if( packetPatternSlot.shift )
2015-04-29 02:30:53 +02:00
{
2014-04-17 06:38:43 +02:00
inv = playerInv;
2015-04-29 02:30:53 +02:00
}
2014-04-17 06:38:43 +02:00
if( inv.simulateAdd( out.getItemStack() ) != null )
2015-04-29 02:30:53 +02:00
{
2014-04-17 06:38:43 +02:00
return;
2015-04-29 02:30:53 +02:00
}
2014-04-17 06:38:43 +02:00
2014-12-29 15:13:47 +01:00
IAEItemStack extracted = Platform.poweredExtraction( this.powerSrc, this.cellInv, out, this.mySrc );
EntityPlayer p = this.getPlayerInv().player;
if( extracted != null )
2014-04-17 06:38:43 +02:00
{
inv.addItems( extracted.getItemStack() );
if( p instanceof EntityPlayerMP )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.updateHeld( (EntityPlayerMP) p );
2015-04-29 02:30:53 +02:00
}
2014-12-29 15:13:47 +01:00
this.detectAndSendChanges();
2014-04-17 06:38:43 +02:00
return;
}
InventoryCrafting ic = new InventoryCrafting( new ContainerNull(), 3, 3 );
InventoryCrafting real = new InventoryCrafting( new ContainerNull(), 3, 3 );
for( int x = 0; x < 9; x++ )
2014-04-17 06:38:43 +02:00
{
ic.setInventorySlotContents( x, packetPatternSlot.pattern[x] == null ? null : packetPatternSlot.pattern[x].getItemStack() );
}
IRecipe r = Platform.findMatchingRecipe( ic, p.worldObj );
if( r == null )
2015-04-29 02:30:53 +02:00
{
2014-04-17 06:38:43 +02:00
return;
2015-04-29 02:30:53 +02:00
}
2014-04-17 06:38:43 +02:00
2014-12-29 15:13:47 +01:00
IMEMonitor<IAEItemStack> storage = this.ct.getItemInventory();
2014-04-17 06:38:43 +02:00
IItemList<IAEItemStack> all = storage.getStorageList();
ItemStack is = r.getCraftingResult( ic );
for( int x = 0; x < ic.getSizeInventory(); x++ )
2014-04-17 06:38:43 +02:00
{
if( ic.getStackInSlot( x ) != null )
2014-04-17 06:38:43 +02:00
{
ItemStack pulled = Platform.extractItemsByRecipe( this.powerSrc, this.mySrc, storage, p.worldObj, r, is, ic, ic.getStackInSlot( x ), x, all, Actionable.MODULATE, ItemViewCell.createFilter( this.getViewCells() ) );
2014-09-28 19:41:51 +02:00
real.setInventorySlotContents( x, pulled );
2014-04-17 06:38:43 +02:00
}
}
IRecipe rr = Platform.findMatchingRecipe( real, p.worldObj );
if( rr == r && Platform.isSameItemPrecise( rr.getCraftingResult( real ), is ) )
2014-04-17 06:38:43 +02:00
{
2014-12-29 15:13:47 +01:00
SlotCrafting sc = new SlotCrafting( p, real, this.cOut, 0, 0, 0 );
2014-04-17 06:38:43 +02:00
sc.onPickupFromSlot( p, is );
for( int x = 0; x < real.getSizeInventory(); x++ )
2014-04-17 06:38:43 +02:00
{
ItemStack failed = playerInv.addItems( real.getStackInSlot( x ) );
if( failed != null )
2015-04-29 02:30:53 +02:00
{
2014-04-17 06:38:43 +02:00
p.dropPlayerItemWithRandomChoice( failed, false );
2015-04-29 02:30:53 +02:00
}
2014-04-17 06:38:43 +02:00
}
inv.addItems( is );
if( p instanceof EntityPlayerMP )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.updateHeld( (EntityPlayerMP) p );
2015-04-29 02:30:53 +02:00
}
2014-12-29 15:13:47 +01:00
this.detectAndSendChanges();
2014-04-17 06:38:43 +02:00
}
else
{
for( int x = 0; x < real.getSizeInventory(); x++ )
2014-04-17 06:38:43 +02:00
{
ItemStack failed = real.getStackInSlot( x );
if( failed != null )
2014-04-17 06:38:43 +02:00
{
2014-12-29 15:13:47 +01:00
this.cellInv.injectItems( AEItemStack.create( failed ), Actionable.MODULATE, new MachineSource( this.ct ) );
2014-04-17 06:38:43 +02:00
}
}
}
}
}
@Override
public void detectAndSendChanges()
{
super.detectAndSendChanges();
if( Platform.isServer() )
{
if( this.craftingMode != this.ct.isCraftingRecipe() )
{
this.craftingMode = this.ct.isCraftingRecipe();
this.updateOrderOfOutputSlots();
}
}
}
2014-04-17 06:38:43 +02:00
@Override
public void onUpdate( String field, Object oldValue, Object newValue )
{
super.onUpdate( field, oldValue, newValue );
if( field.equals( "craftingMode" ) )
{
this.getAndUpdateOutput();
this.updateOrderOfOutputSlots();
2014-04-17 06:38:43 +02:00
}
}
2014-07-02 03:43:08 +02:00
@Override
public void onSlotChange( Slot s )
2014-07-02 03:43:08 +02:00
{
if( s == this.patternSlotOUT && Platform.isServer() )
2014-07-02 03:43:08 +02:00
{
for( Object crafter : this.crafters )
2014-07-02 03:43:08 +02:00
{
ICrafting icrafting = (ICrafting) crafter;
2014-07-02 03:43:08 +02:00
for( Object g : this.inventorySlots )
{
if( g instanceof OptionalSlotFake || g instanceof SlotFakeCraftingMatrix )
{
Slot sri = (Slot) g;
icrafting.sendSlotContents( this, sri.slotNumber, sri.getStack() );
}
}
( (EntityPlayerMP) icrafting ).isChangingQuantityOnly = false;
}
2014-12-29 15:13:47 +01:00
this.detectAndSendChanges();
}
2014-07-02 03:43:08 +02:00
}
public void clear()
{
for( Slot s : this.craftingSlots )
2015-04-29 02:30:53 +02:00
{
s.putStack( null );
2015-04-29 02:30:53 +02:00
}
for( Slot s : this.outputSlots )
2015-04-29 02:30:53 +02:00
{
s.putStack( null );
2015-04-29 02:30:53 +02:00
}
2014-12-29 15:13:47 +01:00
this.detectAndSendChanges();
this.getAndUpdateOutput();
}
@Override
public IInventory getInventoryByName( String name )
{
if( name.equals( "player" ) )
{
2014-12-29 15:13:47 +01:00
return this.invPlayer;
}
2014-12-29 15:13:47 +01:00
return this.ct.getInventoryByName( name );
}
@Override
public boolean useRealItems()
{
return false;
}
2014-04-08 02:43:57 +02:00
}