Fix Chests not saving the custom name given to them when placed in the world, and ensure the custom name get's returned to the user when broken. Closes #182

This commit is contained in:
alexbegt 2019-06-06 12:32:14 -04:00
parent ac7ab8ab3c
commit a1339cf6db
2 changed files with 63 additions and 6 deletions

View File

@ -25,15 +25,18 @@ import net.minecraft.block.state.BlockFaceShape;
import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs; import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Enchantments;
import net.minecraft.init.Items; import net.minecraft.init.Items;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryHelper; import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagByte; import net.minecraft.nbt.NBTTagByte;
import net.minecraft.stats.StatList;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumBlockRenderType; import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
@ -45,6 +48,7 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.Explosion; import net.minecraft.world.Explosion;
import net.minecraft.world.IBlockAccess; import net.minecraft.world.IBlockAccess;
import net.minecraft.world.ILockableContainer; import net.minecraft.world.ILockableContainer;
import net.minecraft.world.IWorldNameable;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
@ -198,6 +202,11 @@ public class BlockIronChest extends Block
teic.setFacing(placer.getHorizontalFacing().getOpposite()); teic.setFacing(placer.getHorizontalFacing().getOpposite());
worldIn.notifyBlockUpdate(pos, state, state, 3); worldIn.notifyBlockUpdate(pos, state, state, 3);
if (stack.hasDisplayName())
{
teic.setCustomName(stack.getDisplayName());
}
} }
} }
@ -302,20 +311,68 @@ public class BlockIronChest extends Block
public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune) public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
{ {
Random rand = world instanceof World ? ((World) world).rand : RANDOM; Random rand = world instanceof World ? ((World) world).rand : RANDOM;
TileEntity tileentity = world.getTileEntity(pos);
int count = quantityDropped(state, fortune, rand); int count = quantityDropped(state, fortune, rand);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
Item item = this.getItemDropped(state, rand, fortune); Item item = this.getItemDropped(state, rand, fortune);
if (item != Items.AIR) if (item != Items.AIR)
{ {
ItemStack stack = new ItemStack(item, 1, this.damageDropped(state)); ItemStack stack = new ItemStack(item, 1, this.damageDropped(state));
if (tileentity instanceof IWorldNameable && ((IWorldNameable) tileentity).hasCustomName())
{
stack.setStackDisplayName(((IWorldNameable) tileentity).getName());
}
if (state.getValue(VARIANT_PROP) == IronChestType.DIRTCHEST9000) if (state.getValue(VARIANT_PROP) == IronChestType.DIRTCHEST9000)
{ {
stack.setTagInfo("dirtchest", new NBTTagByte((byte) 1)); stack.setTagInfo("dirtchest", new NBTTagByte((byte) 1));
} }
drops.add(stack); drops.add(stack);
} }
} }
} }
@Override
public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack stack)
{
if (te instanceof IWorldNameable && ((IWorldNameable) te).hasCustomName())
{
player.addStat(StatList.getBlockStats(this));
player.addExhaustion(0.005F);
if (worldIn.isRemote)
{
return;
}
int i = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, stack);
Item item = this.getItemDropped(state, worldIn.rand, i);
if (item == Items.AIR)
{
return;
}
ItemStack itemstack = new ItemStack(item, 1, this.damageDropped(state));
if (state.getValue(VARIANT_PROP) == IronChestType.DIRTCHEST9000)
{
itemstack.setTagInfo("dirtchest", new NBTTagByte((byte) 1));
}
itemstack.setStackDisplayName(((IWorldNameable) te).getName());
spawnAsEntity(worldIn, pos, itemstack);
}
else
{
super.harvestBlock(worldIn, player, pos, state, (TileEntity) null, stack);
}
}
} }

View File

@ -797,22 +797,22 @@ public class TileEntityIronShulkerBox extends TileEntityLockableLoot implements
public ItemStack getDrop(IBlockState state, boolean inBreakBlock) public ItemStack getDrop(IBlockState state, boolean inBreakBlock)
{ {
BlockIronShulkerBox block = (BlockIronShulkerBox) state.getBlock(); BlockIronShulkerBox block = (BlockIronShulkerBox) state.getBlock();
if (!isCleared() && (!inBreakBlock || shouldDropInBreakBlock())) if (!this.isCleared() && (!inBreakBlock || this.shouldDropInBreakBlock()))
{ {
if (!beenUpgraded()) if (!this.beenUpgraded())
{ {
ItemStack itemstack = new ItemStack(Item.getItemFromBlock(block), 1, state.getValue(BlockIronShulkerBox.VARIANT_PROP).ordinal()); ItemStack itemstack = new ItemStack(Item.getItemFromBlock(block), 1, state.getValue(BlockIronShulkerBox.VARIANT_PROP).ordinal());
NBTTagCompound nbttagcompound = new NBTTagCompound(); NBTTagCompound nbttagcompound = new NBTTagCompound();
NBTTagCompound nbttagcompound1 = new NBTTagCompound(); NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound.setTag("BlockEntityTag", saveToNbt(nbttagcompound1)); nbttagcompound.setTag("BlockEntityTag", this.saveToNbt(nbttagcompound1));
itemstack.setTagCompound(nbttagcompound); itemstack.setTagCompound(nbttagcompound);
if (hasCustomName()) if (this.hasCustomName())
{ {
itemstack.setStackDisplayName(getName()); itemstack.setStackDisplayName(this.getName());
setCustomName(""); this.setCustomName("");
} }
return itemstack; return itemstack;