storage block retains its items on break

This commit is contained in:
Raoul Van den Berge
2016-01-31 17:11:00 +01:00
parent e6cbbe285b
commit 3df1843a7f
5 changed files with 121 additions and 10 deletions

View File

@@ -1,20 +1,26 @@
package storagecraft.block; package storagecraft.block;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.minecraft.block.properties.IProperty; import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum; import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockState; import net.minecraft.block.state.BlockState;
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.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos; import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World; import net.minecraft.world.World;
import storagecraft.StorageCraft; import storagecraft.StorageCraft;
import storagecraft.StorageCraftBlocks;
import storagecraft.StorageCraftGUI; import storagecraft.StorageCraftGUI;
import storagecraft.item.ItemBlockStorage;
import storagecraft.tile.TileStorage; import storagecraft.tile.TileStorage;
public class BlockStorage extends BlockMachine public class BlockStorage extends BlockMachine
@@ -31,7 +37,7 @@ public class BlockStorage extends BlockMachine
{ {
for (int i = 0; i <= 4; i++) for (int i = 0; i <= 4; i++)
{ {
subItems.add(new ItemStack(item, 1, i)); subItems.add(ItemBlockStorage.initNBT(new ItemStack(item, 1, i)));
} }
} }
@@ -74,4 +80,54 @@ public class BlockStorage extends BlockMachine
return true; return true;
} }
@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase player, ItemStack stack)
{
super.onBlockPlacedBy(world, pos, state, player, stack);
NBTTagCompound tag = stack.getTagCompound();
if (tag != null && tag.hasKey(TileStorage.NBT_STORAGE))
{
((TileStorage) world.getTileEntity(pos)).setStorageTag((NBTTagCompound) tag.getTag(TileStorage.NBT_STORAGE));
}
}
@Override
public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
{
List<ItemStack> drops = new ArrayList<ItemStack>();
ItemStack stack = new ItemStack(StorageCraftBlocks.STORAGE, 1, StorageCraftBlocks.STORAGE.getMetaFromState(state));
NBTTagCompound tag = new NBTTagCompound();
tag.setTag(TileStorage.NBT_STORAGE, ((TileStorage) world.getTileEntity(pos)).getStorageTag());
stack.setTagCompound(tag);
drops.add(stack);
return drops;
}
@Override
public boolean removedByPlayer(World world, BlockPos pos, EntityPlayer player, boolean willHarvest)
{
if (willHarvest)
{
return true;
}
return super.removedByPlayer(world, pos, player, willHarvest);
}
@Override
public void harvestBlock(World world, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity tile)
{
super.harvestBlock(world, player, pos, state, tile);
world.setBlockToAir(pos);
}
} }

View File

@@ -1,6 +1,15 @@
package storagecraft.item; package storagecraft.item;
import java.util.List;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import storagecraft.block.EnumStorageType;
import storagecraft.storage.NBTStorage;
import storagecraft.tile.TileStorage;
public class ItemBlockStorage extends ItemBlockBase public class ItemBlockStorage extends ItemBlockBase
{ {
@@ -8,4 +17,40 @@ public class ItemBlockStorage extends ItemBlockBase
{ {
super(block); super(block);
} }
@Override
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean b)
{
EnumStorageType type = EnumStorageType.getById(stack.getMetadata());
NBTTagCompound tag = stack.getTagCompound().getCompoundTag(TileStorage.NBT_STORAGE);
if (type == EnumStorageType.TYPE_CREATIVE)
{
list.add(String.format(StatCollector.translateToLocal("misc.storagecraft:storage.stored"), NBTStorage.getStored(tag)));
}
else
{
list.add(String.format(StatCollector.translateToLocal("misc.storagecraft:storage.stored_capacity"), NBTStorage.getStored(tag), type.getCapacity()));
}
}
@Override
public void onCreated(ItemStack stack, World world, EntityPlayer player)
{
super.onCreated(stack, world, player);
initNBT(stack);
}
public static ItemStack initNBT(ItemStack stack)
{
NBTTagCompound tag = new NBTTagCompound();
tag.setTag(TileStorage.NBT_STORAGE, NBTStorage.getBaseNBT());
stack.setTagCompound(tag);
return stack;
}
} }

View File

@@ -32,7 +32,7 @@ public class ItemStorageCell extends ItemBase
{ {
for (int i = 0; i < 5; ++i) for (int i = 0; i < 5; ++i)
{ {
list.add(initNBT(new ItemStack(item, 1, i))); list.add(NBTStorage.initNBT(new ItemStack(item, 1, i)));
} }
} }
@@ -54,13 +54,6 @@ public class ItemStorageCell extends ItemBase
{ {
super.onCreated(stack, world, player); super.onCreated(stack, world, player);
initNBT(stack); NBTStorage.initNBT(stack);
}
private ItemStack initNBT(ItemStack stack)
{
stack.setTagCompound(NBTStorage.getBaseNBT());
return stack;
} }
} }

View File

@@ -149,4 +149,11 @@ public class NBTStorage implements IStorage
return tag; return tag;
} }
public static ItemStack initNBT(ItemStack stack)
{
stack.setTagCompound(NBTStorage.getBaseNBT());
return stack;
}
} }

View File

@@ -283,6 +283,16 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
return new NBTStorage(tag, getCapacity(), priority); return new NBTStorage(tag, getCapacity(), priority);
} }
public NBTTagCompound getStorageTag()
{
return tag;
}
public void setStorageTag(NBTTagCompound tag)
{
this.tag = tag;
}
@Override @Override
public int getPriority() public int getPriority()
{ {