Merge pull request #25 from tomevoll/mc1.9
Store Energy in item when ccontrolelr is broken
This commit is contained in:
@@ -1,26 +1,40 @@
|
|||||||
package refinedstorage.block;
|
package refinedstorage.block;
|
||||||
|
|
||||||
|
import net.minecraft.block.Block;
|
||||||
|
import net.minecraft.block.BlockContainer;
|
||||||
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.properties.PropertyInteger;
|
import net.minecraft.block.properties.PropertyInteger;
|
||||||
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.EntityLivingBase;
|
||||||
|
import net.minecraft.entity.item.EntityItem;
|
||||||
import net.minecraft.entity.player.EntityPlayer;
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
|
import net.minecraft.init.Enchantments;
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.ItemBlock;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
|
import net.minecraft.stats.StatList;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraft.util.EnumFacing;
|
import net.minecraft.util.EnumFacing;
|
||||||
import net.minecraft.util.EnumHand;
|
import net.minecraft.util.EnumHand;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.util.math.RayTraceResult;
|
||||||
import net.minecraft.world.IBlockAccess;
|
import net.minecraft.world.IBlockAccess;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import refinedstorage.RefinedStorage;
|
import refinedstorage.RefinedStorage;
|
||||||
|
import refinedstorage.RefinedStorageBlocks;
|
||||||
import refinedstorage.RefinedStorageGui;
|
import refinedstorage.RefinedStorageGui;
|
||||||
import refinedstorage.item.ItemBlockBase;
|
import refinedstorage.item.ItemBlockBase;
|
||||||
|
import refinedstorage.item.ItemBlockController;
|
||||||
import refinedstorage.tile.TileController;
|
import refinedstorage.tile.TileController;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class BlockController extends BlockBase {
|
public class BlockController extends BlockBase {
|
||||||
public static final PropertyEnum TYPE = PropertyEnum.create("type", EnumControllerType.class);
|
public static final PropertyEnum TYPE = PropertyEnum.create("type", EnumControllerType.class);
|
||||||
@@ -33,7 +47,7 @@ public class BlockController extends BlockBase {
|
|||||||
@Override
|
@Override
|
||||||
public void getSubBlocks(Item item, CreativeTabs tab, List subItems) {
|
public void getSubBlocks(Item item, CreativeTabs tab, List subItems) {
|
||||||
for (int i = 0; i <= 1; i++) {
|
for (int i = 0; i <= 1; i++) {
|
||||||
subItems.add(new ItemStack(item, 1, i));
|
subItems.add(ItemBlockController.initNBT(new ItemStack(item, 1, i)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,9 +99,57 @@ public class BlockController extends BlockBase {
|
|||||||
public void breakBlock(World world, BlockPos pos, IBlockState state) {
|
public void breakBlock(World world, BlockPos pos, IBlockState state) {
|
||||||
((TileController) world.getTileEntity(pos)).onDestroyed();
|
((TileController) world.getTileEntity(pos)).onDestroyed();
|
||||||
|
|
||||||
super.breakBlock(world, pos, state);
|
super.breakBlock(world,pos,state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest) {
|
||||||
|
if (willHarvest) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase player, ItemStack itemStack) {
|
||||||
|
super.onBlockPlacedBy(world, pos, state, player, itemStack);
|
||||||
|
|
||||||
|
NBTTagCompound tag = itemStack.getTagCompound();
|
||||||
|
if(tag != null)
|
||||||
|
{
|
||||||
|
TileEntity tile = world.getTileEntity(pos);
|
||||||
|
if(tile instanceof TileController)
|
||||||
|
{
|
||||||
|
TileController controller = (TileController)tile;
|
||||||
|
controller.readNBT(tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@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)).writeNBT(tag);
|
||||||
|
stack.setTagCompound(tag);
|
||||||
|
|
||||||
|
drops.add(stack);
|
||||||
|
|
||||||
|
return drops;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasComparatorInputOverride(IBlockState state) {
|
public boolean hasComparatorInputOverride(IBlockState state) {
|
||||||
return true;
|
return true;
|
||||||
@@ -100,6 +162,8 @@ public class BlockController extends BlockBase {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Item createItemForBlock() {
|
public Item createItemForBlock() {
|
||||||
return new ItemBlockBase(this, true);
|
return new ItemBlockController();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
61
src/main/java/refinedstorage/item/ItemBlockController.java
Normal file
61
src/main/java/refinedstorage/item/ItemBlockController.java
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
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.EnumControllerType;
|
||||||
|
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) {
|
||||||
|
EnumControllerType type = EnumControllerType.values()[stack.getMetadata()];
|
||||||
|
|
||||||
|
int energyStored = 0;
|
||||||
|
int capacity = TileController.ENERGY_CAPACITY;
|
||||||
|
int percent = 0;
|
||||||
|
|
||||||
|
if(type == EnumControllerType.CREATIVE) {
|
||||||
|
energyStored = capacity;
|
||||||
|
percent = 100;
|
||||||
|
}
|
||||||
|
else if (stack.getTagCompound() != null && stack.getTagCompound().hasKey(TileController.NBT_ENERGY)) {
|
||||||
|
NBTTagCompound tag = stack.getTagCompound();
|
||||||
|
|
||||||
|
energyStored = tag.getInteger(TileController.NBT_ENERGY);
|
||||||
|
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) {
|
||||||
|
EnumControllerType type = EnumControllerType.values()[stack.getMetadata()];
|
||||||
|
NBTTagCompound tag = stack.getTagCompound();
|
||||||
|
if(tag == null) tag = new NBTTagCompound();
|
||||||
|
|
||||||
|
tag.setInteger(TileController.NBT_ENERGY, type == EnumControllerType.CREATIVE ? TileController.ENERGY_CAPACITY : 0);
|
||||||
|
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ import refinedstorage.network.MessageTileUpdate;
|
|||||||
|
|
||||||
public abstract class TileBase extends TileEntity implements ITickable {
|
public abstract class TileBase extends TileEntity implements ITickable {
|
||||||
public static final String NBT_DIRECTION = "Direction";
|
public static final String NBT_DIRECTION = "Direction";
|
||||||
|
public static final String NBT_ENERGY = "Energy";
|
||||||
|
|
||||||
public static final int UPDATE_RANGE = 32;
|
public static final int UPDATE_RANGE = 32;
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ import refinedstorage.util.InventoryUtils;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class TileController extends TileBase implements IEnergyReceiver, INetworkTile, IRedstoneModeConfig {
|
public class TileController extends TileBase implements IEnergyReceiver, INetworkTile, IRedstoneModeConfig {
|
||||||
|
|
||||||
public class ClientSideMachine {
|
public class ClientSideMachine {
|
||||||
public ItemStack stack;
|
public ItemStack stack;
|
||||||
public int energyUsage;
|
public int energyUsage;
|
||||||
@@ -44,6 +45,8 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
|||||||
public int z;
|
public int z;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final int ENERGY_CAPACITY = 32000;
|
||||||
|
|
||||||
private List<ItemGroup> itemGroups = new ArrayList<ItemGroup>();
|
private List<ItemGroup> itemGroups = new ArrayList<ItemGroup>();
|
||||||
private List<IStorage> storages = new ArrayList<IStorage>();
|
private List<IStorage> storages = new ArrayList<IStorage>();
|
||||||
private List<WirelessGridConsumer> wirelessGridConsumers = new ArrayList<WirelessGridConsumer>();
|
private List<WirelessGridConsumer> wirelessGridConsumers = new ArrayList<WirelessGridConsumer>();
|
||||||
@@ -56,7 +59,7 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
|||||||
|
|
||||||
private List<BlockPos> visited = new ArrayList<BlockPos>();
|
private List<BlockPos> visited = new ArrayList<BlockPos>();
|
||||||
|
|
||||||
private EnergyStorage energy = new EnergyStorage(32000);
|
private EnergyStorage energy = new EnergyStorage(ENERGY_CAPACITY);
|
||||||
private int energyUsage;
|
private int energyUsage;
|
||||||
|
|
||||||
private boolean destroyed = false;
|
private boolean destroyed = false;
|
||||||
@@ -362,7 +365,10 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
|||||||
@Override
|
@Override
|
||||||
public void readFromNBT(NBTTagCompound nbt) {
|
public void readFromNBT(NBTTagCompound nbt) {
|
||||||
super.readFromNBT(nbt);
|
super.readFromNBT(nbt);
|
||||||
|
readNBT(nbt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void readNBT(NBTTagCompound nbt) {
|
||||||
energy.readFromNBT(nbt);
|
energy.readFromNBT(nbt);
|
||||||
|
|
||||||
if (nbt.hasKey(RedstoneMode.NBT)) {
|
if (nbt.hasKey(RedstoneMode.NBT)) {
|
||||||
@@ -373,9 +379,11 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor
|
|||||||
@Override
|
@Override
|
||||||
public void writeToNBT(NBTTagCompound nbt) {
|
public void writeToNBT(NBTTagCompound nbt) {
|
||||||
super.writeToNBT(nbt);
|
super.writeToNBT(nbt);
|
||||||
|
writeNBT(nbt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeNBT(NBTTagCompound nbt) {
|
||||||
energy.writeToNBT(nbt);
|
energy.writeToNBT(nbt);
|
||||||
|
|
||||||
nbt.setInteger(RedstoneMode.NBT, redstoneMode.id);
|
nbt.setInteger(RedstoneMode.NBT, redstoneMode.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user