Compatiblity with IInventory and ISidedInventory

This commit is contained in:
Calclavia 2012-10-28 18:00:46 +08:00
parent a554b914bd
commit 501d44fa44
3 changed files with 133 additions and 77 deletions

View file

@ -1,5 +1,6 @@
package assemblyline.machines;
import net.minecraft.src.AxisAlignedBB;
import net.minecraft.src.CreativeTabs;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.Material;
@ -11,68 +12,68 @@ import assemblyline.AssemblyLine;
import assemblyline.render.RenderHelper;
/**
* A metadata block containing a bunch of machines with direction.
* A metadata block containing a bunch of machines
* with direction.
*
* @author Darkguardsman, Calclavia
*
*
*/
public class BlockInteraction extends BlockMachine
{
public static enum MachineType
{
SORTER("Sorter", 0, TileEntitySorter.class),
MANIPULATOR("Manipulator", 4, TileEntityManipulator.class),
INVALID_1("Invalid", 8, null),
INVALID_2("Invalid", 12, null);
SORTER("Sorter", 0, TileEntitySorter.class), MANIPULATOR("Manipulator", 4, TileEntityManipulator.class), INVALID_1("Invalid", 8, null), INVALID_2("Invalid", 12, null);
public String name;
public int metadata;
public Class<? extends TileEntity> tileEntity;
MachineType(String name, int metadata, Class<? extends TileEntity> tileEntity)
{
this.name = name;
this.metadata = metadata;
this.tileEntity = tileEntity;
}
public static MachineType getBase(int metadata)
public static MachineType get(int metadata)
{
for(MachineType value : MachineType.values())
for (MachineType value : MachineType.values())
{
if(metadata >= value.metadata && metadata < value.metadata + 4)
{
return value;
}
if (metadata >= value.metadata && metadata < value.metadata + 4) { return value; }
}
return null;
}
/**
* Gets the direction based on the metadata
* Gets the direction based on the
* metadata
*
* @return A direction value from 0 to 4.
*/
public static int getDirection(int metadata)
{
return metadata - MachineType.getBase(metadata).metadata;
return metadata - MachineType.get(metadata).metadata;
}
/**
* @param currentDirection - An integer from 0 to 4.
* @return The metadata this block should change into.
* @param currentDirection
* - An integer from 0 to 4.
* @return The metadata this block should
* change into.
*/
public int getNextDirectionMeta(int currentDirection)
{
currentDirection ++;
if(currentDirection >= 4)
currentDirection++;
if (currentDirection >= 4)
{
currentDirection = 0;
}
return currentDirection + this.metadata;
}
/**
* Creates a new TIleEntity.
*/
@ -89,7 +90,7 @@ public class BlockInteraction extends BlockMachine
}
}
}
public BlockInteraction(int id)
{
super("Interaction Machine", id, Material.iron);
@ -98,7 +99,7 @@ public class BlockInteraction extends BlockMachine
public int damageDropped(int metadata)
{
return MachineType.getBase(metadata).metadata;
return MachineType.get(metadata).metadata;
}
public boolean onMachineActivated(World par1World, int x, int y, int z, EntityPlayer par5EntityPlayer)
@ -106,29 +107,31 @@ public class BlockInteraction extends BlockMachine
if (!par1World.isRemote)
{
int metadata = par1World.getBlockMetadata(x, y, z);
par5EntityPlayer.openGui(AssemblyLine.instance, MachineType.getBase(metadata).metadata, par1World, x, y, z);
par5EntityPlayer.openGui(AssemblyLine.instance, MachineType.get(metadata).metadata, par1World, x, y, z);
return true;
}
return true;
}
@Override
public boolean onUseWrench(World par1World, int x, int y, int z, EntityPlayer par5EntityPlayer)
{
{
int metadata = par1World.getBlockMetadata(x, y, z);
MachineType machineType = MachineType.getBase(metadata);
MachineType machineType = MachineType.get(metadata);
par1World.setBlockAndMetadataWithNotify(x, y, z, this.blockID, machineType.getNextDirectionMeta(MachineType.getDirection(metadata)));
return true;
}
@Override
public boolean onSneakUseWrench(World par1World, int x, int y, int z, EntityPlayer par5EntityPlayer)
{
int metadata = par1World.getBlockMetadata(x, y, z);
if(MachineType.getBase(metadata) == MachineType.MANIPULATOR)
if (MachineType.get(metadata) == MachineType.MANIPULATOR)
{
((TileEntityManipulator)par1World.getBlockTileEntity(x, y, z)).isWrenchedToOutput = !((TileEntityManipulator)par1World.getBlockTileEntity(x, y, z)).isWrenchedToOutput;
((TileEntityManipulator) par1World.getBlockTileEntity(x, y, z)).isWrenchedToOutput = !((TileEntityManipulator) par1World.getBlockTileEntity(x, y, z)).isWrenchedToOutput;
if (!par1World.isRemote)
par5EntityPlayer.addChatMessage("Manipulator Output: " + ((TileEntityManipulator) par1World.getBlockTileEntity(x, y, z)).isWrenchedToOutput);
return true;
}
else
@ -137,10 +140,36 @@ public class BlockInteraction extends BlockMachine
}
}
/**
* Returns the bounding box of the wired
* rectangular prism to render.
*/
@Override
public AxisAlignedBB getSelectedBoundingBoxFromPool(World par1World, int x, int y, int z)
{
return this.getCollisionBoundingBoxFromPool(par1World, x, y, z);
}
/**
* Returns a bounding box from the pool of
* bounding boxes (this means this box can
* change after the pool has been cleared to
* be reused)
*/
@Override
public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int x, int y, int z)
{
int metadata = par1World.getBlockMetadata(x, y, z);
if (MachineType.get(metadata) == MachineType.MANIPULATOR) { return AxisAlignedBB.getAABBPool().addOrModifyAABBInPool((double) x + this.minX, (double) y + this.minY, (double) z + this.minZ, (double) x + this.maxX, (double) y + 0.3f, (double) z + this.maxZ); }
return AxisAlignedBB.getAABBPool().addOrModifyAABBInPool((double) x + this.minX, (double) y + this.minY, (double) z + this.minZ, (double) x + this.maxX, (double) y + this.maxY, (double) z + this.maxZ);
}
@Override
public TileEntity createNewTileEntity(World var1, int metadata)
{
return MachineType.getBase(metadata).instantiateTileEntity();
return MachineType.get(metadata).instantiateTileEntity();
}
@Override

View file

@ -31,7 +31,7 @@ public class ItemBlockInteraction extends ItemBlock
public String getItemNameIS(ItemStack itemstack)
{
return MachineType.getBase(itemstack.getItemDamage()).name;
return MachineType.get(itemstack.getItemDamage()).name;
}
@Override

View file

@ -4,24 +4,18 @@ import java.util.List;
import net.minecraft.src.AxisAlignedBB;
import net.minecraft.src.EntityItem;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.IInventory;
import net.minecraft.src.INetworkManager;
import net.minecraft.src.ItemStack;
import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.Packet250CustomPayload;
import net.minecraft.src.TileEntity;
import net.minecraft.src.TileEntityChest;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.ISidedInventory;
import universalelectricity.core.Vector3;
import universalelectricity.electricity.ElectricInfo;
import universalelectricity.prefab.TileEntityElectricityReceiver;
import universalelectricity.prefab.network.IPacketReceiver;
import assemblyline.machines.BlockInteraction.MachineType;
import com.google.common.io.ByteArrayDataInput;
public class TileEntityManipulator extends TileEntityElectricityReceiver
{
/**
@ -33,9 +27,10 @@ public class TileEntityManipulator extends TileEntityElectricityReceiver
* The amount of watts received.
*/
public double wattsReceived = 0;
/**
* Is the manipulator wrenched to turn into output mode?
* Is the manipulator wrenched to turn into
* output mode?
*/
public boolean isWrenchedToOutput = false;
@ -50,7 +45,7 @@ public class TileEntityManipulator extends TileEntityElectricityReceiver
{
super.updateEntity();
if (this.worldObj.isRemote)
if (!this.worldObj.isRemote)
{
if (!this.isDisabled())
{
@ -63,7 +58,6 @@ public class TileEntityManipulator extends TileEntityElectricityReceiver
* this manipulator.
*/
Vector3 inputPosition = Vector3.get(this);
inputPosition.modifyPositionFromSide(this.getBeltDirection());
Vector3 outputPosition = Vector3.get(this);
outputPosition.modifyPositionFromSide(this.getBeltDirection().getOpposite());
@ -79,14 +73,22 @@ public class TileEntityManipulator extends TileEntityElectricityReceiver
{
if (remainingStack.stackSize > 0)
{
EntityItem var23 = new EntityItem(worldObj, entity.posX, entity.posY + 0.1D, entity.posZ, remainingStack);
worldObj.spawnEntityInWorld(var23);
EntityItem entityItem = new EntityItem(worldObj, outputPosition.x + 0.5, outputPosition.y + 0.8, outputPosition.z + 0.5, remainingStack);
entityItem.motionX = 0;
entityItem.motionZ = 0;
worldObj.spawnEntityInWorld(entityItem);
}
}
entity.setDead();
}
}
else
{
/**
* Finds the connected inventory and outputs the items upon a redstone pulse.
*/
}
}
}
}
@ -102,7 +104,7 @@ public class TileEntityManipulator extends TileEntityElectricityReceiver
{
TileEntity tileEntity = position.getTileEntity(this.worldObj);
if (tileEntity != null)
if (tileEntity != null && itemStack != null)
{
/**
* Try to put items into a chest.
@ -130,32 +132,35 @@ public class TileEntityManipulator extends TileEntityElectricityReceiver
for (TileEntityChest chest : chests)
{
if (itemStack != null && itemStack.stackSize > 0)
for (int i = 0; i < chest.getSizeInventory(); i++)
{
for (int i = 0; i < chest.getSizeInventory(); i++)
{
ItemStack stackInChest = chest.getStackInSlot(i);
if (stackInChest == null)
{
chest.setInventorySlotContents(i, itemStack);
return null;
}
else if (stackInChest.getItem().equals(itemStack.getItem()) && stackInChest.getItemDamage() == itemStack.getItemDamage())
{
int rejectedAmount = Math.max((stackInChest.stackSize + itemStack.stackSize) - stackInChest.getItem().getItemStackLimit(), 0);
stackInChest.stackSize = Math.min(Math.max((stackInChest.stackSize + itemStack.stackSize - rejectedAmount), 0), stackInChest.getItem().getItemStackLimit());
itemStack.stackSize = rejectedAmount;
chest.setInventorySlotContents(i, stackInChest);
if (itemStack.stackSize <= 0) { return null; }
}
}
itemStack = this.addStackToInventory(i, chest, itemStack);
if(itemStack == null) return null;
}
}
}
else if (tileEntity instanceof ISidedInventory)
{
ISidedInventory inventory = (ISidedInventory) tileEntity;
int startIndex = inventory.getStartInventorySide(this.getBeltDirection());
for (int i = startIndex; i < inventory.getSizeInventorySide(this.getBeltDirection()); i++)
{
itemStack = this.addStackToInventory(startIndex, inventory, itemStack);
if(itemStack == null) return null;
}
}
else if (tileEntity instanceof IInventory)
{
IInventory inventory = (IInventory) tileEntity;
for (int i = 0; i < inventory.getSizeInventory(); i++)
{
itemStack = this.addStackToInventory(i, inventory, itemStack);
if(itemStack == null) return null;
}
}
}
if (itemStack.stackSize <= 0) { return null; }
@ -163,13 +168,35 @@ public class TileEntityManipulator extends TileEntityElectricityReceiver
return itemStack;
}
public ItemStack addStackToInventory(int slotIndex, IInventory inventory, ItemStack itemStack)
{
ItemStack stackInChest = inventory.getStackInSlot(slotIndex);
if (stackInChest == null)
{
inventory.setInventorySlotContents(slotIndex, itemStack);
return null;
}
else if (stackInChest.getItem().equals(itemStack.getItem()) && stackInChest.getItemDamage() == itemStack.getItemDamage())
{
int rejectedAmount = Math.max((stackInChest.stackSize + itemStack.stackSize) - stackInChest.getItem().getItemStackLimit(), 0);
stackInChest.stackSize = Math.min(Math.max((stackInChest.stackSize + itemStack.stackSize - rejectedAmount), 0), stackInChest.getItem().getItemStackLimit());
itemStack.stackSize = rejectedAmount;
inventory.setInventorySlotContents(slotIndex, stackInChest);
if (itemStack.stackSize <= 0) { return null; }
}
return itemStack;
}
/**
* If the manipulator is powered, it will
* output items instead of input.
*/
public boolean isOutput()
{
return this.isWrenchedToOutput || this.worldObj.isBlockIndirectlyGettingPowered(this.xCoord, this.yCoord, this.zCoord) || this.worldObj.isBlockGettingPowered(this.xCoord, this.yCoord, this.zCoord);
return this.isWrenchedToOutput;
}
public ForgeDirection getBeltDirection()
@ -188,7 +215,7 @@ public class TileEntityManipulator extends TileEntityElectricityReceiver
{
this.wattsReceived += ElectricInfo.getWatts(amps, voltage);
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{