Added lightning to EM Contractor

This commit is contained in:
Calclavia 2013-08-03 21:27:12 -04:00
parent 37d9200af2
commit 50fe6c44d0
2 changed files with 266 additions and 254 deletions

View file

@ -135,7 +135,7 @@ public class Vector3
return new Vector3(this.x + offset.x, this.y + offset.y, this.z + offset.z); return new Vector3(this.x + offset.x, this.y + offset.y, this.z + offset.z);
} }
public Vector3 translate(int offset) public Vector3 translate(double offset)
{ {
return new Vector3(this.x + offset, this.y + offset, this.z + offset); return new Vector3(this.x + offset, this.y + offset, this.z + offset);
} }

View file

@ -13,17 +13,20 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import resonantinduction.PacketHandler; import resonantinduction.PacketHandler;
import resonantinduction.ResonantInduction;
import resonantinduction.api.ITesla; import resonantinduction.api.ITesla;
import resonantinduction.base.IPacketReceiver; import resonantinduction.base.IPacketReceiver;
import resonantinduction.base.TileEntityBase;
import resonantinduction.base.Vector3;
import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataInput;
/** /**
* *
* @author AidanBrady * @author AidanBrady
* *
*/ */
public class TileEntityEMContractor extends TileEntity implements IPacketReceiver, ITesla public class TileEntityEMContractor extends TileEntityBase implements IPacketReceiver, ITesla
{ {
public static int MAX_REACH = 40; public static int MAX_REACH = 40;
public static int PUSH_DELAY = 5; public static int PUSH_DELAY = 5;
@ -34,7 +37,7 @@ public class TileEntityEMContractor extends TileEntity implements IPacketReceive
private ForgeDirection facing = ForgeDirection.UP; private ForgeDirection facing = ForgeDirection.UP;
public int pushDelay; public int pushDelay;
public float energyStored; public float energyStored;
public AxisAlignedBB operationBounds; public AxisAlignedBB operationBounds;
@ -48,167 +51,171 @@ public class TileEntityEMContractor extends TileEntity implements IPacketReceive
@Override @Override
public void updateEntity() public void updateEntity()
{ {
pushDelay = Math.max(0, pushDelay - 1); super.updateEntity();
this.pushDelay = Math.max(0, pushDelay - 1);
if (isLatched() && canFunction()) if (canFunction())
{ {
TileEntity inventoryTile = getLatched(); if (isLatched())
IInventory inventory = (IInventory) inventoryTile;
if (!suck && pushDelay == 0)
{ {
if (!(inventoryTile instanceof ISidedInventory)) TileEntity inventoryTile = getLatched();
IInventory inventory = (IInventory) inventoryTile;
if (!suck && pushDelay == 0)
{ {
for (int i = inventory.getSizeInventory() - 1; i >= 0; i--) if (!(inventoryTile instanceof ISidedInventory))
{ {
if (inventory.getStackInSlot(i) != null) for (int i = inventory.getSizeInventory() - 1; i >= 0; i--)
{ {
ItemStack toSend = inventory.getStackInSlot(i).copy(); if (inventory.getStackInSlot(i) != null)
toSend.stackSize = 1;
EntityItem item = getItemWithPosition(toSend);
if (!worldObj.isRemote)
{ {
worldObj.spawnEntityInWorld(item); ItemStack toSend = inventory.getStackInSlot(i).copy();
}
inventory.decrStackSize(i, 1);
pushDelay = PUSH_DELAY;
break;
}
}
}
else
{
ISidedInventory sidedInventory = (ISidedInventory) inventoryTile;
int[] slots = sidedInventory.getAccessibleSlotsFromSide(facing.ordinal());
if (slots != null)
{
for (int get = slots.length - 1; get >= 0; get--)
{
int slotID = slots[get];
if (sidedInventory.getStackInSlot(slotID) != null)
{
ItemStack toSend = sidedInventory.getStackInSlot(slotID);
toSend.stackSize = 1; toSend.stackSize = 1;
if (sidedInventory.canExtractItem(slotID, toSend, facing.ordinal())) EntityItem item = getItemWithPosition(toSend);
if (!worldObj.isRemote)
{ {
EntityItem item = getItemWithPosition(toSend); worldObj.spawnEntityInWorld(item);
if (!worldObj.isRemote)
{
worldObj.spawnEntityInWorld(item);
}
sidedInventory.decrStackSize(slotID, 1);
pushDelay = PUSH_DELAY;
break;
} }
inventory.decrStackSize(i, 1);
pushDelay = PUSH_DELAY;
break;
} }
} }
} }
} else
}
else if (suck)
{
if (suckBounds != null)
{
List<EntityItem> list = worldObj.getEntitiesWithinAABB(EntityItem.class, suckBounds);
for (EntityItem item : list)
{ {
ItemStack itemStack = item.getEntityItem(); ISidedInventory sidedInventory = (ISidedInventory) inventoryTile;
int[] slots = sidedInventory.getAccessibleSlotsFromSide(facing.ordinal());
if (!(inventoryTile instanceof ISidedInventory)) if (slots != null)
{ {
for (int i = 0; i <= inventory.getSizeInventory() - 1; i++) for (int get = slots.length - 1; get >= 0; get--)
{
if (inventory.isItemValidForSlot(i, itemStack))
{
ItemStack inSlot = inventory.getStackInSlot(i);
if (inSlot == null)
{
inventory.setInventorySlotContents(i, itemStack);
item.setDead();
break;
}
else if (inSlot.isItemEqual(itemStack) && inSlot.stackSize < inSlot.getMaxStackSize())
{
if (inSlot.stackSize + itemStack.stackSize <= inSlot.getMaxStackSize())
{
ItemStack toSet = itemStack.copy();
toSet.stackSize += inSlot.stackSize;
inventory.setInventorySlotContents(i, toSet);
item.setDead();
break;
}
else
{
int rejects = (inSlot.stackSize + itemStack.stackSize) - inSlot.getMaxStackSize();
ItemStack toSet = itemStack.copy();
toSet.stackSize = inSlot.getMaxStackSize();
ItemStack remains = itemStack.copy();
remains.stackSize = rejects;
inventory.setInventorySlotContents(i, toSet);
item.setEntityItemStack(remains);
}
}
}
}
}
else
{
ISidedInventory sidedInventory = (ISidedInventory) inventoryTile;
int[] slots = sidedInventory.getAccessibleSlotsFromSide(facing.ordinal());
for (int get = 0; get <= slots.length - 1; get++)
{ {
int slotID = slots[get]; int slotID = slots[get];
if (sidedInventory.isItemValidForSlot(slotID, itemStack) && sidedInventory.canInsertItem(slotID, itemStack, facing.ordinal())) if (sidedInventory.getStackInSlot(slotID) != null)
{ {
ItemStack inSlot = inventory.getStackInSlot(slotID); ItemStack toSend = sidedInventory.getStackInSlot(slotID);
toSend.stackSize = 1;
if (inSlot == null) if (sidedInventory.canExtractItem(slotID, toSend, facing.ordinal()))
{ {
inventory.setInventorySlotContents(slotID, itemStack); EntityItem item = getItemWithPosition(toSend);
item.setDead();
if (!worldObj.isRemote)
{
worldObj.spawnEntityInWorld(item);
}
sidedInventory.decrStackSize(slotID, 1);
pushDelay = PUSH_DELAY;
break; break;
} }
else if (inSlot.isItemEqual(itemStack) && inSlot.stackSize < inSlot.getMaxStackSize()) }
{ }
if (inSlot.stackSize + itemStack.stackSize <= inSlot.getMaxStackSize()) }
{ }
ItemStack toSet = itemStack.copy(); }
toSet.stackSize += inSlot.stackSize; else if (suck)
{
if (suckBounds != null)
{
List<EntityItem> list = worldObj.getEntitiesWithinAABB(EntityItem.class, suckBounds);
inventory.setInventorySlotContents(slotID, toSet); for (EntityItem item : list)
{
ItemStack itemStack = item.getEntityItem();
if (!(inventoryTile instanceof ISidedInventory))
{
for (int i = 0; i <= inventory.getSizeInventory() - 1; i++)
{
if (inventory.isItemValidForSlot(i, itemStack))
{
ItemStack inSlot = inventory.getStackInSlot(i);
if (inSlot == null)
{
inventory.setInventorySlotContents(i, itemStack);
item.setDead(); item.setDead();
break; break;
} }
else else if (inSlot.isItemEqual(itemStack) && inSlot.stackSize < inSlot.getMaxStackSize())
{ {
int rejects = (inSlot.stackSize + itemStack.stackSize) - inSlot.getMaxStackSize(); if (inSlot.stackSize + itemStack.stackSize <= inSlot.getMaxStackSize())
{
ItemStack toSet = itemStack.copy();
toSet.stackSize += inSlot.stackSize;
ItemStack toSet = itemStack.copy(); inventory.setInventorySlotContents(i, toSet);
toSet.stackSize = inSlot.getMaxStackSize(); item.setDead();
break;
}
else
{
int rejects = (inSlot.stackSize + itemStack.stackSize) - inSlot.getMaxStackSize();
ItemStack remains = itemStack.copy(); ItemStack toSet = itemStack.copy();
remains.stackSize = rejects; toSet.stackSize = inSlot.getMaxStackSize();
inventory.setInventorySlotContents(slotID, toSet); ItemStack remains = itemStack.copy();
item.setEntityItemStack(remains); remains.stackSize = rejects;
inventory.setInventorySlotContents(i, toSet);
item.setEntityItemStack(remains);
}
}
}
}
}
else
{
ISidedInventory sidedInventory = (ISidedInventory) inventoryTile;
int[] slots = sidedInventory.getAccessibleSlotsFromSide(facing.ordinal());
for (int get = 0; get <= slots.length - 1; get++)
{
int slotID = slots[get];
if (sidedInventory.isItemValidForSlot(slotID, itemStack) && sidedInventory.canInsertItem(slotID, itemStack, facing.ordinal()))
{
ItemStack inSlot = inventory.getStackInSlot(slotID);
if (inSlot == null)
{
inventory.setInventorySlotContents(slotID, itemStack);
item.setDead();
break;
}
else if (inSlot.isItemEqual(itemStack) && inSlot.stackSize < inSlot.getMaxStackSize())
{
if (inSlot.stackSize + itemStack.stackSize <= inSlot.getMaxStackSize())
{
ItemStack toSet = itemStack.copy();
toSet.stackSize += inSlot.stackSize;
inventory.setInventorySlotContents(slotID, toSet);
item.setDead();
break;
}
else
{
int rejects = (inSlot.stackSize + itemStack.stackSize) - inSlot.getMaxStackSize();
ItemStack toSet = itemStack.copy();
toSet.stackSize = inSlot.getMaxStackSize();
ItemStack remains = itemStack.copy();
remains.stackSize = rejects;
inventory.setInventorySlotContents(slotID, toSet);
item.setEntityItemStack(remains);
}
} }
} }
} }
@ -217,142 +224,147 @@ public class TileEntityEMContractor extends TileEntity implements IPacketReceive
} }
} }
} }
}
if (operationBounds != null && canFunction()) if (operationBounds != null)
{
List<Entity> list = worldObj.getEntitiesWithinAABB(Entity.class, operationBounds);
energyStored -= ENERGY_USAGE;
for (Entity entity : list)
{ {
if (entity instanceof EntityItem) List<Entity> list = worldObj.getEntitiesWithinAABB(Entity.class, operationBounds);
energyStored -= ENERGY_USAGE;
for (Entity entity : list)
{ {
EntityItem entityItem = (EntityItem) entity; if (entity instanceof EntityItem)
switch (facing)
{ {
case DOWN: EntityItem entityItem = (EntityItem) entity;
if (!worldObj.isRemote)
{
entityItem.setPosition(xCoord + 0.5, entityItem.posY, zCoord + 0.5);
}
entityItem.motionX = 0; if (this.worldObj.isRemote && this.ticks % 5 == 0)
entityItem.motionZ = 0; {
ResonantInduction.proxy.renderElectricShock(this.worldObj, new Vector3(this).translate(0.5), new Vector3(entityItem));
}
if (!suck) switch (facing)
{ {
entityItem.motionY = Math.max(-MAX_SPEED, entityItem.motionY - ACCELERATION); case DOWN:
} if (!worldObj.isRemote)
else {
{ entityItem.setPosition(xCoord + 0.5, entityItem.posY, zCoord + 0.5);
entityItem.motionY = Math.min((MAX_SPEED * 4), entityItem.motionY + (ACCELERATION * 5)); }
}
entityItem.isAirBorne = true; entityItem.motionX = 0;
break; entityItem.motionZ = 0;
case UP:
if (!worldObj.isRemote)
{
entityItem.setPosition(xCoord + 0.5, entityItem.posY, zCoord + 0.5);
}
entityItem.motionX = 0; if (!suck)
entityItem.motionZ = 0; {
entityItem.motionY = Math.max(-MAX_SPEED, entityItem.motionY - ACCELERATION);
}
else
{
entityItem.motionY = Math.min((MAX_SPEED * 4), entityItem.motionY + (ACCELERATION * 5));
}
if (!suck) entityItem.isAirBorne = true;
{ break;
entityItem.motionY = Math.min((MAX_SPEED * 4), entityItem.motionY + (ACCELERATION * 5)); case UP:
} if (!worldObj.isRemote)
else {
{ entityItem.setPosition(xCoord + 0.5, entityItem.posY, zCoord + 0.5);
entityItem.motionY = Math.max(-MAX_SPEED, entityItem.motionY - ACCELERATION); }
}
entityItem.isAirBorne = true; entityItem.motionX = 0;
break; entityItem.motionZ = 0;
case NORTH:
if (!worldObj.isRemote)
{
entityItem.setPosition(xCoord + 0.5, yCoord + 0.5, entityItem.posZ);
}
entityItem.motionX = 0; if (!suck)
entityItem.motionY = 0; {
entityItem.motionY = Math.min((MAX_SPEED * 4), entityItem.motionY + (ACCELERATION * 5));
}
else
{
entityItem.motionY = Math.max(-MAX_SPEED, entityItem.motionY - ACCELERATION);
}
if (!suck) entityItem.isAirBorne = true;
{ break;
entityItem.motionZ = Math.max(-MAX_SPEED, entityItem.motionZ - ACCELERATION); case NORTH:
} if (!worldObj.isRemote)
else {
{ entityItem.setPosition(xCoord + 0.5, yCoord + 0.5, entityItem.posZ);
entityItem.motionZ = Math.min(MAX_SPEED, entityItem.motionZ + ACCELERATION); }
}
entityItem.isAirBorne = true; entityItem.motionX = 0;
break; entityItem.motionY = 0;
case SOUTH:
if (!worldObj.isRemote)
{
entityItem.setPosition(xCoord + 0.5, yCoord + 0.5, entityItem.posZ);
}
entityItem.motionX = 0; if (!suck)
entityItem.motionY = 0; {
entityItem.motionZ = Math.max(-MAX_SPEED, entityItem.motionZ - ACCELERATION);
}
else
{
entityItem.motionZ = Math.min(MAX_SPEED, entityItem.motionZ + ACCELERATION);
}
if (!suck) entityItem.isAirBorne = true;
{ break;
entityItem.motionZ = Math.min(MAX_SPEED, entityItem.motionZ + ACCELERATION); case SOUTH:
} if (!worldObj.isRemote)
else {
{ entityItem.setPosition(xCoord + 0.5, yCoord + 0.5, entityItem.posZ);
entityItem.motionZ = Math.max(-MAX_SPEED, entityItem.motionZ - ACCELERATION); }
}
entityItem.isAirBorne = true; entityItem.motionX = 0;
break; entityItem.motionY = 0;
case WEST:
if (!worldObj.isRemote)
{
entityItem.setPosition(entityItem.posX, yCoord + 0.5, zCoord + 0.5);
}
entityItem.motionY = 0; if (!suck)
entityItem.motionZ = 0; {
entityItem.motionZ = Math.min(MAX_SPEED, entityItem.motionZ + ACCELERATION);
}
else
{
entityItem.motionZ = Math.max(-MAX_SPEED, entityItem.motionZ - ACCELERATION);
}
if (!suck) entityItem.isAirBorne = true;
{ break;
entityItem.motionX = Math.max(-MAX_SPEED, entityItem.motionX - ACCELERATION); case WEST:
} if (!worldObj.isRemote)
else {
{ entityItem.setPosition(entityItem.posX, yCoord + 0.5, zCoord + 0.5);
entityItem.motionX = Math.min(MAX_SPEED, entityItem.motionX + ACCELERATION); }
}
entityItem.isAirBorne = true; entityItem.motionY = 0;
break; entityItem.motionZ = 0;
case EAST:
if (!worldObj.isRemote)
{
entityItem.setPosition(entityItem.posX, yCoord + 0.5, zCoord + 0.5);
}
entityItem.motionY = 0; if (!suck)
entityItem.motionZ = 0; {
entityItem.motionX = Math.max(-MAX_SPEED, entityItem.motionX - ACCELERATION);
}
else
{
entityItem.motionX = Math.min(MAX_SPEED, entityItem.motionX + ACCELERATION);
}
if (!suck) entityItem.isAirBorne = true;
{ break;
entityItem.motionX = Math.min(MAX_SPEED, entityItem.motionX + ACCELERATION); case EAST:
} if (!worldObj.isRemote)
else {
{ entityItem.setPosition(entityItem.posX, yCoord + 0.5, zCoord + 0.5);
entityItem.motionX = Math.max(-MAX_SPEED, entityItem.motionX - ACCELERATION); }
}
entityItem.isAirBorne = true; entityItem.motionY = 0;
break; entityItem.motionZ = 0;
if (!suck)
{
entityItem.motionX = Math.min(MAX_SPEED, entityItem.motionX + ACCELERATION);
}
else
{
entityItem.motionX = Math.max(-MAX_SPEED, entityItem.motionX - ACCELERATION);
}
entityItem.isAirBorne = true;
break;
}
} }
} }
} }
@ -471,10 +483,10 @@ public class TileEntityEMContractor extends TileEntity implements IPacketReceive
updateBounds(); updateBounds();
} }
public boolean canFunction() public boolean canFunction()
{ {
return worldObj.getBlockPowerInput(xCoord, yCoord, zCoord) > 0; return !this.worldObj.isBlockIndirectlyGettingPowered(this.xCoord, this.yCoord, this.zCoord);
} }
@Override @Override
@ -519,20 +531,20 @@ public class TileEntityEMContractor extends TileEntity implements IPacketReceive
} }
@Override @Override
public float transfer(float transferEnergy, boolean doTransfer) public float transfer(float transferEnergy, boolean doTransfer)
{ {
float energyToUse = Math.min(transferEnergy, ENERGY_USAGE-energyStored); float energyToUse = Math.min(transferEnergy, ENERGY_USAGE - energyStored);
if(doTransfer) if (doTransfer)
{ {
energyStored += energyToUse; energyStored += energyToUse;
} }
return energyToUse; return energyToUse;
} }
@Override @Override
public boolean canReceive(TileEntity transferTile) public boolean canReceive(TileEntity transferTile)
{ {
return true; return true;
} }