use of ca.wescook.nutrition.nutrients.Nutrient in project Nutrition by WesCook.
the class EffectsList method parseJson.
// Parse JSON data into more useful objects
public static void parseJson() {
for (JsonEffect effectRaw : jsonEffects) {
// Skip if effect is not enabled, or if field omitted (null)
if (effectRaw.enabled != null && !effectRaw.enabled)
continue;
// Get potion from config
Potion potion = Potion.getPotionFromResourceLocation(effectRaw.potion);
if (potion == null) {
Log.error("Potion '" + effectRaw.potion + "' is not valid (" + effectRaw.name + ").");
continue;
}
// Copying and cleaning data
Effect effect = new Effect();
effect.name = effectRaw.name;
effect.potion = potion;
effect.minimum = effectRaw.minimum;
effect.maximum = effectRaw.maximum;
effect.detect = effectRaw.detect;
// Amplifier defaults to 0 if undefined
effect.amplifier = (effectRaw.amplifier != null) ? effectRaw.amplifier : 0;
// Default the cumulative modifier to 1 if not defined
effect.cumulativeModifier = (effectRaw.cumulative_modifier != null) ? effectRaw.cumulative_modifier : 1;
// If nutrients are unspecified in file, this defaults to include every nutrient
if (effectRaw.nutrients.size() == 0) {
effect.nutrients.addAll(NutrientList.get());
} else {
// Field has been set, so fetch nutrients by name
for (String nutrientName : effectRaw.nutrients) {
Nutrient nutrient = NutrientList.getByName(nutrientName);
if (nutrient != null)
// Nutrient checks out, add to list
effect.nutrients.add(nutrient);
else
Log.error("Nutrient " + nutrientName + " not found (" + effectRaw.name + ").");
}
}
// Register effect
effects.add(effect);
}
}
use of ca.wescook.nutrition.nutrients.Nutrient in project Nutrition by WesCook.
the class EffectsManager method getEffectsInThreshold.
// Returns which effects match threshold conditions
private static List<Effect> getEffectsInThreshold(EntityPlayer player) {
// Get info
Map<Nutrient, Float> playerNutrition = player.getCapability(CapProvider.NUTRITION_CAPABILITY, null).get();
// Effects being turned on
List<Effect> effectsInThreshold = new ArrayList<>();
// Read in list of potion effects to apply
for (Effect effect : EffectsList.get()) {
// Apply effect based on "detect" condition
switch(effect.detect) {
// If any nutrient is within the threshold
case "any":
{
// Loop relevant nutrients
for (Nutrient nutrient : effect.nutrients) {
// If any are found within threshold
if (playerNutrition.get(nutrient) >= effect.minimum && playerNutrition.get(nutrient) <= effect.maximum) {
// Add effect, once
effectsInThreshold.add(effect);
break;
}
}
}
break;
// If the average of all nutrients is within the threshold
case "average":
{
// Reset counter each new loop
Float total = 0f;
Float average;
// Loop relevant nutrients
for (Nutrient nutrient : effect.nutrients) // Add each value to total
total += playerNutrition.get(nutrient);
// Divide by number of nutrients for average (division by zero check)
int size = playerNutrition.size();
average = (size != 0) ? total / size : -1f;
// Check average is inside the threshold
if (average >= effect.minimum && average <= effect.maximum)
effectsInThreshold.add(effect);
}
break;
// If all nutrients are within the threshold
case "all":
{
// Condition starts true, and must be triggered to fail
boolean allWithinThreshold = true;
// Loop relevant nutrients
for (Nutrient nutrient : effect.nutrients) {
if (// If nutrient isn't within threshold
!(playerNutrition.get(nutrient) >= effect.minimum && playerNutrition.get(nutrient) <= effect.maximum))
// Fail check
allWithinThreshold = false;
}
// If check wasn't failed, set effect
if (allWithinThreshold)
effectsInThreshold.add(effect);
}
break;
// For each nutrient within the threshold, the amplifier increases by one
case "cumulative":
{
// Reset counter each new loop
int cumulativeCount = 0;
// Loop relevant nutrients
for (Nutrient nutrient : effect.nutrients) {
// For each nutrient found within threshold
if (playerNutrition.get(nutrient) >= effect.minimum && playerNutrition.get(nutrient) <= effect.maximum)
cumulativeCount++;
}
// Save number of nutrients found as amplifier
// We're saving this for the entire effect, which is crazy hacky.
// However it's otherwise unused, and the simplest way of storing this information.
effect.amplifier = (cumulativeCount * effect.cumulativeModifier) - 1;
// If any were found, set effect
if (cumulativeCount > 0) {
// Add effect, once
effectsInThreshold.add(effect);
}
}
break;
}
}
return effectsInThreshold;
}
use of ca.wescook.nutrition.nutrients.Nutrient in project Nutrition by WesCook.
the class ChatCommand method commandSetNutrition.
private void commandSetNutrition(ICommandSender sender, String[] args) {
// If missing parameter, offer help
if (args.length != 3) {
sender.sendMessage(new TextComponentString("Invalid format. /nutrition set <nutrient> <value>"));
return;
}
// Valid number check
Float newValue;
if (NumberUtils.isCreatable(args[2]))
newValue = Float.parseFloat(args[2]);
else {
sender.sendMessage(new TextComponentString("Value is not a number."));
return;
}
// Range check (don't sue me Oracle)
if (!(newValue >= 0 && newValue <= 100)) {
sender.sendMessage(new TextComponentString("Value is not between 0 and 100."));
return;
}
// Write nutrient name and percentage to chat
EntityPlayer player = (EntityPlayer) sender;
Nutrient nutrient = NutrientList.getByName(args[1]);
if (nutrient != null) {
player.getCapability(CapProvider.NUTRITION_CAPABILITY, null).set(nutrient, newValue, true);
sender.sendMessage(new TextComponentString(nutrient.name + " updated!"));
} 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 EventTooltip method tooltipEvent.
@SubscribeEvent
public void tooltipEvent(ItemTooltipEvent event) {
ItemStack itemStack = event.getItemStack();
String tooltip = null;
// Get out if not a food item
if (!NutrientUtils.isValidFood(itemStack))
return;
// Create readable list of nutrients
StringJoiner stringJoiner = new StringJoiner(", ");
List<Nutrient> foundNutrients = NutrientUtils.getFoodNutrients(itemStack);
for (// Loop through nutrients from food
Nutrient nutrient : // Loop through nutrients from food
foundNutrients) stringJoiner.add(I18n.format("nutrient." + Nutrition.MODID + ":" + nutrient.name));
String nutrientString = stringJoiner.toString();
// Get nutrition value
float nutritionValue = NutrientUtils.calculateNutrition(itemStack, foundNutrients);
// Build tooltip
if (!nutrientString.equals("")) {
tooltip = I18n.format("tooltip." + Nutrition.MODID + ":nutrients") + " " + TextFormatting.DARK_GREEN + nutrientString + TextFormatting.DARK_AQUA + " (" + String.format("%.1f", nutritionValue) + "%)";
}
// Add to item tooltip
if (tooltip != null)
event.getToolTip().add(tooltip);
}
use of ca.wescook.nutrition.nutrients.Nutrient in project Nutrition by WesCook.
the class CapStorage method readNBT.
// Load serialized data from disk
@Override
public void readNBT(Capability<CapInterface> capability, CapInterface instance, EnumFacing side, NBTBase nbt) {
HashMap<Nutrient, Float> clientNutrients = new HashMap<Nutrient, Float>();
Float value;
// Read in nutrients from file
for (Nutrient nutrient : NutrientList.get()) {
// For each nutrient
if (// If it's found in player file
((NBTTagCompound) nbt).hasKey(nutrient.name))
// Read value in
value = ((NBTTagCompound) nbt).getFloat(nutrient.name);
else
// Set to default
value = (float) Config.startingNutrition;
// Add to map
clientNutrients.put(nutrient, value);
}
// Replace nutrient data with map
// Note: Syncing throws network errors at this stage
instance.set(clientNutrients, false);
}
Aggregations