use of buildcraft.api.transport.EnumWirePart in project BuildCraft by BuildCraft.
the class BlockPipeHolder method rayTraceWire.
@Nullable
public static EnumWirePart rayTraceWire(BlockPos pos, Vec3d start, Vec3d end) {
Vec3d realStart = start.subtract(pos.getX(), pos.getY(), pos.getZ());
Vec3d realEnd = end.subtract(pos.getX(), pos.getY(), pos.getZ());
EnumWirePart best = null;
double dist = 1000;
for (EnumWirePart part : EnumWirePart.VALUES) {
RayTraceResult trace = part.boundingBoxPossible.calculateIntercept(realStart, realEnd);
if (trace != null) {
if (best == null) {
best = part;
dist = trace.hitVec.squareDistanceTo(realStart);
} else {
double nextDist = trace.hitVec.squareDistanceTo(realStart);
if (dist > nextDist) {
best = part;
dist = nextDist;
}
}
}
}
return best;
}
use of buildcraft.api.transport.EnumWirePart in project BuildCraft by BuildCraft.
the class BlockPipeHolder method removedByPlayer.
@Override
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest) {
if (world.isRemote) {
return false;
}
TilePipeHolder tile = getPipe(world, pos, false);
if (tile == null) {
return super.removedByPlayer(state, world, pos, player, willHarvest);
}
NonNullList<ItemStack> toDrop = NonNullList.create();
RayTraceResult trace = rayTrace(world, pos, player);
EnumFacing side = null;
EnumWirePart part = null;
EnumWireBetween between = null;
if (trace != null && trace.subHit > 6) {
side = getPartSideHit(trace);
part = getWirePartHit(trace);
between = getWireBetweenHit(trace);
}
if (side != null) {
removePluggable(side, tile, toDrop);
if (!player.capabilities.isCreativeMode) {
InventoryUtil.dropAll(world, pos, toDrop);
}
return false;
} else if (part != null) {
toDrop.add(new ItemStack(BCTransportItems.wire, 1, tile.wireManager.getColorOfPart(part).getMetadata()));
tile.wireManager.removePart(part);
if (!player.capabilities.isCreativeMode) {
InventoryUtil.dropAll(world, pos, toDrop);
}
tile.scheduleNetworkUpdate(IPipeHolder.PipeMessageReceiver.WIRES);
return false;
} else if (between != null) {
toDrop.add(new ItemStack(BCTransportItems.wire, between.to == null ? 2 : 1, tile.wireManager.getColorOfPart(between.parts[0]).getMetadata()));
if (between.to == null) {
tile.wireManager.removeParts(Arrays.asList(between.parts));
} else {
tile.wireManager.removePart(between.parts[0]);
}
if (!player.capabilities.isCreativeMode) {
InventoryUtil.dropAll(world, pos, toDrop);
}
tile.scheduleNetworkUpdate(IPipeHolder.PipeMessageReceiver.WIRES);
return false;
} else {
toDrop.addAll(getDrops(world, pos, state, 0));
for (EnumFacing face : EnumFacing.VALUES) {
removePluggable(face, tile, NonNullList.create());
}
}
if (!player.capabilities.isCreativeMode) {
InventoryUtil.dropAll(world, pos, toDrop);
}
return super.removedByPlayer(state, world, pos, player, willHarvest);
}
use of buildcraft.api.transport.EnumWirePart in project BuildCraft by BuildCraft.
the class BlockPipeHolder method rayTrace.
@Nullable
public RayTraceResult rayTrace(World world, BlockPos pos, Vec3d start, Vec3d end) {
TilePipeHolder tile = getPipe(world, pos, false);
if (tile == null) {
return computeTrace(null, pos, start, end, FULL_BLOCK_AABB, 400);
}
RayTraceResult best = null;
Pipe pipe = tile.getPipe();
boolean computed = false;
if (pipe != null) {
computed = true;
best = computeTrace(best, pos, start, end, BOX_CENTER, 0);
for (EnumFacing face : EnumFacing.VALUES) {
float conSize = pipe.getConnectedDist(face);
if (conSize > 0) {
AxisAlignedBB aabb = BOX_FACES[face.ordinal()];
if (conSize != 0.25f) {
Vec3d center = VecUtil.offset(new Vec3d(0.5, 0.5, 0.5), face, 0.25 + (conSize / 2));
Vec3d radius = new Vec3d(0.25, 0.25, 0.25);
radius = VecUtil.replaceValue(radius, face.getAxis(), conSize / 2);
Vec3d min = center.subtract(radius);
Vec3d max = center.add(radius);
aabb = BoundingBoxUtil.makeFrom(min, max);
}
best = computeTrace(best, pos, start, end, aabb, face.ordinal() + 1);
}
}
}
for (EnumFacing face : EnumFacing.VALUES) {
PipePluggable pluggable = tile.getPluggable(face);
if (pluggable != null) {
AxisAlignedBB bb = pluggable.getBoundingBox();
best = computeTrace(best, pos, start, end, bb, face.ordinal() + 1 + 6);
computed = true;
}
}
for (EnumWirePart part : tile.getWireManager().parts.keySet()) {
best = computeTrace(best, pos, start, end, part.boundingBox, part.ordinal() + 1 + 6 + 6);
computed = true;
}
for (EnumWireBetween between : tile.getWireManager().betweens.keySet()) {
best = computeTrace(best, pos, start, end, between.boundingBox, between.ordinal() + 1 + 6 + 6 + 8);
computed = true;
}
if (!computed) {
return computeTrace(null, pos, start, end, FULL_BLOCK_AABB, 400);
}
return best;
}
use of buildcraft.api.transport.EnumWirePart in project BuildCraft by BuildCraft.
the class BlockPipeHolder method addCollisionBoxToList.
// Collisions
@Override
public void addCollisionBoxToList(IBlockState state, World world, BlockPos pos, AxisAlignedBB entityBox, List<AxisAlignedBB> collidingBoxes, Entity entityIn, boolean isPistonMoving) {
TilePipeHolder tile = getPipe(world, pos, false);
if (tile == null) {
addCollisionBoxToList(pos, entityBox, collidingBoxes, FULL_BLOCK_AABB);
return;
}
boolean added = false;
Pipe pipe = tile.getPipe();
if (pipe != null) {
addCollisionBoxToList(pos, entityBox, collidingBoxes, BOX_CENTER);
added = true;
for (EnumFacing face : EnumFacing.VALUES) {
float conSize = pipe.getConnectedDist(face);
if (conSize > 0) {
AxisAlignedBB aabb = BOX_FACES[face.ordinal()];
if (conSize != 0.25f) {
Vec3d center = VecUtil.offset(new Vec3d(0.5, 0.5, 0.5), face, 0.25 + (conSize / 2));
Vec3d radius = new Vec3d(0.25, 0.25, 0.25);
radius = VecUtil.replaceValue(radius, face.getAxis(), conSize / 2);
Vec3d min = center.subtract(radius);
Vec3d max = center.add(radius);
aabb = BoundingBoxUtil.makeFrom(min, max);
}
addCollisionBoxToList(pos, entityBox, collidingBoxes, aabb);
}
}
}
for (EnumFacing face : EnumFacing.VALUES) {
PipePluggable pluggable = tile.getPluggable(face);
if (pluggable != null) {
AxisAlignedBB bb = pluggable.getBoundingBox();
addCollisionBoxToList(pos, entityBox, collidingBoxes, bb);
added = true;
}
}
for (EnumWirePart part : tile.getWireManager().parts.keySet()) {
addCollisionBoxToList(pos, entityBox, collidingBoxes, part.boundingBox);
added = true;
}
for (EnumWireBetween between : tile.getWireManager().betweens.keySet()) {
addCollisionBoxToList(pos, entityBox, collidingBoxes, between.boundingBox);
added = true;
}
if (!added) {
addCollisionBoxToList(pos, entityBox, collidingBoxes, FULL_BLOCK_AABB);
}
}
use of buildcraft.api.transport.EnumWirePart in project BuildCraft by BuildCraft.
the class BlockPipeHolder method onBlockActivated.
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
TilePipeHolder tile = getPipe(world, pos, false);
if (tile == null) {
return false;
}
RayTraceResult trace = rayTrace(world, pos, player);
if (trace == null) {
return false;
}
EnumFacing realSide = getPartSideHit(trace);
if (realSide == null) {
realSide = side;
}
if (trace.subHit > 6 && trace.subHit <= 12) {
PipePluggable existing = tile.getPluggable(realSide);
if (existing != null) {
return existing.onPluggableActivate(player, trace, hitX, hitY, hitZ);
}
}
EnumPipePart part = trace.subHit == 0 ? EnumPipePart.CENTER : EnumPipePart.fromFacing(realSide);
ItemStack held = player.getHeldItem(hand);
Item item = held.isEmpty() ? null : held.getItem();
PipePluggable existing = tile.getPluggable(realSide);
if (item instanceof IItemPluggable && existing == null) {
IItemPluggable itemPlug = (IItemPluggable) item;
PipePluggable plug = itemPlug.onPlace(held, tile, realSide, player, hand);
if (plug == null) {
return false;
} else {
tile.replacePluggable(realSide, plug);
if (!player.capabilities.isCreativeMode) {
held.shrink(1);
}
return true;
}
}
if (item instanceof ItemWire) {
EnumWirePart wirePartHit = getWirePartHit(trace);
EnumWirePart wirePart;
TilePipeHolder attachTile = tile;
if (wirePartHit != null) {
WireNode node = new WireNode(pos, wirePartHit);
node = node.offset(trace.sideHit);
wirePart = node.part;
if (!node.pos.equals(pos)) {
attachTile = getPipe(world, node.pos, false);
}
} else {
wirePart = EnumWirePart.get((trace.hitVec.x % 1 + 1) % 1 > 0.5, (trace.hitVec.y % 1 + 1) % 1 > 0.5, (trace.hitVec.z % 1 + 1) % 1 > 0.5);
}
if (wirePart != null && attachTile != null) {
boolean attached = attachTile.getWireManager().addPart(wirePart, EnumDyeColor.byMetadata(held.getMetadata()));
attachTile.scheduleNetworkUpdate(IPipeHolder.PipeMessageReceiver.WIRES);
if (attached && !player.capabilities.isCreativeMode) {
held.shrink(1);
}
if (attached) {
return true;
}
}
}
Pipe pipe = tile.getPipe();
if (pipe == null) {
return false;
}
if (pipe.behaviour.onPipeActivate(player, trace, hitX, hitY, hitZ, part)) {
return true;
}
if (pipe.flow.onFlowActivate(player, trace, hitX, hitY, hitZ, part)) {
return true;
}
return false;
}
Aggregations