Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworked quern processing for food so it handles weight in recipe. #781

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/API/com/bioxx/tfc/api/Food.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

public class Food
{
private static final String NBT_FOOD_DECAY = "foodDecay";
private static final String NBT_FOOD_WEIGHT = "foodWeight";
public static final int DRYHOURS = 4;
public static final int SMOKEHOURS = 12;

Expand Down Expand Up @@ -176,16 +178,20 @@ public static boolean isSameSmoked(int[] f1, int[] f2)
public static void setDecay(ItemStack is, float value)
{
NBTTagCompound nbt = getNBT(is);
nbt.setFloat("foodDecay", value);
nbt.setFloat(NBT_FOOD_DECAY, value);
if(value > getWeight(is))
is.stackSize = 0;
}

public static boolean hasDecay(ItemStack is){
return is.hasTagCompound() && is.getTagCompound().hasKey(NBT_FOOD_DECAY);
}

public static float getDecay(ItemStack is)
{
NBTTagCompound nbt = getNBT(is);
if (nbt.hasKey("foodDecay"))
return nbt.getFloat("foodDecay");
if (nbt.hasKey(NBT_FOOD_DECAY))
return nbt.getFloat(NBT_FOOD_DECAY);
else
return 0;
}
Expand All @@ -208,16 +214,20 @@ public static int getDecayTimer(ItemStack is)
public static void setWeight(ItemStack is, float value)
{
NBTTagCompound nbt = getNBT(is);
nbt.setFloat("foodWeight", value);
nbt.setFloat(NBT_FOOD_WEIGHT, value);
if(getDecay(is) > value || value <= 0)
is.stackSize = 0;
}

public static boolean hasWeight(ItemStack is){
return is.hasTagCompound() && is.getTagCompound().hasKey(NBT_FOOD_WEIGHT);
}

public static float getWeight(ItemStack is)
{
NBTTagCompound nbt = getNBT(is);
if (nbt.hasKey("foodWeight"))
return nbt.getFloat("foodWeight");
if (nbt.hasKey(NBT_FOOD_WEIGHT))
return nbt.getFloat(NBT_FOOD_WEIGHT);
else
return 0;
}
Expand Down
87 changes: 48 additions & 39 deletions src/Common/com/bioxx/tfc/TileEntities/TEQuern.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

import com.bioxx.tfc.TerraFirmaCraft;
import com.bioxx.tfc.Core.TFC_Core;
import com.bioxx.tfc.Core.Player.FoodStatsTFC;
import com.bioxx.tfc.Entities.Mobs.EntityCowTFC;
import com.bioxx.tfc.Food.ItemFoodTFC;
import com.bioxx.tfc.api.Food;
import com.bioxx.tfc.api.Constant.Global;
import com.bioxx.tfc.api.Crafting.QuernManager;
import com.bioxx.tfc.api.Crafting.QuernRecipe;
Expand Down Expand Up @@ -83,48 +85,55 @@ public boolean processItem()
storage[1] = null;
}

if (qr.getInItem().getItem() instanceof IFood)
{
if(storage[1] != null &&
storage[1].hasTagCompound() &&
storage[1].getTagCompound().hasKey("foodWeight") &&
storage[1].getTagCompound().hasKey("foodDecay") &&
storage[0].hasTagCompound() &&
storage[0].getTagCompound().hasKey("foodWeight") &&
storage[0].getTagCompound().hasKey("foodDecay"))
{
//float flourDecay = storage[0].getTagCompound().getFloat("foodDecay");
float slot0Weight = storage[0].getTagCompound().getFloat("foodWeight");
float slot1Weight = storage[1].getTagCompound().getFloat("foodWeight");
float newWeight = slot0Weight + slot1Weight;

if(newWeight > Global.FOOD_MAX_WEIGHT)
{
storage[1].getTagCompound().setFloat("foodWeight", newWeight - Global.FOOD_MAX_WEIGHT);

ItemStack tossStack = storage[1].copy();
tossStack.getTagCompound().setFloat("foodWeight", Global.FOOD_MAX_WEIGHT);
ejectItem(tossStack);
if (qr.getInItem().getItem() instanceof IFood) {
if (Food.hasWeight(storage[0]) && Food.hasDecay(storage[0])) {
float inputWeight = Food.getWeight(storage[0]);
float inputDecay = Food.getDecay(storage[0]);
// Available weight for recipe depends on decay.
if(inputDecay > 0){
inputWeight -= inputDecay;
}
else
{
storage[1].getTagCompound().setFloat("foodWeight", newWeight);
float usedInputWeight = inputWeight;
float resultWeight = inputWeight;

// Determines result weight based on recipe weight if available
if (Food.hasWeight(qr.getResult()) && Food.hasWeight(qr.getInItem())) {
float recipeResultWeight = Food.getWeight(qr.getResult());
float recipeInputWeight = Food.getWeight(qr.getInItem());
if (recipeInputWeight < inputWeight) {
resultWeight = recipeResultWeight;
usedInputWeight = recipeInputWeight;
}else{
float ratio = inputWeight/recipeInputWeight;
resultWeight = recipeResultWeight * ratio;
}
}
storage[0] = null;
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
return true;
}

if(storage[1] == null &&
storage[0].hasTagCompound() &&
storage[0].getTagCompound().hasKey("foodWeight") &&
storage[0].getTagCompound().hasKey("foodDecay"))
{
storage[1] = qr.getResult().copy();
float flourWeight = storage[0].getTagCompound().getFloat("foodWeight");
float flourDecay = storage[0].getTagCompound().getFloat("foodDecay");
ItemFoodTFC.createTag(storage[1], flourWeight, flourDecay);
storage[0] = null;
// Output slot is empty, create new item
if (storage[1] == null) {
storage[1] = qr.getResult().copy();
ItemFoodTFC.createTag(storage[1], resultWeight);
// Output slot already has item, add weight to it.
} else if (Food.hasWeight(storage[1]) && Food.hasDecay(storage[1])) {
float outputWeight = Food.getWeight(storage[1]);
float newWeight = outputWeight + resultWeight;
if (newWeight > Global.FOOD_MAX_WEIGHT) {
ItemStack tossStack = storage[1].copy();
Food.setWeight(tossStack, Global.FOOD_MAX_WEIGHT);
ejectItem(tossStack);
// This reset decay on the output itemStack.
ItemFoodTFC.createTag(storage[1],newWeight - Global.FOOD_MAX_WEIGHT);
} else {
Food.setWeight(storage[1], newWeight);
}
} else {
return false;
}

// Consume weight from input.
if(FoodStatsTFC.reduceFood(storage[0], usedInputWeight)){
storage[0] = null;
}
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
return true;
}
Expand Down