Search in sources :

Example 21 with FluidTank

use of net.minecraftforge.fluids.capability.templates.FluidTank in project BluePower by Qmunity.

the class NetworkUtils method getSyncedFieldsForField.

private static List<SyncedField> getSyncedFieldsForField(Field field, Object te, Class searchedAnnotation) {
    boolean isLazy = field.getAnnotation(LazySynced.class) != null;
    List<SyncedField> syncedFields = new ArrayList<SyncedField>();
    SyncedField syncedField = getSyncedFieldForField(field, te);
    if (syncedField != null) {
        syncedFields.add(syncedField.setLazy(isLazy));
        return syncedFields;
    } else {
        Object o;
        try {
            int filteredIndex = field.getAnnotation(FilteredSynced.class) != null ? field.getAnnotation(FilteredSynced.class).index() : -1;
            field.setAccessible(true);
            o = field.get(te);
            if (o instanceof int[]) {
                int[] array = (int[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedInt(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedInt(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o instanceof float[]) {
                float[] array = (float[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedFloat(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedFloat(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o instanceof double[]) {
                double[] array = (double[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedDouble(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedDouble(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o instanceof boolean[]) {
                boolean[] array = (boolean[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedBoolean(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedBoolean(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o instanceof String[]) {
                String[] array = (String[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedString(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedString(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o.getClass().isArray() && o.getClass().getComponentType().isEnum()) {
                Object[] enumArray = (Object[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedEnum(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < enumArray.length; i++) {
                        syncedFields.add(new SyncedEnum(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o instanceof ItemStack[]) {
                ItemStack[] array = (ItemStack[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedItemStack(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedItemStack(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (o instanceof FluidTank[]) {
                FluidTank[] array = (FluidTank[]) o;
                if (filteredIndex >= 0) {
                    syncedFields.add(new SyncedFluidTank(te, field).setArrayIndex(filteredIndex).setLazy(isLazy));
                } else {
                    for (int i = 0; i < array.length; i++) {
                        syncedFields.add(new SyncedFluidTank(te, field).setArrayIndex(i).setLazy(isLazy));
                    }
                }
                return syncedFields;
            }
            if (field.getType().isArray()) {
                Object[] array = (Object[]) o;
                for (Object obj : array) {
                    syncedFields.addAll(getSyncedFields(obj, searchedAnnotation));
                }
            } else {
                syncedFields.addAll(getSyncedFields(o, searchedAnnotation));
            }
            if (syncedFields.size() > 0)
                return syncedFields;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BPLog.warning("Field " + field + " didn't produce any syncable fields!");
        return syncedFields;
    }
}
Also used : ArrayList(java.util.ArrayList) SyncedBoolean(com.bluepowermod.network.annotation.SyncedField.SyncedBoolean) SyncedString(com.bluepowermod.network.annotation.SyncedField.SyncedString) SyncedInt(com.bluepowermod.network.annotation.SyncedField.SyncedInt) SyncedDouble(com.bluepowermod.network.annotation.SyncedField.SyncedDouble) SyncedEnum(com.bluepowermod.network.annotation.SyncedField.SyncedEnum) SyncedItemStack(com.bluepowermod.network.annotation.SyncedField.SyncedItemStack) FluidTank(net.minecraftforge.fluids.capability.templates.FluidTank) SyncedFluidTank(com.bluepowermod.network.annotation.SyncedField.SyncedFluidTank) SyncedItemStack(com.bluepowermod.network.annotation.SyncedField.SyncedItemStack) ItemStack(net.minecraft.item.ItemStack) SyncedFluidTank(com.bluepowermod.network.annotation.SyncedField.SyncedFluidTank) SyncedFloat(com.bluepowermod.network.annotation.SyncedField.SyncedFloat) SyncedString(com.bluepowermod.network.annotation.SyncedField.SyncedString)

Example 22 with FluidTank

use of net.minecraftforge.fluids.capability.templates.FluidTank in project Electrodynamics by aurilisdev.

the class RenderCoolantResavoir method render.

@Override
public void render(TileCoolantResavoir entity, float tick, PoseStack stack, MultiBufferSource source, int light, int overlay) {
    FluidTank tank = ((ComponentFluidHandlerSimple) entity.getComponent(ComponentType.FluidHandler)).getOutputTanks()[0];
    if (!tank.isEmpty() && tank.getFluidAmount() > 0) {
        FluidStack fluid = tank.getFluid();
        float yHeight = Math.max(Math.min((float) tank.getFluidAmount() / (float) tank.getCapacity(), MAX_Y), MIN_Y);
        AABB aabb = new AABB(MIN_X, MIN_Y, MIN_Z, MAX_X, yHeight, MAX_Z);
        VertexConsumer builder = source.getBuffer(Sheets.translucentCullBlockSheet());
        RenderingUtils.renderFluidBox(stack, Minecraft.getInstance(), builder, aabb, fluid, light, overlay);
    }
}
Also used : FluidTank(net.minecraftforge.fluids.capability.templates.FluidTank) FluidStack(net.minecraftforge.fluids.FluidStack) VertexConsumer(com.mojang.blaze3d.vertex.VertexConsumer) AABB(net.minecraft.world.phys.AABB)

Example 23 with FluidTank

use of net.minecraftforge.fluids.capability.templates.FluidTank in project Electrodynamics by aurilisdev.

the class FluidUtilities method fillItem.

public static void fillItem(GenericTile tile) {
    ComponentInventory inv = tile.getComponent(ComponentType.Inventory);
    AbstractFluidHandler<?> handler = tile.getComponent(ComponentType.FluidHandler);
    FluidTank[] tanks = handler.getOutputTanks();
    List<ItemStack> buckets = inv.getOutputBucketContents();
    for (int i = 0; i < buckets.size(); i++) {
        ItemStack stack = buckets.get(i);
        FluidTank tank = tanks[i];
        if (!stack.isEmpty() && !(stack.getItem() instanceof BucketItem) && !CapabilityUtils.isFluidItemNull()) {
            FluidStack fluid = tank.getFluid();
            int amtFilled = CapabilityUtils.simFill(stack, fluid);
            FluidStack taken = new FluidStack(fluid.getFluid(), amtFilled);
            CapabilityUtils.fill(stack, taken);
            tank.drain(taken, FluidAction.EXECUTE);
        }
    }
}
Also used : FluidTank(net.minecraftforge.fluids.capability.templates.FluidTank) ComponentInventory(electrodynamics.prefab.tile.components.type.ComponentInventory) FluidStack(net.minecraftforge.fluids.FluidStack) BucketItem(net.minecraft.world.item.BucketItem) ItemStack(net.minecraft.world.item.ItemStack)

Example 24 with FluidTank

use of net.minecraftforge.fluids.capability.templates.FluidTank in project Electrodynamics by aurilisdev.

the class TileCombustionChamber method tickServer.

protected void tickServer(ComponentTickable tickable) {
    ComponentDirection direction = getComponent(ComponentType.Direction);
    Direction facing = direction.getDirection();
    if (output == null) {
        output = new CachedTileOutput(level, worldPosition.relative(facing.getClockWise()));
    }
    if (tickable.getTicks() % 40 == 0) {
        output.update();
    }
    if (tickable.getTicks() % 5 == 0) {
        this.<ComponentPacketHandler>getComponent(ComponentType.PacketHandler).sendGuiPacketToTracking();
    }
    ComponentFluidHandlerMulti handler = getComponent(ComponentType.FluidHandler);
    FluidUtilities.drainItem(this);
    FluidTank tank = handler.getInputTanks()[0];
    if (burnTime <= 0) {
        running = false;
        if (tank.getFluidAmount() > 0) {
            CombustionFuelSource source = CombustionFuelSource.getSourceFromFluid(tank.getFluid().getFluid());
            tank.getFluid().shrink(source.getFluidUsage());
            multiplier = source.getPowerMultiplier();
            running = true;
            burnTime = TICKS_PER_MILLIBUCKET;
        }
    } else {
        running = true;
    }
    if (burnTime > 0) {
        --burnTime;
    }
    if (running && burnTime > 0 && output.valid()) {
        ElectricityUtils.receivePower(output.getSafe(), facing.getClockWise().getOpposite(), getProduced(), false);
    }
}
Also used : FluidTank(net.minecraftforge.fluids.capability.templates.FluidTank) CachedTileOutput(electrodynamics.prefab.utilities.object.CachedTileOutput) CombustionFuelSource(electrodynamics.prefab.utilities.tile.CombustionFuelSource) ComponentDirection(electrodynamics.prefab.tile.components.type.ComponentDirection) Direction(net.minecraft.core.Direction) ComponentDirection(electrodynamics.prefab.tile.components.type.ComponentDirection) ComponentFluidHandlerMulti(electrodynamics.prefab.tile.components.type.ComponentFluidHandlerMulti)

Example 25 with FluidTank

use of net.minecraftforge.fluids.capability.templates.FluidTank in project Electrodynamics by aurilisdev.

the class TileCreativeFluidSource method tickServer.

private void tickServer(ComponentTickable tick) {
    ComponentFluidHandlerSimple handler = (ComponentFluidHandlerSimple) getComponent(ComponentType.FluidHandler);
    ComponentInventory inv = getComponent(ComponentType.Inventory);
    ItemStack input = inv.getItem(0);
    ItemStack output = inv.getItem(1);
    FluidTank tank = handler.getInputTanks()[0];
    tank.setFluid(new FluidStack(tank.getFluid(), tank.getCapacity()));
    // set tank fluid from slot 1
    if (!input.isEmpty() && CapabilityUtils.hasFluidItemCap(input)) {
        tank.setFluid(new FluidStack(CapabilityUtils.simDrain(input, Integer.MAX_VALUE).getFluid(), tank.getCapacity()));
    }
    // fill item in slot 2
    if (!output.isEmpty() && CapabilityUtils.hasFluidItemCap(output)) {
        boolean isBucket = output.getItem() instanceof BucketItem;
        FluidStack tankFluid = handler.getFluidInTank(0);
        int amtTaken = CapabilityUtils.simFill(output, handler.getFluidInTank(0));
        Fluid fluid = tankFluid.getFluid();
        if (amtTaken > 0 && !isBucket) {
            CapabilityUtils.fill(output, new FluidStack(fluid, amtTaken));
        } else if (amtTaken >= 1000 && isBucket && (fluid.isSame(Fluids.WATER) || fluid.isSame(Fluids.LAVA))) {
            if (fluid.isSame(Fluids.WATER)) {
                inv.setItem(1, new ItemStack(Items.WATER_BUCKET, 1));
            } else {
                inv.setItem(1, new ItemStack(Items.LAVA_BUCKET, 1));
            }
        }
    }
    // try to output to pipe
    ComponentDirection componentDirection = getComponent(ComponentType.Direction);
    for (Direction relative : handler.relativeOutputDirections) {
        Direction direction = BlockEntityUtils.getRelativeSide(componentDirection.getDirection(), relative.getOpposite());
        BlockPos face = getBlockPos().relative(direction.getOpposite());
        BlockEntity faceTile = getLevel().getBlockEntity(face);
        if (faceTile != null) {
            boolean electroPipe = faceTile instanceof GenericTilePipe;
            LazyOptional<IFluidHandler> cap = faceTile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, direction);
            if (cap.isPresent()) {
                IFluidHandler fHandler = cap.resolve().get();
                for (FluidTank fluidTank : handler.getOutputTanks()) {
                    FluidStack tankFluid = fluidTank.getFluid();
                    if (electroPipe) {
                        if (fluidTank.getFluidAmount() > 0) {
                            fHandler.fill(tankFluid, FluidAction.EXECUTE);
                        }
                    } else {
                        int amtAccepted = fHandler.fill(tankFluid, FluidAction.SIMULATE);
                        FluidStack taken = new FluidStack(tankFluid.getFluid(), amtAccepted);
                        fHandler.fill(taken, FluidAction.EXECUTE);
                    }
                }
            }
        }
    }
}
Also used : FluidStack(net.minecraftforge.fluids.FluidStack) Fluid(net.minecraft.world.level.material.Fluid) Direction(net.minecraft.core.Direction) ComponentDirection(electrodynamics.prefab.tile.components.type.ComponentDirection) GenericTilePipe(electrodynamics.common.tile.generic.GenericTilePipe) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) FluidTank(net.minecraftforge.fluids.capability.templates.FluidTank) ComponentInventory(electrodynamics.prefab.tile.components.type.ComponentInventory) BucketItem(net.minecraft.world.item.BucketItem) ComponentDirection(electrodynamics.prefab.tile.components.type.ComponentDirection) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) ComponentFluidHandlerSimple(electrodynamics.prefab.tile.components.type.ComponentFluidHandlerSimple) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Aggregations

FluidTank (net.minecraftforge.fluids.capability.templates.FluidTank)35 FluidStack (net.minecraftforge.fluids.FluidStack)20 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)8 ItemStack (net.minecraft.world.item.ItemStack)7 BlockPos (net.minecraft.core.BlockPos)6 Direction (net.minecraft.core.Direction)6 ComponentInventory (electrodynamics.prefab.tile.components.type.ComponentInventory)5 CompoundTag (net.minecraft.nbt.CompoundTag)5 ArrayList (java.util.ArrayList)4 BucketItem (net.minecraft.world.item.BucketItem)4 Level (net.minecraft.world.level.Level)4 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)4 ComponentDirection (electrodynamics.prefab.tile.components.type.ComponentDirection)3 Nonnull (javax.annotation.Nonnull)3 Nullable (javax.annotation.Nullable)3 ItemStack (net.minecraft.item.ItemStack)3 BlockState (net.minecraft.world.level.block.state.BlockState)3 AABB (net.minecraft.world.phys.AABB)3 VertexConsumer (com.mojang.blaze3d.vertex.VertexConsumer)2 Iterate (com.simibubi.create.foundation.utility.Iterate)2