2014-02-05 08:25:44 +01:00
|
|
|
package appeng.container.slot;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.inventory.InventoryCrafting;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.item.crafting.IRecipe;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import appeng.api.config.Actionable;
|
|
|
|
import appeng.api.config.PowerMultiplier;
|
|
|
|
import appeng.api.networking.energy.IEnergySource;
|
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
|
|
|
import appeng.api.storage.IMEMonitor;
|
|
|
|
import appeng.api.storage.IStorageMonitorable;
|
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
|
|
|
import appeng.container.ContainerNull;
|
|
|
|
import appeng.helpers.InventoryAction;
|
|
|
|
import appeng.util.InventoryAdaptor;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
import appeng.util.inv.AdaptorPlayerHand;
|
|
|
|
import appeng.util.item.AEItemStack;
|
|
|
|
|
|
|
|
public class SlotCraftingTerm extends AppEngCraftingSlot
|
|
|
|
{
|
|
|
|
|
2014-04-11 05:43:18 +02:00
|
|
|
private final IInventory craftInv;
|
|
|
|
private final IInventory pattern;
|
2014-02-05 08:25:44 +01:00
|
|
|
|
|
|
|
private final BaseActionSource mySrc;
|
|
|
|
private final IEnergySource energySrc;
|
|
|
|
private final IStorageMonitorable storage;
|
|
|
|
|
|
|
|
public SlotCraftingTerm(EntityPlayer player, BaseActionSource mySrc, IEnergySource energySrc, IStorageMonitorable storage, IInventory cMatrix,
|
2014-04-11 05:43:18 +02:00
|
|
|
IInventory secondMatrix, IInventory output, int x, int y) {
|
2014-02-05 08:25:44 +01:00
|
|
|
super( player, cMatrix, output, 0, x, y );
|
|
|
|
this.energySrc = energySrc;
|
|
|
|
this.storage = storage;
|
|
|
|
this.mySrc = mySrc;
|
2014-04-11 05:43:18 +02:00
|
|
|
pattern = cMatrix;
|
|
|
|
craftInv = secondMatrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IInventory getCraftingMatrix()
|
|
|
|
{
|
|
|
|
return craftInv;
|
2014-02-05 08:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canTakeStack(EntityPlayer par1EntityPlayer)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPickupFromSlot(EntityPlayer p, ItemStack is)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private ItemStack extractItemsByRecipe(IMEMonitor<IAEItemStack> src, World w, IRecipe r, ItemStack output, InventoryCrafting ci,
|
|
|
|
ItemStack providedTemplate, int slot, IItemList<IAEItemStack> aitems)
|
|
|
|
{
|
|
|
|
if ( energySrc.extractAEPower( 1, Actionable.SIMULATE, PowerMultiplier.CONFIG ) > 0.9 )
|
|
|
|
{
|
|
|
|
if ( providedTemplate == null )
|
|
|
|
return null;
|
|
|
|
|
|
|
|
AEItemStack ae_req = AEItemStack.create( providedTemplate );
|
|
|
|
ae_req.setStackSize( 1 );
|
|
|
|
|
|
|
|
IAEItemStack ae_ext = src.extractItems( ae_req, Actionable.MODULATE, mySrc );
|
|
|
|
if ( ae_ext != null )
|
|
|
|
{
|
|
|
|
ItemStack extracted = ae_ext.getItemStack();
|
|
|
|
if ( extracted != null )
|
|
|
|
{
|
|
|
|
energySrc.extractAEPower( 1, Actionable.MODULATE, PowerMultiplier.CONFIG );
|
|
|
|
return extracted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( aitems != null && (ae_req.isOre() || providedTemplate.hasTagCompound() || providedTemplate.isItemStackDamageable()) )
|
|
|
|
{
|
|
|
|
for (IAEItemStack x : aitems)
|
|
|
|
{
|
|
|
|
ItemStack sh = x.getItemStack();
|
|
|
|
if ( (Platform.isSameItemType( providedTemplate, sh ) || ae_req.sameOre( x )) && !Platform.isSameItem( sh, output ) )
|
|
|
|
{ // Platform.isSameItemType( sh, providedTemplate )
|
|
|
|
ItemStack cp = Platform.cloneItemStack( sh );
|
|
|
|
cp.stackSize = 1;
|
|
|
|
ci.setInventorySlotContents( slot, cp );
|
|
|
|
if ( r.matches( ci, w ) && Platform.isSameItem( r.getCraftingResult( ci ), output ) )
|
|
|
|
{
|
|
|
|
IAEItemStack ex = src.extractItems( AEItemStack.create( cp ), Actionable.MODULATE, mySrc );
|
|
|
|
if ( ex != null )
|
|
|
|
{
|
|
|
|
energySrc.extractAEPower( 1, Actionable.MODULATE, PowerMultiplier.CONFIG );
|
|
|
|
return ex.getItemStack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ci.setInventorySlotContents( slot, providedTemplate );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-04-11 05:43:18 +02:00
|
|
|
public ItemStack craftItem(EntityPlayer p, ItemStack request, IMEMonitor<IAEItemStack> inv, IItemList all)
|
2014-02-05 08:25:44 +01:00
|
|
|
{
|
|
|
|
// update crafting matrx...
|
|
|
|
ItemStack is = getStack();
|
|
|
|
|
|
|
|
if ( is != null && Platform.isSameItem( request, is ) )
|
|
|
|
{
|
2014-04-11 05:43:18 +02:00
|
|
|
ItemStack[] set = new ItemStack[pattern.getSizeInventory()];
|
2014-02-05 08:25:44 +01:00
|
|
|
|
|
|
|
// add one of each item to the items on the board...
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
{
|
|
|
|
InventoryCrafting ic = new InventoryCrafting( new ContainerNull(), 3, 3 );
|
|
|
|
for (int x = 0; x < 9; x++)
|
2014-04-11 05:43:18 +02:00
|
|
|
ic.setInventorySlotContents( x, pattern.getStackInSlot( x ) );
|
2014-02-05 08:25:44 +01:00
|
|
|
|
|
|
|
IRecipe r = Platform.findMatchingRecipe( ic, p.worldObj );
|
|
|
|
|
|
|
|
if ( r == null )
|
|
|
|
return null;
|
|
|
|
|
|
|
|
is = r.getCraftingResult( ic );
|
|
|
|
|
|
|
|
if ( r != null && inv != null )
|
|
|
|
{
|
2014-04-11 05:43:18 +02:00
|
|
|
for (int x = 0; x < pattern.getSizeInventory(); x++)
|
2014-02-05 08:25:44 +01:00
|
|
|
{
|
2014-04-11 05:43:18 +02:00
|
|
|
if ( pattern.getStackInSlot( x ) != null )
|
2014-02-05 08:25:44 +01:00
|
|
|
{
|
2014-04-11 05:43:18 +02:00
|
|
|
set[x] = extractItemsByRecipe( inv, p.worldObj, r, is, ic, pattern.getStackInSlot( x ), x, all );
|
2014-02-05 08:25:44 +01:00
|
|
|
ic.setInventorySlotContents( x, set[x] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-11 05:43:18 +02:00
|
|
|
if ( preCraft( p, inv, set, is ) )
|
2014-02-05 08:25:44 +01:00
|
|
|
{
|
2014-04-11 05:43:18 +02:00
|
|
|
super.onPickupFromSlot( p, is );
|
|
|
|
|
|
|
|
postCraft( p, inv, set, is );
|
2014-02-05 08:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// shouldn't be nessiary...
|
2014-04-11 05:43:18 +02:00
|
|
|
p.openContainer.onCraftMatrixChanged( getCraftingMatrix() );
|
2014-02-05 08:25:44 +01:00
|
|
|
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-04-11 05:43:18 +02:00
|
|
|
public boolean preCraft(EntityPlayer p, IMEMonitor<IAEItemStack> inv, ItemStack[] set, ItemStack result)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void postCraft(EntityPlayer p, IMEMonitor<IAEItemStack> inv, ItemStack set[], ItemStack result)
|
|
|
|
{
|
|
|
|
List<ItemStack> drops = new ArrayList();
|
|
|
|
|
|
|
|
// add one of each item to the items on the board...
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
{
|
|
|
|
// set new items onto the crafting table...
|
|
|
|
for (int x = 0; x < getCraftingMatrix().getSizeInventory(); x++)
|
|
|
|
{
|
|
|
|
if ( getCraftingMatrix().getStackInSlot( x ) == null )
|
|
|
|
getCraftingMatrix().setInventorySlotContents( x, set[x] );
|
|
|
|
else if ( set[x] != null )
|
|
|
|
{
|
|
|
|
// eek! put it back!
|
|
|
|
IAEItemStack fail = inv.injectItems( AEItemStack.create( set[x] ), Actionable.MODULATE, mySrc );
|
|
|
|
if ( fail != null )
|
|
|
|
drops.add( fail.getItemStack() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( drops.size() > 0 )
|
|
|
|
Platform.spawnDrops( p.worldObj, (int) p.posX, (int) p.posY, (int) p.posZ, drops );
|
|
|
|
}
|
|
|
|
|
2014-02-05 08:25:44 +01:00
|
|
|
public void doClick(InventoryAction action, EntityPlayer who)
|
|
|
|
{
|
|
|
|
if ( getStack() == null )
|
|
|
|
return;
|
|
|
|
if ( Platform.isClient() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
IMEMonitor<IAEItemStack> inv = storage.getItemInventory();
|
|
|
|
int howManyPerCraft = getStack().stackSize;
|
|
|
|
int maxTimesToCraft = 0;
|
|
|
|
|
|
|
|
InventoryAdaptor ia = null;
|
|
|
|
if ( action == InventoryAction.CRAFT_SHIFT ) // craft into player inventory...
|
|
|
|
{
|
|
|
|
ia = InventoryAdaptor.getAdaptor( who, null );
|
|
|
|
maxTimesToCraft = (int) Math.floor( (double) getStack().getMaxStackSize() / (double) howManyPerCraft );
|
|
|
|
}
|
|
|
|
else if ( action == InventoryAction.CRAFT_STACK ) // craft into hand, full stack
|
|
|
|
{
|
|
|
|
ia = new AdaptorPlayerHand( who );
|
|
|
|
maxTimesToCraft = (int) Math.floor( (double) getStack().getMaxStackSize() / (double) howManyPerCraft );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// pick up what was crafted...
|
|
|
|
{
|
|
|
|
ia = new AdaptorPlayerHand( who );
|
|
|
|
maxTimesToCraft = 1;
|
|
|
|
}
|
|
|
|
|
2014-04-11 05:43:18 +02:00
|
|
|
maxTimesToCraft = CapCraftingAttempts( maxTimesToCraft );
|
|
|
|
|
2014-02-05 08:25:44 +01:00
|
|
|
if ( ia == null )
|
|
|
|
return;
|
|
|
|
|
|
|
|
ItemStack rs = Platform.cloneItemStack( getStack() );
|
|
|
|
if ( rs == null )
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (int x = 0; x < maxTimesToCraft; x++)
|
|
|
|
{
|
|
|
|
if ( ia.simulateAdd( rs ) == null )
|
|
|
|
{
|
|
|
|
IItemList<IAEItemStack> all = inv.getStorageList();
|
|
|
|
ItemStack extra = ia.addItems( craftItem( who, rs, inv, all ) );
|
|
|
|
if ( extra != null )
|
|
|
|
{
|
|
|
|
List<ItemStack> drops = new ArrayList();
|
|
|
|
drops.add( extra );
|
|
|
|
Platform.spawnDrops( who.worldObj, (int) who.posX, (int) who.posY, (int) who.posZ, drops );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-11 05:43:18 +02:00
|
|
|
protected int CapCraftingAttempts(int maxTimesToCraft)
|
|
|
|
{
|
|
|
|
return maxTimesToCraft;
|
|
|
|
}
|
|
|
|
|
2014-02-05 08:25:44 +01:00
|
|
|
}
|