Search in sources :

Example 1 with IEnergyContainer

use of mekanism.api.energy.IEnergyContainer in project Mekanism by mekanism.

the class CommonPlayerTickHandler method onLivingJump.

@SubscribeEvent
public void onLivingJump(LivingJumpEvent event) {
    if (event.getEntityLiving() instanceof PlayerEntity) {
        PlayerEntity player = (PlayerEntity) event.getEntityLiving();
        IModule<ModuleHydraulicPropulsionUnit> module = MekanismAPI.getModuleHelper().load(player.getItemBySlot(EquipmentSlotType.FEET), MekanismModules.HYDRAULIC_PROPULSION_UNIT);
        if (module != null && module.isEnabled() && Mekanism.keyMap.has(player.getUUID(), KeySync.BOOST)) {
            float boost = module.getCustomInstance().getBoost();
            FloatingLong usage = MekanismConfig.gear.mekaSuitBaseJumpEnergyUsage.get().multiply(boost / 0.1F);
            IEnergyContainer energyContainer = module.getEnergyContainer();
            if (module.canUseEnergy(player, energyContainer, usage, false)) {
                // if we're sprinting with the boost module, limit the height
                IModule<ModuleLocomotiveBoostingUnit> boostModule = MekanismAPI.getModuleHelper().load(player.getItemBySlot(EquipmentSlotType.LEGS), MekanismModules.LOCOMOTIVE_BOOSTING_UNIT);
                if (boostModule != null && boostModule.isEnabled() && boostModule.getCustomInstance().canFunction(boostModule, player)) {
                    boost = (float) Math.sqrt(boost);
                }
                player.setDeltaMovement(player.getDeltaMovement().add(0, boost, 0));
                module.useEnergy(player, energyContainer, usage, true);
            }
        }
    }
}
Also used : FloatingLong(mekanism.api.math.FloatingLong) ModuleLocomotiveBoostingUnit(mekanism.common.content.gear.mekasuit.ModuleLocomotiveBoostingUnit) IEnergyContainer(mekanism.api.energy.IEnergyContainer) ModuleHydraulicPropulsionUnit(mekanism.common.content.gear.mekasuit.ModuleHydraulicPropulsionUnit) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 2 with IEnergyContainer

use of mekanism.api.energy.IEnergyContainer in project Mekanism by mekanism.

the class RenderTickHandler method renderOverlay.

@SubscribeEvent
public void renderOverlay(RenderGameOverlayEvent.Post event) {
    if (event.getType() == ElementType.HOTBAR) {
        if (!minecraft.player.isSpectator() && MekanismConfig.client.enableHUD.get() && MekanismClient.renderHUD) {
            int count = 0;
            Map<EquipmentSlotType, List<ITextComponent>> renderStrings = new LinkedHashMap<>();
            for (EquipmentSlotType slotType : EQUIPMENT_ORDER) {
                ItemStack stack = minecraft.player.getItemBySlot(slotType);
                if (stack.getItem() instanceof IItemHUDProvider) {
                    List<ITextComponent> list = new ArrayList<>();
                    ((IItemHUDProvider) stack.getItem()).addHUDStrings(list, minecraft.player, stack, slotType);
                    int size = list.size();
                    if (size > 0) {
                        renderStrings.put(slotType, list);
                        count += size;
                    }
                }
            }
            MatrixStack matrix = event.getMatrixStack();
            if (count > 0) {
                int start = (renderStrings.size() * 2) + (count * 9);
                boolean alignLeft = MekanismConfig.client.alignHUDLeft.get();
                MainWindow window = event.getWindow();
                int y = window.getGuiScaledHeight();
                float hudScale = MekanismConfig.client.hudScale.get();
                int yScale = (int) ((1 / hudScale) * y);
                matrix.pushPose();
                matrix.scale(hudScale, hudScale, hudScale);
                for (Map.Entry<EquipmentSlotType, List<ITextComponent>> entry : renderStrings.entrySet()) {
                    for (ITextComponent text : entry.getValue()) {
                        drawString(window, matrix, text, alignLeft, yScale - start, 0xC8C8C8);
                        start -= 9;
                    }
                    start -= 2;
                }
                matrix.popPose();
            }
            if (minecraft.player.getItemBySlot(EquipmentSlotType.HEAD).getItem() instanceof ItemMekaSuitArmor) {
                hudRenderer.renderHUD(matrix, event.getPartialTicks());
            }
        }
    } else if (event.getType() == ElementType.ARMOR) {
        FloatingLong capacity = FloatingLong.ZERO, stored = FloatingLong.ZERO;
        for (ItemStack stack : minecraft.player.inventory.armor) {
            if (stack.getItem() instanceof ItemMekaSuitArmor) {
                IEnergyContainer container = StorageUtils.getEnergyContainer(stack, 0);
                if (container != null) {
                    capacity = capacity.plusEqual(container.getMaxEnergy());
                    stored = stored.plusEqual(container.getEnergy());
                }
            }
        }
        if (!capacity.isZero()) {
            int x = event.getWindow().getGuiScaledWidth() / 2 - 91;
            int y = event.getWindow().getGuiScaledHeight() - ForgeIngameGui.left_height + 2;
            int length = (int) Math.round(stored.divide(capacity).doubleValue() * 79);
            MatrixStack matrix = event.getMatrixStack();
            GuiUtils.renderExtendedTexture(matrix, GuiBar.BAR, 2, 2, x, y, 81, 6);
            minecraft.getTextureManager().bind(POWER_BAR);
            AbstractGui.blit(matrix, x + 1, y + 1, length, 4, 0, 0, length, 4, 79, 4);
            minecraft.getTextureManager().bind(AbstractGui.GUI_ICONS_LOCATION);
            ForgeIngameGui.left_height += 8;
        }
    }
}
Also used : ItemMekaSuitArmor(mekanism.common.item.gear.ItemMekaSuitArmor) EquipmentSlotType(net.minecraft.inventory.EquipmentSlotType) MatrixStack(com.mojang.blaze3d.matrix.MatrixStack) ITextComponent(net.minecraft.util.text.ITextComponent) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) IItemHUDProvider(mekanism.common.item.interfaces.IItemHUDProvider) FloatingLong(mekanism.api.math.FloatingLong) IEnergyContainer(mekanism.api.energy.IEnergyContainer) MainWindow(net.minecraft.client.MainWindow) ArrayList(java.util.ArrayList) List(java.util.List) ItemStack(net.minecraft.item.ItemStack) Map(java.util.Map) EnumMap(java.util.EnumMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 3 with IEnergyContainer

use of mekanism.api.energy.IEnergyContainer in project Mekanism by mekanism.

the class TileEntityMekanism method getTotalEnergyFilledPercentage.

@ComputerMethod(nameOverride = "getEnergyFilledPercentage", restriction = MethodRestriction.ENERGY)
private double getTotalEnergyFilledPercentage() {
    FloatingLong stored = FloatingLong.ZERO;
    FloatingLong max = FloatingLong.ZERO;
    List<IEnergyContainer> energyContainers = getEnergyContainers(null);
    for (IEnergyContainer energyContainer : energyContainers) {
        stored = stored.plusEqual(energyContainer.getEnergy());
        max = max.plusEqual(energyContainer.getMaxEnergy());
    }
    return stored.divideToLevel(max);
}
Also used : SyncableFloatingLong(mekanism.common.inventory.container.sync.SyncableFloatingLong) FloatingLong(mekanism.api.math.FloatingLong) IEnergyContainer(mekanism.api.energy.IEnergyContainer) BoundComputerMethod(mekanism.common.integration.computer.BoundComputerMethod) ComputerMethod(mekanism.common.integration.computer.annotation.ComputerMethod)

Example 4 with IEnergyContainer

use of mekanism.api.energy.IEnergyContainer in project Mekanism by mekanism.

the class TileComponentEjector method eject.

/**
 * @apiNote Ensure that it can eject before calling this method.
 */
private void eject(TransmissionType type, ConfigInfo info) {
    // Used to keep track of tanks to what sides they output to
    Map<Object, Set<Direction>> outputData = null;
    for (DataType dataType : info.getSupportedDataTypes()) {
        if (dataType.canOutput()) {
            ISlotInfo slotInfo = info.getSlotInfo(dataType);
            if (slotInfo != null) {
                Set<Direction> outputSides = info.getSidesForData(dataType);
                if (!outputSides.isEmpty()) {
                    if (outputData == null) {
                        // Lazy init outputData
                        outputData = new HashMap<>();
                    }
                    if (type.isChemical() && slotInfo instanceof ChemicalSlotInfo) {
                        for (IChemicalTank<?, ?> tank : ((ChemicalSlotInfo<?, ?, ?>) slotInfo).getTanks()) {
                            if (!tank.isEmpty() && (canTankEject == null || canTankEject.test(tank))) {
                                outputData.computeIfAbsent(tank, t -> EnumSet.noneOf(Direction.class)).addAll(outputSides);
                            }
                        }
                    } else if (type == TransmissionType.FLUID && slotInfo instanceof FluidSlotInfo) {
                        for (IExtendedFluidTank tank : ((FluidSlotInfo) slotInfo).getTanks()) {
                            if (!tank.isEmpty()) {
                                outputData.computeIfAbsent(tank, t -> EnumSet.noneOf(Direction.class)).addAll(outputSides);
                            }
                        }
                    } else if (type == TransmissionType.ENERGY && slotInfo instanceof EnergySlotInfo) {
                        for (IEnergyContainer container : ((EnergySlotInfo) slotInfo).getContainers()) {
                            if (!container.isEmpty()) {
                                outputData.computeIfAbsent(container, t -> EnumSet.noneOf(Direction.class)).addAll(outputSides);
                            }
                        }
                    }
                }
            }
        }
    }
    if (outputData != null && !outputData.isEmpty()) {
        for (Map.Entry<Object, Set<Direction>> entry : outputData.entrySet()) {
            if (type.isChemical()) {
                ChemicalUtil.emit(entry.getValue(), (IChemicalTank<?, ?>) entry.getKey(), tile, chemicalEjectRate.getAsLong());
            } else if (type == TransmissionType.FLUID) {
                FluidUtils.emit(entry.getValue(), (IExtendedFluidTank) entry.getKey(), tile, fluidEjectRate.getAsInt());
            } else if (type == TransmissionType.ENERGY) {
                IEnergyContainer container = (IEnergyContainer) entry.getKey();
                CableUtils.emit(entry.getValue(), container, tile, energyEjectRate == null ? container.getMaxEnergy() : energyEjectRate.get());
            }
        }
    }
}
Also used : ISlotInfo(mekanism.common.tile.component.config.slot.ISlotInfo) LongSupplier(java.util.function.LongSupplier) SyncableInt(mekanism.common.inventory.container.sync.SyncableInt) ConfigInfo(mekanism.common.tile.component.config.ConfigInfo) CompoundNBT(net.minecraft.nbt.CompoundNBT) Direction(net.minecraft.util.Direction) FluidSlotInfo(mekanism.common.tile.component.config.slot.FluidSlotInfo) TileTransitRequest(mekanism.common.lib.inventory.TileTransitRequest) Map(java.util.Map) EnumSet(java.util.EnumSet) TileEntityLogisticalTransporterBase(mekanism.common.tile.transmitter.TileEntityLogisticalTransporterBase) EnumMap(java.util.EnumMap) NBTUtils(mekanism.common.util.NBTUtils) Predicate(java.util.function.Predicate) IExtendedFluidTank(mekanism.api.fluid.IExtendedFluidTank) Set(java.util.Set) List(java.util.List) ComputerException(mekanism.common.integration.computer.ComputerException) InventoryUtils(mekanism.common.util.InventoryUtils) RelativeSide(mekanism.api.RelativeSide) ISlotInfo(mekanism.common.tile.component.config.slot.ISlotInfo) NBTConstants(mekanism.api.NBTConstants) ChemicalUtil(mekanism.common.util.ChemicalUtil) EnumColor(mekanism.api.text.EnumColor) CableUtils(mekanism.common.util.CableUtils) SyncableBoolean(mekanism.common.inventory.container.sync.SyncableBoolean) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ISpecificContainerTracker(mekanism.common.inventory.container.MekanismContainer.ISpecificContainerTracker) ChemicalSlotInfo(mekanism.common.tile.component.config.slot.ChemicalSlotInfo) TransmissionType(mekanism.common.lib.transmitter.TransmissionType) TransitResponse(mekanism.common.lib.inventory.TransitRequest.TransitResponse) ComputerMethod(mekanism.common.integration.computer.annotation.ComputerMethod) IEnergyContainer(mekanism.api.energy.IEnergyContainer) Nonnull(javax.annotation.Nonnull) IntSupplier(java.util.function.IntSupplier) Nullable(javax.annotation.Nullable) TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) EnumUtils(mekanism.common.util.EnumUtils) DataType(mekanism.common.tile.component.config.DataType) InventorySlotInfo(mekanism.common.tile.component.config.slot.InventorySlotInfo) TransporterUtils(mekanism.common.util.TransporterUtils) FluidUtils(mekanism.common.util.FluidUtils) NBT(net.minecraftforge.common.util.Constants.NBT) EnergySlotInfo(mekanism.common.tile.component.config.slot.EnergySlotInfo) WorldUtils(mekanism.common.util.WorldUtils) TileEntity(net.minecraft.tileentity.TileEntity) IChemicalTank(mekanism.api.chemical.IChemicalTank) MekanismConfig(mekanism.common.config.MekanismConfig) FloatingLongSupplier(mekanism.api.math.FloatingLongSupplier) ISyncableData(mekanism.common.inventory.container.sync.ISyncableData) FluidSlotInfo(mekanism.common.tile.component.config.slot.FluidSlotInfo) EnumSet(java.util.EnumSet) Set(java.util.Set) EnergySlotInfo(mekanism.common.tile.component.config.slot.EnergySlotInfo) Direction(net.minecraft.util.Direction) ChemicalSlotInfo(mekanism.common.tile.component.config.slot.ChemicalSlotInfo) IEnergyContainer(mekanism.api.energy.IEnergyContainer) DataType(mekanism.common.tile.component.config.DataType) IExtendedFluidTank(mekanism.api.fluid.IExtendedFluidTank) Map(java.util.Map) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap)

Example 5 with IEnergyContainer

use of mekanism.api.energy.IEnergyContainer in project Mekanism by mekanism.

the class EnergyRecipeData method applyToStack.

@Override
public boolean applyToStack(ItemStack stack) {
    if (energyContainers.isEmpty()) {
        return true;
    }
    Item item = stack.getItem();
    Optional<IStrictEnergyHandler> capability = stack.getCapability(Capabilities.STRICT_ENERGY_CAPABILITY).resolve();
    List<IEnergyContainer> energyContainers = new ArrayList<>();
    if (capability.isPresent()) {
        IStrictEnergyHandler energyHandler = capability.get();
        for (int container = 0; container < energyHandler.getEnergyContainerCount(); container++) {
            energyContainers.add(BasicEnergyContainer.create(energyHandler.getMaxEnergy(container), null));
        }
    } else if (item instanceof BlockItem) {
        TileEntityMekanism tile = getTileFromBlock(((BlockItem) item).getBlock());
        if (tile == null || !tile.handles(SubstanceType.ENERGY)) {
            // Something went wrong
            return false;
        }
        for (int container = 0; container < tile.getEnergyContainerCount(); container++) {
            energyContainers.add(BasicEnergyContainer.create(tile.getMaxEnergy(container), null));
        }
    } else {
        return false;
    }
    if (energyContainers.isEmpty()) {
        // We don't actually have any tanks in the output
        return true;
    }
    IMekanismStrictEnergyHandler outputHandler = new IMekanismStrictEnergyHandler() {

        @Nonnull
        @Override
        public List<IEnergyContainer> getEnergyContainers(@Nullable Direction side) {
            return energyContainers;
        }

        @Override
        public void onContentsChanged() {
        }
    };
    boolean hasData = false;
    for (IEnergyContainer energyContainer : this.energyContainers) {
        if (!energyContainer.isEmpty()) {
            hasData = true;
            if (!outputHandler.insertEnergy(energyContainer.getEnergy(), Action.EXECUTE).isZero()) {
                // If we have a remainder, stop trying to insert as our upgraded item's buffer is just full
                break;
            }
        }
    }
    if (hasData) {
        // We managed to transfer it all into valid slots, so save it to the stack
        ItemDataUtils.setList(stack, NBTConstants.ENERGY_CONTAINERS, DataHandlerUtils.writeContainers(energyContainers));
    }
    return true;
}
Also used : TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) ArrayList(java.util.ArrayList) IMekanismStrictEnergyHandler(mekanism.api.energy.IMekanismStrictEnergyHandler) BlockItem(net.minecraft.item.BlockItem) Direction(net.minecraft.util.Direction) IStrictEnergyHandler(mekanism.api.energy.IStrictEnergyHandler) Item(net.minecraft.item.Item) BlockItem(net.minecraft.item.BlockItem) IEnergyContainer(mekanism.api.energy.IEnergyContainer) Nullable(javax.annotation.Nullable)

Aggregations

IEnergyContainer (mekanism.api.energy.IEnergyContainer)34 FloatingLong (mekanism.api.math.FloatingLong)26 BlockPos (net.minecraft.util.math.BlockPos)13 PlayerEntity (net.minecraft.entity.player.PlayerEntity)12 ItemStack (net.minecraft.item.ItemStack)12 Nonnull (javax.annotation.Nonnull)10 Direction (net.minecraft.util.Direction)8 World (net.minecraft.world.World)8 BlockState (net.minecraft.block.BlockState)6 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)6 ArrayList (java.util.ArrayList)5 List (java.util.List)4 Map (java.util.Map)4 Nullable (javax.annotation.Nullable)4 TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)4 TileEntity (net.minecraft.tileentity.TileEntity)4 SyncableFloatingLong (mekanism.common.inventory.container.sync.SyncableFloatingLong)3 EnumMap (java.util.EnumMap)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2