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

224 lines
5.4 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.block;
2014-09-24 02:26:27 +02:00
import java.util.List;
import net.minecraft.block.Block;
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.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
2014-09-24 02:26:27 +02:00
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
2015-06-16 02:44:59 +02:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-12-24 02:07:03 +01:00
2014-09-24 02:26:27 +02:00
import appeng.api.util.IOrientable;
import appeng.api.util.IOrientableBlock;
import appeng.block.misc.BlockLightDetector;
import appeng.block.misc.BlockSkyCompass;
import appeng.block.networking.BlockWireless;
import appeng.me.helpers.IGridProxyable;
import appeng.tile.AEBaseTile;
2014-09-24 02:26:27 +02:00
public class AEBaseItemBlock extends ItemBlock
{
private final AEBaseBlock blockType;
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
public AEBaseItemBlock( final Block id )
{
2014-09-24 02:26:27 +02:00
super( id );
2014-12-29 15:13:47 +01:00
this.blockType = (AEBaseBlock) id;
2015-12-24 02:05:39 +01:00
this.hasSubtypes = this.blockType.hasSubtypes();
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public int getMetadata( final int dmg )
2014-09-24 02:26:27 +02:00
{
if( this.hasSubtypes )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return dmg;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
return 0;
}
@Override
@SideOnly( Side.CLIENT )
@SuppressWarnings( "unchecked" )
2015-09-30 14:24:40 +02:00
public final void addInformation( final ItemStack itemStack, final EntityPlayer player, final List toolTip, final boolean advancedTooltips )
2014-09-24 02:26:27 +02:00
{
this.addCheckedInformation( itemStack, player, toolTip, advancedTooltips );
}
@SideOnly( Side.CLIENT )
2015-09-30 14:24:40 +02:00
public void addCheckedInformation( final ItemStack itemStack, final EntityPlayer player, final List<String> toolTip, final boolean advancedToolTips )
{
this.blockType.addInformation( itemStack, player, toolTip, advancedToolTips );
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public boolean isBookEnchantable( final ItemStack itemstack1, final ItemStack itemstack2 )
{
return false;
}
@Override
2015-09-30 14:24:40 +02:00
public String getUnlocalizedName( final ItemStack is )
2014-09-24 02:26:27 +02:00
{
return this.blockType.getUnlocalizedName( is );
2014-09-24 02:26:27 +02:00
}
2015-12-24 02:03:16 +01:00
2014-09-24 02:26:27 +02:00
@Override
2015-06-16 02:44:59 +02:00
public boolean placeBlockAt(
2015-09-30 14:24:40 +02:00
final ItemStack stack,
final EntityPlayer player,
final World w,
final BlockPos pos,
final EnumFacing side,
final float hitX,
final float hitY,
final float hitZ,
final IBlockState newState )
2014-09-24 02:26:27 +02:00
{
2015-06-16 02:44:59 +02:00
EnumFacing up = null;
EnumFacing forward = null;
2014-09-24 02:26:27 +02:00
if( this.blockType instanceof AEBaseTileBlock )
2014-09-24 02:26:27 +02:00
{
if( this.blockType instanceof BlockLightDetector )
2014-09-24 02:26:27 +02:00
{
2015-06-16 02:44:59 +02:00
up = side;
if( up == EnumFacing.UP || up == EnumFacing.DOWN )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
forward = EnumFacing.SOUTH;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
else
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
forward = EnumFacing.UP;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
else if( this.blockType instanceof BlockWireless || this.blockType instanceof BlockSkyCompass )
2014-09-24 02:26:27 +02:00
{
2015-06-16 02:44:59 +02:00
forward = side;
if( forward == EnumFacing.UP || forward == EnumFacing.DOWN )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
up = EnumFacing.SOUTH;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
else
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
up = EnumFacing.UP;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
else
{
2015-06-16 02:44:59 +02:00
up = EnumFacing.UP;
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final byte rotation = (byte) ( MathHelper.floor_double( ( player.rotationYaw * 4F ) / 360F + 2.5D ) & 3 );
2014-09-24 02:26:27 +02:00
switch( rotation )
2014-09-24 02:26:27 +02:00
{
default:
case 0:
2015-06-16 02:44:59 +02:00
forward = EnumFacing.SOUTH;
break;
case 1:
2015-06-16 02:44:59 +02:00
forward = EnumFacing.WEST;
break;
case 2:
2015-06-16 02:44:59 +02:00
forward = EnumFacing.NORTH;
break;
case 3:
2015-06-16 02:44:59 +02:00
forward = EnumFacing.EAST;
break;
2014-09-24 02:26:27 +02:00
}
if( player.rotationPitch > 65 )
2014-09-24 02:26:27 +02:00
{
up = forward.getOpposite();
2015-06-16 02:44:59 +02:00
forward = EnumFacing.UP;
2014-09-24 02:26:27 +02:00
}
else if( player.rotationPitch < -65 )
2014-09-24 02:26:27 +02:00
{
up = forward.getOpposite();
2015-06-16 02:44:59 +02:00
forward = EnumFacing.DOWN;
2014-09-24 02:26:27 +02:00
}
}
}
2015-09-30 14:22:21 +02:00
IOrientable ori = null;
if( this.blockType instanceof IOrientableBlock )
2014-09-24 02:26:27 +02:00
{
2015-06-16 02:44:59 +02:00
ori = ( (IOrientableBlock) this.blockType ).getOrientable( w, pos );
up = side;
forward = EnumFacing.SOUTH;
if( up.getFrontOffsetY() == 0 )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
forward = EnumFacing.UP;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
2015-06-16 02:44:59 +02:00
if( !this.blockType.isValidOrientation( w, pos, forward, up ) )
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-06-16 02:44:59 +02:00
if( super.placeBlockAt( stack, player, w, pos, side, hitX, hitY, hitZ, newState ) )
2014-09-24 02:26:27 +02:00
{
if( this.blockType instanceof AEBaseTileBlock && !( this.blockType instanceof BlockLightDetector ) )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final AEBaseTile tile = ( (AEBaseTileBlock) this.blockType ).getTileEntity( w, pos );
2014-09-24 02:26:27 +02:00
ori = tile;
if( tile == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return true;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
if( ori.canBeRotated() && !this.blockType.hasCustomRotation() )
2014-09-24 02:26:27 +02:00
{
2015-06-16 02:44:59 +02:00
if( ori.getForward() == null || ori.getUp() == null )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
ori.setOrientation( forward, up );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
if( tile instanceof IGridProxyable )
2014-09-24 02:26:27 +02:00
{
( (IGridProxyable) tile ).getProxy().setOwner( player );
2014-09-24 02:26:27 +02:00
}
tile.onPlacement( stack, player, side );
}
else if( this.blockType instanceof IOrientableBlock )
2014-09-24 02:26:27 +02:00
{
ori.setOrientation( forward, up );
}
return true;
}
return false;
}
}