Improved multiBlock code

This commit is contained in:
DarkGuardsman 2013-12-31 14:20:59 -05:00
parent b9fd994e89
commit 11866075c5
4 changed files with 228 additions and 260 deletions

View file

@ -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();
}

View file

@ -1,36 +1,30 @@
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 BlockMulti()
{
super(DarkCore.CONFIGURATION.getBlock("MultiBlock", DarkCore.getNextID()).getInt(), UniversalElectricity.machine);
super(DarkCore.CONFIGURATION.getBlock("multiBlock", DarkCore.getNextID()).getInt(), UniversalElectricity.machine);
this.setHardness(0.8F);
this.setUnlocalizedName("multiBlock");
}
@ -42,8 +36,33 @@ public class BlockMulti extends BlockContainer implements IExtraBlockInfo
return this;
}
public void createMultiBlockStructure(IMultiBlock tile)
{
TileEntity tileEntity = (TileEntity) tile;
Vector3[] positions = tile.getMultiBlockVectors();
for (Vector3 position : positions)
{
makeFakeBlock(tileEntity.worldObj, new Vector3(tileEntity).translate(position), new Vector3(tileEntity));
}
}
public void destroyMultiBlockStructure(IMultiBlock tile)
{
TileEntity tileEntity = (TileEntity) tile;
Vector3[] positions = tile.getMultiBlockVectors();
for (Vector3 position : positions)
{
new Vector3(tileEntity).translate(position).setBlock(tileEntity.worldObj, 0);
}
new Vector3(tileEntity).setBlock(tileEntity.worldObj, 0);
}
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);
}
@ -69,15 +88,17 @@ public class BlockMulti extends BlockContainer implements IExtraBlockInfo
if (tileEntity instanceof TileEntityMulti)
{
((TileEntityMulti) tileEntity).onBlockRemoval();
((TileEntityMulti) tileEntity).onBlockRemoval(this);
}
super.breakBlock(world, x, y, z, par5, par6);
}
/** Called when the block is right clicked by the player. This modified version detects electric
/**
* 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) */
* 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)
{
@ -85,7 +106,9 @@ public class BlockMulti extends BlockContainer implements IExtraBlockInfo
return tileEntity.onBlockActivated(par1World, x, y, z, par5EntityPlayer);
}
/** Returns the quantity of items to drop on block destruction. */
/**
* Returns the quantity of items to drop on block destruction.
*/
@Override
public int quantityDropped(Random par1Random)
{
@ -117,59 +140,21 @@ public class BlockMulti extends BlockContainer implements IExtraBlockInfo
}
@Override
public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z)
public ItemStack getPickBlock(MovingObjectPosition target, World par1World, int x, int y, int z)
{
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
if (tileEntity instanceof TileEntityMulti)
{
Vector3 mainBlockPosition = ((TileEntityMulti) tileEntity).mainBlockPosition;
TileEntity tileEntity = par1World.getBlockTileEntity(x, y, z);
Vector3 mainBlockPosition = ((TileEntityMulti) tileEntity).getMainBlock();
if (mainBlockPosition != null && !mainBlockPosition.equals(new Vector3(x, y, z)))
if (mainBlockPosition != null)
{
int mainBlockID = mainBlockPosition.getBlockID(world);
int mainBlockID = par1World.getBlockId(mainBlockPosition.intX(), mainBlockPosition.intY(), mainBlockPosition.intZ());
if (mainBlockID > 0)
{
return Block.blocksList[mainBlockID].getPickBlock(target, world, mainBlockPosition.intX(), mainBlockPosition.intY(), mainBlockPosition.intZ());
}
return Block.blocksList[mainBlockID].getPickBlock(target, par1World, mainBlockPosition.intX(), mainBlockPosition.intY(), mainBlockPosition.intZ());
}
}
return null;
}
@Override
public void getTileEntities(int blockID, Set<Pair<String, Class<? extends TileEntity>>> list)
{
list.add(new Pair<String, Class<? extends TileEntity>>("DMMultiBlock", TileEntityMulti.class));
}
@Override
@SideOnly(Side.CLIENT)
public void getClientTileEntityRenderers(List<Pair<Class<? extends TileEntity>, TileEntitySpecialRenderer>> list)
{
}
@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
}
}

View file

@ -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,50 +70,31 @@ 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)
{
IMultiBlock mainBlock = (IMultiBlock) tileEntity;
if (mainBlock != null)
{
mainBlock.onDestroy(this);
}
}
}
}
public boolean onBlockActivated(World par1World, int x, int y, int z, EntityPlayer par5EntityPlayer)
{
if (this.mainBlockPosition != null)
{
TileEntity tileEntity = this.worldObj.getBlockTileEntity(this.mainBlockPosition.intX(), this.mainBlockPosition.intY(), this.mainBlockPosition.intZ());
if (tileEntity != null)
{
if (tileEntity instanceof IMultiBlock)
{
return ((IMultiBlock) tileEntity).onActivated(par5EntityPlayer);
block.destroyMultiBlockStructure((IMultiBlock) tileEntity);
}
}
}
public boolean onBlockActivated(World par1World, int x, int y, int z, EntityPlayer entityPlayer)
{
if (this.getMainBlock() != null)
{
TileEntity tileEntity = this.getMainBlock().getTileEntity(this.worldObj);
if (tileEntity instanceof IBlockActivated)
{
return ((IBlockActivated) tileEntity).onActivated(entityPlayer);
}
}
return false;
}
@ -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;
}
}

View file

@ -4,14 +4,11 @@ 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];
@ -20,15 +17,15 @@ public class SlotSpecific extends Slot
public boolean isInverted = false;
public boolean isMetadataSensitive = false;
public SlotSpecific(IInventory par2IInventory, int par3, int par4, int par5, ItemStack... itemStacks)
public SlotSpecific(IInventory inventory, int slotID, int xPos, int yPos, ItemStack... itemStacks)
{
super(par2IInventory, par3, par4, par5);
super(inventory, slotID, xPos, yPos);
this.setItemStacks(itemStacks);
}
public SlotSpecific(IInventory par2IInventory, int par3, int par4, int par5, Class... validClasses)
public SlotSpecific(IInventory inventory, int slotID, int xPos, int yPos, Class... validClasses)
{
super(par2IInventory, par3, par4, par5);
super(inventory, slotID, xPos, yPos);
this.setClasses(validClasses);
}
@ -56,9 +53,7 @@ public class SlotSpecific extends Slot
return this;
}
/**
* Check if the stack is a valid item for this slot. Always true beside for the armor slots.
*/
/** Check if the stack is a valid item for this slot. Always true beside for the armor slots. */
@Override
public boolean isItemValid(ItemStack compareStack)
{