Added ItemBlock to store energy when controller is broken.

modified:   src/main/java/refinedstorage/block/BlockController.java
	new file:   src/main/java/refinedstorage/item/ItemBlockController.java
	modified:   src/main/java/refinedstorage/tile/TileBase.java
	modified:   src/main/java/refinedstorage/tile/TileController.java
This commit is contained in:
Tom Erik
2016-04-05 17:49:00 +02:00
parent 5733a553a2
commit 7f1fba678e
4 changed files with 84 additions and 23 deletions

View File

@@ -14,6 +14,7 @@ import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Enchantments;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.stats.StatList;
@@ -25,8 +26,10 @@ import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import refinedstorage.RefinedStorage;
import refinedstorage.RefinedStorageBlocks;
import refinedstorage.RefinedStorageGui;
import refinedstorage.item.ItemBlockBase;
import refinedstorage.item.ItemBlockController;
import refinedstorage.tile.TileController;
import java.util.ArrayList;
@@ -44,7 +47,7 @@ public class BlockController extends BlockBase {
@Override
public void getSubBlocks(Item item, CreativeTabs tab, List subItems) {
for (int i = 0; i <= 1; i++) {
subItems.add(new ItemStack(item, 1, i));
subItems.add(ItemBlockController.initNBT(new ItemStack(item, 1, i)));
}
}
@@ -99,27 +102,20 @@ public class BlockController extends BlockBase {
super.breakBlock(world,pos,state);
}
//Unless making a ItemBlock this seems to be the only solution to store NBT on the dropped stack
// any function called after this like get drops will not be able to get the tile
public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te, ItemStack stack)
{
player.addStat(StatList.func_188055_a(this));
player.addExhaustion(0.025F);
harvesters.set(player);
int i = EnchantmentHelper.getEnchantmentLevel(Enchantments.fortune, stack);
if ((te instanceof TileController)) {
TileController controller = (TileController) te;
ItemStack item = new ItemStack(getItemDropped(state, null, 0), 1, damageDropped(state));
NBTTagCompound tag = new NBTTagCompound();
controller.writeItemToNBT(tag);
item.setTagCompound(tag);
if (!worldIn.isRemote && !worldIn.restoringBlockSnapshots)
{
spawnAsEntity(worldIn, pos, item);
}
@Override
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest) {
if (willHarvest) {
return true;
}
harvesters.set(null);
return super.removedByPlayer(state, world, pos, player, willHarvest);
}
@Override
public void harvestBlock(World world, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity tile, ItemStack stack) {
super.harvestBlock(world, player, pos, state, tile, stack);
world.setBlockToAir(pos);
}
@Override
@@ -138,6 +134,22 @@ public class BlockController extends BlockBase {
}
}
@Override
public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
List<ItemStack> drops = new ArrayList<ItemStack>();
ItemStack stack = new ItemStack(RefinedStorageBlocks.CONTROLLER, 1, RefinedStorageBlocks.CONTROLLER.getMetaFromState(state));
NBTTagCompound tag = new NBTTagCompound();
((TileController) world.getTileEntity(pos)).writeItemToNBT(tag);
stack.setTagCompound(tag);
drops.add(stack);
return drops;
}
@Override
public boolean hasComparatorInputOverride(IBlockState state) {
return true;
@@ -150,7 +162,7 @@ public class BlockController extends BlockBase {
@Override
public Item createItemForBlock() {
return new ItemBlockBase(this, true);
return new ItemBlockController();
}

View File

@@ -0,0 +1,46 @@
package refinedstorage.item;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import refinedstorage.RefinedStorageBlocks;
import refinedstorage.block.EnumStorageType;
import refinedstorage.tile.TileController;
import java.util.List;
/**
* Created by zyberwax on 05.04.2016.
*/
public class ItemBlockController extends ItemBlockBase {
public ItemBlockController() {
super(RefinedStorageBlocks.CONTROLLER, true);
}
@Override
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean b) {
if (stack.getTagCompound() != null && stack.getTagCompound().hasKey(TileController.NBT_ENERGY)) {
NBTTagCompound tag = stack.getTagCompound();
int energyStored = tag.getInteger(TileController.NBT_ENERGY);
int capacity = TileController.ENERGY_CAPACITY;
int percent = (int)((float)energyStored / (capacity) * 100);
//TODO: Format numbers ?
list.add("RF: " + energyStored + "/" + capacity + "(" + percent + "%)");
}
}
@Override
public void onCreated(ItemStack stack, World world, EntityPlayer player) {
super.onCreated(stack, world, player);
initNBT(stack);
}
public static ItemStack initNBT(ItemStack stack) {
return stack;
}
}

View File

@@ -20,6 +20,7 @@ import refinedstorage.network.MessageTileUpdate;
public abstract class TileBase extends TileEntity implements ITickable {
public static final String NBT_DIRECTION = "Direction";
public static final String NBT_ENERGY = "Energy";
public static final int UPDATE_RANGE = 32;

View File

@@ -45,6 +45,8 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
public int z;
}
public static final int ENERGY_CAPACITY = 32000;
private List<ItemGroup> itemGroups = new ArrayList<ItemGroup>();
private List<IStorage> storages = new ArrayList<IStorage>();
private List<WirelessGridConsumer> wirelessGridConsumers = new ArrayList<WirelessGridConsumer>();
@@ -57,7 +59,7 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
private List<BlockPos> visited = new ArrayList<BlockPos>();
private EnergyStorage energy = new EnergyStorage(32000);
private EnergyStorage energy = new EnergyStorage(ENERGY_CAPACITY);
private int energyUsage;
private boolean destroyed = false;