This commit is contained in:
Joseph C. Sible 2018-06-16 21:06:41 -04:00 committed by Alexander (alexbegt)
parent 5cdce170c7
commit 08cf8e0bb2
2 changed files with 84 additions and 26 deletions

View File

@ -261,6 +261,40 @@ public class BlockIronShulkerBox extends Block
return state.getValue(VARIANT_PROP).ordinal(); return state.getValue(VARIANT_PROP).ordinal();
} }
/**
* Called when a player removes a block. This is responsible for
* actually destroying the block, and the block is intact at time of call.
* This is called regardless of whether the player can harvest the block or
* not.
*
* Return true if the block is actually destroyed.
*
* Note: When used in multiplayer, this is called on both client and
* server sides!
*
* @param state The current state.
* @param world The current world
* @param player The player damaging the block, may be null
* @param pos Block position in world
* @param willHarvest True if Block.harvestBlock will be called after this, if the return in true.
* Can be useful to delay the destruction of tile entities till after harvestBlock
* @return True if the block is actually destroyed.
*/
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest)
{
return willHarvest || super.removedByPlayer(state, world, pos, player, false);
}
/**
* Spawns the block's drops in the world. By the time this is called the Block has possibly been set to air via
* Block.removedByPlayer
*/
public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack stack)
{
super.harvestBlock(worldIn, player, pos, state, te, stack);
worldIn.setBlockToAir(pos);
}
@Override @Override
public void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player) public void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player)
{ {
@ -271,11 +305,26 @@ public class BlockIronShulkerBox extends Block
} }
/** /**
* Spawns this Block's drops into the World as EntityItems. * This gets a complete list of items dropped from this block.
*
* @param drops add all items this block drops to this drops list
* @param world The current world
* @param pos Block position in world
* @param state Current state
* @param fortune Breakers fortune level
*/ */
@Override public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
{ {
TileEntity tileentity = world.getTileEntity(pos);
if (tileentity instanceof TileEntityIronShulkerBox)
{
ItemStack itemstack = ((TileEntityIronShulkerBox) tileentity).getDrop(state, false);
if(!itemstack.isEmpty())
{
drops.add(itemstack);
}
}
} }
/** /**
@ -288,29 +337,11 @@ public class BlockIronShulkerBox extends Block
if (tileentity instanceof TileEntityIronShulkerBox) if (tileentity instanceof TileEntityIronShulkerBox)
{ {
TileEntityIronShulkerBox tileentityironshulkerbox = (TileEntityIronShulkerBox) tileentity; ItemStack itemstack = ((TileEntityIronShulkerBox) tileentity).getDrop(state, true);
if(!itemstack.isEmpty())
if (!tileentityironshulkerbox.isCleared() && tileentityironshulkerbox.shouldDrop())
{ {
if (!tileentityironshulkerbox.beenUpgraded())
{
ItemStack itemstack = new ItemStack(Item.getItemFromBlock(this), 1, state.getValue(VARIANT_PROP).ordinal());
NBTTagCompound nbttagcompound = new NBTTagCompound();
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound.setTag("BlockEntityTag", ((TileEntityIronShulkerBox) tileentity).saveToNbt(nbttagcompound1));
itemstack.setTagCompound(nbttagcompound);
if (tileentityironshulkerbox.hasCustomName())
{
itemstack.setStackDisplayName(tileentityironshulkerbox.getName());
tileentityironshulkerbox.setCustomName("");
}
spawnAsEntity(worldIn, pos, itemstack); spawnAsEntity(worldIn, pos, itemstack);
} }
}
worldIn.updateComparatorOutputLevel(pos, state.getBlock()); worldIn.updateComparatorOutputLevel(pos, state.getBlock());
} }

View File

@ -34,6 +34,7 @@ import net.minecraft.inventory.Container;
import net.minecraft.inventory.ISidedInventory; import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.ItemStackHelper; import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.EnumDyeColor; import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager; import net.minecraft.network.NetworkManager;
@ -777,9 +778,35 @@ public class TileEntityIronShulkerBox extends TileEntityLockableLoot implements
this.destroyedByCreativePlayer = destoryedByCreativeUser; this.destroyedByCreativePlayer = destoryedByCreativeUser;
} }
public boolean shouldDrop() public boolean shouldDropInBreakBlock()
{ {
return !this.isDestroyedByCreativePlayer() || !this.isEmpty() || this.hasCustomName() || this.lootTable != null; return this.isDestroyedByCreativePlayer() && (!this.isEmpty() || this.hasCustomName() || this.lootTable != null);
}
public ItemStack getDrop(IBlockState state, boolean inBreakBlock) {
BlockIronShulkerBox block = (BlockIronShulkerBox) state.getBlock();
if (!isCleared() && (!inBreakBlock || shouldDropInBreakBlock()))
{
if (!beenUpgraded())
{
ItemStack itemstack = new ItemStack(Item.getItemFromBlock(block), 1, state.getValue(BlockIronShulkerBox.VARIANT_PROP).ordinal());
NBTTagCompound nbttagcompound = new NBTTagCompound();
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound.setTag("BlockEntityTag", saveToNbt(nbttagcompound1));
itemstack.setTagCompound(nbttagcompound);
if (hasCustomName())
{
itemstack.setStackDisplayName(getName());
setCustomName("");
}
return itemstack;
}
}
return ItemStack.EMPTY;
} }
protected void sendTopStacksPacket() protected void sendTopStacksPacket()