Port 2d9d5463a make BlockBounding a little more aware of mainPos triggering infinite recursion

This commit is contained in:
pupnewfster committed 2019-04-09 11:35:08 -04:00
1 parent 1e5e853241
commit 5a9423521a
1 file changed
+36 -32
@@ -2,6 +2,7 @@ package mekanism.common.block;
import java.util.Random;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import mekanism.common.block.states.BlockStateBounding;
import mekanism.common.tile.TileEntityAdvancedBoundingBlock;
import mekanism.common.tile.TileEntityBoundingBlock;
@@ -23,6 +24,15 @@ import net.minecraft.world.World;
public class BlockBounding extends Block {
@Nullable
private static BlockPos getMainBlockPos(World world, BlockPos thisPos) {
TileEntity te = world.getTileEntity(thisPos);
if (te instanceof TileEntityBoundingBlock && !thisPos.equals(((TileEntityBoundingBlock) te).mainPos)) {
return ((TileEntityBoundingBlock) te).mainPos;
}
return null;
}
public BlockBounding() {
super(Material.IRON);
setHardness(3.5F);
@@ -44,27 +54,23 @@ public class BlockBounding extends Block {
@Override
public int getMetaFromState(IBlockState state) {
boolean isAdvanced = state.getValue(BlockStateBounding.advancedProperty);
return isAdvanced ? 1 : 0;
return state.getValue(BlockStateBounding.advancedProperty) ? 1 : 0;
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand,
EnumFacing side, float hitX, float hitY, float hitZ) {
try {
TileEntityBoundingBlock tileEntity = (TileEntityBoundingBlock) world.getTileEntity(pos);
IBlockState state1 = world.getBlockState(tileEntity.mainPos);
return state1.getBlock()
.onBlockActivated(world, tileEntity.mainPos, state1, player, hand, side, hitX, hitY, hitZ);
} catch (Exception e) {
BlockPos mainPos = getMainBlockPos(world, pos);
if (mainPos == null) {
return false;
}
IBlockState state1 = world.getBlockState(mainPos);
return state1.getBlock().onBlockActivated(world, mainPos, state1, player, hand, side, hitX, hitY, hitZ);
}
@Override
public void breakBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state) {
super.breakBlock(world, pos, state);
world.removeTileEntity(pos);
}
@@ -72,37 +78,37 @@ public class BlockBounding extends Block {
@Override
public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world,
@Nonnull BlockPos pos, EntityPlayer player) {
try {
TileEntityBoundingBlock tileEntity = (TileEntityBoundingBlock) world.getTileEntity(pos);
IBlockState state1 = world.getBlockState(tileEntity.mainPos);
return state1.getBlock().getPickBlock(state1, target, world, tileEntity.mainPos, player);
} catch (Exception e) {
BlockPos mainPos = getMainBlockPos(world, pos);
if (mainPos == null) {
return ItemStack.EMPTY;
}
IBlockState state1 = world.getBlockState(mainPos);
return state1.getBlock().getPickBlock(state1, target, world, mainPos, player);
}
@Override
public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos,
@Nonnull EntityPlayer player, boolean willHarvest) {
try {
TileEntityBoundingBlock tileEntity = (TileEntityBoundingBlock) world.getTileEntity(pos);
IBlockState state1 = world.getBlockState(tileEntity.mainPos);
return state1.getBlock().removedByPlayer(state1, world, tileEntity.mainPos, player, willHarvest);
} catch (Exception e) {
BlockPos mainPos = getMainBlockPos(world, pos);
if (mainPos == null) {
return false;
}
IBlockState state1 = world.getBlockState(mainPos);
return state1.getBlock().removedByPlayer(state1, world, mainPos, player, willHarvest);
}
@Override
@Deprecated
public void neighborChanged(IBlockState state, World world, BlockPos pos, Block neighborBlock,
BlockPos neighborPos) {
try {
TileEntityBoundingBlock tileEntity = (TileEntityBoundingBlock) world.getTileEntity(pos);
TileEntityBoundingBlock tileEntity = (TileEntityBoundingBlock) world.getTileEntity(pos);
if (tileEntity != null) {
tileEntity.onNeighborChange(state.getBlock());
IBlockState state1 = world.getBlockState(tileEntity.mainPos);
state1.getBlock().neighborChanged(state1, world, tileEntity.mainPos, neighborBlock, neighborPos);
} catch (Exception ignored) {
}
BlockPos mainPos = getMainBlockPos(world, pos);
if (mainPos != null) {
IBlockState state1 = world.getBlockState(mainPos);
state1.getBlock().neighborChanged(state1, world, mainPos, neighborBlock, neighborPos);
}
}
@@ -110,13 +116,12 @@ public class BlockBounding extends Block {
@Deprecated
public float getPlayerRelativeBlockHardness(IBlockState state, @Nonnull EntityPlayer player, @Nonnull World world,
@Nonnull BlockPos pos) {
try {
TileEntityBoundingBlock tileEntity = (TileEntityBoundingBlock) world.getTileEntity(pos);
IBlockState state1 = world.getBlockState(tileEntity.mainPos);
return state1.getBlock().getPlayerRelativeBlockHardness(state1, player, world, tileEntity.mainPos);
} catch (Exception e) {
BlockPos mainPos = getMainBlockPos(world, pos);
if (mainPos == null) {
return super.getPlayerRelativeBlockHardness(state, player, world, pos);
}
IBlockState state1 = world.getBlockState(mainPos);
return state1.getBlock().getPlayerRelativeBlockHardness(state1, player, world, mainPos);
}
@Override
@@ -158,8 +163,7 @@ public class BlockBounding extends Block {
public TileEntity createTileEntity(@Nonnull World world, @Nonnull IBlockState state) {
if (state.getValue(BlockStateBounding.advancedProperty)) {
return new TileEntityAdvancedBoundingBlock();
} else {
return new TileEntityBoundingBlock();
}
return new TileEntityBoundingBlock();
}
}
}