package lostcave.deathchests.block; import java.util.ArrayList; import java.util.Random; import java.util.UUID; import cpw.mods.fml.common.FMLCommonHandler; import lostcave.deathchests.util.Config; import lostcave.deathchests.util.Debug; import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ChatComponentText; import net.minecraft.util.IIcon; import net.minecraft.world.Explosion; import net.minecraft.world.World; public class BlockDeathChest extends BlockContainer { private static final BlockDeathChest instance = new BlockDeathChest(); public static BlockDeathChest getInstance() { return instance; } private IIcon[] icons = new IIcon[6]; private BlockDeathChest() { super(Material.rock); this.setBlockName("death_chest"); this.disableStats(); this.setBlockUnbreakable(); // same value as bedrock this.setResistance(6000000.0F); this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F); this.setBlockTextureName("deathchests:deathchest_top"); } @Override public void registerBlockIcons(IIconRegister register) { icons[0] = register.registerIcon("deathchests:deathchest_top"); icons[1] = register.registerIcon("deathchests:deathchest_top"); icons[2] = register.registerIcon("deathchests:deathchest_side"); icons[3] = register.registerIcon("deathchests:deathchest_side"); icons[4] = register.registerIcon("deathchests:deathchest_side"); icons[5] = register.registerIcon("deathchests:deathchest_side"); } @Override public IIcon getIcon(int side, int meta) { return icons[side]; } @Override public boolean isOpaqueCube() { return false; } @Override public boolean renderAsNormalBlock() { return true; } @Override protected boolean canSilkHarvest() { return false; } @Override public boolean canDropFromExplosion(Explosion p_149659_1_) { return false; } @Override public Item getItemDropped(int p_149650_1_, Random p_149650_2_, int p_149650_3_) { return null; } /** * Called upon block activation (right click on the block.) */ @Override public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_) { if (world.isRemote) return true; TileDeathChest tileDeathChest = (TileDeathChest) world.getTileEntity(x, y, z); if (tileDeathChest != null) { tileDeathChest.requestItems(player); return true; } else return true; } /** * Called when a player hits the block. Args: world, x, y, z, player */ @Override public void onBlockClicked(World world, int x, int y, int z, EntityPlayer player) { if (world.isRemote) return; TileDeathChest tileDeathChest = (TileDeathChest) world.getTileEntity(x, y, z); if (tileDeathChest != null) { tileDeathChest.requestItems(player); } } /** * Returns a new instance of a block's tile entity class. Called on placing the * block. */ @Override public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) { return new TileDeathChest(); } public static class TileDeathChest extends TileEntity { private ArrayList containedItems = new ArrayList<>(); public UUID owner = null; public void requestItems(EntityPlayer player) { if (player.getGameProfile().getId() == owner || Config.allowOtherPlayers) { this.worldObj.setBlockToAir(this.xCoord, this.yCoord, this.zCoord); for (int i = 0; i < containedItems.size(); i++) { this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, xCoord, yCoord, zCoord, containedItems.get(i))); } } else { player.addChatMessage(new ChatComponentText(Config.notAllowedMessage)); } } public void addItemStack(ItemStack itemStack) { containedItems.add(itemStack); } @Override public void readFromNBT(NBTTagCompound compound) { Debug.out("Loading death chest from disk"); super.readFromNBT(compound); containedItems = new ArrayList<>(); NBTTagList stacks = (NBTTagList) compound.getTag("stored_items"); for (int i = 0; i < stacks.tagCount(); i++) { ItemStack stack = ItemStack.loadItemStackFromNBT(stacks.getCompoundTagAt(i)); Debug.out(stack.getDisplayName()); containedItems.add(stack); } Debug.out("--"); } @Override public void writeToNBT(NBTTagCompound compound) { Debug.out("Writing death chest to disk"); super.writeToNBT(compound); NBTTagList stacks = new NBTTagList(); for (int i = 0; i < containedItems.size(); i++) { NBTTagCompound itemStackCompound = new NBTTagCompound(); containedItems.get(i).writeToNBT(itemStackCompound); stacks.appendTag(itemStackCompound); } compound.setTag("stored_items", stacks); } } }