assemblyline/src/main/java/assemblyline/common/machine/TileEntityRejector.java

94 lines
3.2 KiB
Java
Raw Normal View History

2022-10-26 19:42:44 +02:00
package assemblyline.common.machine;
import java.util.List;
2023-02-10 17:51:10 +01:00
import assemblyline.common.machine.imprinter.TileEntityFilterable;
2022-10-26 19:42:44 +02:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.common.util.ForgeDirection;
import universalelectricity.core.vector.Vector3;
2023-02-10 17:51:10 +01:00
public class TileEntityRejector extends TileEntityFilterable {
2022-10-26 19:42:44 +02:00
public boolean firePiston = false;
@Override
protected int getMaxTransferRange() {
return 20;
}
@Override
public void onUpdate() {
super.onUpdate();
if (this.ticks % 5L == 0L && !this.isDisabled()) {
int metadata = this.getBlockMetadata();
this.firePiston = false;
Vector3 searchPosition = new Vector3(this);
searchPosition.modifyPositionFromSide(this.getDirection());
2023-02-10 17:51:10 +01:00
TileEntity tileEntity
= searchPosition.getTileEntity((IBlockAccess) this.worldObj);
2022-10-26 19:42:44 +02:00
try {
boolean flag = false;
if (this.isRunning()) {
2023-02-10 17:51:10 +01:00
AxisAlignedBB bounds = AxisAlignedBB.getBoundingBox(
(double) searchPosition.x,
(double) searchPosition.y,
(double) searchPosition.z,
(double) (searchPosition.x + 1.0),
(double) (searchPosition.y + 1.0),
(double) (searchPosition.z + 1.0)
);
List<Entity> entitiesInFront
= this.worldObj.getEntitiesWithinAABB(Entity.class, bounds);
2022-10-26 19:42:44 +02:00
for (Entity entity : entitiesInFront) {
2023-02-10 17:51:10 +01:00
if (!this.canEntityBeThrow(entity))
continue;
2022-10-26 19:42:44 +02:00
this.throwItem(this.getDirection(), entity);
flag = true;
}
}
2023-02-10 17:51:10 +01:00
} catch (Exception e) {
2022-10-26 19:42:44 +02:00
e.printStackTrace();
}
}
}
public void throwItem(ForgeDirection side, Entity entity) {
this.firePiston = true;
2023-02-10 17:51:10 +01:00
entity.motionX = (double) side.offsetX * 0.2;
2022-10-26 19:42:44 +02:00
entity.motionY += 0.10000000298023223;
2023-02-10 17:51:10 +01:00
entity.motionZ = (double) side.offsetZ * 0.2;
2022-10-26 19:42:44 +02:00
this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
public boolean canEntityBeThrow(Entity entity) {
if (entity instanceof EntityItem) {
2023-02-10 17:51:10 +01:00
EntityItem entityItem = (EntityItem) entity;
2022-10-26 19:42:44 +02:00
ItemStack itemStack = entityItem.getEntityItem();
return this.isFiltering(itemStack);
}
return false;
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
this.firePiston = nbt.getBoolean("piston");
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
nbt.setBoolean("piston", this.firePiston);
}
@Override
public boolean canConnect(ForgeDirection dir) {
return dir != this.getDirection();
}
}