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-09-24 02:26:27 +02:00
|
|
|
package appeng.tile.grindstone;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-12-29 21:59:05 +01:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import appeng.api.AEApi;
|
|
|
|
import appeng.api.features.IGrinderEntry;
|
|
|
|
import appeng.api.implementations.tiles.ICrankable;
|
|
|
|
import appeng.api.util.WorldCoord;
|
|
|
|
import appeng.tile.AEBaseInvTile;
|
|
|
|
import appeng.tile.inventory.AppEngInternalInventory;
|
|
|
|
import appeng.tile.inventory.InvOperation;
|
|
|
|
import appeng.util.InventoryAdaptor;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
import appeng.util.inv.WrapperInventoryRange;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
public class TileGrinder extends AEBaseInvTile implements ICrankable
|
|
|
|
{
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private final int[] inputs = { 0, 1, 2 };
|
|
|
|
private final int[] sides = { 0, 1, 2, 3, 4, 5 };
|
|
|
|
private final AppEngInternalInventory inv = new AppEngInternalInventory( this, 7 );
|
|
|
|
private int points;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void setOrientation( final ForgeDirection inForward, final ForgeDirection inUp )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
super.setOrientation( inForward, inUp );
|
2015-05-08 23:37:09 +02:00
|
|
|
this.getBlockType().onNeighborBlockChange( this.worldObj, this.xCoord, this.yCoord, this.zCoord, Platform.AIR_BLOCK );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
|
|
|
public IInventory getInternalInventory()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return this.inv;
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void onChangeInventory( final IInventory inv, final int slot, final InvOperation mc, final ItemStack removed, final ItemStack added )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public boolean canInsertItem( final int slotIndex, final ItemStack insertingItem, final int side )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( AEApi.instance().registries().grinder().getRecipeForInput( insertingItem ) == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-03-22 11:07:25 +01:00
|
|
|
return slotIndex >= 0 && slotIndex <= 2;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public boolean canExtractItem( final int slotIndex, final ItemStack extractedItem, final int side )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-03-22 11:07:25 +01:00
|
|
|
return slotIndex >= 3 && slotIndex <= 5;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public int[] getAccessibleSlotsBySide( final ForgeDirection side )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.sides;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canTurn()
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( Platform.isClient() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( null == this.getStackInSlot( 6 ) ) // Add if there isn't one...
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final IInventory src = new WrapperInventoryRange( this, this.inputs, true );
|
2015-04-03 08:54:31 +02:00
|
|
|
for( int x = 0; x < src.getSizeInventory(); x++ )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
ItemStack item = src.getStackInSlot( x );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( item == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
continue;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
final IGrinderEntry r = AEApi.instance().registries().grinder().getRecipeForInput( item );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( r != null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( item.stackSize >= r.getInput().stackSize )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
item.stackSize -= r.getInput().stackSize;
|
2015-09-25 23:10:56 +02:00
|
|
|
final ItemStack ais = item.copy();
|
2014-09-24 02:26:27 +02:00
|
|
|
ais.stackSize = r.getInput().stackSize;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( item.stackSize <= 0 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
item = null;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
src.setInventorySlotContents( x, item );
|
|
|
|
this.setInventorySlotContents( 6, ais );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void applyTurn()
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( Platform.isClient() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.points++;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
final ItemStack processing = this.getStackInSlot( 6 );
|
|
|
|
final IGrinderEntry r = AEApi.instance().registries().grinder().getRecipeForInput( processing );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( r != null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( r.getEnergyCost() > this.points )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.points = 0;
|
2015-09-25 23:10:56 +02:00
|
|
|
final InventoryAdaptor sia = InventoryAdaptor.getAdaptor( new WrapperInventoryRange( this, 3, 3, true ), ForgeDirection.EAST );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.addItem( sia, r.getOutput() );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
float chance = ( Platform.getRandomInt() % 2000 ) / 2000.0f;
|
|
|
|
if( chance <= r.getOptionalChance() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.addItem( sia, r.getOptionalOutput() );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
chance = ( Platform.getRandomInt() % 2000 ) / 2000.0f;
|
|
|
|
if( chance <= r.getSecondOptionalChance() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.addItem( sia, r.getSecondOptionalOutput() );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
this.setInventorySlotContents( 6, null );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
private void addItem( final InventoryAdaptor sia, final ItemStack output )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
if( output == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
final ItemStack notAdded = sia.addItems( output );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( notAdded != null )
|
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final WorldCoord wc = new WorldCoord( this.xCoord, this.yCoord, this.zCoord );
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
wc.add( this.getForward(), 1 );
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
final List<ItemStack> out = new ArrayList<ItemStack>();
|
2015-04-03 08:54:31 +02:00
|
|
|
out.add( notAdded );
|
|
|
|
|
|
|
|
Platform.spawnDrops( this.worldObj, wc.x, wc.y, wc.z, out );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public boolean canCrankAttach( final ForgeDirection directionToCrank )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-01-01 21:16:04 +01:00
|
|
|
return this.getUp() == directionToCrank;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|