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());
}
}
}
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."));
}
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++;
}
}
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();
}
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++;
}
}
Aggregations