From af9e9e535a71d6e302861f259ce6b515b20f59b8 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Sun, 14 Jun 2015 12:35:28 +0100 Subject: [PATCH] Now stores eu when broken with a wrench. Added empty and full ones to creative tab. --- src/main/java/techreborn/init/ModBlocks.java | 2 +- .../techreborn/itemblocks/ItemBlockAesu.java | 92 +++++++++++++++++++ src/main/java/techreborn/tiles/TileAesu.java | 43 ++++++++- 3 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 src/main/java/techreborn/itemblocks/ItemBlockAesu.java diff --git a/src/main/java/techreborn/init/ModBlocks.java b/src/main/java/techreborn/init/ModBlocks.java index 19a1332f4..467a5b460 100644 --- a/src/main/java/techreborn/init/ModBlocks.java +++ b/src/main/java/techreborn/init/ModBlocks.java @@ -233,7 +233,7 @@ public class ModBlocks { GameRegistry.registerBlock(Idsu, "idsu"); Aesu = new BlockAesu(Material.rock); - GameRegistry.registerBlock(Aesu, "aesu"); + GameRegistry.registerBlock(Aesu, ItemBlockAesu.class, "aesu"); GameRegistry.registerTileEntity(TileAesu.class, "TileAesuTR"); Lesu = new BlockLesu(Material.rock); diff --git a/src/main/java/techreborn/itemblocks/ItemBlockAesu.java b/src/main/java/techreborn/itemblocks/ItemBlockAesu.java new file mode 100644 index 000000000..8c1e9c3f5 --- /dev/null +++ b/src/main/java/techreborn/itemblocks/ItemBlockAesu.java @@ -0,0 +1,92 @@ +package techreborn.itemblocks; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import ic2.api.item.ElectricItem; +import net.minecraft.block.Block; +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; +import techreborn.init.ModBlocks; +import techreborn.tiles.TileAesu; +import techreborn.tiles.TileQuantumChest; + +import java.util.List; + +public class ItemBlockAesu extends ItemBlock { + + public ItemBlockAesu(Block p_i45328_1_) + { + super(p_i45328_1_); + } + + @SuppressWarnings( + { "rawtypes", "unchecked" }) + @Override + @SideOnly(Side.CLIENT) + public void addInformation(ItemStack stack, EntityPlayer player, List list, + boolean par4) + { + if (stack != null && stack.hasTagCompound()) + { + if (stack.getTagCompound().getCompoundTag("tileEntity") != null) + list.add(stack.getTagCompound().getCompoundTag("tileEntity") + .getDouble("energy") + + " eu"); + } + } + + @Override + public boolean placeBlockAt(ItemStack stack, EntityPlayer player, + World world, int x, int y, int z, int side, float hitX, float hitY, + float hitZ, int metadata) + { + if (!world.setBlock(x, y, z, ModBlocks.Aesu, metadata, 3)) + { + return false; + } + if (world.getBlock(x, y, z) == ModBlocks.Aesu) + { + world.getBlock(x, y, z).onBlockPlacedBy(world, x, y, z, player, + stack); + world.getBlock(x, y, z).onPostBlockPlaced(world, x, y, z, metadata); + } + if (stack != null && stack.hasTagCompound()) + { + ((TileAesu) world.getTileEntity(x, y, z)) + .readFromNBTWithoutCoords(stack.getTagCompound() + .getCompoundTag("tileEntity")); + } + return true; + } + + @SuppressWarnings( + { "rawtypes", "unchecked" }) + @SideOnly(Side.CLIENT) + public void getSubItems(Item item, CreativeTabs par2CreativeTabs, List itemList){ + itemList.add(getDropWithNBT(0)); + itemList.add(getDropWithNBT(1000000000)); + } + + public ItemStack getDropWithNBT(double energy) + { + NBTTagCompound tileEntity = new NBTTagCompound(); + ItemStack dropStack = new ItemStack(ModBlocks.Aesu, 1); + writeToNBTWithoutCoords(tileEntity, energy); + dropStack.setTagCompound(new NBTTagCompound()); + dropStack.stackTagCompound.setTag("tileEntity", tileEntity); + return dropStack; + } + + public void writeToNBTWithoutCoords(NBTTagCompound tagCompound, double energy) + { + tagCompound.setDouble("energy", energy); + tagCompound.setDouble("euChange", 0); + tagCompound.setDouble("euLastTick", 0); + tagCompound.setBoolean("active", false); + } +} diff --git a/src/main/java/techreborn/tiles/TileAesu.java b/src/main/java/techreborn/tiles/TileAesu.java index a0e948d83..1ec879041 100644 --- a/src/main/java/techreborn/tiles/TileAesu.java +++ b/src/main/java/techreborn/tiles/TileAesu.java @@ -1,9 +1,12 @@ package techreborn.tiles; +import ic2.api.energy.EnergyNet; import ic2.api.tile.IWrenchable; import ic2.core.block.wiring.TileEntityElectricBlock; +import ic2.core.util.Util; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import techreborn.config.ConfigTechReborn; import techreborn.init.ModBlocks; import techreborn.util.Inventory; @@ -33,17 +36,25 @@ public class TileAesu extends TileEntityElectricBlock implements IWrenchable { if(ticks == ConfigTechReborn.aveargeEuOutTickTime){ euChange = -1; ticks = 0; + } else { ticks ++; euChange += energy - euLastTick; + if(euLastTick == energy){ + euChange = 0; + } } + euLastTick = energy; } @Override public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) { - return true; + if(!entityPlayer.isSneaking()){ + return true; + } + return false; } @Override @@ -78,7 +89,7 @@ public class TileAesu extends TileEntityElectricBlock implements IWrenchable { @Override public ItemStack getWrenchDrop(EntityPlayer entityPlayer) { - return new ItemStack(ModBlocks.Aesu, 1); + return getDropWithNBT(); } public boolean isComplete() @@ -133,5 +144,33 @@ public class TileAesu extends TileEntityElectricBlock implements IWrenchable { return (euChange / ticks); } + public ItemStack getDropWithNBT() + { + NBTTagCompound tileEntity = new NBTTagCompound(); + ItemStack dropStack = new ItemStack(ModBlocks.Aesu, 1); + writeToNBTWithoutCoords(tileEntity); + dropStack.setTagCompound(new NBTTagCompound()); + dropStack.stackTagCompound.setTag("tileEntity", tileEntity); + return dropStack; + } + + public void writeToNBTWithoutCoords(NBTTagCompound tagCompound) + { + tagCompound.setDouble("energy", this.energy); + tagCompound.setDouble("euChange", euChange); + tagCompound.setDouble("euLastTick", euLastTick); + tagCompound.setBoolean("active", this.getActive()); + tagCompound.setByte("redstoneMode", this.redstoneMode); + inventory.writeToNBT(tagCompound); + } + + public void readFromNBTWithoutCoords(NBTTagCompound nbttagcompound) { + this.energy = Util.limit(nbttagcompound.getDouble("energy"), 0.0D, (double) this.maxStorage + EnergyNet.instance.getPowerFromTier(this.tier)); + this.redstoneMode = nbttagcompound.getByte("redstoneMode"); + this.euChange = nbttagcompound.getDouble("euChange"); + this.euLastTick = nbttagcompound.getDouble("euLastTick"); + inventory.readFromNBT(nbttagcompound); + } + }