Add/change recipes by plugin

This commit is contained in:
Sn0wStorm
2019-11-09 17:02:48 +01:00
parent 81ea014e45
commit 318963b379
12 changed files with 697 additions and 41 deletions
+169 -4
View File
@@ -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;
}
}
}
+9
View File
@@ -17,6 +17,15 @@ public class BEffect {
private boolean hidden = false;
public BEffect(PotionEffectType type, short minlvl, short maxlvl, short minduration, short maxduration, boolean hidden) {
this.type = type;
this.minlvl = minlvl;
this.maxlvl = maxlvl;
this.minduration = minduration;
this.maxduration = maxduration;
this.hidden = hidden;
}
public BEffect(String effectString) {
String[] effectSplit = effectString.split("/");
String effect = effectSplit[0];
+270 -1
View File
@@ -5,6 +5,8 @@ import com.dre.brewery.Brew;
import com.dre.brewery.P;
import com.dre.brewery.filedata.BConfig;
import com.dre.brewery.utility.Tuple;
import org.apache.commons.lang.NotImplementedException;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
@@ -12,11 +14,13 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class BRecipe {
public static List<BRecipe> recipes = new ArrayList<>();
public static int numConfigRecipes; // The number of recipes in the list that are from config
private String[] name;
private List<RecipeItem> ingredients = new ArrayList<>(); // Items and amounts
@@ -30,8 +34,33 @@ public class BRecipe {
private int alcohol; // Alcohol in perfect potion
private List<Tuple<Integer, String>> lore; // Custom Lore on the Potion. The int is for Quality Lore, 0 = any, 1,2,3 = Bad,Middle,Good
private List<BEffect> effects = new ArrayList<>(); // Special Effects when drinking
private boolean saveInData; // If this recipe should be saved in data and loaded again when the server restarts. Applicable to non-config recipes
public BRecipe() {
private BRecipe() {
}
/**
* New BRecipe with Name
* Use new BRecipe.Builder() for easier Recipe Creation
*
* @param name The name for all qualities
*/
public BRecipe(String name, @NotNull PotionColor color) {
this.name = new String[] {name};
this.color = color;
difficulty = 5;
}
/**
* New BRecipe with Names
* Use new BRecipe.Builder() for easier Recipe Creation
*
* @param names {name bad, name normal, name good}
*/
public BRecipe(String[] names, @NotNull PotionColor color) {
this.name = names;
this.color = color;
difficulty = 5;
}
@Nullable
@@ -380,6 +409,22 @@ public class BRecipe {
return new Brew(bIngredients, quality, 0, distillruns, getAge(), wood, getRecipeName(), false, true, 0);
}
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);
}
}
}
}
// Getter
@@ -433,6 +478,13 @@ public class BRecipe {
return false;
}
// Getters
public List<RecipeItem> getIngredients() {
return ingredients;
}
public int getCookingTime() {
return cookingTime;
}
@@ -500,11 +552,97 @@ public class BRecipe {
return effects;
}
public boolean isSaveInData() {
return saveInData;
}
// Setters
/**
* When Changing ingredients, Accepted Lists have to be updated in BCauldronRecipe
*/
public void setIngredients(List<RecipeItem> ingredients) {
this.ingredients = ingredients;
}
public void setCookingTime(int cookingTime) {
this.cookingTime = cookingTime;
}
public void setDistillruns(byte distillruns) {
this.distillruns = distillruns;
}
public void setDistillTime(int distillTime) {
this.distillTime = distillTime;
}
public void setWood(byte wood) {
this.wood = wood;
}
public void setAge(int age) {
this.age = age;
}
public void setColor(@NotNull PotionColor color) {
this.color = color;
}
public void setDifficulty(int difficulty) {
this.difficulty = difficulty;
}
public void setAlcohol(int alcohol) {
this.alcohol = alcohol;
}
public void setLore(List<Tuple<Integer, String>> lore) {
this.lore = lore;
}
public void setEffects(List<BEffect> effects) {
this.effects = effects;
}
public void setSaveInData(boolean saveInData) {
throw new NotImplementedException();
//this.saveInData = saveInData;
}
@Override
public String toString() {
return "BRecipe{" + getRecipeName() + '}';
}
/**
* Gets a Modifiable Sublist of the Recipes that are loaded by config
* Changes are directly reflected by the main list of all recipes
* Changes to the main List of all recipes will make the reference to this sublist invalid
*
* After adding or removing elements, BRecipe.numConfigRecipes MUST be updated!
*/
public static List<BRecipe> getConfigRecipes() {
return recipes.subList(0, numConfigRecipes);
}
/**
* Gets a Modifiable Sublist of the Recipes that are added by plugins
* Changes are directly reflected by the main list of all recipes
* Changes to the main List of all recipes will make the reference to this sublist invalid
*/
public static List<BRecipe> getAddedRecipes() {
return recipes.subList(numConfigRecipes, recipes.size());
}
/**
* Gets the main List of all recipes
*/
public static List<BRecipe> getAllRecipes() {
return recipes;
}
public static BRecipe get(String name) {
for (BRecipe recipe : recipes) {
if (recipe.getRecipeName().equalsIgnoreCase(name)) {
@@ -513,4 +651,135 @@ public class BRecipe {
}
return null;
}
/*public static void saveAddedRecipes(ConfigurationSection cfg) {
int i = 0;
for (BRecipe recipe : getAddedRecipes()) {
if (recipe.isSaveInData()) {
cfg.set(i + ".name", recipe.name);
}
}
}*/
public static class Builder {
private BRecipe recipe;
public Builder(String name) {
recipe = new BRecipe(name, PotionColor.WATER);
}
public Builder(String... names) {
recipe = new BRecipe(names, PotionColor.WATER);
}
public Builder addIngredient(RecipeItem... item) {
Collections.addAll(recipe.ingredients, item);
return this;
}
public Builder addIngredient(ItemStack... item) {
for (ItemStack i : item) {
CustomItem customItem = new CustomItem(i);
customItem.setAmount(i.getAmount());
recipe.ingredients.add(customItem);
}
return this;
}
public Builder difficulty(int difficulty) {
recipe.difficulty = difficulty;
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 cook(int cookTime) {
recipe.cookingTime = cookTime;
return this;
}
public Builder distill(byte distillRuns, int distillTime) {
recipe.distillruns = distillRuns;
recipe.distillTime = distillTime;
return this;
}
public Builder age(int age, byte wood) {
recipe.age = age;
recipe.wood = wood;
return this;
}
public Builder alcohol(int alcohol) {
recipe.alcohol = alcohol;
return this;
}
public Builder addLore(String line) {
return addLore(0, line);
}
/**
* Add a Line of Lore
*
* @param quality 0 for any quality, 1: bad, 2: normal, 3: good
* @param line The Line for custom lore to add
* @return this
*/
public Builder addLore(int quality, String line) {
if (quality < 0 || quality > 3) {
throw new IllegalArgumentException("Lore Quality must be 0 - 3");
}
if (recipe.lore == null) {
recipe.lore = new ArrayList<>();
}
recipe.lore.add(new Tuple<>(quality, line));
return this;
}
public Builder addEffects(BEffect... effects) {
Collections.addAll(recipe.effects, effects);
return this;
}
public BRecipe get() {
if (recipe.name == null) {
throw new IllegalArgumentException("Recipe name is null");
}
if (recipe.name.length != 1 && recipe.name.length != 3) {
throw new IllegalArgumentException("Recipe name neither 1 nor 3");
}
if (BRecipe.get(recipe.getRecipeName()) != null) {
throw new IllegalArgumentException("Recipe with name " + recipe.getRecipeName() + " already exists");
}
if (recipe.color == null) {
throw new IllegalArgumentException("Recipe has no color");
}
if (recipe.ingredients == null || recipe.ingredients.isEmpty()) {
throw new IllegalArgumentException("Recipe has no ingredients");
}
if (!recipe.isValid()) {
throw new IllegalArgumentException("Recipe has not valid");
}
for (RecipeItem ingredient : recipe.ingredients) {
ingredient.makeImmutable();
}
return recipe;
}
}
}
@@ -101,4 +101,8 @@ public class PotionColor {
}
}
public static PotionColor fromColor(Color color) {
return new PotionColor(color);
}
}