Search in sources :

Example 1 with Nutrient

use of ca.wescook.nutrition.nutrients.Nutrient in project Nutrition by WesCook.

the class EventEatFood method rightClickBlock.

// Detect eating cake
@SubscribeEvent
public void rightClickBlock(PlayerInteractEvent.RightClickBlock event) {
    // Only run on server
    EntityPlayer player = (EntityPlayer) event.getEntity();
    if (player.getEntityWorld().isRemote)
        return;
    // Get info
    World world = event.getWorld();
    IBlockState blockState = world.getBlockState(event.getPos());
    // Get out if not cake
    if (!(blockState.getBlock() instanceof BlockCake)) {
        return;
    }
    // Should we let them eat cake?
    if (player.canEat(false) || Config.allowOverEating) {
        // Calculate nutrition
        // Get cake Item from registry name
        Item item = Item.getByNameOrId(blockState.getBlock().getRegistryName().toString());
        ItemStack itemStack = new ItemStack(item);
        List<Nutrient> foundNutrients = NutrientUtils.getFoodNutrients(itemStack);
        float nutritionValue = NutrientUtils.calculateNutrition(itemStack, foundNutrients);
        // Add to each nutrient
        player.getCapability(CapProvider.NUTRITION_CAPABILITY, null).add(foundNutrients, nutritionValue, true);
        // If full but over-eating, simulate cake eating
        if (!player.canEat(false) && Config.allowOverEating) {
            int cakeBites = blockState.getValue(BlockCake.BITES);
            if (cakeBites < 6)
                world.setBlockState(event.getPos(), blockState.withProperty(BlockCake.BITES, cakeBites + 1), 3);
            else
                world.setBlockToAir(event.getPos());
        }
    }
}
Also used : Item(net.minecraft.item.Item) IBlockState(net.minecraft.block.state.IBlockState) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Nutrient(ca.wescook.nutrition.nutrients.Nutrient) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) BlockCake(net.minecraft.block.BlockCake) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 2 with Nutrient

use of ca.wescook.nutrition.nutrients.Nutrient in project Nutrition by WesCook.

the class ChatCommand method commandGetNutrition.

private void commandGetNutrition(ICommandSender sender, String[] args) {
    // If missing parameter, offer help
    if (args.length != 2) {
        sender.sendMessage(new TextComponentString("Invalid format.  /nutrition get <nutrient>"));
        return;
    }
    // Write nutrient name and percentage to chat
    EntityPlayer player = (EntityPlayer) sender;
    Nutrient nutrient = NutrientList.getByName(args[1]);
    if (nutrient != null) {
        Float nutrientValue = player.getCapability(CapProvider.NUTRITION_CAPABILITY, null).get(nutrient);
        sender.sendMessage(new TextComponentString(nutrient.name + ": " + String.format("%.2f", nutrientValue) + "%"));
    } else
        // Write error message
        sender.sendMessage(new TextComponentString("'" + args[1] + "' is not a valid nutrient."));
}
Also used : EntityPlayer(net.minecraft.entity.player.EntityPlayer) Nutrient(ca.wescook.nutrition.nutrients.Nutrient) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 3 with Nutrient

use of ca.wescook.nutrition.nutrients.Nutrient in project Nutrition by WesCook.

the class NutritionGui method redrawLabels.

// Called when needing to propagate the window with new information
public void redrawLabels() {
    // Clear existing labels for nutrition value or screen changes
    labelList.clear();
    // Draw title
    String nutritionTitle = I18n.format("gui." + Nutrition.MODID + ":nutrition_title");
    labelList.add(label = new GuiLabel(fontRenderer, 0, (width / 2) - (fontRenderer.getStringWidth(nutritionTitle) / 2), top + TITLE_VERTICAL_OFFSET, 0, 0, 0xffffffff));
    label.addLine(nutritionTitle);
    // Nutrients names and values
    int i = 0;
    for (Nutrient nutrient : NutrientList.get()) {
        // Create labels for each nutrient type name
        labelList.add(label = new GuiLabel(fontRenderer, 0, left + LABEL_NAME_HORIZONTAL_OFFSET, top + LABEL_VERTICAL_OFFSET + (i * NUTRITION_DISTANCE), 0, 0, 0xffffffff));
        // Add name from localization file
        label.addLine(I18n.format("nutrient." + Nutrition.MODID + ":" + nutrient.name));
        // Create percent value labels for each nutrient value
        labelList.add(label = new GuiLabel(fontRenderer, 0, left + LABEL_VALUE_HORIZONTAL_OFFSET + labelCharacterPadding, top + LABEL_VERTICAL_OFFSET + (i * NUTRITION_DISTANCE), 0, 0, 0xffffffff));
        if (// Ensure local nutrition data exists
        ClientProxy.nutrientData != null && ClientProxy.nutrientData.get(nutrient) != null)
            label.addLine(Math.round(ClientProxy.nutrientData.get(nutrient)) + "%%");
        else
            label.addLine(I18n.format("gui." + Nutrition.MODID + ":updating"));
        i++;
    }
}
Also used : Nutrient(ca.wescook.nutrition.nutrients.Nutrient) GuiLabel(net.minecraft.client.gui.GuiLabel)

Example 4 with Nutrient

use of ca.wescook.nutrition.nutrients.Nutrient in project Nutrition by WesCook.

the class NutritionGui method initGui.

// Called when GUI is opened or resized
@Override
public void initGui() {
    // Nutrition sync request
    ModPacketHandler.NETWORK_CHANNEL.sendToServer(new PacketNutritionRequest.Message());
    // Calculate label offset for long nutrition names
    for (Nutrient nutrient : NutrientList.get()) {
        // Get width of localized string
        int nutrientWidth = fontRenderer.getStringWidth(I18n.format("nutrient." + Nutrition.MODID + ":" + nutrient.name));
        // Round to nearest multiple of 4
        nutrientWidth = (nutrientWidth / 4) * 4;
        if (nutrientWidth > labelCharacterPadding)
            this.labelCharacterPadding = nutrientWidth;
    }
    // Update dynamic GUI size
    super.updateContainerSize(GUI_BASE_WIDTH + labelCharacterPadding, GUI_BASE_HEIGHT + (NutrientList.get().size() * NUTRITION_DISTANCE));
    // Add Close button
    buttonList.add(buttonClose = new GuiButton(0, (width / 2) - (CLOSE_BUTTON_WIDTH / 2), bottom - CLOSE_BUTTON_HEIGHT - CLOSE_BUTTON_OFFSET, CLOSE_BUTTON_WIDTH, CLOSE_BUTTON_HEIGHT, I18n.format("gui." + Nutrition.MODID + ":close")));
    // Draw labels
    redrawLabels();
}
Also used : GuiButton(net.minecraft.client.gui.GuiButton) PacketNutritionRequest(ca.wescook.nutrition.network.PacketNutritionRequest) Nutrient(ca.wescook.nutrition.nutrients.Nutrient)

Example 5 with Nutrient

use of ca.wescook.nutrition.nutrients.Nutrient in project Nutrition by WesCook.

the class NutritionGui method drawNutritionBars.

private void drawNutritionBars() {
    int i = 0;
    for (Nutrient nutrient : NutrientList.get()) {
        // Calculate percentage width for nutrition bars
        // If null, setPlayerNutrition to 0, else getPlayerNutrition true value
        float currentNutrient = (ClientProxy.nutrientData != null && ClientProxy.nutrientData.get(nutrient) != null) ? Math.round(ClientProxy.nutrientData.get(nutrient)) : 0;
        int nutritionBarDisplayWidth = (int) (currentNutrient / 100 * NUTRITION_BAR_WIDTH);
        // Draw icons
        itemRender.renderItemIntoGUI(nutrient.icon, left + NUTRITION_ICON_HORIZONTAL_OFFSET, top + NUTRITION_ICON_VERTICAL_OFFSET + (i * NUTRITION_DISTANCE));
        // Draw black background
        drawRect(left + NUTRITION_BAR_HORIZONTAL_OFFSET + labelCharacterPadding - 1, top + NUTRITION_BAR_VERTICAL_OFFSET + (i * NUTRITION_DISTANCE) - 1, left + NUTRITION_BAR_HORIZONTAL_OFFSET + NUTRITION_BAR_WIDTH + labelCharacterPadding + 1, top + NUTRITION_BAR_VERTICAL_OFFSET + (i * NUTRITION_DISTANCE) + NUTRITION_BAR_HEIGHT + 1, 0xff000000);
        // Draw colored bar
        drawRect(left + NUTRITION_BAR_HORIZONTAL_OFFSET + labelCharacterPadding, top + NUTRITION_BAR_VERTICAL_OFFSET + (i * NUTRITION_DISTANCE), left + NUTRITION_BAR_HORIZONTAL_OFFSET + nutritionBarDisplayWidth + labelCharacterPadding, top + NUTRITION_BAR_VERTICAL_OFFSET + (i * NUTRITION_DISTANCE) + NUTRITION_BAR_HEIGHT, nutrient.color);
        i++;
    }
}
Also used : Nutrient(ca.wescook.nutrition.nutrients.Nutrient)

Aggregations

Nutrient (ca.wescook.nutrition.nutrients.Nutrient)10 EntityPlayer (net.minecraft.entity.player.EntityPlayer)3 ItemStack (net.minecraft.item.ItemStack)2 TextComponentString (net.minecraft.util.text.TextComponentString)2 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)2 PacketNutritionRequest (ca.wescook.nutrition.network.PacketNutritionRequest)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 StringJoiner (java.util.StringJoiner)1 BlockCake (net.minecraft.block.BlockCake)1 IBlockState (net.minecraft.block.state.IBlockState)1 GuiButton (net.minecraft.client.gui.GuiButton)1 GuiLabel (net.minecraft.client.gui.GuiLabel)1 Item (net.minecraft.item.Item)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 Potion (net.minecraft.potion.Potion)1 PotionEffect (net.minecraft.potion.PotionEffect)1 World (net.minecraft.world.World)1