Orientation Fixes when Placing Blocks with Tile Entities

Sky Compass Rendering (no rotation yet)
This commit is contained in:
Sebastian Hartte
2020-06-03 01:19:18 +02:00
parent 58afa6539f
commit 36eb8c807e
21 changed files with 432 additions and 407 deletions
+130 -120
View File
@@ -21,15 +21,20 @@ package appeng.block;
import java.util.List;
import appeng.api.util.IOrientable;
import appeng.api.util.IOrientableBlock;
import appeng.block.misc.BlockSkyCompass;
import appeng.me.helpers.IGridProxyable;
import appeng.tile.AEBaseTile;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.World;
@@ -73,123 +78,128 @@ public class AEBaseBlockItem extends BlockItem
return this.blockType.getTranslationKey();
}
@Override
public ActionResultType tryPlace(BlockItemUseContext context) {
// @Override
// public boolean placeBlock( final ItemStack stack, final PlayerEntity player, final World w, final BlockPos pos, final Direction side, final float hitX, final float hitY, final float hitZ, final BlockState newState )
// {
// Direction up = null;
// Direction forward = null;
//
// if( this.blockType instanceof AEBaseTileBlock )
// {
// if( this.blockType instanceof BlockLightDetector )
// {
// up = side;
// if( up == Direction.UP || up == Direction.DOWN )
// {
// forward = Direction.SOUTH;
// }
// else
// {
// forward = Direction.UP;
// }
// }
// else if( this.blockType instanceof BlockWireless || this.blockType instanceof BlockSkyCompass )
// {
// forward = side;
// if( forward == Direction.UP || forward == Direction.DOWN )
// {
// up = Direction.SOUTH;
// }
// else
// {
// up = Direction.UP;
// }
// }
// else
// {
// up = Direction.UP;
//
// final byte rotation = (byte) ( MathHelper.floor( ( player.rotationYaw * 4F ) / 360F + 2.5D ) & 3 );
//
// switch( rotation )
// {
// default:
// case 0:
// forward = Direction.SOUTH;
// break;
// case 1:
// forward = Direction.WEST;
// break;
// case 2:
// forward = Direction.NORTH;
// break;
// case 3:
// forward = Direction.EAST;
// break;
// }
//
// if( player.rotationPitch > 65 )
// {
// up = forward.getOpposite();
// forward = Direction.UP;
// }
// else if( player.rotationPitch < -65 )
// {
// up = forward.getOpposite();
// forward = Direction.DOWN;
// }
// }
// }
//
// IOrientable ori = null;
// if( this.blockType instanceof IOrientableBlock )
// {
// ori = ( (IOrientableBlock) this.blockType ).getOrientable( w, pos );
// up = side;
// forward = Direction.SOUTH;
// if( up.getYOffset() == 0 )
// {
// forward = Direction.UP;
// }
// }
//
// if( !this.blockType.isValidOrientation( w, pos, forward, up ) )
// {
// return false;
// }
//
// if( super.placeBlock( stack, player, w, pos, side, hitX, hitY, hitZ, newState ) )
// {
// if( this.blockType instanceof AEBaseTileBlock && !( this.blockType instanceof BlockLightDetector ) )
// {
// final AEBaseTile tile = ( (AEBaseTileBlock) this.blockType ).getTileEntity( w, pos );
// ori = tile;
//
// if( tile == null )
// {
// return true;
// }
//
// if( ori.canBeRotated() && !this.blockType.hasCustomRotation() )
// {
// ori.setOrientation( forward, up );
// }
//
// if( tile instanceof IGridProxyable )
// {
// ( (IGridProxyable) tile ).getProxy().setOwner( player );
// }
//
// tile.onPlacement( stack, player, side );
// }
// else if( this.blockType instanceof IOrientableBlock )
// {
// ori.setOrientation( forward, up );
// }
//
// return true;
// }
// return false;
// }
Direction up = null;
Direction forward = null;
Direction side = context.getFace();
PlayerEntity player = context.getPlayer();
if( this.blockType instanceof AEBaseTileBlock )
{
// FIXME if( this.blockType instanceof BlockLightDetector )
// FIXME {
// FIXME up = side;
// FIXME if( up == Direction.UP || up == Direction.DOWN )
// FIXME {
// FIXME forward = Direction.SOUTH;
// FIXME }
// FIXME else
// FIXME {
// FIXME forward = Direction.UP;
// FIXME }
// FIXME }
/* FIXME else */ if( /*this.blockType instanceof BlockWireless ||*/ this.blockType instanceof BlockSkyCompass)
{
forward = side;
if( forward == Direction.UP || forward == Direction.DOWN )
{
up = Direction.SOUTH;
}
else
{
up = Direction.UP;
}
}
else
{
up = Direction.UP;
// FIXME: investigate placementYaw
final byte rotation = (byte) ( MathHelper.floor( ( player.rotationYaw * 4F ) / 360F + 2.5D ) & 3 );
switch( rotation )
{
default:
case 0:
forward = Direction.SOUTH;
break;
case 1:
forward = Direction.WEST;
break;
case 2:
forward = Direction.NORTH;
break;
case 3:
forward = Direction.EAST;
break;
}
if( player.rotationPitch > 65 )
{
up = forward.getOpposite();
forward = Direction.UP;
}
else if( player.rotationPitch < -65 )
{
up = forward.getOpposite();
forward = Direction.DOWN;
}
}
}
IOrientable ori = null;
if( this.blockType instanceof IOrientableBlock)
{
ori = ( (IOrientableBlock) this.blockType ).getOrientable( context.getWorld(), context.getPos() );
up = side;
forward = Direction.SOUTH;
if( up.getYOffset() == 0 )
{
forward = Direction.UP;
}
}
if( !this.blockType.isValidOrientation( context.getWorld(), context.getPos(), forward, up ) )
{
return ActionResultType.FAIL;
}
ActionResultType result = super.tryPlace(context);
if (result != ActionResultType.SUCCESS) {
return result;
}
if( this.blockType instanceof AEBaseTileBlock /* FIXME && !( this.blockType instanceof BlockLightDetector ) */ )
{
final AEBaseTile tile = ( (AEBaseTileBlock<?>) this.blockType ).getTileEntity( context.getWorld(), context.getPos() );
ori = tile;
if( tile == null )
{
return ActionResultType.SUCCESS;
}
if( ori.canBeRotated() && !this.blockType.hasCustomRotation() )
{
ori.setOrientation( forward, up );
}
if( tile instanceof IGridProxyable)
{
( (IGridProxyable) tile ).getProxy().setOwner( player );
}
tile.onPlacement( context );
}
else if( this.blockType instanceof IOrientableBlock )
{
ori.setOrientation( forward, up );
}
return ActionResultType.SUCCESS;
}
}
@@ -122,9 +122,11 @@ public class BlockLightDetector extends AEBaseTileBlock implements IOrientableBl
return this.canPlaceAt( w, pos, up.getOpposite() );
}
private boolean canPlaceAt( final World w, final BlockPos pos, final Direction dir )
private boolean canPlaceAt( final IBlockReader w, final BlockPos pos, final Direction dir )
{
return w.isSideSolid( pos.offset( dir ), dir.getOpposite(), false );
final BlockPos test = pos.offset( dir );
BlockState blockstate = w.getBlockState(test);
return blockstate.isSolidSide(w, test, dir.getOpposite());
}
@Override
@@ -19,48 +19,31 @@
package appeng.block.misc;
import java.util.Collections;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.BlockStateContainer;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.util.BlockRenderType;
import net.minecraft.util.Direction;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.IWorldReader;
import net.minecraft.world.World;
import net.minecraftforge.common.property.ExtendedBlockState;
import net.minecraftforge.common.property.IUnlistedProperty;
import net.minecraftforge.common.property.PropertyFloat;
import appeng.block.AEBaseTileBlock;
import appeng.helpers.ICustomCollision;
import appeng.tile.misc.TileSkyCompass;
public class BlockSkyCompass extends AEBaseTileBlock implements ICustomCollision
public class BlockSkyCompass extends AEBaseTileBlock<TileSkyCompass>
{
// Rotation is expressed as radians
public static final PropertyFloat ROTATION = new PropertyFloat( "rotation" );
public BlockSkyCompass()
public BlockSkyCompass(Block.Properties props)
{
super( Material.MISCELLANEOUS );
this.setLightOpacity( 0 );
this.setFullSize( false );
this.setOpaque( false );
}
@Override
protected BlockStateContainer createBlockState()
{
return new ExtendedBlockState( this, this.getAEStates(), new IUnlistedProperty[] { FORWARD, UP, ROTATION } );
super(props);
}
@Override
@@ -74,9 +57,11 @@ public class BlockSkyCompass extends AEBaseTileBlock implements ICustomCollision
return this.canPlaceAt( w, pos, forward.getOpposite() );
}
private boolean canPlaceAt( final World w, final BlockPos pos, final Direction dir )
private boolean canPlaceAt( final IBlockReader w, final BlockPos pos, final Direction dir )
{
return w.isSideSolid( pos.offset( dir ), dir.getOpposite(), false );
final BlockPos test = pos.offset( dir );
BlockState blockstate = w.getBlockState(test);
return blockstate.isSolidSide(w, test, dir.getOpposite());
}
@Override
@@ -111,8 +96,10 @@ public class BlockSkyCompass extends AEBaseTileBlock implements ICustomCollision
}
@Override
public Iterable<AxisAlignedBB> getSelectedBoundingBoxesFromPool( final World w, final BlockPos pos, final Entity thePlayer, final boolean b )
{
public VoxelShape getShape(BlockState state, IBlockReader w, BlockPos pos, ISelectionContext context) {
// TODO: This definitely needs to be memoized
final TileSkyCompass tile = this.getTileEntity( w, pos );
if( tile != null )
{
@@ -167,27 +154,20 @@ public class BlockSkyCompass extends AEBaseTileBlock implements ICustomCollision
break;
}
return Collections.singletonList( new AxisAlignedBB( minX, minY, minZ, maxX, maxY, maxZ ) );
return VoxelShapes.create(new AxisAlignedBB( minX, minY, minZ, maxX, maxY, maxZ ) );
}
return Collections.singletonList( new AxisAlignedBB( 0.0, 0, 0.0, 1.0, 1.0, 1.0 ) );
return VoxelShapes.empty();
}
@Override
public void addCollidingBlockToList( final World w, final BlockPos pos, final AxisAlignedBB bb, final List<AxisAlignedBB> out, final Entity e )
{
public VoxelShape getCollisionShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
return VoxelShapes.empty();
}
@Override
public BlockRenderType getRenderType( BlockState state )
public BlockRenderType getRenderType(BlockState state )
{
return BlockRenderType.ENTITYBLOCK_ANIMATED;
}
@Override
public boolean isFullBlock( BlockState state )
{
return false;
}
}
@@ -19,29 +19,21 @@
package appeng.block.misc;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import appeng.bootstrap.*;
import appeng.client.render.tesr.SkyCompassTESR;
import appeng.tile.misc.TileSkyCompass;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import appeng.bootstrap.BlockRenderingCustomizer;
import appeng.bootstrap.IBlockRendering;
import appeng.bootstrap.IItemRendering;
import appeng.client.render.model.SkyCompassModel;
import appeng.client.render.tesr.SkyCompassTESR;
public class SkyCompassRendering extends BlockRenderingCustomizer
public class SkyCompassRendering extends TileEntityRenderingCustomizer<TileSkyCompass>
{
private static final ModelResourceLocation ITEM_MODEL = new ModelResourceLocation( "appliedenergistics2:sky_compass", "normal" );
@Override
@OnlyIn( Dist.CLIENT )
public void customize( IBlockRendering rendering, IItemRendering itemRendering )
{
rendering.tileEntityRenderer( new SkyCompassTESR() );
itemRendering.model( ITEM_MODEL );
itemRendering.builtInModel( "models/block/builtin/sky_compass", new SkyCompassModel() );
public void customize(TileEntityRendering<TileSkyCompass> rendering) {
rendering.tileEntityRenderer( SkyCompassTESR::new );
}
}