use of blusunrize.immersiveengineering.common.util.network.MessageObstructedConnection in project ImmersiveEngineering by BluSunrize.
the class ApiUtils method doCoilUse.
public static EnumActionResult doCoilUse(IWireCoil coil, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
TileEntity tileEntity = world.getTileEntity(pos);
if (tileEntity instanceof IImmersiveConnectable && ((IImmersiveConnectable) tileEntity).canConnect()) {
ItemStack stack = player.getHeldItem(hand);
TargetingInfo target = new TargetingInfo(side, hitX, hitY, hitZ);
WireType wire = coil.getWireType(stack);
BlockPos masterPos = ((IImmersiveConnectable) tileEntity).getConnectionMaster(wire, target);
Vec3i offset = pos.subtract(masterPos);
tileEntity = world.getTileEntity(masterPos);
if (!(tileEntity instanceof IImmersiveConnectable) || !((IImmersiveConnectable) tileEntity).canConnect())
return EnumActionResult.PASS;
if (!((IImmersiveConnectable) tileEntity).canConnectCable(wire, target, offset) || !coil.canConnectCable(stack, tileEntity)) {
if (!world.isRemote)
player.sendStatusMessage(new TextComponentTranslation(Lib.CHAT_WARN + "wrongCable"), true);
return EnumActionResult.FAIL;
}
if (!world.isRemote)
if (!ItemNBTHelper.hasKey(stack, "linkingPos")) {
ItemNBTHelper.setIntArray(stack, "linkingPos", new int[] { world.provider.getDimension(), masterPos.getX(), masterPos.getY(), masterPos.getZ(), offset.getX(), offset.getY(), offset.getZ() });
NBTTagCompound targetNbt = new NBTTagCompound();
target.writeToNBT(targetNbt);
ItemNBTHelper.setTagCompound(stack, "targettingInfo", targetNbt);
} else {
int[] array = ItemNBTHelper.getIntArray(stack, "linkingPos");
BlockPos linkPos = new BlockPos(array[1], array[2], array[3]);
Vec3i offsetLink = BlockPos.NULL_VECTOR;
if (array.length == 7)
offsetLink = new Vec3i(array[4], array[5], array[6]);
TileEntity tileEntityLinkingPos = world.getTileEntity(linkPos);
int distanceSq = (int) Math.ceil(linkPos.distanceSq(masterPos));
// not squared yet
int maxLengthSq = coil.getMaxLength(stack);
maxLengthSq *= maxLengthSq;
if (array[0] != world.provider.getDimension())
player.sendStatusMessage(new TextComponentTranslation(Lib.CHAT_WARN + "wrongDimension"), true);
else if (linkPos.equals(masterPos))
player.sendStatusMessage(new TextComponentTranslation(Lib.CHAT_WARN + "sameConnection"), true);
else if (distanceSq > maxLengthSq)
player.sendStatusMessage(new TextComponentTranslation(Lib.CHAT_WARN + "tooFar"), true);
else {
TargetingInfo targetLink = TargetingInfo.readFromNBT(ItemNBTHelper.getTagCompound(stack, "targettingInfo"));
if (!(tileEntityLinkingPos instanceof IImmersiveConnectable) || !((IImmersiveConnectable) tileEntityLinkingPos).canConnectCable(wire, targetLink, offsetLink) || !((IImmersiveConnectable) tileEntityLinkingPos).getConnectionMaster(wire, targetLink).equals(linkPos) || !coil.canConnectCable(stack, tileEntityLinkingPos))
player.sendStatusMessage(new TextComponentTranslation(Lib.CHAT_WARN + "invalidPoint"), true);
else {
IImmersiveConnectable nodeHere = (IImmersiveConnectable) tileEntity;
IImmersiveConnectable nodeLink = (IImmersiveConnectable) tileEntityLinkingPos;
boolean connectionExists = false;
Set<Connection> outputs = ImmersiveNetHandler.INSTANCE.getConnections(world, Utils.toCC(nodeHere));
if (outputs != null)
for (Connection con : outputs) {
if (con.end.equals(Utils.toCC(nodeLink)))
connectionExists = true;
}
if (connectionExists)
player.sendStatusMessage(new TextComponentTranslation(Lib.CHAT_WARN + "connectionExists"), true);
else {
Set<BlockPos> ignore = new HashSet<>();
ignore.addAll(nodeHere.getIgnored(nodeLink));
ignore.addAll(nodeLink.getIgnored(nodeHere));
Connection tmpConn = new Connection(Utils.toCC(nodeHere), Utils.toCC(nodeLink), wire, (int) Math.sqrt(distanceSq));
Vec3d start = nodeHere.getConnectionOffset(tmpConn, target, pos.subtract(masterPos));
Vec3d end = nodeLink.getConnectionOffset(tmpConn, targetLink, offsetLink).add(linkPos.getX() - masterPos.getX(), linkPos.getY() - masterPos.getY(), linkPos.getZ() - masterPos.getZ());
BlockPos.MutableBlockPos failedReason = new BlockPos.MutableBlockPos();
boolean canSee = ApiUtils.raytraceAlongCatenaryRelative(tmpConn, (p) -> {
if (ignore.contains(p.getLeft()))
return false;
IBlockState state = world.getBlockState(p.getLeft());
if (ApiUtils.preventsConnection(world, p.getLeft(), state, p.getMiddle(), p.getRight())) {
failedReason.setPos(p.getLeft());
return true;
}
return false;
}, (p) -> {
}, start, end);
if (canSee) {
Connection conn = ImmersiveNetHandler.INSTANCE.addAndGetConnection(world, Utils.toCC(nodeHere), Utils.toCC(nodeLink), (int) Math.sqrt(distanceSq), wire);
nodeHere.connectCable(wire, target, nodeLink, offset);
nodeLink.connectCable(wire, targetLink, nodeHere, offsetLink);
ImmersiveNetHandler.INSTANCE.addBlockData(world, conn);
IESaveData.setDirty(world.provider.getDimension());
Utils.unlockIEAdvancement(player, "main/connect_wire");
if (!player.capabilities.isCreativeMode)
coil.consumeWire(stack, (int) Math.sqrt(distanceSq));
((TileEntity) nodeHere).markDirty();
world.addBlockEvent(masterPos, ((TileEntity) nodeHere).getBlockType(), -1, 0);
IBlockState state = world.getBlockState(masterPos);
world.notifyBlockUpdate(masterPos, state, state, 3);
((TileEntity) nodeLink).markDirty();
world.addBlockEvent(linkPos, ((TileEntity) nodeLink).getBlockType(), -1, 0);
state = world.getBlockState(linkPos);
world.notifyBlockUpdate(linkPos, state, state, 3);
} else {
player.sendStatusMessage(new TextComponentTranslation(Lib.CHAT_WARN + "cantSee"), true);
ImmersiveEngineering.packetHandler.sendToAllAround(new MessageObstructedConnection(tmpConn, failedReason, player.world), new NetworkRegistry.TargetPoint(player.world.provider.getDimension(), player.posX, player.posY, player.posZ, 64));
}
}
}
}
ItemNBTHelper.remove(stack, "linkingPos");
ItemNBTHelper.remove(stack, "targettingInfo");
}
return EnumActionResult.SUCCESS;
}
return EnumActionResult.PASS;
}
Aggregations