2017-05-01 13:32:39 +00:00
|
|
|
/**
|
|
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
|
|
* Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission.
|
|
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
package dan200.computercraft.shared.turtle.upgrades;
|
|
|
|
|
|
|
|
import dan200.computercraft.api.turtle.ITurtleAccess;
|
|
|
|
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
|
|
|
|
import dan200.computercraft.shared.turtle.core.TurtlePlayer;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.inventory.InventoryCrafting;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.item.crafting.CraftingManager;
|
2017-05-01 13:51:26 +00:00
|
|
|
import net.minecraft.util.text.TextComponentString;
|
|
|
|
import net.minecraft.util.text.ITextComponent;
|
2017-05-01 13:32:39 +00:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraft.world.WorldServer;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
public class TurtleInventoryCrafting extends InventoryCrafting
|
|
|
|
{
|
2017-05-01 14:48:44 +00:00
|
|
|
private ITurtleAccess m_turtle;
|
2017-05-01 13:32:39 +00:00
|
|
|
private int m_xStart;
|
|
|
|
private int m_yStart;
|
|
|
|
|
|
|
|
public TurtleInventoryCrafting( ITurtleAccess turtle )
|
|
|
|
{
|
2017-05-01 14:48:44 +00:00
|
|
|
super( null, 0, 0 );
|
|
|
|
m_turtle = turtle;
|
2017-05-01 13:32:39 +00:00
|
|
|
m_xStart = 0;
|
|
|
|
m_yStart = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ItemStack tryCrafting( int xStart, int yStart )
|
|
|
|
{
|
|
|
|
m_xStart = xStart;
|
|
|
|
m_yStart = yStart;
|
|
|
|
|
|
|
|
// Check the non-relevant parts of the inventory are empty
|
|
|
|
for( int x=0; x<TileTurtle.INVENTORY_WIDTH; ++x )
|
|
|
|
{
|
|
|
|
for( int y=0; y<TileTurtle.INVENTORY_HEIGHT; ++y )
|
|
|
|
{
|
|
|
|
if( x < m_xStart || x >= m_xStart + 3 ||
|
|
|
|
y < m_yStart || y >= m_yStart + 3 )
|
|
|
|
{
|
|
|
|
if( m_turtle.getInventory().getStackInSlot( x + y * TileTurtle.INVENTORY_WIDTH ) != null )
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the actual crafting
|
2017-05-01 14:48:44 +00:00
|
|
|
return CraftingManager.getInstance().findMatchingRecipe( this, m_turtle.getWorld() );
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<ItemStack> doCrafting( World world, int maxCount )
|
|
|
|
{
|
|
|
|
if( world.isRemote || !(world instanceof WorldServer) )
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
// Find out what we can craft
|
|
|
|
ItemStack result = tryCrafting( 0, 0 );
|
2017-05-01 13:32:39 +00:00
|
|
|
if( result == null )
|
|
|
|
{
|
|
|
|
result = tryCrafting( 0, 1 );
|
|
|
|
}
|
|
|
|
if( result == null )
|
|
|
|
{
|
|
|
|
result = tryCrafting( 1, 0 );
|
|
|
|
}
|
|
|
|
if( result == null )
|
|
|
|
{
|
|
|
|
result = tryCrafting( 1, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Craft it
|
2017-05-01 14:48:44 +00:00
|
|
|
if( result != null )
|
|
|
|
{
|
|
|
|
// Special case: craft(0) just returns an empty list if crafting was possible
|
2017-05-01 13:32:39 +00:00
|
|
|
ArrayList<ItemStack> results = new ArrayList<ItemStack>();
|
2017-05-01 14:48:44 +00:00
|
|
|
if( maxCount == 0 )
|
|
|
|
{
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find out how many we can craft
|
|
|
|
int numToCraft = 1;
|
|
|
|
int size = getSizeInventory();
|
|
|
|
if( maxCount > 1 )
|
|
|
|
{
|
|
|
|
int minStackSize = 0;
|
|
|
|
for( int n=0; n<size; ++n )
|
|
|
|
{
|
|
|
|
ItemStack stack = getStackInSlot( n );
|
|
|
|
if( stack != null && (minStackSize == 0 || minStackSize > stack.stackSize) )
|
|
|
|
{
|
|
|
|
minStackSize = stack.stackSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( minStackSize > 1 )
|
|
|
|
{
|
|
|
|
numToCraft = Math.min( minStackSize, result.getMaxStackSize() / result.stackSize );
|
|
|
|
numToCraft = Math.min( numToCraft, maxCount );
|
|
|
|
result.stackSize = result.stackSize * numToCraft;
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
|
|
|
|
// Do post-pickup stuff
|
2017-05-01 14:48:44 +00:00
|
|
|
TurtlePlayer turtlePlayer = new TurtlePlayer( (WorldServer)world );
|
2017-05-01 13:32:39 +00:00
|
|
|
result.onCrafting( world, turtlePlayer, numToCraft );
|
|
|
|
results.add( result );
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
// Consume resources from the inventory
|
2017-05-01 13:51:26 +00:00
|
|
|
ItemStack[] remainingItems = CraftingManager.getInstance().getRemainingItems( this, world );
|
2017-05-01 14:48:44 +00:00
|
|
|
for( int n=0; n<size; ++n )
|
|
|
|
{
|
|
|
|
ItemStack stack = getStackInSlot( n );
|
|
|
|
if( stack != null )
|
|
|
|
{
|
2017-05-01 13:32:39 +00:00
|
|
|
decrStackSize( n, numToCraft );
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
ItemStack replacement = remainingItems[n];
|
2017-05-01 13:32:39 +00:00
|
|
|
if( replacement != null )
|
|
|
|
{
|
|
|
|
if( !(replacement.isItemStackDamageable() && replacement.getItemDamage() >= replacement.getMaxDamage()) )
|
|
|
|
{
|
|
|
|
replacement.stackSize = Math.min( numToCraft, replacement.getMaxStackSize() );
|
|
|
|
if( getStackInSlot( n ) == null )
|
|
|
|
{
|
|
|
|
setInventorySlotContents( n, replacement );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
results.add( replacement );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 14:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
return results;
|
2017-05-01 14:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
2017-05-01 13:32:39 +00:00
|
|
|
public ItemStack getStackInRowAndColumn(int x, int y)
|
|
|
|
{
|
|
|
|
if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight() )
|
|
|
|
{
|
2017-05-01 14:48:44 +00:00
|
|
|
return getStackInSlot( x + y * getWidth() );
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getWidth()
|
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getHeight()
|
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int modifyIndex( int index )
|
|
|
|
{
|
|
|
|
int x = m_xStart + (index % getWidth());
|
|
|
|
int y = m_yStart + (index / getHeight());
|
|
|
|
if( x >= 0 && x < TileTurtle.INVENTORY_WIDTH &&
|
|
|
|
y >= 0 && y < TileTurtle.INVENTORY_HEIGHT )
|
|
|
|
{
|
|
|
|
return x + y * TileTurtle.INVENTORY_WIDTH;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IInventory implementation
|
2017-05-01 14:48:44 +00:00
|
|
|
|
|
|
|
@Override
|
2017-05-01 13:32:39 +00:00
|
|
|
public int getSizeInventory()
|
|
|
|
{
|
|
|
|
return getWidth() * getHeight();
|
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
2017-05-01 13:32:39 +00:00
|
|
|
public ItemStack getStackInSlot( int i )
|
|
|
|
{
|
|
|
|
i = modifyIndex( i );
|
2017-05-01 14:48:44 +00:00
|
|
|
return m_turtle.getInventory().getStackInSlot( i );
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName()
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasCustomName()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
2017-05-01 13:51:26 +00:00
|
|
|
public ITextComponent getDisplayName()
|
2017-05-01 13:32:39 +00:00
|
|
|
{
|
2017-05-01 13:51:26 +00:00
|
|
|
return new TextComponentString( "" );
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
2017-05-01 13:32:39 +00:00
|
|
|
public ItemStack removeStackFromSlot( int i )
|
|
|
|
{
|
|
|
|
i = modifyIndex( i );
|
2017-05-01 14:48:44 +00:00
|
|
|
return m_turtle.getInventory().removeStackFromSlot( i );
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
2017-05-01 13:32:39 +00:00
|
|
|
public ItemStack decrStackSize( int i, int size )
|
|
|
|
{
|
|
|
|
i = modifyIndex( i );
|
|
|
|
return m_turtle.getInventory().decrStackSize( i, size );
|
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
2017-05-01 13:32:39 +00:00
|
|
|
public void setInventorySlotContents( int i, ItemStack stack )
|
|
|
|
{
|
|
|
|
i = modifyIndex( i );
|
|
|
|
m_turtle.getInventory().setInventorySlotContents( i, stack );
|
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
2017-05-01 13:32:39 +00:00
|
|
|
public int getInventoryStackLimit()
|
|
|
|
{
|
|
|
|
return m_turtle.getInventory().getInventoryStackLimit();
|
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
2017-05-01 13:32:39 +00:00
|
|
|
public void markDirty()
|
|
|
|
{
|
|
|
|
m_turtle.getInventory().markDirty();
|
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
2017-05-01 13:32:39 +00:00
|
|
|
public boolean isUseableByPlayer( EntityPlayer player )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
@Override
|
2017-05-01 13:32:39 +00:00
|
|
|
public void openInventory( EntityPlayer player )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void closeInventory( EntityPlayer player )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot( int i, ItemStack stack )
|
|
|
|
{
|
|
|
|
i = modifyIndex( i );
|
|
|
|
return m_turtle.getInventory().isItemValidForSlot( i, stack );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getField( int id )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setField( int id, int value )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getFieldCount()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clear()
|
|
|
|
{
|
|
|
|
for( int i=0; i<getSizeInventory(); ++i )
|
|
|
|
{
|
|
|
|
int j = modifyIndex( i );
|
|
|
|
m_turtle.getInventory().setInventorySlotContents( j, null );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|