satisfactory/src/items/Item.java

87 lines
1.9 KiB
Java

package items;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Item {
protected boolean isRaw = false;
private String name;
private Set<Recipe> recipes;
private Recipe preference = null;
public int sum = 0;
protected Item(String name, Set<Recipe> recipes) {
this.name = name;
this.recipes = recipes;
Database.add(this);
for (Recipe recipe : recipes) {
add(recipe);
}
}
public Item(String name, Recipe... recipes) {
this(name, new HashSet<>(Arrays.asList(recipes)));
}
public Item(String name) {
this(name, new HashSet<>());
}
public void add(Recipe recipe) {
add(recipe, 1);
}
public void add(Recipe recipe, int output) {
recipes.add(recipe);
recipe.checkOutput(this, output);
}
public String getName() {
return name;
}
public Set<Recipe> getRecipes() {
return recipes;
}
public boolean isRaw() {
return isRaw;
}
public float getProductionRate() {
Recipe recipe = getRecipe();
if (recipe == null) {
return 0;
}
return recipe.getProductionRate(this);
}
public Recipe getRecipe() {
Recipe recipe = preference;
if (recipe == null) {
recipe = recipes.stream().findFirst().orElse(null);
if (recipe == null) {
if (Database.preferences.containsKey(this)) {
recipe = Database.preferences.get(this);
}
}
}
return recipe;
}
@Override
public String toString() {
return "Item{" +
"name='" + name + '\'' +
", recipes=" + recipes +
'}';
}
public void setPreference(Recipe preference) {
this.preference = preference;
}
}