Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
CollectFood: consider furnace when calculating food potential
Browse files Browse the repository at this point in the history
  • Loading branch information
TacoTechnica committed Aug 10, 2021
1 parent 1473252 commit c9349e0
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/main/java/adris/altoclef/tasks/resources/CollectFoodTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import adris.altoclef.util.SmeltTarget;
import adris.altoclef.util.WorldUtil;
import adris.altoclef.util.csharpisbetter.TimerGame;
import adris.altoclef.util.slots.FurnaceSlot;
import net.minecraft.block.*;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity;
Expand All @@ -19,6 +20,8 @@
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.FurnaceScreenHandler;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.util.math.BlockPos;

import java.util.Objects;
Expand Down Expand Up @@ -65,30 +68,39 @@ public CollectFoodTask(double unitsNeeded) {
_unitsNeeded = unitsNeeded;
}

private static double getFoodPotential(ItemStack food) {
if (food == null) return 0;
int count = food.getCount();
if (count <= 0) return 0;
for (CookableFoodTarget cookable : COOKABLE_FOODS) {
if (food.getItem() == cookable.getRaw()) {
assert cookable.getCooked().getFoodComponent() != null;
return count * cookable.getCooked().getFoodComponent().getHunger();
}
}
// We're just an ordinary item.
if (food.getItem().isFood()) {
assert food.getItem().getFoodComponent() != null;
return count * food.getItem().getFoodComponent().getHunger();
}
return 0;
}

// Gets the units of food if we were to convert all of our raw resources to food.
@SuppressWarnings("RedundantCast")
private static double calculateFoodPotential(AltoClef mod) {
double potentialFood = 0;
for (ItemStack food : mod.getInventoryTracker().getAvailableFoods()) {
int count = food.getCount();
boolean cookedFound = false;
for (CookableFoodTarget cookable : COOKABLE_FOODS) {
if (food.getItem() == cookable.getRaw()) {
assert cookable.getCooked().getFoodComponent() != null;
potentialFood += count * cookable.getCooked().getFoodComponent().getHunger();
cookedFound = true;
break;
}
}
if (cookedFound) continue;
// We're just an ordinary item.
if (food.getItem().isFood()) {
assert food.getItem().getFoodComponent() != null;
potentialFood += count * food.getItem().getFoodComponent().getHunger();
}
potentialFood += getFoodPotential(food);
}
int potentialBread = (int) (mod.getInventoryTracker().getItemCount(Items.WHEAT) / 3) + mod.getInventoryTracker().getItemCount(Items.HAY_BLOCK) * 3;
int potentialBread = (int) (mod.getInventoryTracker().getItemCount(Items.WHEAT) / 3) + mod.getInventoryTracker().getItemCountIncludingTable(Items.HAY_BLOCK) * 3;
potentialFood += Objects.requireNonNull(Items.BREAD.getFoodComponent()).getHunger() * potentialBread;
// Check smelting
ScreenHandler screen = mod.getPlayer().currentScreenHandler;
if (screen instanceof FurnaceScreenHandler) {
potentialFood += getFoodPotential(mod.getInventoryTracker().getItemStackInSlot(FurnaceSlot.INPUT_SLOT_MATERIALS));
potentialFood += getFoodPotential(mod.getInventoryTracker().getItemStackInSlot(FurnaceSlot.OUTPUT_SLOT));
}
return potentialFood;
}

Expand Down Expand Up @@ -371,11 +383,11 @@ public CookableFoodTarget(String rawFood, Class mobToKill) {
}

private Item getRaw() {
return TaskCatalogue.getItemMatches(rawFood)[0];
return Objects.requireNonNull(TaskCatalogue.getItemMatches(rawFood))[0];
}

private Item getCooked() {
return TaskCatalogue.getItemMatches(cookedFood)[0];
return Objects.requireNonNull(TaskCatalogue.getItemMatches(cookedFood))[0];
}

public int getCookedUnits() {
Expand Down

0 comments on commit c9349e0

Please sign in to comment.