Port #4952 by @danielzgtg Item drop compatibility (#5384)

* Port #4952 by @danielzgtg Item drop compatibility

* Make the dif slightly easier to read by reordering a couple things

* Extract some of the drop logic into super classes to make it easier to read what is going on

* Add missing Nonnull annotation

* Inline the patched harvest block method

* Remove unneeded listener for BlockBounding and fix bounding blocks being set to air not removing the full thing

* Make it easier to tell at a glance that it passes it as false for willHarvest

* give BlockMekanismSimple a better name
This commit is contained in:
Richard Freimer authored and GitHub committed 2019-05-02 11:16:26 -04:00
1 parent 36d1a9c501
commit d5db11eea3
17 files changed
+307 -567

No files matched your search

@@ -1,31 +1,31 @@
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;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFlowerPot;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
public class BlockBounding extends Block {
@Nullable
private static BlockPos getMainBlockPos(World world, BlockPos thisPos) {
private static BlockPos getMainBlockPos(IBlockAccess world, BlockPos thisPos) {
TileEntity te = world.getTileEntity(thisPos);
if (te instanceof TileEntityBoundingBlock && !thisPos.equals(((TileEntityBoundingBlock) te).mainPos)) {
return ((TileEntityBoundingBlock) te).mainPos;
@@ -33,6 +33,20 @@ public class BlockBounding extends Block {
return null;
}
/**
* Removes the main block if it is not already air.
*/
private static void removeMainBlock(World world, BlockPos thisPos) {
BlockPos mainPos = getMainBlockPos(world, thisPos);
if (mainPos != null) {
IBlockState state = world.getBlockState(mainPos);
if (!state.getBlock().isAir(state, world, mainPos)) {
//Set the main block to air, which will invalidate the rest of the bounding blocks
world.setBlockToAir(mainPos);
}
}
}
public BlockBounding() {
super(Material.IRON);
setHardness(3.5F);
@@ -70,10 +84,16 @@ public class BlockBounding extends Block {
@Override
public void breakBlock(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state) {
//Remove the main block if a bounding block gets broken by being directly replaced
removeMainBlock(world, pos);
super.breakBlock(world, pos, state);
world.removeTileEntity(pos);
}
/**
* {@inheritDoc} Delegate to main {@link Block#getPickBlock(IBlockState, RayTraceResult, World, BlockPos,
* EntityPlayer)}.
*/
@Nonnull
@Override
public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world,
@@ -86,15 +106,63 @@ public class BlockBounding extends Block {
return state1.getBlock().getPickBlock(state1, target, world, mainPos, player);
}
/**
* {@inheritDoc} Keep tile entity in world until after {@link Block#getDrops(NonNullList, IBlockAccess, BlockPos,
* IBlockState, int)}. Used together with {@link Block#harvestBlock(World, EntityPlayer, BlockPos, IBlockState,
* TileEntity, ItemStack)}.
*
* @author Forge
* @see BlockFlowerPot#removedByPlayer(IBlockState, World, BlockPos, EntityPlayer, boolean)
*/
@Override
public boolean removedByPlayer(@Nonnull IBlockState state, World world, @Nonnull BlockPos pos,
@Nonnull EntityPlayer player, boolean willHarvest) {
if (willHarvest) {
return true;
}
removeMainBlock(world, pos);
return super.removedByPlayer(state, world, pos, player, false);
}
/**
* {@inheritDoc} Delegate to main {@link Block#getDrops(NonNullList, IBlockAccess, BlockPos, IBlockState, int)}.
*/
@Override
public void getDrops(@Nonnull NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos,
@Nonnull IBlockState state, int fortune) {
BlockPos mainPos = getMainBlockPos(world, pos);
if (mainPos == null) {
return false;
return;
}
IBlockState state1 = world.getBlockState(mainPos);
return state1.getBlock().removedByPlayer(state1, world, mainPos, player, willHarvest);
state1.getBlock().getDrops(drops, world, mainPos, state1, fortune);
}
/**
* {@inheritDoc} Used together with {@link Block#removedByPlayer(IBlockState, World, BlockPos, EntityPlayer,
* boolean)}.
*
* @author Forge
* @see BlockFlowerPot#harvestBlock(World, EntityPlayer, BlockPos, IBlockState, TileEntity, ItemStack)
*/
@Override
public void harvestBlock(@Nonnull World world, EntityPlayer player, @Nonnull BlockPos pos,
@Nonnull IBlockState state, TileEntity te, ItemStack stack) {
super.harvestBlock(world, player, pos, state, te, stack);
world.setBlockToAir(pos);
}
/**
* Returns that this "cannot" be silk touched. This is so that {@link Block#getSilkTouchDrop(IBlockState)} is not
* called, because only {@link Block#getDrops(NonNullList, IBlockAccess, BlockPos, IBlockState, int)} supports tile
* entities. Our blocks keep their inventory and other behave like they are being silk touched by default anyway.
*
* @return false
*/
@Override
@Deprecated
protected boolean canSilkHarvest() {
return false;
}
@Override
@@ -124,17 +192,6 @@ public class BlockBounding extends Block {
return state1.getBlock().getPlayerRelativeBlockHardness(state1, player, world, mainPos);
}
@Override
public int quantityDropped(Random random) {
return 0;
}
@Nonnull
@Override
public Item getItemDropped(IBlockState state, Random random, int fortune) {
return Items.AIR;
}
@Nonnull
@Override
@Deprecated