Applied-Energistics-2-tiler.../src/main/java/appeng/tile/grindstone/TileGrinder.java

207 lines
5.2 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-09-24 02:26:27 +02:00
package appeng.tile.grindstone;
2014-09-24 02:26:27 +02:00
import java.util.ArrayList;
import java.util.List;
2015-06-16 02:44:59 +02:00
import net.minecraft.block.state.IBlockState;
2014-09-24 02:26:27 +02:00
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.EnumFacing;
2015-12-24 02:07:03 +01:00
2014-09-24 02:26:27 +02:00
import appeng.api.AEApi;
import appeng.api.features.IGrinderRecipe;
2014-09-24 02:26:27 +02:00
import appeng.api.implementations.tiles.ICrankable;
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;
2014-09-24 02:26:27 +02:00
public class TileGrinder extends AEBaseInvTile implements ICrankable
{
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-30 14:24:40 +02:00
public void setOrientation( final EnumFacing inForward, final EnumFacing inUp )
2014-09-24 02:26:27 +02:00
{
super.setOrientation( inForward, inUp );
2015-09-30 14:26:54 +02:00
final IBlockState state = this.worldObj.getBlockState( this.pos );
this.getBlockType().neighborChanged( state, this.worldObj, this.pos, state.getBlock() );
2014-09-24 02:26:27 +02:00
}
@Override
public IInventory getInternalInventory()
2014-09-24 02:26:27 +02:00
{
return this.inv;
}
2014-09-24 02:26:27 +02:00
@Override
2015-09-30 14:24:40 +02:00
public void onChangeInventory( final IInventory inv, final int slot, final InvOperation mc, final ItemStack removed, final ItemStack added )
{
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean canInsertItem( final int slotIndex, final ItemStack insertingItem, final EnumFacing side )
2014-09-24 02:26:27 +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
return slotIndex >= 0 && slotIndex <= 2;
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean canExtractItem( final int slotIndex, final ItemStack extractedItem, final EnumFacing side )
2014-09-24 02:26:27 +02:00
{
return slotIndex >= 3 && slotIndex <= 5;
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public int[] getAccessibleSlotsBySide( final EnumFacing 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()
{
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
if( null == this.getStackInSlot( 6 ) ) // Add if there isn't one...
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final IInventory src = new WrapperInventoryRange( this, this.inputs, true );
for( int x = 0; x < src.getSizeInventory(); x++ )
2014-09-24 02:26:27 +02:00
{
ItemStack item = src.getStackInSlot( x );
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
final IGrinderRecipe r = AEApi.instance().registries().grinder().getRecipeForInput( item );
if( r != null )
2014-09-24 02:26:27 +02:00
{
if( item.stackSize >= r.getInput().stackSize )
2014-09-24 02:26:27 +02:00
{
item.stackSize -= r.getInput().stackSize;
2015-09-30 14:24:40 +02:00
final ItemStack ais = item.copy();
2014-09-24 02:26:27 +02:00
ais.stackSize = r.getInput().stackSize;
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()
{
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-30 14:24:40 +02:00
final ItemStack processing = this.getStackInSlot( 6 );
final IGrinderRecipe r = AEApi.instance().registries().grinder().getRecipeForInput( processing );
if( r != null )
2014-09-24 02:26:27 +02:00
{
if( r.getRequiredTurns() > 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-30 14:24:40 +02:00
final InventoryAdaptor sia = InventoryAdaptor.getAdaptor( new WrapperInventoryRange( this, 3, 3, true ), EnumFacing.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
r.getOptionalOutput().ifPresent( itemStack ->
2015-04-29 02:30:53 +02:00
{
final float chance = ( Platform.getRandomInt() % 2000 ) / 2000.0f;
if( chance <= r.getOptionalChance() )
{
this.addItem( sia, itemStack );
}
} );
2014-09-24 02:26:27 +02:00
r.getSecondOptionalOutput().ifPresent( itemStack ->
2015-04-29 02:30:53 +02:00
{
final float chance = ( Platform.getRandomInt() % 2000 ) / 2000.0f;
if( chance <= r.getSecondOptionalChance() )
{
this.addItem( sia, itemStack );
}
} );
2014-09-24 02:26:27 +02:00
this.setInventorySlotContents( 6, null );
}
}
2015-09-30 14:24:40 +02:00
private void addItem( final InventoryAdaptor sia, final ItemStack output )
{
if( output == null )
2015-04-29 02:30:53 +02:00
{
return;
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final ItemStack notAdded = sia.addItems( output );
if( notAdded != null )
{
2015-09-30 14:24:40 +02:00
final List<ItemStack> out = new ArrayList<ItemStack>();
out.add( notAdded );
2015-09-30 14:26:54 +02:00
Platform.spawnDrops( this.worldObj, this.pos.offset( this.getForward() ), out );
}
}
2014-09-24 02:26:27 +02:00
@Override
2015-09-30 14:24:40 +02:00
public boolean canCrankAttach( final EnumFacing directionToCrank )
2014-09-24 02:26:27 +02:00
{
return this.getUp() == directionToCrank;
2014-09-24 02:26:27 +02:00
}
}