Search in sources :

Example 1 with IEnergy

use of com.bewitchment.api.capability.IEnergy in project Bewitchment by Um-Mitternacht.

the class EnergyHandler method addEnergy.

/**
 * Adds the given value to the energy of the player.
 * <p>
 * If the amount drained is greater than the available amount,
 * it automatically gets stored as overchannel
 * </p>
 *
 * @param player The player
 * @param amount The amount
 * @return If the amount was greater or equal than 0 and less or equal than the max amount
 */
public static boolean addEnergy(EntityPlayer player, int amount) {
    final Optional<IEnergy> optional = getEnergy(player);
    boolean mod = false;
    if (optional.isPresent()) {
        IEnergy energy = optional.get();
        mod = energy.set(energy.get() + amount);
        energy.tickReset();
        if (player instanceof EntityPlayerMP)
            energy.syncTo((EntityPlayerMP) player);
    }
    return mod;
}
Also used : IEnergy(com.bewitchment.api.capability.IEnergy) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP)

Example 2 with IEnergy

use of com.bewitchment.api.capability.IEnergy in project Bewitchment by Um-Mitternacht.

the class EnergyHUD method renderOverlay.

@SubscribeEvent
public void renderOverlay(RenderGameOverlayEvent.Post event) {
    if (event.getType() == RenderGameOverlayEvent.ElementType.HOTBAR && renderTime > 0) {
        Minecraft mc = Minecraft.getMinecraft();
        TextureManager manager = mc.getTextureManager();
        Optional<IEnergy> optional = EnergyHandler.getEnergy(mc.player);
        if (optional.isPresent()) {
            IEnergy energy = optional.get();
            GlStateManager.pushMatrix();
            GlStateManager.enableBlend();
            ScaledResolution resolution = event.getResolution();
            double interpEnergy = 0;
            if (oldEnergy >= 0) {
                interpEnergy = (double) (energy.get() - oldEnergy) * event.getPartialTicks() + oldEnergy;
            } else {
                interpEnergy = energy.get();
            }
            double filled = interpEnergy / energy.getMax();
            // System.out.println("fil: " + filled + ", chg: " + energy.get() + ", max: " + energy.getMax());
            int height = ConfigHandler.CLIENT.ENERGY_HUD.height;
            int width = ConfigHandler.CLIENT.ENERGY_HUD.width;
            int x = ConfigHandler.CLIENT.ENERGY_HUD.x;
            int y = resolution.getScaledHeight() - ConfigHandler.CLIENT.ENERGY_HUD.y;
            if (ConfigHandler.CLIENT.ENERGY_HUD.hide) {
                GlStateManager.color(1F, 1F, 1F, visible);
            }
            double barWidth = width * 7 / 25;
            GlStateManager.disableCull();
            manager.bindTexture(ResourceLocations.ENERGY_BACKGROUND[0]);
            renderTexture(x + 9, y + 88, barWidth, -(height - 28D) * filled, 0, filled);
            if (visible == 1f) {
                GlStateManager.pushMatrix();
                GlStateManager.color(1F, 1F, 1F, visible == 1F ? barAlpha : visible);
                manager.bindTexture(ResourceLocations.ENERGY_BACKGROUND[1]);
                renderTexture(x + 9, y + 88, barWidth, -(height - 28D) * filled, 0, filled);
                GlStateManager.enableCull();
                GlStateManager.popMatrix();
            }
            if (ConfigHandler.CLIENT.ENERGY_HUD.hide) {
                GlStateManager.color(1F, 1F, 1F, visible);
            }
            manager.bindTexture(energy.getType().getTexture());
            renderTexture(x, y, width, height, 0, 1);
            int textColor = 0x990066;
            if (ConfigHandler.CLIENT.ENERGY_HUD.hide) {
                int alpha = (int) (visible * 255);
                textColor = alpha << 24 | 0x990066;
            }
            String text = "E: " + energy.get();
            mc.fontRenderer.drawStringWithShadow(text, x, y - 10, textColor);
            GlStateManager.disableBlend();
            GlStateManager.popMatrix();
        }
    }
}
Also used : TextureManager(net.minecraft.client.renderer.texture.TextureManager) IEnergy(com.bewitchment.api.capability.IEnergy) ScaledResolution(net.minecraft.client.gui.ScaledResolution) Minecraft(net.minecraft.client.Minecraft) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 3 with IEnergy

use of com.bewitchment.api.capability.IEnergy in project Bewitchment by Um-Mitternacht.

the class RitualInfusion method onFinish.

@Override
public void onFinish(EntityPlayer player, TileEntity tile, World world, BlockPos pos, NBTTagCompound data) {
    if (player == null) {
        return;
    }
    Optional<IEnergy> eng = EnergyHandler.getEnergy(player);
    if (eng.isPresent()) {
        IEnergy ien = eng.get();
        ien.setType(type);
        if (player instanceof EntityPlayerMP)
            ien.syncTo((EntityPlayerMP) player);
    }
}
Also used : IEnergy(com.bewitchment.api.capability.IEnergy) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP)

Example 4 with IEnergy

use of com.bewitchment.api.capability.IEnergy in project Bewitchment by Um-Mitternacht.

the class EnergyHUD method onTick.

@SubscribeEvent
public void onTick(TickEvent.ClientTickEvent event) {
    if (event.phase == TickEvent.Phase.END && Minecraft.getMinecraft().player != null) {
        Optional<IEnergy> optional = EnergyHandler.getEnergy(Minecraft.getMinecraft().player);
        if (optional.isPresent()) {
            IEnergy energy = optional.get();
            if (lastPulsed > 0)
                lastPulsed--;
            boolean energyChanged = (oldEnergy != energy.get());
            if (energyChanged)
                shouldPulse = lastPulsed == 0;
            if (energyChanged || isItemEnergyUsing()) {
                oldEnergy = energy.get();
                renderTime = 60;
                visible = 1F;
            }
            if (renderTime > 0 && energy.get() == energy.getMax()) {
                if (ConfigHandler.CLIENT.ENERGY_HUD.hide && renderTime < 20) {
                    visible -= 0.05F;
                    visible = MathHelper.clamp(visible, 0F, 1F);
                }
                renderTime--;
            }
            if (shouldPulse) {
                if (!reverse) {
                    barAlpha += 0.15F;
                    if (barAlpha > 1F) {
                        barAlpha = 1F;
                        reverse = true;
                    }
                } else {
                    barAlpha -= 0.15F;
                    if (barAlpha < 0F) {
                        barAlpha = 0;
                        reverse = false;
                        shouldPulse = false;
                        lastPulsed = 40;
                    }
                }
            }
        }
    }
}
Also used : IEnergy(com.bewitchment.api.capability.IEnergy) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 5 with IEnergy

use of com.bewitchment.api.capability.IEnergy in project Bewitchment by Um-Mitternacht.

the class CommandIncantation method execute.

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
    if (args.length < 1)
        throw new WrongUsageException("commands.incantation.usage");
    if (sender.getCommandSenderEntity() == null)
        return;
    final String command = args[0];
    if (ModIncantations.getCommands().containsKey(command)) {
        IIncantation incantation = ModIncantations.getCommands().get(command);
        if (sender.getCommandSenderEntity() instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer) sender.getCommandSenderEntity();
            Optional<IEnergy> ienopt = EnergyHandler.getEnergy(player);
            if (ienopt.isPresent()) {
                if (ienopt.get().get() >= incantation.getCost()) {
                    EnergyHandler.addEnergy(player, -incantation.getCost());
                    incantation.cast(player, args);
                } else {
                    throw new CommandException("commands.incantation.no_energy", sender.getName());
                }
            } else {
                throw new CommandException("commands.incantation.no_energy", sender.getName());
            }
        }
    } else {
        throw new CommandException("commands.incantation.notFound", sender.getName());
    }
}
Also used : IIncantation(com.bewitchment.api.incantation.IIncantation) WrongUsageException(net.minecraft.command.WrongUsageException) IEnergy(com.bewitchment.api.capability.IEnergy) EntityPlayer(net.minecraft.entity.player.EntityPlayer) CommandException(net.minecraft.command.CommandException)

Aggregations

IEnergy (com.bewitchment.api.capability.IEnergy)8 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)5 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)4 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 IIncantation (com.bewitchment.api.incantation.IIncantation)1 Minecraft (net.minecraft.client.Minecraft)1 ScaledResolution (net.minecraft.client.gui.ScaledResolution)1 TextureManager (net.minecraft.client.renderer.texture.TextureManager)1 CommandException (net.minecraft.command.CommandException)1 WrongUsageException (net.minecraft.command.WrongUsageException)1