storage block retains its items on break
This commit is contained in:
@@ -1,20 +1,26 @@
|
||||
package storagecraft.block;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import net.minecraft.block.properties.IProperty;
|
||||
import net.minecraft.block.properties.PropertyEnum;
|
||||
import net.minecraft.block.state.BlockState;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.BlockPos;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
import storagecraft.StorageCraft;
|
||||
import storagecraft.StorageCraftBlocks;
|
||||
import storagecraft.StorageCraftGUI;
|
||||
import storagecraft.item.ItemBlockStorage;
|
||||
import storagecraft.tile.TileStorage;
|
||||
|
||||
public class BlockStorage extends BlockMachine
|
||||
@@ -31,7 +37,7 @@ public class BlockStorage extends BlockMachine
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,15 @@
|
||||
package storagecraft.item;
|
||||
|
||||
import java.util.List;
|
||||
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
|
||||
{
|
||||
@@ -8,4 +17,40 @@ public class ItemBlockStorage extends ItemBlockBase
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@@ -32,7 +32,7 @@ public class ItemStorageCell extends ItemBase
|
||||
{
|
||||
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);
|
||||
|
||||
initNBT(stack);
|
||||
}
|
||||
|
||||
private ItemStack initNBT(ItemStack stack)
|
||||
{
|
||||
stack.setTagCompound(NBTStorage.getBaseNBT());
|
||||
|
||||
return stack;
|
||||
NBTStorage.initNBT(stack);
|
||||
}
|
||||
}
|
||||
|
@@ -149,4 +149,11 @@ public class NBTStorage implements IStorage
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
public static ItemStack initNBT(ItemStack stack)
|
||||
{
|
||||
stack.setTagCompound(NBTStorage.getBaseNBT());
|
||||
|
||||
return stack;
|
||||
}
|
||||
}
|
||||
|
@@ -283,6 +283,16 @@ public class TileStorage extends TileMachine implements IStorageProvider, IStora
|
||||
return new NBTStorage(tag, getCapacity(), priority);
|
||||
}
|
||||
|
||||
public NBTTagCompound getStorageTag()
|
||||
{
|
||||
return tag;
|
||||
}
|
||||
|
||||
public void setStorageTag(NBTTagCompound tag)
|
||||
{
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority()
|
||||
{
|
||||
|
Reference in New Issue
Block a user