From 11866075c5650581b21ae55c261bba30a4e72fb0 Mon Sep 17 00:00:00 2001 From: DarkGuardsman Date: Tue, 31 Dec 2013 14:20:59 -0500 Subject: [PATCH] Improved multiBlock code --- .../minecraft/interfaces/IMultiBlock.java | 15 +- .../minecraft/prefab/BlockMulti.java | 253 ++++++++---------- .../minecraft/prefab/TileEntityMulti.java | 83 +++--- .../minecraft/prefab/invgui/SlotSpecific.java | 137 +++++----- 4 files changed, 228 insertions(+), 260 deletions(-) diff --git a/src/com/builtbroken/minecraft/interfaces/IMultiBlock.java b/src/com/builtbroken/minecraft/interfaces/IMultiBlock.java index 295f88b92..80f18ef9e 100644 --- a/src/com/builtbroken/minecraft/interfaces/IMultiBlock.java +++ b/src/com/builtbroken/minecraft/interfaces/IMultiBlock.java @@ -1,22 +1,13 @@ package com.builtbroken.minecraft.interfaces; - -import net.minecraft.tileentity.TileEntity; import universalelectricity.api.vector.Vector3; /** Interface to be applied to tile entity blocks that occupies more than one block space. Useful for * large machines. * * @author Calclavia */ -public interface IMultiBlock extends IBlockActivated +public interface IMultiBlock { - /** Called when this multiblock is created - * - * @param placedPosition - The position the block was placed at */ - public void onCreate(Vector3 placedPosition); - - /** Called when one of the multiblocks of this block is destroyed - * - * @param callingBlock - The tile entity who called the onDestroy function */ - public void onDestroy(TileEntity callingBlock); + /** @return An array of Vector3 containing the multiblock relative coordinates to be constructed. */ + public Vector3[] getMultiBlockVectors(); } diff --git a/src/com/builtbroken/minecraft/prefab/BlockMulti.java b/src/com/builtbroken/minecraft/prefab/BlockMulti.java index 974e08996..f3e2681de 100644 --- a/src/com/builtbroken/minecraft/prefab/BlockMulti.java +++ b/src/com/builtbroken/minecraft/prefab/BlockMulti.java @@ -1,175 +1,160 @@ package com.builtbroken.minecraft.prefab; -import java.util.List; import java.util.Random; -import java.util.Set; + +import com.builtbroken.minecraft.DarkCore; +import com.builtbroken.minecraft.interfaces.IMultiBlock; import net.minecraft.block.Block; import net.minecraft.block.BlockContainer; import net.minecraft.client.renderer.texture.IconRegister; -import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.MovingObjectPosition; import net.minecraft.world.World; -import net.minecraftforge.common.Configuration; import universalelectricity.api.UniversalElectricity; import universalelectricity.api.vector.Vector3; - -import com.builtbroken.common.Pair; -import com.builtbroken.minecraft.DarkCore; -import com.builtbroken.minecraft.IExtraInfo.IExtraBlockInfo; - import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; -public class BlockMulti extends BlockContainer implements IExtraBlockInfo +public class BlockMulti extends BlockContainer { - public String textureName = null; + public String textureName = null; - public BlockMulti() - { - super(DarkCore.CONFIGURATION.getBlock("MultiBlock", DarkCore.getNextID()).getInt(), UniversalElectricity.machine); - this.setHardness(0.8F); - this.setUnlocalizedName("multiBlock"); - } + public BlockMulti() + { + super(DarkCore.CONFIGURATION.getBlock("multiBlock", DarkCore.getNextID()).getInt(), UniversalElectricity.machine); + this.setHardness(0.8F); + this.setUnlocalizedName("multiBlock"); + } - @Override - public BlockMulti setTextureName(String name) - { - this.textureName = name; - return this; - } + @Override + public BlockMulti setTextureName(String name) + { + this.textureName = name; + return this; + } - public void makeFakeBlock(World worldObj, Vector3 position, Vector3 mainBlock) - { - worldObj.setBlock(position.intX(), position.intY(), position.intZ(), this.blockID); - ((TileEntityMulti) worldObj.getBlockTileEntity(position.intX(), position.intY(), position.intZ())).setMainBlock(mainBlock); - } + public void createMultiBlockStructure(IMultiBlock tile) + { + TileEntity tileEntity = (TileEntity) tile; + Vector3[] positions = tile.getMultiBlockVectors(); - @SideOnly(Side.CLIENT) - @Override - public void registerIcons(IconRegister iconRegister) - { - if (this.textureName != null) - { - this.blockIcon = iconRegister.registerIcon(this.textureName); - } - else - { - super.registerIcons(iconRegister); - } - } + for (Vector3 position : positions) + { + makeFakeBlock(tileEntity.worldObj, new Vector3(tileEntity).translate(position), new Vector3(tileEntity)); + } + } - @Override - public void breakBlock(World world, int x, int y, int z, int par5, int par6) - { - TileEntity tileEntity = world.getBlockTileEntity(x, y, z); + public void destroyMultiBlockStructure(IMultiBlock tile) + { + TileEntity tileEntity = (TileEntity) tile; + Vector3[] positions = tile.getMultiBlockVectors(); - if (tileEntity instanceof TileEntityMulti) - { - ((TileEntityMulti) tileEntity).onBlockRemoval(); - } + for (Vector3 position : positions) + { + new Vector3(tileEntity).translate(position).setBlock(tileEntity.worldObj, 0); + } - super.breakBlock(world, x, y, z, par5, par6); - } + new Vector3(tileEntity).setBlock(tileEntity.worldObj, 0); + } - /** Called when the block is right clicked by the player. This modified version detects electric - * items and wrench actions on your machine block. Do not override this function. Use - * machineActivated instead! (It does the same thing) */ - @Override - public boolean onBlockActivated(World par1World, int x, int y, int z, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9) - { - TileEntityMulti tileEntity = (TileEntityMulti) par1World.getBlockTileEntity(x, y, z); - return tileEntity.onBlockActivated(par1World, x, y, z, par5EntityPlayer); - } + public void makeFakeBlock(World worldObj, Vector3 position, Vector3 mainBlock) + { + // Creates a fake block, then sets the relative main block position. + worldObj.setBlock(position.intX(), position.intY(), position.intZ(), this.blockID); + ((TileEntityMulti) worldObj.getBlockTileEntity(position.intX(), position.intY(), position.intZ())).setMainBlock(mainBlock); + } - /** Returns the quantity of items to drop on block destruction. */ - @Override - public int quantityDropped(Random par1Random) - { - return 0; - } + @SideOnly(Side.CLIENT) + @Override + public void registerIcons(IconRegister iconRegister) + { + if (this.textureName != null) + { + this.blockIcon = iconRegister.registerIcon(this.textureName); + } + else + { + super.registerIcons(iconRegister); + } + } - @Override - public int getRenderType() - { - return -1; - } + @Override + public void breakBlock(World world, int x, int y, int z, int par5, int par6) + { + TileEntity tileEntity = world.getBlockTileEntity(x, y, z); - @Override - public boolean isOpaqueCube() - { - return false; - } + if (tileEntity instanceof TileEntityMulti) + { + ((TileEntityMulti) tileEntity).onBlockRemoval(this); + } - @Override - public boolean renderAsNormalBlock() - { - return false; - } + super.breakBlock(world, x, y, z, par5, par6); + } - @Override - public TileEntity createNewTileEntity(World var1) - { - return new TileEntityMulti(); - } + /** + * Called when the block is right clicked by the player. This modified version detects electric + * items and wrench actions on your machine block. Do not override this function. Use + * machineActivated instead! (It does the same thing) + */ + @Override + public boolean onBlockActivated(World par1World, int x, int y, int z, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9) + { + TileEntityMulti tileEntity = (TileEntityMulti) par1World.getBlockTileEntity(x, y, z); + return tileEntity.onBlockActivated(par1World, x, y, z, par5EntityPlayer); + } - @Override - public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z) - { - TileEntity tileEntity = world.getBlockTileEntity(x, y, z); - if (tileEntity instanceof TileEntityMulti) - { - Vector3 mainBlockPosition = ((TileEntityMulti) tileEntity).mainBlockPosition; + /** + * Returns the quantity of items to drop on block destruction. + */ + @Override + public int quantityDropped(Random par1Random) + { + return 0; + } - if (mainBlockPosition != null && !mainBlockPosition.equals(new Vector3(x, y, z))) - { - int mainBlockID = mainBlockPosition.getBlockID(world); + @Override + public int getRenderType() + { + return -1; + } - if (mainBlockID > 0) - { - return Block.blocksList[mainBlockID].getPickBlock(target, world, mainBlockPosition.intX(), mainBlockPosition.intY(), mainBlockPosition.intZ()); - } - } - } + @Override + public boolean isOpaqueCube() + { + return false; + } - return null; - } + @Override + public boolean renderAsNormalBlock() + { + return false; + } - @Override - public void getTileEntities(int blockID, Set>> list) - { - list.add(new Pair>("DMMultiBlock", TileEntityMulti.class)); + @Override + public TileEntity createNewTileEntity(World var1) + { + return new TileEntityMulti(); + } - } + @Override + public ItemStack getPickBlock(MovingObjectPosition target, World par1World, int x, int y, int z) + { + TileEntity tileEntity = par1World.getBlockTileEntity(x, y, z); + Vector3 mainBlockPosition = ((TileEntityMulti) tileEntity).getMainBlock(); - @Override - @SideOnly(Side.CLIENT) - public void getClientTileEntityRenderers(List, TileEntitySpecialRenderer>> list) - { + if (mainBlockPosition != null) + { + int mainBlockID = par1World.getBlockId(mainBlockPosition.intX(), mainBlockPosition.intY(), mainBlockPosition.intZ()); - } + if (mainBlockID > 0) + { + return Block.blocksList[mainBlockID].getPickBlock(target, par1World, mainBlockPosition.intX(), mainBlockPosition.intY(), mainBlockPosition.intZ()); + } + } - @Override - public boolean hasExtraConfigs() - { - // TODO Auto-generated method stub - return false; - } - - @Override - public void loadExtraConfigs(Configuration config) - { - // TODO Auto-generated method stub - - } - - @Override - public void loadOreNames() - { - // TODO Auto-generated method stub - - } + return null; + } } \ No newline at end of file diff --git a/src/com/builtbroken/minecraft/prefab/TileEntityMulti.java b/src/com/builtbroken/minecraft/prefab/TileEntityMulti.java index 074d5e24f..797ba3ceb 100644 --- a/src/com/builtbroken/minecraft/prefab/TileEntityMulti.java +++ b/src/com/builtbroken/minecraft/prefab/TileEntityMulti.java @@ -8,6 +8,7 @@ import net.minecraft.world.World; import universalelectricity.api.vector.Vector3; import com.builtbroken.minecraft.DarkCore; +import com.builtbroken.minecraft.interfaces.IBlockActivated; import com.builtbroken.minecraft.interfaces.IMultiBlock; import com.builtbroken.minecraft.network.ISimplePacketReceiver; import com.builtbroken.minecraft.network.PacketHandler; @@ -15,22 +16,23 @@ import com.google.common.io.ByteArrayDataInput; import cpw.mods.fml.common.network.Player; -/** This is a multiblock to be used for blocks that are bigger than one block. - * - * @author Calclavia */ public class TileEntityMulti extends TileEntity implements ISimplePacketReceiver -{ - // The the position of the main block - public Vector3 mainBlockPosition; +{ + private Vector3 hostPosition; - public TileEntityMulti() + public Vector3 getMainBlock() { + if (this.hostPosition != null) + { + return new Vector3(this).translate(this.hostPosition); + } + return null; } public void setMainBlock(Vector3 mainBlock) { - this.mainBlockPosition = mainBlock; + this.hostPosition = mainBlock.clone().translate(new Vector3(this).invert()); if (!this.worldObj.isRemote) { @@ -38,14 +40,25 @@ public class TileEntityMulti extends TileEntity implements ISimplePacketReceiver } } + @Override + public Packet getDescriptionPacket() + { + if (this.hostPosition != null) + { + return PacketHandler.instance().getTilePacket(DarkCore.CHANNEL, "desc", this, this.hostPosition.intX(), this.hostPosition.intY(), this.hostPosition.intZ()); + } + + return null; + } + @Override public boolean simplePacket(String id, ByteArrayDataInput data, Player player) { try { - if (id.equalsIgnoreCase("MainBlock")) + if (id.equalsIgnoreCase("desc")) { - this.mainBlockPosition = new Vector3(data.readInt(), data.readInt(), data.readInt()); + this.hostPosition = new Vector3(data.readInt(), data.readInt(), data.readInt()); return true; } } @@ -57,47 +70,28 @@ public class TileEntityMulti extends TileEntity implements ISimplePacketReceiver return false; } - @Override - public Packet getDescriptionPacket() + public void onBlockRemoval(BlockMulti block) { - if (this.mainBlockPosition != null) + if (this.getMainBlock() != null) { - return PacketHandler.instance().getTilePacket(DarkCore.CHANNEL, "MainBlock", this, this.mainBlockPosition.intX(), this.mainBlockPosition.intY(), this.mainBlockPosition.intZ()); - } + TileEntity tileEntity = this.getMainBlock().getTileEntity(this.worldObj); - return null; - } - - public void onBlockRemoval() - { - if (this.mainBlockPosition != null) - { - TileEntity tileEntity = this.worldObj.getBlockTileEntity(this.mainBlockPosition.intX(), this.mainBlockPosition.intY(), this.mainBlockPosition.intZ()); - - if (tileEntity != null && tileEntity instanceof IMultiBlock) + if (tileEntity instanceof IMultiBlock) { - IMultiBlock mainBlock = (IMultiBlock) tileEntity; - - if (mainBlock != null) - { - mainBlock.onDestroy(this); - } + block.destroyMultiBlockStructure((IMultiBlock) tileEntity); } } } - public boolean onBlockActivated(World par1World, int x, int y, int z, EntityPlayer par5EntityPlayer) + public boolean onBlockActivated(World par1World, int x, int y, int z, EntityPlayer entityPlayer) { - if (this.mainBlockPosition != null) + if (this.getMainBlock() != null) { - TileEntity tileEntity = this.worldObj.getBlockTileEntity(this.mainBlockPosition.intX(), this.mainBlockPosition.intY(), this.mainBlockPosition.intZ()); + TileEntity tileEntity = this.getMainBlock().getTileEntity(this.worldObj); - if (tileEntity != null) + if (tileEntity instanceof IBlockActivated) { - if (tileEntity instanceof IMultiBlock) - { - return ((IMultiBlock) tileEntity).onActivated(par5EntityPlayer); - } + return ((IBlockActivated) tileEntity).onActivated(entityPlayer); } } @@ -109,7 +103,11 @@ public class TileEntityMulti extends TileEntity implements ISimplePacketReceiver public void readFromNBT(NBTTagCompound nbt) { super.readFromNBT(nbt); - this.mainBlockPosition = new Vector3(nbt.getCompoundTag("mainBlockPosition")); + + if (nbt.hasKey("mainBlockPosition")) + { + this.hostPosition = new Vector3(nbt.getCompoundTag("mainBlockPosition")); + } } /** Writes a tile entity to NBT. */ @@ -118,9 +116,9 @@ public class TileEntityMulti extends TileEntity implements ISimplePacketReceiver { super.writeToNBT(nbt); - if (this.mainBlockPosition != null) + if (this.hostPosition != null) { - nbt.setCompoundTag("mainBlockPosition", this.mainBlockPosition.writeToNBT(new NBTTagCompound())); + nbt.setCompoundTag("mainBlockPosition", this.hostPosition.writeToNBT(new NBTTagCompound())); } } @@ -132,5 +130,4 @@ public class TileEntityMulti extends TileEntity implements ISimplePacketReceiver { return false; } - } \ No newline at end of file diff --git a/src/com/builtbroken/minecraft/prefab/invgui/SlotSpecific.java b/src/com/builtbroken/minecraft/prefab/invgui/SlotSpecific.java index a20358e77..7c4bab5fc 100644 --- a/src/com/builtbroken/minecraft/prefab/invgui/SlotSpecific.java +++ b/src/com/builtbroken/minecraft/prefab/invgui/SlotSpecific.java @@ -4,92 +4,87 @@ import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; -/** - * Creates a slot with a specific amount of items that matches the slot's requirements. Allows easy +/** Creates a slot with a specific amount of items that matches the slot's requirements. Allows easy * shift right clicking management and slot blocking in classes. In your container you can use * this.getSlot(i).isItemValid to justify the player's shift clicking actions to match the slot. * - * @author Calclavia - * - */ + * @author Calclavia */ public class SlotSpecific extends Slot { - public ItemStack[] validItemStacks = new ItemStack[0]; - public Class[] validClasses = new Class[0]; + public ItemStack[] validItemStacks = new ItemStack[0]; + public Class[] validClasses = new Class[0]; - public boolean isInverted = false; - public boolean isMetadataSensitive = false; + public boolean isInverted = false; + public boolean isMetadataSensitive = false; - public SlotSpecific(IInventory par2IInventory, int par3, int par4, int par5, ItemStack... itemStacks) - { - super(par2IInventory, par3, par4, par5); - this.setItemStacks(itemStacks); - } + public SlotSpecific(IInventory inventory, int slotID, int xPos, int yPos, ItemStack... itemStacks) + { + super(inventory, slotID, xPos, yPos); + this.setItemStacks(itemStacks); + } - public SlotSpecific(IInventory par2IInventory, int par3, int par4, int par5, Class... validClasses) - { - super(par2IInventory, par3, par4, par5); - this.setClasses(validClasses); - } + public SlotSpecific(IInventory inventory, int slotID, int xPos, int yPos, Class... validClasses) + { + super(inventory, slotID, xPos, yPos); + this.setClasses(validClasses); + } - public SlotSpecific setMetadataSensitive() - { - this.isMetadataSensitive = true; - return this; - } + public SlotSpecific setMetadataSensitive() + { + this.isMetadataSensitive = true; + return this; + } - public SlotSpecific setItemStacks(ItemStack... validItemStacks) - { - this.validItemStacks = validItemStacks; - return this; - } + public SlotSpecific setItemStacks(ItemStack... validItemStacks) + { + this.validItemStacks = validItemStacks; + return this; + } - public SlotSpecific setClasses(Class... validClasses) - { - this.validClasses = validClasses; - return this; - } + public SlotSpecific setClasses(Class... validClasses) + { + this.validClasses = validClasses; + return this; + } - public SlotSpecific toggleInverted() - { - this.isInverted = !this.isInverted; - return this; - } + public SlotSpecific toggleInverted() + { + this.isInverted = !this.isInverted; + return this; + } - /** - * Check if the stack is a valid item for this slot. Always true beside for the armor slots. - */ - @Override - public boolean isItemValid(ItemStack compareStack) - { - boolean returnValue = false; + /** Check if the stack is a valid item for this slot. Always true beside for the armor slots. */ + @Override + public boolean isItemValid(ItemStack compareStack) + { + boolean returnValue = false; - for (ItemStack itemStack : this.validItemStacks) - { - if (compareStack.isItemEqual(itemStack) || (!this.isMetadataSensitive && compareStack.itemID == itemStack.itemID)) - { - returnValue = true; - break; - } - } + for (ItemStack itemStack : this.validItemStacks) + { + if (compareStack.isItemEqual(itemStack) || (!this.isMetadataSensitive && compareStack.itemID == itemStack.itemID)) + { + returnValue = true; + break; + } + } - if (!returnValue) - { - for (Class clazz : this.validClasses) - { - if (clazz.equals(compareStack.getItem().getClass()) || clazz.isInstance(compareStack.getItem())) - { - returnValue = true; - break; - } - } - } + if (!returnValue) + { + for (Class clazz : this.validClasses) + { + if (clazz.equals(compareStack.getItem().getClass()) || clazz.isInstance(compareStack.getItem())) + { + returnValue = true; + break; + } + } + } - if (this.isInverted) - { - return !returnValue; - } + if (this.isInverted) + { + return !returnValue; + } - return returnValue; - } + return returnValue; + } }