Applied-Energistics-2-tiler.../src/main/java/appeng/block/grindstone/BlockCrank.java

164 lines
4.6 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>.
*/
package appeng.block.grindstone;
import java.util.EnumSet;
2014-02-09 02:34:52 +01:00
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
2015-06-16 02:44:59 +02:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
2014-08-07 04:02:08 +02:00
import net.minecraftforge.common.util.FakePlayer;
2015-12-24 02:07:03 +01:00
2014-01-23 20:02:48 +01:00
import appeng.api.implementations.tiles.ICrankable;
import appeng.block.AEBaseTileBlock;
import appeng.client.render.BaseBlockRender;
import appeng.client.render.blocks.RenderBlockCrank;
import appeng.core.features.AEFeature;
import appeng.core.stats.Stats;
import appeng.tile.AEBaseTile;
import appeng.tile.grindstone.TileCrank;
public class BlockCrank extends AEBaseTileBlock
{
public BlockCrank()
{
super( Material.wood );
2014-12-29 15:13:47 +01:00
this.setTileEntity( TileCrank.class );
this.setLightOpacity( 0 );
2015-01-17 15:07:01 +01:00
this.setHarvestLevel( "axe", 0 );
this.setFullSize( this.setOpaque( false ) );
this.setFeature( EnumSet.of( AEFeature.GrindStone ) );
}
@Override
public Class<? extends BaseBlockRender> getRenderer()
{
return RenderBlockCrank.class;
}
2015-12-24 02:03:16 +01:00
@Override
public boolean onActivated( final World w, final BlockPos pos, final EntityPlayer player, final EnumFacing side, final float hitX, final float hitY, final float hitZ )
{
if( player instanceof FakePlayer || player == null )
2014-11-29 12:34:33 +01:00
{
2015-06-16 02:44:59 +02:00
this.dropCrank( w, pos );
2014-08-07 04:02:08 +02:00
return true;
2014-11-29 12:34:33 +01:00
}
2014-08-07 04:02:08 +02:00
2015-09-30 14:24:40 +02:00
final AEBaseTile tile = this.getTileEntity( w, pos );
if( tile instanceof TileCrank )
{
if( ( (TileCrank) tile ).power() )
{
2014-11-29 12:34:33 +01:00
Stats.TurnedCranks.addToPlayer( player, 1 );
}
}
2014-08-07 04:02:08 +02:00
return true;
}
2015-09-30 14:24:40 +02:00
private void dropCrank( final World world, final BlockPos pos )
{
2015-06-16 02:44:59 +02:00
world.destroyBlock( pos, true ); // w.destroyBlock( x, y, z, true );
world.markBlockForUpdate( pos );
}
2015-12-24 02:03:16 +01:00
@Override
public void onBlockPlacedBy( final World world, final BlockPos pos, final IBlockState state, final EntityLivingBase placer, final ItemStack stack )
{
2015-09-30 14:24:40 +02:00
final AEBaseTile tile = this.getTileEntity( world, pos );
if( tile != null )
{
2015-09-30 14:24:40 +02:00
final EnumFacing mnt = this.findCrankable( world, pos );
2015-06-16 02:44:59 +02:00
EnumFacing forward = EnumFacing.UP;
if( mnt == EnumFacing.UP || mnt == EnumFacing.DOWN )
2014-11-29 12:34:33 +01:00
{
2015-06-16 02:44:59 +02:00
forward = EnumFacing.SOUTH;
2014-11-29 12:34:33 +01:00
}
tile.setOrientation( forward, mnt.getOpposite() );
}
else
2014-11-29 12:34:33 +01:00
{
2015-06-16 02:44:59 +02:00
this.dropCrank( world, pos );
2014-11-29 12:34:33 +01:00
}
}
2015-12-24 02:03:16 +01:00
@Override
public boolean isValidOrientation( final World w, final BlockPos pos, final EnumFacing forward, final EnumFacing up )
{
2015-09-30 14:24:40 +02:00
final TileEntity te = w.getTileEntity( pos );
2015-06-16 02:44:59 +02:00
return !( te instanceof TileCrank ) || this.isCrankable( w, pos, up.getOpposite() );
}
2015-09-30 14:24:40 +02:00
private EnumFacing findCrankable( final World world, final BlockPos pos )
{
2015-09-30 14:24:40 +02:00
for( final EnumFacing dir : EnumFacing.VALUES )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
if( this.isCrankable( world, pos, dir ) )
{
return dir;
}
2015-04-29 02:30:53 +02:00
}
2015-06-16 02:44:59 +02:00
return null;
}
2015-09-30 14:24:40 +02:00
private boolean isCrankable( final World world, final BlockPos pos, final EnumFacing offset )
{
2015-12-24 02:03:16 +01:00
final BlockPos o = pos.offset( offset );
2015-09-30 14:24:40 +02:00
final TileEntity te = world.getTileEntity( o );
return te instanceof ICrankable && ( (ICrankable) te ).canCrankAttach( offset.getOpposite() );
}
2015-12-24 02:03:16 +01:00
@Override
public void onNeighborBlockChange( final World world, final BlockPos pos, final IBlockState state, final Block neighborBlock )
{
2015-06-16 02:44:59 +02:00
2015-09-30 14:24:40 +02:00
final AEBaseTile tile = this.getTileEntity( world, pos );
if( tile != null )
{
2015-06-16 02:44:59 +02:00
if( !this.isCrankable( world, pos, tile.getUp().getOpposite() ) )
2014-11-29 12:34:33 +01:00
{
2015-06-16 02:44:59 +02:00
this.dropCrank( world, pos );
2014-11-29 12:34:33 +01:00
}
}
else
2014-11-29 12:34:33 +01:00
{
2015-06-16 02:44:59 +02:00
this.dropCrank( world, pos );
2014-11-29 12:34:33 +01:00
}
}
@Override
2015-09-30 14:24:40 +02:00
public boolean canPlaceBlockAt( final World world, final BlockPos pos )
{
2015-06-16 02:44:59 +02:00
return this.findCrankable( world, pos ) != null;
}
}