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

521 lines
13 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
2015-05-18 00:30:08 +02:00
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
2014-11-14 12:02:52 +01:00
*
* 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.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
2014-09-24 02:26:27 +02:00
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
2014-09-24 02:26:27 +02:00
import net.minecraft.block.material.Material;
2015-06-16 02:44:59 +02:00
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.BlockStateContainer;
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.client.Minecraft;
2014-10-03 17:05:21 +02:00
import net.minecraft.creativetab.CreativeTabs;
2014-09-24 02:26:27 +02:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
2014-10-03 17:05:21 +02:00
import net.minecraft.item.Item;
2014-09-24 02:26:27 +02:00
import net.minecraft.item.ItemStack;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
2014-09-24 02:26:27 +02:00
import net.minecraft.world.IBlockAccess;
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.helpers.AEGlassMaterial;
import appeng.helpers.ICustomCollision;
import appeng.util.LookDirection;
import appeng.util.Platform;
public abstract class AEBaseBlock extends Block
2014-09-24 02:26:27 +02:00
{
private boolean isOpaque = true;
private boolean isFullSize = true;
private boolean hasSubtypes = false;
private boolean isInventory = false;
protected AxisAlignedBB boundingBox = FULL_BLOCK_AABB;
2015-06-16 02:44:59 +02:00
@Override
public boolean isVisuallyOpaque()
{
return this.isOpaque() && this.isFullSize();
2015-06-16 02:44:59 +02:00
}
2015-09-30 14:24:40 +02:00
protected AEBaseBlock( final Material mat )
2014-09-24 02:26:27 +02:00
{
super( mat );
2014-09-24 02:26:27 +02:00
if( mat == AEGlassMaterial.INSTANCE || mat == Material.GLASS )
2015-04-29 02:30:53 +02:00
{
this.setSoundType( SoundType.GLASS );
2015-04-29 02:30:53 +02:00
}
else if( mat == Material.ROCK )
2015-04-29 02:30:53 +02:00
{
this.setSoundType( SoundType.STONE );
2015-04-29 02:30:53 +02:00
}
else if( mat == Material.WOOD )
2015-04-29 02:30:53 +02:00
{
this.setSoundType( SoundType.WOOD );
2015-04-29 02:30:53 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
this.setSoundType( SoundType.METAL );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
this.setLightOpacity( 255 );
this.setLightLevel( 0 );
this.setHardness( 2.2F );
this.setHarvestLevel( "pickaxe", 0 );
2014-09-24 02:26:27 +02:00
}
@Override
public String toString()
2014-09-24 02:26:27 +02:00
{
String regName = getRegistryName() != null ? getRegistryName().getResourcePath() : "unregistered";
return getClass().getSimpleName() + "[" + regName + "]";
2014-09-24 02:26:27 +02:00
}
2015-06-16 02:44:59 +02:00
@Override
protected BlockStateContainer createBlockState()
2015-06-16 02:44:59 +02:00
{
return new BlockStateContainer( this, this.getAEStates() );
2015-06-16 02:44:59 +02:00
}
2015-06-16 02:44:59 +02:00
protected IProperty[] getAEStates()
{
return new IProperty[0];
}
public boolean isOpaque()
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
return this.isOpaque;
2014-09-24 02:26:27 +02:00
}
2014-09-24 02:26:27 +02:00
@Override
public boolean isNormalCube( IBlockState state )
2014-09-24 02:26:27 +02:00
{
return this.isFullSize() && this.isOpaque();
2014-09-24 02:26:27 +02:00
}
protected ICustomCollision getCustomCollision( final World w, final BlockPos pos )
2014-09-24 02:26:27 +02:00
{
2015-06-16 02:44:59 +02:00
if( this instanceof ICustomCollision )
{
return (ICustomCollision) this;
}
return null;
2014-09-24 02:26:27 +02:00
}
@Override
public AxisAlignedBB getBoundingBox( IBlockState state, IBlockAccess source, BlockPos pos )
{
return boundingBox;
}
@Override
public void addCollisionBoxToList( final IBlockState state, final World w, final BlockPos pos, final AxisAlignedBB bb, final List<AxisAlignedBB> out, final Entity e )
2014-09-24 02:26:27 +02:00
{
2015-06-16 02:44:59 +02:00
final ICustomCollision collisionHandler = this.getCustomCollision( w, pos );
2014-09-24 02:26:27 +02:00
if( collisionHandler != null && bb != null )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final List<AxisAlignedBB> tmp = new ArrayList<AxisAlignedBB>();
2015-06-16 02:44:59 +02:00
collisionHandler.addCollidingBlockToList( w, pos, bb, tmp, e );
2015-09-30 14:24:40 +02:00
for( final AxisAlignedBB b : tmp )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final AxisAlignedBB offset = b.offset( pos.getX(), pos.getY(), pos.getZ() );
2015-06-16 02:44:59 +02:00
if( bb.intersectsWith( offset ) )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
out.add( offset );
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
{
super.addCollisionBoxToList( state, w, pos, bb, out, e );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
2014-09-24 02:26:27 +02:00
@Override
@SideOnly( Side.CLIENT )
public AxisAlignedBB getSelectedBoundingBox( IBlockState state, final World w, final BlockPos pos )
2014-09-24 02:26:27 +02:00
{
2015-06-16 02:44:59 +02:00
final ICustomCollision collisionHandler = this.getCustomCollision( w, pos );
2014-09-24 02:26:27 +02:00
if( collisionHandler != null )
2014-09-24 02:26:27 +02:00
{
if( Platform.isClient() )
2014-09-24 02:26:27 +02:00
{
final EntityPlayer player = Minecraft.getMinecraft().thePlayer;
final LookDirection ld = Platform.getPlayerRay( player, Platform.getEyeOffset( player ) );
2014-09-24 02:26:27 +02:00
2015-06-16 02:44:59 +02:00
final Iterable<AxisAlignedBB> bbs = collisionHandler.getSelectedBoundingBoxesFromPool( w, pos, Minecraft.getMinecraft().thePlayer, true );
AxisAlignedBB br = null;
double lastDist = 0;
2015-09-30 14:24:40 +02:00
for( final AxisAlignedBB bb : bbs )
2014-09-24 02:26:27 +02:00
{
this.boundingBox = bb;
2014-09-24 02:26:27 +02:00
final RayTraceResult r = super.collisionRayTrace( state, w, pos, ld.getA(), ld.getB() );
this.boundingBox = FULL_BLOCK_AABB;
if( r != null )
2014-09-24 02:26:27 +02:00
{
final double xLen = ( ld.getA().xCoord - r.hitVec.xCoord );
final double yLen = ( ld.getA().yCoord - r.hitVec.yCoord );
final double zLen = ( ld.getA().zCoord - r.hitVec.zCoord );
final double thisDist = xLen * xLen + yLen * yLen + zLen * zLen;
if( br == null || lastDist > thisDist )
{
lastDist = thisDist;
br = bb;
}
2014-09-24 02:26:27 +02:00
}
}
if( br != null )
{
br = new AxisAlignedBB( br.minX + pos.getX(), br.minY + pos.getY(), br.minZ + pos.getZ(), br.maxX + pos.getX(), br.maxY + pos.getY(), br.maxZ + pos.getZ() );
return br;
}
}
2014-09-24 02:26:27 +02:00
2015-06-16 02:44:59 +02:00
AxisAlignedBB b = null; // new AxisAlignedBB( 16d, 16d, 16d, 0d, 0d, 0d );
2015-09-30 14:24:40 +02:00
for( final AxisAlignedBB bx : collisionHandler.getSelectedBoundingBoxesFromPool( w, pos, null, false ) )
2014-09-24 02:26:27 +02:00
{
if( b == null )
2015-06-16 02:44:59 +02:00
{
b = bx;
continue;
}
final double minX = Math.min( b.minX, bx.minX );
final double minY = Math.min( b.minY, bx.minY );
final double minZ = Math.min( b.minZ, bx.minZ );
final double maxX = Math.max( b.maxX, bx.maxX );
final double maxY = Math.max( b.maxY, bx.maxY );
final double maxZ = Math.max( b.maxZ, bx.maxZ );
b = new AxisAlignedBB( minX, minY, minZ, maxX, maxY, maxZ );
2014-09-24 02:26:27 +02:00
}
if( b == null )
2015-12-24 02:11:17 +01:00
{
2015-06-16 02:44:59 +02:00
b = new AxisAlignedBB( 16d, 16d, 16d, 0d, 0d, 0d );
2015-12-24 02:11:17 +01:00
}
2015-06-16 02:44:59 +02:00
else
2015-12-24 02:11:17 +01:00
{
b = new AxisAlignedBB( b.minX + pos.getX(), b.minY + pos.getY(), b.minZ + pos.getZ(), b.maxX + pos.getX(), b.maxY + pos.getY(), b.maxZ + pos.getZ() );
2015-12-24 02:11:17 +01:00
}
return b;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
return super.getSelectedBoundingBox( state, w, pos );
2014-09-24 02:26:27 +02:00
}
@Override
public final boolean isOpaqueCube( IBlockState state )
2014-09-24 02:26:27 +02:00
{
return this.isOpaque();
2014-09-24 02:26:27 +02:00
}
@Override
public RayTraceResult collisionRayTrace( final IBlockState state, final World w, final BlockPos pos, final Vec3d a, final Vec3d b )
2014-09-24 02:26:27 +02:00
{
2015-06-16 02:44:59 +02:00
final ICustomCollision collisionHandler = this.getCustomCollision( w, pos );
2014-09-24 02:26:27 +02:00
if( collisionHandler != null )
2014-09-24 02:26:27 +02:00
{
2015-06-16 02:44:59 +02:00
final Iterable<AxisAlignedBB> bbs = collisionHandler.getSelectedBoundingBoxesFromPool( w, pos, null, true );
RayTraceResult br = null;
2014-09-24 02:26:27 +02:00
double lastDist = 0;
2015-09-30 14:24:40 +02:00
for( final AxisAlignedBB bb : bbs )
2014-09-24 02:26:27 +02:00
{
this.boundingBox = bb;
2014-09-24 02:26:27 +02:00
final RayTraceResult r = super.collisionRayTrace( state, w, pos, a, b );
2014-09-24 02:26:27 +02:00
this.boundingBox = FULL_BLOCK_AABB;
2014-09-24 02:26:27 +02:00
if( r != null )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final double xLen = ( a.xCoord - r.hitVec.xCoord );
final double yLen = ( a.yCoord - r.hitVec.yCoord );
final double zLen = ( a.zCoord - r.hitVec.zCoord );
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final double thisDist = xLen * xLen + yLen * yLen + zLen * zLen;
if( br == null || lastDist > thisDist )
2014-09-24 02:26:27 +02:00
{
lastDist = thisDist;
br = r;
}
}
}
if( br != null )
2014-09-24 02:26:27 +02:00
{
return br;
}
2014-09-24 02:26:27 +02:00
return null;
}
this.boundingBox = FULL_BLOCK_AABB;
return super.collisionRayTrace( state, w, pos, a, b );
2014-09-24 02:26:27 +02:00
}
public boolean onActivated( final World w, final BlockPos pos, final EntityPlayer player, final EnumHand hand, final @Nullable ItemStack heldItem, final EnumFacing side, final float hitX, final float hitY, final float hitZ )
2014-09-24 02:26:27 +02:00
{
return false;
2014-09-24 02:26:27 +02:00
}
@Override
@SideOnly( Side.CLIENT )
@SuppressWarnings( "unchecked" )
2015-09-30 14:24:40 +02:00
public final void getSubBlocks( final Item item, final CreativeTabs tabs, final List itemStacks )
2014-09-24 02:26:27 +02:00
{
this.getCheckedSubBlocks( item, tabs, itemStacks );
}
2014-09-24 02:26:27 +02:00
@Override
public boolean hasComparatorInputOverride( IBlockState state )
{
2015-12-24 02:05:39 +01:00
return this.isInventory();
2014-09-24 02:26:27 +02:00
}
@Override
public int getComparatorInputOverride( IBlockState state, final World worldIn, final BlockPos pos )
2014-09-24 02:26:27 +02:00
{
return 0;
2014-09-24 02:26:27 +02:00
}
@Override
public boolean isNormalCube( IBlockState state, final IBlockAccess world, final BlockPos pos )
2014-09-24 02:26:27 +02:00
{
return this.isFullSize();
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
public IOrientable getOrientable( final IBlockAccess w, final BlockPos pos )
2014-09-24 02:26:27 +02:00
{
if( this instanceof IOrientableBlock )
{
IOrientableBlock orientable = (IOrientableBlock) this;
return orientable.getOrientable( w, pos );
}
return null;
}
@Override
public boolean rotateBlock( final World w, final BlockPos pos, final EnumFacing axis )
{
2015-06-16 02:44:59 +02:00
final IOrientable rotatable = this.getOrientable( w, pos );
if( rotatable != null && rotatable.canBeRotated() )
{
if( this.hasCustomRotation() )
{
2014-12-29 15:13:47 +01:00
this.customRotateBlock( rotatable, axis );
return true;
}
else
{
2015-06-16 02:44:59 +02:00
EnumFacing forward = rotatable.getForward();
EnumFacing up = rotatable.getUp();
for( int rs = 0; rs < 4; rs++ )
{
forward = Platform.rotateAround( forward, axis );
up = Platform.rotateAround( up, axis );
2015-06-16 02:44:59 +02:00
if( this.isValidOrientation( w, pos, forward, up ) )
{
rotatable.setOrientation( forward, up );
return true;
}
}
}
}
2015-06-16 02:44:59 +02:00
return super.rotateBlock( w, pos, axis );
}
protected boolean hasCustomRotation()
{
return false;
}
2015-09-30 14:24:40 +02:00
protected void customRotateBlock( final IOrientable rotatable, final EnumFacing axis )
{
}
2015-09-30 14:24:40 +02:00
public boolean isValidOrientation( final World w, final BlockPos pos, final EnumFacing forward, final EnumFacing up )
{
return true;
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public EnumFacing[] getValidRotations( final World w, final BlockPos pos )
2014-09-24 02:26:27 +02:00
{
2015-06-16 02:44:59 +02:00
return new EnumFacing[0];
2014-09-24 02:26:27 +02:00
}
@SideOnly( Side.CLIENT )
2015-09-30 14:24:40 +02:00
public void getCheckedSubBlocks( final Item item, final CreativeTabs tabs, final List<ItemStack> itemStacks )
{
2015-06-16 02:44:59 +02:00
super.getSubBlocks( item, tabs, itemStacks );
}
2015-09-30 14:24:40 +02:00
public String getUnlocalizedName( final ItemStack is )
{
2015-06-16 02:44:59 +02:00
return this.getUnlocalizedName();
}
2016-09-17 17:00:10 +02:00
@Override
2015-09-30 14:24:40 +02:00
public void addInformation( final ItemStack is, final EntityPlayer player, final List<String> lines, final boolean advancedItemTooltips )
{
2015-06-16 02:44:59 +02:00
}
2015-06-16 02:44:59 +02:00
public boolean hasSubtypes()
{
return this.hasSubtypes;
}
public EnumFacing mapRotation( final IOrientable ori, final EnumFacing dir )
{
// case DOWN: return bottomIcon;
// case UP: return blockIcon;
// case NORTH: return northIcon;
// case SOUTH: return southIcon;
// case WEST: return sideIcon;
// case EAST: return sideIcon;
2015-06-16 02:44:59 +02:00
final EnumFacing forward = ori.getForward();
final EnumFacing up = ori.getUp();
if( forward == null || up == null )
2015-04-29 02:30:53 +02:00
{
return dir;
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final int west_x = forward.getFrontOffsetY() * up.getFrontOffsetZ() - forward.getFrontOffsetZ() * up.getFrontOffsetY();
final int west_y = forward.getFrontOffsetZ() * up.getFrontOffsetX() - forward.getFrontOffsetX() * up.getFrontOffsetZ();
final int west_z = forward.getFrontOffsetX() * up.getFrontOffsetY() - forward.getFrontOffsetY() * up.getFrontOffsetX();
2015-09-30 14:22:21 +02:00
EnumFacing west = null;
2015-09-30 14:24:40 +02:00
for( final EnumFacing dx : EnumFacing.VALUES )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
if( dx.getFrontOffsetX() == west_x && dx.getFrontOffsetY() == west_y && dx.getFrontOffsetZ() == west_z )
2015-04-29 02:30:53 +02:00
{
west = dx;
2015-04-29 02:30:53 +02:00
}
}
if( west == null )
2015-12-24 02:11:17 +01:00
{
2015-06-16 02:44:59 +02:00
return dir;
2015-12-24 02:11:17 +01:00
}
if( dir == forward )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return EnumFacing.SOUTH;
2015-04-29 02:30:53 +02:00
}
if( dir == forward.getOpposite() )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return EnumFacing.NORTH;
2015-04-29 02:30:53 +02:00
}
if( dir == up )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return EnumFacing.UP;
2015-04-29 02:30:53 +02:00
}
if( dir == up.getOpposite() )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return EnumFacing.DOWN;
2015-04-29 02:30:53 +02:00
}
if( dir == west )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return EnumFacing.WEST;
2015-04-29 02:30:53 +02:00
}
if( dir == west.getOpposite() )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return EnumFacing.EAST;
2015-04-29 02:30:53 +02:00
}
2015-06-16 02:44:59 +02:00
return null;
}
public boolean isFullSize()
{
2015-12-24 02:10:22 +01:00
return this.isFullSize;
}
2015-12-24 02:09:38 +01:00
public boolean setFullSize( final boolean isFullSize )
{
this.isFullSize = isFullSize;
return isFullSize;
}
2015-12-24 02:09:38 +01:00
public boolean setOpaque( final boolean isOpaque )
{
this.isOpaque = isOpaque;
return isOpaque;
}
2015-12-24 02:05:39 +01:00
public boolean isInventory()
{
2015-12-24 02:10:22 +01:00
return this.isInventory;
2015-12-24 02:05:39 +01:00
}
2015-12-24 02:09:38 +01:00
public void setInventory( final boolean isInventory )
2015-12-24 02:05:39 +01:00
{
this.isInventory = isInventory;
}
2015-12-24 02:09:38 +01:00
public void setHasSubtypes( final boolean hasSubtypes )
2015-12-24 02:05:39 +01:00
{
this.hasSubtypes = hasSubtypes;
}
2014-09-24 02:26:27 +02:00
}