mirror of
https://github.com/exituser/Brewery.git
synced 2026-09-17 06:38:56 +00:00
Add/change recipes by plugin
This commit is contained in:
@@ -2,12 +2,15 @@ package com.dre.brewery.recipe;
|
||||
|
||||
import com.dre.brewery.P;
|
||||
import com.dre.brewery.utility.Tuple;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -15,6 +18,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class BCauldronRecipe {
|
||||
public static List<BCauldronRecipe> recipes = new ArrayList<>();
|
||||
public static int numConfigRecipes;
|
||||
public static List<RecipeItem> acceptedCustom = new ArrayList<>(); // All accepted custom and other items
|
||||
public static Set<Material> acceptedSimple = EnumSet.noneOf(Material.class); // All accepted simple items
|
||||
public static Set<Material> acceptedMaterials = EnumSet.noneOf(Material.class); // Fast cache for all accepted Materials
|
||||
@@ -24,20 +28,33 @@ public class BCauldronRecipe {
|
||||
//private List<String> particles
|
||||
private PotionColor color;
|
||||
private List<String> lore;
|
||||
private boolean saveInData; // If this recipe should be saved in data and loaded again when the server restarts. Applicable to non-config recipes
|
||||
|
||||
|
||||
/**
|
||||
* A New Cauldron Recipe with the given name
|
||||
* Use new BCauldronRecipe.Builder() for easier Cauldron Recipe Creation
|
||||
*
|
||||
* @param name Name of the Cauldron Recipe
|
||||
*/
|
||||
public BCauldronRecipe(String name) {
|
||||
this.name = name;
|
||||
color = PotionColor.CYAN;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static BCauldronRecipe fromConfig(ConfigurationSection cfg, String id) {
|
||||
BCauldronRecipe recipe = new BCauldronRecipe();
|
||||
|
||||
recipe.name = cfg.getString(id + ".name");
|
||||
if (recipe.name != null) {
|
||||
recipe.name = P.p.color(recipe.name);
|
||||
String name = cfg.getString(id + ".name");
|
||||
if (name != null) {
|
||||
name = P.p.color(name);
|
||||
} else {
|
||||
P.p.errorLog("Missing name for Cauldron-Recipe: " + id);
|
||||
return null;
|
||||
}
|
||||
|
||||
BCauldronRecipe recipe = new BCauldronRecipe(name);
|
||||
|
||||
recipe.ingredients = BRecipe.loadIngredients(cfg, id);
|
||||
if (recipe.ingredients == null || recipe.ingredients.isEmpty()) {
|
||||
P.p.errorLog("No ingredients for Cauldron-Recipe: " + recipe.name);
|
||||
@@ -66,6 +83,9 @@ public class BCauldronRecipe {
|
||||
return recipe;
|
||||
}
|
||||
|
||||
|
||||
// Getter
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return name;
|
||||
@@ -86,6 +106,32 @@ public class BCauldronRecipe {
|
||||
return lore;
|
||||
}
|
||||
|
||||
public boolean isSaveInData() {
|
||||
return saveInData;
|
||||
}
|
||||
|
||||
|
||||
// Setter
|
||||
|
||||
/**
|
||||
* When Changing ingredients, Accepted Lists have to be updated in BCauldronRecipe
|
||||
*/
|
||||
public void setIngredients(@NotNull List<RecipeItem> ingredients) {
|
||||
this.ingredients = ingredients;
|
||||
}
|
||||
|
||||
public void setColor(@NotNull PotionColor color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public void setLore(List<String> lore) {
|
||||
this.lore = lore;
|
||||
}
|
||||
|
||||
public void setSaveInData(boolean saveInData) {
|
||||
this.saveInData = saveInData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find how much these ingredients match the given ones from 0-10.
|
||||
* If any ingredient is missing, returns 0
|
||||
@@ -132,11 +178,64 @@ public class BCauldronRecipe {
|
||||
return match;
|
||||
}
|
||||
|
||||
public void updateAcceptedLists() {
|
||||
for (RecipeItem ingredient : getIngredients()) {
|
||||
if (ingredient.hasMaterials()) {
|
||||
BCauldronRecipe.acceptedMaterials.addAll(ingredient.getMaterials());
|
||||
}
|
||||
if (ingredient instanceof SimpleItem) {
|
||||
BCauldronRecipe.acceptedSimple.add(((SimpleItem) ingredient).getMaterial());
|
||||
} else {
|
||||
// Add it as acceptedCustom
|
||||
if (!BCauldronRecipe.acceptedCustom.contains(ingredient)) {
|
||||
BCauldronRecipe.acceptedCustom.add(ingredient);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BCauldronRecipe{" + name + '}';
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static BCauldronRecipe get(String name) {
|
||||
for (BCauldronRecipe recipe : recipes) {
|
||||
if (recipe.name.equalsIgnoreCase(name)) {
|
||||
return recipe;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a Modifiable Sublist of the CauldronRecipes that are loaded by config
|
||||
* Changes are directly reflected by the main list of all recipes
|
||||
* Changes to the main List of all CauldronRecipes will make the reference to this sublist invalid
|
||||
*
|
||||
* After adding or removing elements, CauldronRecipes.numConfigRecipes MUST be updated!
|
||||
*/
|
||||
public static List<BCauldronRecipe> getConfigRecipes() {
|
||||
return recipes.subList(0, numConfigRecipes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a Modifiable Sublist of the CauldronRecipes that are added by plugins
|
||||
* Changes are directly reflected by the main list of all recipes
|
||||
* Changes to the main List of all CauldronRecipes will make the reference to this sublist invalid
|
||||
*/
|
||||
public static List<BCauldronRecipe> getAddedRecipes() {
|
||||
return recipes.subList(numConfigRecipes, recipes.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the main List of all CauldronRecipes
|
||||
*/
|
||||
public static List<BCauldronRecipe> getAllRecipes() {
|
||||
return recipes;
|
||||
}
|
||||
|
||||
/*public static boolean acceptItem(ItemStack item) {
|
||||
if (acceptedMaterials.contains(item.getType())) {
|
||||
// Extremely fast way to check for most items
|
||||
@@ -172,4 +271,70 @@ public class BCauldronRecipe {
|
||||
}
|
||||
return null;
|
||||
}*/
|
||||
|
||||
public static class Builder {
|
||||
private BCauldronRecipe recipe;
|
||||
|
||||
public Builder(String name) {
|
||||
recipe = new BCauldronRecipe(name);
|
||||
}
|
||||
|
||||
|
||||
public Builder addIngredient(RecipeItem... item) {
|
||||
if (recipe.ingredients == null) {
|
||||
recipe.ingredients = new ArrayList<>();
|
||||
}
|
||||
Collections.addAll(recipe.ingredients, item);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addIngredient(ItemStack... item) {
|
||||
if (recipe.ingredients == null) {
|
||||
recipe.ingredients = new ArrayList<>();
|
||||
}
|
||||
for (ItemStack i : item) {
|
||||
recipe.ingredients.add(new CustomItem(i));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder color(String colorString) {
|
||||
recipe.color = PotionColor.fromString(colorString);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder color(PotionColor color) {
|
||||
recipe.color = color;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder color(Color color) {
|
||||
recipe.color = PotionColor.fromColor(color);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder addLore(String line) {
|
||||
if (recipe.lore == null) {
|
||||
recipe.lore = new ArrayList<>();
|
||||
}
|
||||
recipe.lore.add(line);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BCauldronRecipe get() {
|
||||
if (recipe.name == null) {
|
||||
throw new IllegalArgumentException("CauldronRecipe name is null");
|
||||
}
|
||||
if (BCauldronRecipe.get(recipe.getName()) != null) {
|
||||
throw new IllegalArgumentException("CauldronRecipe with name " + recipe.getName() + " already exists");
|
||||
}
|
||||
if (recipe.color == null) {
|
||||
throw new IllegalArgumentException("CauldronRecipe has no color");
|
||||
}
|
||||
if (recipe.ingredients == null || recipe.ingredients.isEmpty()) {
|
||||
throw new IllegalArgumentException("CauldronRecipe has no ingredients");
|
||||
}
|
||||
return recipe;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user