Refactored shaped recipes
This commit is contained in:
@@ -13,9 +13,9 @@ public interface TestableItem {
|
||||
boolean matches(@Nullable ItemStack itemStack);
|
||||
|
||||
/**
|
||||
* Get a displayed itemstack, for autocraft.
|
||||
* Get an example item.
|
||||
*
|
||||
* @return The item, displayed.
|
||||
* @return The item.
|
||||
*/
|
||||
ItemStack getItem();
|
||||
}
|
||||
|
||||
@@ -2,13 +2,10 @@ package com.willfp.eco.core.recipe;
|
||||
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import com.willfp.eco.core.recipe.recipes.ShapedCraftingRecipe;
|
||||
import com.willfp.eco.core.recipe.recipes.CraftingRecipe;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.RecipeChoice;
|
||||
import org.bukkit.inventory.ShapedRecipe;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -18,7 +15,7 @@ public class Recipes {
|
||||
/**
|
||||
* Registry of all recipes.
|
||||
*/
|
||||
private static final BiMap<NamespacedKey, ShapedCraftingRecipe> RECIPES = HashBiMap.create();
|
||||
private static final BiMap<NamespacedKey, CraftingRecipe> RECIPES = HashBiMap.create();
|
||||
|
||||
|
||||
/**
|
||||
@@ -26,28 +23,8 @@ public class Recipes {
|
||||
*
|
||||
* @param recipe The recipe.
|
||||
*/
|
||||
public void register(@NotNull final ShapedCraftingRecipe recipe) {
|
||||
public void register(@NotNull final CraftingRecipe recipe) {
|
||||
RECIPES.forcePut(recipe.getKey(), recipe);
|
||||
|
||||
Bukkit.getServer().removeRecipe(recipe.getKey());
|
||||
Bukkit.getServer().removeRecipe(recipe.getDisplayedKey());
|
||||
|
||||
ShapedRecipe shapedRecipe = new ShapedRecipe(recipe.getKey(), recipe.getOutput());
|
||||
shapedRecipe.shape("012", "345", "678");
|
||||
for (int i = 0; i < 9; i++) {
|
||||
char character = String.valueOf(i).toCharArray()[0];
|
||||
shapedRecipe.setIngredient(character, recipe.getMaterialAtIndex(i));
|
||||
}
|
||||
|
||||
ShapedRecipe displayedRecipe = new ShapedRecipe(recipe.getDisplayedKey(), recipe.getOutput());
|
||||
displayedRecipe.shape("012", "345", "678");
|
||||
for (int i = 0; i < 9; i++) {
|
||||
char character = String.valueOf(i).toCharArray()[0];
|
||||
displayedRecipe.setIngredient(character, new RecipeChoice.ExactChoice(recipe.getDisplayedAtIndex(i)));
|
||||
}
|
||||
|
||||
Bukkit.getServer().addRecipe(shapedRecipe);
|
||||
Bukkit.getServer().addRecipe(displayedRecipe);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,19 +34,19 @@ public class Recipes {
|
||||
* @return The match, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
public ShapedCraftingRecipe getMatch(@NotNull final ItemStack[] matrix) {
|
||||
public CraftingRecipe getMatch(@NotNull final ItemStack[] matrix) {
|
||||
return RECIPES.values().stream().filter(recipe -> recipe.test(matrix)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get shaped recipe by key.
|
||||
* Get recipe by key.
|
||||
*
|
||||
* @param key The key.
|
||||
* @return The shaped recipe, or null if not found.
|
||||
* @return The recipe, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
public ShapedCraftingRecipe getShapedRecipe(@NotNull final NamespacedKey key) {
|
||||
ShapedCraftingRecipe recipe = RECIPES.get(key);
|
||||
public CraftingRecipe getRecipe(@NotNull final NamespacedKey key) {
|
||||
CraftingRecipe recipe = RECIPES.get(key);
|
||||
if (recipe != null) {
|
||||
return recipe;
|
||||
}
|
||||
|
||||
@@ -1,28 +1,13 @@
|
||||
package com.willfp.eco.core.recipe.recipes;
|
||||
|
||||
import com.willfp.eco.core.items.TestableItem;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CraftingRecipe {
|
||||
/**
|
||||
* Get item material at a specific index.
|
||||
*
|
||||
* @param index The index to check.
|
||||
* @return The material.
|
||||
*/
|
||||
Material getMaterialAtIndex(int index);
|
||||
|
||||
/**
|
||||
* Get "real" item at specific index.
|
||||
*
|
||||
* @param index The index to check.
|
||||
* @return The item.
|
||||
*/
|
||||
ItemStack getDisplayedAtIndex(int index);
|
||||
|
||||
/**
|
||||
* Test matrix against recipe.
|
||||
*
|
||||
@@ -41,7 +26,7 @@ public interface CraftingRecipe {
|
||||
*
|
||||
* @return The parts.
|
||||
*/
|
||||
TestableItem[] getParts();
|
||||
List<TestableItem> getParts();
|
||||
|
||||
/**
|
||||
* Get the recipe key.
|
||||
|
||||
@@ -1,24 +1,28 @@
|
||||
package com.willfp.eco.core.recipe.recipes;
|
||||
|
||||
import com.willfp.eco.core.PluginDependent;
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import com.willfp.eco.core.PluginDependent;
|
||||
import com.willfp.eco.core.items.TestableItem;
|
||||
import com.willfp.eco.core.recipe.Recipes;
|
||||
import com.willfp.eco.core.recipe.parts.EmptyTestableItem;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.RecipeChoice;
|
||||
import org.bukkit.inventory.ShapedRecipe;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public final class ShapedCraftingRecipe extends PluginDependent implements CraftingRecipe {
|
||||
/**
|
||||
* Recipe parts.
|
||||
*/
|
||||
@Getter
|
||||
private final TestableItem[] parts;
|
||||
private final List<TestableItem> parts;
|
||||
|
||||
/**
|
||||
* The key of the recipe.
|
||||
@@ -40,7 +44,7 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft
|
||||
|
||||
private ShapedCraftingRecipe(@NotNull final EcoPlugin plugin,
|
||||
@NotNull final String key,
|
||||
@NotNull final TestableItem[] parts,
|
||||
@NotNull final List<TestableItem> parts,
|
||||
@NotNull final ItemStack output) {
|
||||
super(plugin);
|
||||
|
||||
@@ -50,21 +54,11 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Material getMaterialAtIndex(final int index) {
|
||||
return parts[index].getItem().getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getDisplayedAtIndex(final int index) {
|
||||
return parts[index].getItem();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(@NotNull final ItemStack[] matrix) {
|
||||
boolean matches = true;
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if (!parts[i].matches(matrix[i])) {
|
||||
if (!parts.get(i).matches(matrix[i])) {
|
||||
matches = false;
|
||||
}
|
||||
}
|
||||
@@ -75,15 +69,26 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft
|
||||
@Override
|
||||
public void register() {
|
||||
Recipes.register(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EcoShapedRecipe{"
|
||||
+ "parts=" + Arrays.toString(parts)
|
||||
+ ", key='" + key + '\''
|
||||
+ ", output=" + output
|
||||
+ '}';
|
||||
Bukkit.getServer().removeRecipe(this.getKey());
|
||||
Bukkit.getServer().removeRecipe(this.getDisplayedKey());
|
||||
|
||||
ShapedRecipe shapedRecipe = new ShapedRecipe(this.getKey(), this.getOutput());
|
||||
shapedRecipe.shape("012", "345", "678");
|
||||
for (int i = 0; i < 9; i++) {
|
||||
char character = String.valueOf(i).toCharArray()[0];
|
||||
shapedRecipe.setIngredient(character, parts.get(i).getItem().getType());
|
||||
}
|
||||
|
||||
ShapedRecipe displayedRecipe = new ShapedRecipe(this.getDisplayedKey(), this.getOutput());
|
||||
displayedRecipe.shape("012", "345", "678");
|
||||
for (int i = 0; i < 9; i++) {
|
||||
char character = String.valueOf(i).toCharArray()[0];
|
||||
displayedRecipe.setIngredient(character, new RecipeChoice.ExactChoice(parts.get(i).getItem()));
|
||||
}
|
||||
|
||||
Bukkit.getServer().addRecipe(shapedRecipe);
|
||||
Bukkit.getServer().addRecipe(displayedRecipe);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,7 +107,7 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft
|
||||
/**
|
||||
* The recipe parts.
|
||||
*/
|
||||
private final TestableItem[] recipeParts = new TestableItem[9];
|
||||
private final List<TestableItem> recipeParts = new ArrayList<>(9);
|
||||
|
||||
/**
|
||||
* The output of the recipe.
|
||||
@@ -140,7 +145,7 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft
|
||||
*/
|
||||
public Builder setRecipePart(@NotNull final RecipePosition position,
|
||||
@NotNull final TestableItem part) {
|
||||
this.recipeParts[position.getIndex()] = part;
|
||||
recipeParts.set(position.getIndex(), part);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -153,7 +158,7 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft
|
||||
*/
|
||||
public Builder setRecipePart(final int position,
|
||||
@NotNull final TestableItem part) {
|
||||
this.recipeParts[position] = part;
|
||||
recipeParts.set(position, part);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -175,8 +180,8 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft
|
||||
*/
|
||||
public ShapedCraftingRecipe build() {
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if (recipeParts[i] == null) {
|
||||
recipeParts[i] = new EmptyTestableItem();
|
||||
if (recipeParts.get(i) == null) {
|
||||
recipeParts.set(i, new EmptyTestableItem());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user