From d8639613ee5c14ac2808a72293957ccaa76ea3c3 Mon Sep 17 00:00:00 2001 From: LemADEC Date: Sat, 25 Jun 2016 00:30:48 +0200 Subject: [PATCH] Implemented block saving with their content Added tooltip to item blocks to show their content --- src/main/java/cr0s/warpdrive/WarpDrive.java | 6 +- .../block/BlockAbstractContainer.java | 52 ++++++++++++++- .../block/ItemBlockAbstractBase.java | 63 +++++++++++++++++++ .../ItemBlockForceFieldProjector.java | 23 +++++++ .../forcefield/ItemBlockForceFieldRelay.java | 14 +++++ 5 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 src/main/java/cr0s/warpdrive/block/ItemBlockAbstractBase.java create mode 100644 src/main/java/cr0s/warpdrive/block/forcefield/ItemBlockForceFieldProjector.java create mode 100644 src/main/java/cr0s/warpdrive/block/forcefield/ItemBlockForceFieldRelay.java diff --git a/src/main/java/cr0s/warpdrive/WarpDrive.java b/src/main/java/cr0s/warpdrive/WarpDrive.java index 72c470fc..b2c1b9af 100644 --- a/src/main/java/cr0s/warpdrive/WarpDrive.java +++ b/src/main/java/cr0s/warpdrive/WarpDrive.java @@ -411,12 +411,12 @@ public class WarpDrive implements LoadingCallback { // FORCE FIELD PROJECTOR blockForceFieldProjectors[index] = new BlockForceFieldProjector(tier); - GameRegistry.registerBlock(blockForceFieldProjectors[index], "blockProjector" + tier); + GameRegistry.registerBlock(blockForceFieldProjectors[index], ItemBlockForceFieldProjector.class, "blockProjector" + tier); GameRegistry.registerTileEntity(TileEntityForceFieldProjector.class, MODID + ":blockProjector" + tier); - + // FORCE FIELD RELAY blockForceFieldRelays[index] = new BlockForceFieldRelay(tier); - GameRegistry.registerBlock(blockForceFieldRelays[index], "blockForceFieldRelay" + tier); + GameRegistry.registerBlock(blockForceFieldRelays[index], ItemBlockForceFieldRelay.class, "blockForceFieldRelay" + tier); GameRegistry.registerTileEntity(TileEntityForceFieldRelay.class, MODID + ":blockForceFieldRelay" + tier); } /* TODO diff --git a/src/main/java/cr0s/warpdrive/block/BlockAbstractContainer.java b/src/main/java/cr0s/warpdrive/block/BlockAbstractContainer.java index 343ed603..61711d75 100644 --- a/src/main/java/cr0s/warpdrive/block/BlockAbstractContainer.java +++ b/src/main/java/cr0s/warpdrive/block/BlockAbstractContainer.java @@ -1,11 +1,15 @@ package cr0s.warpdrive.block; +import cr0s.warpdrive.block.forcefield.TileEntityForceFieldProjector; import net.minecraft.block.Block; import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.MovingObjectPosition; import net.minecraft.world.World; import cr0s.warpdrive.WarpDrive; import cr0s.warpdrive.api.IBlockUpdateDetector; @@ -32,8 +36,8 @@ public abstract class BlockAbstractContainer extends BlockContainer { } @Override - public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityLiving, ItemStack itemstack) { - super.onBlockPlacedBy(world, x, y, z, entityLiving, itemstack); + public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityLiving, ItemStack itemStack) { + super.onBlockPlacedBy(world, x, y, z, entityLiving, itemStack); if (isRotating) { if (entityLiving != null) { int metadata; @@ -64,6 +68,50 @@ public abstract class BlockAbstractContainer extends BlockContainer { world.setBlockMetadataWithNotify(x, y, z, metadata, 3); } } + + TileEntity tileEntity = world.getTileEntity(x, y, z); + if (itemStack.hasTagCompound()) { + NBTTagCompound nbtTagCompound = (NBTTagCompound)itemStack.getTagCompound().copy(); + nbtTagCompound.setInteger("x", x); + nbtTagCompound.setInteger("y", y); + nbtTagCompound.setInteger("z", z); + tileEntity.readFromNBT(nbtTagCompound); + world.markBlockForUpdate(x, y, z); + } + } + + @Override + public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z, boolean willHarvest) { + return willHarvest || super.removedByPlayer(world, player, x, y, z, false); + } + + @Override + protected void dropBlockAsItem(World world, int x, int y, int z, ItemStack itemStack) { + TileEntity tileEntity = world.getTileEntity(x, y, z); + if (tileEntity == null) { + WarpDrive.logger.error("Missing tile entity for " + this + " at " + world + " " + x + " " + y + " " + z); + } else { + NBTTagCompound nbtTagCompound = new NBTTagCompound(); + tileEntity.writeToNBT(nbtTagCompound); + nbtTagCompound.removeTag("x"); + nbtTagCompound.removeTag("y"); + nbtTagCompound.removeTag("z"); + itemStack.setTagCompound(nbtTagCompound); + } + world.setBlockToAir(x, y, z); + super.dropBlockAsItem(world, x, y, z, itemStack); + } + + public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z, EntityPlayer entityPlayer) { + ItemStack itemStack = super.getPickBlock(target, world, x, y, z, entityPlayer); + TileEntity tileEntity = world.getTileEntity(x, y, z); + NBTTagCompound nbtTagCompound = new NBTTagCompound(); + tileEntity.writeToNBT(nbtTagCompound); + nbtTagCompound.removeTag("x"); + nbtTagCompound.removeTag("y"); + nbtTagCompound.removeTag("z"); + itemStack.setTagCompound(nbtTagCompound); + return itemStack; } @Override diff --git a/src/main/java/cr0s/warpdrive/block/ItemBlockAbstractBase.java b/src/main/java/cr0s/warpdrive/block/ItemBlockAbstractBase.java new file mode 100644 index 00000000..e0ac7698 --- /dev/null +++ b/src/main/java/cr0s/warpdrive/block/ItemBlockAbstractBase.java @@ -0,0 +1,63 @@ +package cr0s.warpdrive.block; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import cr0s.warpdrive.WarpDrive; +import net.minecraft.block.Block; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.IIcon; +import net.minecraft.util.StatCollector; + +import java.util.List; + +public class ItemBlockAbstractBase extends ItemBlock { + + public ItemBlockAbstractBase(Block block) { + super(block); // sets field_150939_a to block + } + + @Override + @SideOnly(Side.CLIENT) + public IIcon getIconFromDamage(int p_77617_1_) { + return field_150939_a.getIcon(2, p_77617_1_); + } + + @Override + public int getMetadata(int damage) { + return damage; + } + + public String getStatus(final NBTTagCompound nbtTagCompound) { + TileEntity tileEntity = field_150939_a.createTileEntity(null, 0); + if (tileEntity instanceof TileEntityAbstractEnergy) { + if (nbtTagCompound != null) { + tileEntity.readFromNBT(nbtTagCompound); + } + return ((TileEntityAbstractEnergy)tileEntity).getStatus(); + + } else { + return ""; + } + } + + @Override + public void addInformation(ItemStack itemStack, EntityPlayer entityPlayer, List list, boolean advancedItemTooltips) { + super.addInformation(itemStack, entityPlayer, list, advancedItemTooltips); + + String tooltipName1 = getUnlocalizedName(itemStack) + ".tooltip"; + if (StatCollector.canTranslate(tooltipName1)) { + WarpDrive.addTooltip(list, StatCollector.translateToLocalFormatted(tooltipName1)); + } + + String tooltipName2 = getUnlocalizedName() + ".tooltip"; + if ((!tooltipName1.equals(tooltipName2)) && StatCollector.canTranslate(tooltipName2)) { + WarpDrive.addTooltip(list, StatCollector.translateToLocalFormatted(tooltipName2)); + } + + WarpDrive.addTooltip(list, StatCollector.translateToLocalFormatted(getStatus(itemStack.getTagCompound()))); + } +} diff --git a/src/main/java/cr0s/warpdrive/block/forcefield/ItemBlockForceFieldProjector.java b/src/main/java/cr0s/warpdrive/block/forcefield/ItemBlockForceFieldProjector.java new file mode 100644 index 00000000..9dc1ebd5 --- /dev/null +++ b/src/main/java/cr0s/warpdrive/block/forcefield/ItemBlockForceFieldProjector.java @@ -0,0 +1,23 @@ +package cr0s.warpdrive.block.forcefield; + +import cr0s.warpdrive.block.ItemBlockAbstractBase; +import net.minecraft.block.Block; +import net.minecraft.item.ItemStack; + +public class ItemBlockForceFieldProjector extends ItemBlockAbstractBase { + + public ItemBlockForceFieldProjector(Block block) { + super(block); + setMaxDamage(0); + setHasSubtypes(true); + setUnlocalizedName("warpdrive.forcefield.projector"); + } + + @Override + public String getUnlocalizedName(ItemStack itemstack) { + if (itemstack == null) { + return getUnlocalizedName(); + } + return getUnlocalizedName() + (itemstack.getItemDamage() == 1 ? ".double" : ".single"); + } +} diff --git a/src/main/java/cr0s/warpdrive/block/forcefield/ItemBlockForceFieldRelay.java b/src/main/java/cr0s/warpdrive/block/forcefield/ItemBlockForceFieldRelay.java new file mode 100644 index 00000000..80f6b6d8 --- /dev/null +++ b/src/main/java/cr0s/warpdrive/block/forcefield/ItemBlockForceFieldRelay.java @@ -0,0 +1,14 @@ +package cr0s.warpdrive.block.forcefield; + +import cr0s.warpdrive.block.ItemBlockAbstractBase; +import net.minecraft.block.Block; + +public class ItemBlockForceFieldRelay extends ItemBlockAbstractBase { + + public ItemBlockForceFieldRelay(Block block) { + super(block); + setMaxDamage(0); + setHasSubtypes(false); + setUnlocalizedName("warpdrive.forcefield.relay"); + } +}