Search in sources :

Example 1 with MessageObstructedConnection

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;
}
Also used : net.minecraft.util(net.minecraft.util) IItemHandler(net.minecraftforge.items.IItemHandler) ImmersiveEngineering(blusunrize.immersiveengineering.ImmersiveEngineering) Item(net.minecraft.item.Item) IESaveData(blusunrize.immersiveengineering.common.IESaveData) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) IngredientStack(blusunrize.immersiveengineering.api.crafting.IngredientStack) ListenableFutureTask(com.google.common.util.concurrent.ListenableFutureTask) Block(net.minecraft.block.Block) Side(net.minecraftforge.fml.relauncher.Side) OreDictionary(net.minecraftforge.oredict.OreDictionary) Connection.vertices(blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection.vertices) Triple(org.apache.commons.lang3.tuple.Triple) VertexFormat(net.minecraft.client.renderer.vertex.VertexFormat) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) net.minecraft.util.math(net.minecraft.util.math) Predicate(java.util.function.Predicate) BakedQuad(net.minecraft.client.renderer.block.model.BakedQuad) IVertexConsumer(net.minecraftforge.client.model.pipeline.IVertexConsumer) VertexFormatElement(net.minecraft.client.renderer.vertex.VertexFormatElement) Vector3f(org.lwjgl.util.vector.Vector3f) EntityPlayer(net.minecraft.entity.player.EntityPlayer) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) FluidStack(net.minecraftforge.fluids.FluidStack) Ingredient(net.minecraft.item.crafting.Ingredient) FluidUtil(net.minecraftforge.fluids.FluidUtil) java.util(java.util) Connection(blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ItemStack(net.minecraft.item.ItemStack) ItemHandlerHelper(net.minecraftforge.items.ItemHandlerHelper) IGeneralMultiblock(blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IGeneralMultiblock) blusunrize.immersiveengineering.api.energy.wires(blusunrize.immersiveengineering.api.energy.wires) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) TextureAtlasSprite(net.minecraft.client.renderer.texture.TextureAtlasSprite) NBTTagInt(net.minecraft.nbt.NBTTagInt) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) MessageObstructedConnection(blusunrize.immersiveengineering.common.util.network.MessageObstructedConnection) Utils(blusunrize.immersiveengineering.common.util.Utils) World(net.minecraft.world.World) EventHandler(blusunrize.immersiveengineering.common.EventHandler) TextureMap(net.minecraft.client.renderer.texture.TextureMap) ItemNBTHelper(blusunrize.immersiveengineering.common.util.ItemNBTHelper) Consumer(java.util.function.Consumer) IBlockState(net.minecraft.block.state.IBlockState) NetworkRegistry(net.minecraftforge.fml.common.network.NetworkRegistry) UnpackedBakedQuad(net.minecraftforge.client.model.pipeline.UnpackedBakedQuad) Matrix4(blusunrize.immersiveengineering.common.util.chickenbones.Matrix4) EntityLivingBase(net.minecraft.entity.EntityLivingBase) TileEntity(net.minecraft.tileentity.TileEntity) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) IBlockState(net.minecraft.block.state.IBlockState) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Connection(blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection) MessageObstructedConnection(blusunrize.immersiveengineering.common.util.network.MessageObstructedConnection) MessageObstructedConnection(blusunrize.immersiveengineering.common.util.network.MessageObstructedConnection) TileEntity(net.minecraft.tileentity.TileEntity) ItemStack(net.minecraft.item.ItemStack)

Aggregations

ImmersiveEngineering (blusunrize.immersiveengineering.ImmersiveEngineering)1 IngredientStack (blusunrize.immersiveengineering.api.crafting.IngredientStack)1 blusunrize.immersiveengineering.api.energy.wires (blusunrize.immersiveengineering.api.energy.wires)1 Connection (blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection)1 Connection.vertices (blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection.vertices)1 EventHandler (blusunrize.immersiveengineering.common.EventHandler)1 IESaveData (blusunrize.immersiveengineering.common.IESaveData)1 IGeneralMultiblock (blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IGeneralMultiblock)1 ItemNBTHelper (blusunrize.immersiveengineering.common.util.ItemNBTHelper)1 Utils (blusunrize.immersiveengineering.common.util.Utils)1 Matrix4 (blusunrize.immersiveengineering.common.util.chickenbones.Matrix4)1 MessageObstructedConnection (blusunrize.immersiveengineering.common.util.network.MessageObstructedConnection)1 AtomicDouble (com.google.common.util.concurrent.AtomicDouble)1 ListenableFutureTask (com.google.common.util.concurrent.ListenableFutureTask)1 java.util (java.util)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Nonnull (javax.annotation.Nonnull)1