buildcraft/common/buildcraft/transport/pipes/PipeItemsObsidian.java

279 lines
8 KiB
Java
Raw Normal View History

2012-05-09 22:43:05 +02:00
/**
2014-02-15 09:21:40 +01:00
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
2012-05-09 22:43:05 +02:00
*/
2012-07-25 12:45:15 +02:00
package buildcraft.transport.pipes;
2012-05-09 22:43:05 +02:00
import buildcraft.BuildCraftTransport;
import buildcraft.api.core.IIconProvider;
2012-07-25 12:45:15 +02:00
import buildcraft.api.core.Position;
import buildcraft.api.power.IPowerReceptor;
2013-07-01 23:21:51 +02:00
import buildcraft.api.power.PowerHandler;
import buildcraft.api.power.PowerHandler.PowerReceiver;
import buildcraft.api.power.PowerHandler.Type;
2013-09-03 11:12:13 +02:00
import buildcraft.core.inventory.ITransactor;
import buildcraft.core.inventory.Transactor;
import buildcraft.core.inventory.filters.StackFilter;
import buildcraft.transport.TravelingItem;
2012-09-05 22:29:38 +02:00
import buildcraft.core.proxy.CoreProxy;
2012-07-25 12:45:15 +02:00
import buildcraft.transport.Pipe;
import buildcraft.transport.PipeIconProvider;
2012-07-25 12:45:15 +02:00
import buildcraft.transport.PipeTransportItems;
import buildcraft.transport.pipes.events.PipeEvent;
import buildcraft.transport.pipes.events.PipeEventItem;
import buildcraft.transport.utils.TransportUtils;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2014-02-08 20:39:39 +01:00
2013-09-03 11:12:13 +02:00
import java.util.Arrays;
2013-07-12 23:21:44 +02:00
import java.util.List;
2014-02-08 20:39:39 +01:00
2013-07-12 23:21:44 +02:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.item.EntityMinecartChest;
import net.minecraft.entity.projectile.EntityArrow;
2014-02-08 20:39:39 +01:00
import net.minecraft.init.Items;
2013-07-12 23:21:44 +02:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.ForgeDirection;
2012-07-25 12:45:15 +02:00
public class PipeItemsObsidian extends Pipe<PipeTransportItems> implements IPowerReceptor {
2012-05-09 22:43:05 +02:00
2013-09-03 11:12:13 +02:00
private static final PowerHandler.PerditionCalculator PERDITION = new PowerHandler.PerditionCalculator(0.5F);
2013-07-01 23:21:51 +02:00
private PowerHandler powerHandler;
2012-06-04 22:48:18 +02:00
private int[] entitiesDropped;
2012-05-09 22:43:05 +02:00
private int entitiesDroppedIndex = 0;
2014-02-08 20:39:39 +01:00
public PipeItemsObsidian(Item item) {
super(new PipeTransportItems(), item);
2012-05-09 22:43:05 +02:00
2012-06-04 22:48:18 +02:00
entitiesDropped = new int[32];
2013-09-03 11:12:13 +02:00
Arrays.fill(entitiesDropped, -1);
2012-05-09 22:43:05 +02:00
2013-07-01 23:21:51 +02:00
powerHandler = new PowerHandler(this, Type.MACHINE);
powerHandler.configure(1, 64, 1, 256);
2013-09-03 11:12:13 +02:00
powerHandler.setPerdition(PERDITION);
2012-05-09 22:43:05 +02:00
}
@Override
@SideOnly(Side.CLIENT)
public IIconProvider getIconProvider() {
return BuildCraftTransport.instance.pipeIconProvider;
}
2013-07-18 01:24:57 +02:00
@Override
public int getIconIndex(ForgeDirection direction) {
return PipeIconProvider.TYPE.PipeItemsObsidian.ordinal();
2012-05-09 22:43:05 +02:00
}
@Override
public void onEntityCollidedWithBlock(Entity entity) {
super.onEntityCollidedWithBlock(entity);
2012-06-04 22:48:18 +02:00
if (entity.isDead)
2012-05-09 22:43:05 +02:00
return;
2012-12-17 23:30:54 +01:00
if (canSuck(entity, 0)) {
2012-05-09 22:43:05 +02:00
pullItemIntoPipe(entity, 0);
2012-12-17 23:30:54 +01:00
}
2012-06-04 22:48:18 +02:00
}
2012-05-09 22:43:05 +02:00
private AxisAlignedBB getSuckingBox(ForgeDirection orientation, int distance) {
if (orientation == ForgeDirection.UNKNOWN)
2012-05-09 22:43:05 +02:00
return null;
2013-07-16 21:58:31 +02:00
Position p1 = new Position(container.xCoord, container.yCoord, container.zCoord, orientation);
Position p2 = new Position(container.xCoord, container.yCoord, container.zCoord, orientation);
2012-05-09 22:43:05 +02:00
switch (orientation) {
2013-07-18 01:24:57 +02:00
case EAST:
p1.x += distance;
p2.x += 1 + distance;
break;
case WEST:
p1.x -= (distance - 1);
p2.x -= distance;
break;
case UP:
case DOWN:
p1.x += distance + 1;
p2.x -= distance;
p1.z += distance + 1;
p2.z -= distance;
break;
case SOUTH:
p1.z += distance;
p2.z += distance + 1;
break;
case NORTH:
default:
p1.z -= (distance - 1);
p2.z -= distance;
break;
2012-05-09 22:43:05 +02:00
}
switch (orientation) {
2013-07-18 01:24:57 +02:00
case EAST:
case WEST:
p1.y += distance + 1;
p2.y -= distance;
p1.z += distance + 1;
p2.z -= distance;
break;
case UP:
p1.y += distance + 1;
p2.y += distance;
break;
case DOWN:
p1.y -= (distance - 1);
p2.y -= distance;
break;
case SOUTH:
case NORTH:
default:
p1.y += distance + 1;
p2.y -= distance;
p1.x += distance + 1;
p2.x -= distance;
break;
2012-05-09 22:43:05 +02:00
}
Position min = p1.min(p2);
Position max = p1.max(p2);
return AxisAlignedBB.getBoundingBox(min.x, min.y, min.z, max.x, max.y, max.z);
2012-05-09 22:43:05 +02:00
}
@Override
2013-07-01 23:21:51 +02:00
public void doWork(PowerHandler workProvider) {
2013-07-18 01:24:57 +02:00
for (int j = 1; j < 5; ++j) {
2013-09-03 11:12:13 +02:00
if (suckItem(j))
2012-05-09 22:43:05 +02:00
return;
2013-07-18 01:24:57 +02:00
}
2012-05-09 22:43:05 +02:00
}
2013-09-03 11:12:13 +02:00
private boolean suckItem(int distance) {
2012-05-09 22:43:05 +02:00
AxisAlignedBB box = getSuckingBox(getOpenOrientation(), distance);
2012-06-04 22:48:18 +02:00
if (box == null)
2012-05-09 22:43:05 +02:00
return false;
@SuppressWarnings("rawtypes")
2014-02-08 14:47:31 +01:00
List<Entity> discoveredEntities = (List<Entity>) container.getWorldObj().getEntitiesWithinAABB(Entity.class, box);
2012-05-09 22:43:05 +02:00
2013-09-03 11:12:13 +02:00
for (Entity entity : discoveredEntities) {
if (canSuck(entity, distance)) {
pullItemIntoPipe(entity, distance);
return true;
}
2012-05-09 22:43:05 +02:00
2013-09-03 11:12:13 +02:00
if (distance == 1 && entity instanceof EntityMinecartChest) {
EntityMinecartChest cart = (EntityMinecartChest) entity;
if (!cart.isDead) {
ITransactor trans = Transactor.getTransactorFor(cart);
ForgeDirection openOrientation = getOpenOrientation();
ItemStack stack = trans.remove(StackFilter.ALL, openOrientation, false);
if (stack != null && powerHandler.useEnergy(1, 1, true) == 1) {
trans.remove(StackFilter.ALL, openOrientation, true);
2014-02-08 14:47:31 +01:00
EntityItem entityitem = new EntityItem(container.getWorldObj(), cart.posX, cart.posY + 0.3F, cart.posZ, stack);
2013-09-03 11:12:13 +02:00
entityitem.delayBeforeCanPickup = 10;
2014-02-08 14:47:31 +01:00
container.getWorldObj().spawnEntityInWorld(entityitem);
2013-09-03 11:12:13 +02:00
pullItemIntoPipe(entityitem, 1);
return true;
2012-05-09 22:43:05 +02:00
}
}
}
2013-07-18 01:24:57 +02:00
}
2012-05-09 22:43:05 +02:00
return false;
}
public void pullItemIntoPipe(Entity entity, int distance) {
if (container.getWorldObj().isRemote) {
2012-05-09 22:43:05 +02:00
return;
}
2012-05-09 22:43:05 +02:00
ForgeDirection orientation = getOpenOrientation().getOpposite();
2012-05-09 22:43:05 +02:00
if (orientation != ForgeDirection.UNKNOWN) {
2014-02-08 14:47:31 +01:00
container.getWorldObj().playSoundAtEntity(entity, "random.pop", 0.2F, ((container.getWorldObj().rand.nextFloat() - container.getWorldObj().rand.nextFloat()) * 0.7F + 1.0F) * 2.0F);
2012-05-09 22:43:05 +02:00
ItemStack stack = null;
double speed = 0.01F;
if (entity instanceof EntityItem) {
EntityItem item = (EntityItem) entity;
ItemStack contained = item.getEntityItem();
2014-02-08 14:47:31 +01:00
CoreProxy.proxy.obsidianPipePickup(container.getWorldObj(), item, this.container);
2012-05-09 22:43:05 +02:00
double energyUsed = powerHandler.useEnergy(distance, contained.stackSize * distance, true);
2012-05-09 22:43:05 +02:00
2012-12-18 20:43:22 +01:00
if (distance == 0 || energyUsed / distance == contained.stackSize) {
stack = contained;
2012-09-05 22:29:38 +02:00
CoreProxy.proxy.removeEntity(entity);
2012-12-17 23:30:54 +01:00
} else {
2012-12-18 20:43:22 +01:00
stack = contained.splitStack((int) (energyUsed / distance));
2012-12-17 23:30:54 +01:00
}
2012-05-09 22:43:05 +02:00
2012-06-08 02:13:31 +02:00
speed = Math.sqrt(item.motionX * item.motionX + item.motionY * item.motionY + item.motionZ * item.motionZ);
2012-05-09 22:43:05 +02:00
speed = speed / 2F - 0.05;
2012-12-17 23:30:54 +01:00
if (speed < 0.01) {
2012-05-09 22:43:05 +02:00
speed = 0.01;
2012-12-17 23:30:54 +01:00
}
2012-05-09 22:43:05 +02:00
} else if (entity instanceof EntityArrow) {
2013-07-01 23:21:51 +02:00
powerHandler.useEnergy(distance, distance, true);
2014-02-08 20:39:39 +01:00
stack = new ItemStack(Items.arrow, 1);
2012-09-05 22:29:38 +02:00
CoreProxy.proxy.removeEntity(entity);
2012-05-09 22:43:05 +02:00
}
TravelingItem item = TravelingItem.make(container.xCoord + 0.5, container.yCoord + TransportUtils.getPipeFloorOf(stack), container.zCoord + 0.5, stack);
2012-05-09 22:43:05 +02:00
item.setSpeed((float) speed);
2012-05-09 22:43:05 +02:00
transport.injectItem(item, orientation);
2012-05-09 22:43:05 +02:00
}
}
public void eventHandler(PipeEventItem.DropItem event) {
if (entitiesDroppedIndex + 1 >= entitiesDropped.length)
2012-05-09 22:43:05 +02:00
entitiesDroppedIndex = 0;
else
2012-05-09 22:43:05 +02:00
entitiesDroppedIndex++;
2014-02-08 20:39:39 +01:00
entitiesDropped[entitiesDroppedIndex] = event.entity.getEntityId();
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
public boolean canSuck(Entity entity, int distance) {
2012-05-09 22:43:05 +02:00
if (!entity.isEntityAlive())
2012-06-04 22:48:18 +02:00
return false;
if (entity instanceof EntityItem) {
2012-05-09 22:43:05 +02:00
EntityItem item = (EntityItem) entity;
if (item.getEntityItem().stackSize <= 0)
2012-05-09 22:43:05 +02:00
return false;
2013-07-18 01:24:57 +02:00
for (int i = 0; i < entitiesDropped.length; ++i) {
2014-02-08 20:39:39 +01:00
if (item.getEntityId() == entitiesDropped[i])
2012-05-09 22:43:05 +02:00
return false;
2013-07-18 01:24:57 +02:00
}
2012-05-09 22:43:05 +02:00
2013-07-01 23:21:51 +02:00
return powerHandler.useEnergy(1, distance, false) >= distance;
} else if (entity instanceof EntityArrow) {
EntityArrow arrow = (EntityArrow) entity;
2013-09-03 11:12:13 +02:00
return arrow.canBePickedUp == 1 && powerHandler.useEnergy(1, distance, false) >= distance;
}
return false;
2012-05-09 22:43:05 +02:00
}
@Override
2013-07-01 23:21:51 +02:00
public PowerReceiver getPowerReceiver(ForgeDirection side) {
return powerHandler.getPowerReceiver();
2012-05-09 22:43:05 +02:00
}
}