Reworked recipes
This commit is contained in:
@@ -24,8 +24,7 @@ import com.willfp.eco.util.integrations.placeholder.PlaceholderManager;
|
||||
import com.willfp.eco.util.integrations.placeholder.plugins.PlaceholderIntegrationPAPI;
|
||||
import com.willfp.eco.util.optional.Prerequisite;
|
||||
import com.willfp.eco.util.protocollib.AbstractPacketAdapter;
|
||||
import com.willfp.eco.util.recipe.RecipeListener;
|
||||
import com.willfp.eco.util.recipe.RecipeManager;
|
||||
import com.willfp.eco.util.recipe.recipes.RecipeListener;
|
||||
import com.willfp.eco.util.updater.UpdateChecker;
|
||||
import lombok.Getter;
|
||||
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
|
||||
@@ -144,12 +143,6 @@ public abstract class AbstractEcoPlugin extends JavaPlugin {
|
||||
@Getter
|
||||
private final RunnableFactory runnableFactory;
|
||||
|
||||
/**
|
||||
* Recipe handler for crafting recipes.
|
||||
*/
|
||||
@Getter
|
||||
private final RecipeManager recipeManager;
|
||||
|
||||
/**
|
||||
* The loader for all plugin extensions.
|
||||
*
|
||||
@@ -208,7 +201,6 @@ public abstract class AbstractEcoPlugin extends JavaPlugin {
|
||||
this.runnableFactory = new RunnableFactory(this);
|
||||
this.extensionLoader = new EcoExtensionLoader(this);
|
||||
this.configHandler = new ConfigHandler(this);
|
||||
this.recipeManager = new RecipeManager(this);
|
||||
|
||||
this.langYml = new Lang(this);
|
||||
this.configYml = new Config(this);
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.willfp.eco.util.recipe;
|
||||
|
||||
import com.willfp.eco.util.recipe.parts.EmptyRecipePart;
|
||||
import com.willfp.eco.util.recipe.parts.RecipePart;
|
||||
import com.willfp.eco.util.recipe.parts.SimpleRecipePart;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@UtilityClass
|
||||
@SuppressWarnings("deprecation")
|
||||
public final class RecipeParts {
|
||||
/**
|
||||
* All recipe parts.
|
||||
*/
|
||||
private static final Map<NamespacedKey, RecipePart> PARTS = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Register a new recipe part.
|
||||
*
|
||||
* @param key The key of the recipe part.
|
||||
* @param part The recipe part.
|
||||
*/
|
||||
public void registerRecipePart(@NotNull final NamespacedKey key,
|
||||
@NotNull final RecipePart part) {
|
||||
PARTS.put(key, part);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookup recipe part from string.
|
||||
*
|
||||
* @param key The string to test.
|
||||
* @return The found recipe part, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
public RecipePart lookup(@NotNull final String key) {
|
||||
String[] split = key.toLowerCase().split(":");
|
||||
if (split.length == 1) {
|
||||
Material material = Material.getMaterial(key.toUpperCase());
|
||||
if (material == null || material == Material.AIR) {
|
||||
return new EmptyRecipePart();
|
||||
}
|
||||
return new SimpleRecipePart(material);
|
||||
}
|
||||
|
||||
return PARTS.get(new NamespacedKey(split[0], split[1]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if itemStack is a recipe part (used to check for custom items).
|
||||
*
|
||||
* @param itemStack The itemStack to check.
|
||||
* @return If is recipe.
|
||||
*/
|
||||
public boolean isRecipePart(@NotNull final ItemStack itemStack) {
|
||||
return PARTS.values().stream().anyMatch(recipePart -> recipePart.matches(itemStack));
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,8 @@ package com.willfp.eco.util.recipe;
|
||||
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import com.willfp.eco.util.internal.PluginDependent;
|
||||
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||
import com.willfp.eco.util.recipe.recipes.EcoShapedRecipe;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -12,40 +12,34 @@ import org.bukkit.inventory.ShapedRecipe;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@UtilityClass
|
||||
@SuppressWarnings("deprecation")
|
||||
public class RecipeManager extends PluginDependent {
|
||||
public class Recipes {
|
||||
/**
|
||||
* Registry of all recipes.
|
||||
*/
|
||||
private final BiMap<String, EcoShapedRecipe> registry = HashBiMap.create();
|
||||
private static final BiMap<NamespacedKey, EcoShapedRecipe> RECIPES = HashBiMap.create();
|
||||
|
||||
|
||||
/**
|
||||
* Pass an {@link AbstractEcoPlugin} in order to interface with it.
|
||||
* Register a recipe.
|
||||
*
|
||||
* @param plugin The plugin to manage.
|
||||
* @param recipe The recipe.
|
||||
*/
|
||||
public RecipeManager(@NotNull final AbstractEcoPlugin plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
public void register(@NotNull final EcoShapedRecipe recipe) {
|
||||
RECIPES.forcePut(recipe.getKey(), recipe);
|
||||
|
||||
void register(@NotNull final EcoShapedRecipe recipe) {
|
||||
String key = recipe.getKey();
|
||||
registry.forcePut(key, recipe);
|
||||
Bukkit.getServer().removeRecipe(recipe.getKey());
|
||||
Bukkit.getServer().removeRecipe(recipe.getDisplayedKey());
|
||||
|
||||
NamespacedKey baseKey = this.getPlugin().getNamespacedKeyFactory().create(key);
|
||||
Bukkit.getServer().removeRecipe(baseKey);
|
||||
|
||||
NamespacedKey displayedKey = this.getPlugin().getNamespacedKeyFactory().create(key + "_displayed");
|
||||
Bukkit.getServer().removeRecipe(displayedKey);
|
||||
|
||||
ShapedRecipe shapedRecipe = new ShapedRecipe(baseKey, recipe.getOutput());
|
||||
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(displayedKey, recipe.getOutput());
|
||||
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];
|
||||
@@ -64,7 +58,7 @@ public class RecipeManager extends PluginDependent {
|
||||
*/
|
||||
@Nullable
|
||||
public EcoShapedRecipe getMatch(@NotNull final ItemStack[] matrix) {
|
||||
return registry.values().stream().filter(recipe -> recipe.test(matrix)).findFirst().orElse(null);
|
||||
return RECIPES.values().stream().filter(recipe -> recipe.test(matrix)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,7 +68,7 @@ public class RecipeManager extends PluginDependent {
|
||||
* @return The shaped recipe, or null if not found.
|
||||
*/
|
||||
@Nullable
|
||||
public EcoShapedRecipe getShapedRecipe(@NotNull final String key) {
|
||||
return registry.get(key);
|
||||
public EcoShapedRecipe getShapedRecipe(@NotNull final NamespacedKey key) {
|
||||
return RECIPES.get(key);
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package com.willfp.eco.util.recipe.lookup;
|
||||
|
||||
import com.willfp.eco.util.recipe.parts.EmptyRecipePart;
|
||||
import com.willfp.eco.util.recipe.parts.RecipePart;
|
||||
import com.willfp.eco.util.recipe.parts.SimpleRecipePart;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Material;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
@UtilityClass
|
||||
public final class RecipePartUtils {
|
||||
/**
|
||||
* All recipes.
|
||||
*/
|
||||
private static final Map<String, Function<String, RecipePart>> TESTS = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Register a new lookup.
|
||||
*
|
||||
* @param key The key of the lookup.
|
||||
* @param lookupFunction The lookup to register, where the output is the recipe part generated.
|
||||
*/
|
||||
public void registerLookup(@NotNull final String key,
|
||||
@NotNull final Function<String, RecipePart> lookupFunction) {
|
||||
TESTS.put(key, lookupFunction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookup recipe part from string.
|
||||
*
|
||||
* @param key The string to test.
|
||||
* @return The generated recipe part, or null if invalid.
|
||||
*/
|
||||
public RecipePart lookup(@NotNull final String key) {
|
||||
Function<String, RecipePart> lookup = TESTS.get(key);
|
||||
|
||||
if (lookup == null) {
|
||||
Material material = Material.getMaterial(key.toUpperCase());
|
||||
if (material == null || material == Material.AIR) {
|
||||
return new EmptyRecipePart();
|
||||
}
|
||||
return new SimpleRecipePart(material);
|
||||
}
|
||||
|
||||
return lookup.apply(key);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
package com.willfp.eco.util.recipe;
|
||||
package com.willfp.eco.util.recipe.recipes;
|
||||
|
||||
import com.willfp.eco.util.interfaces.Registerable;
|
||||
import com.willfp.eco.util.internal.PluginDependent;
|
||||
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||
import com.willfp.eco.util.recipe.Recipes;
|
||||
import com.willfp.eco.util.recipe.parts.EmptyRecipePart;
|
||||
import com.willfp.eco.util.recipe.parts.RecipePart;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -23,7 +25,13 @@ public final class EcoShapedRecipe extends PluginDependent implements Registerab
|
||||
* The key of the recipe.
|
||||
*/
|
||||
@Getter
|
||||
private final String key;
|
||||
private final NamespacedKey key;
|
||||
|
||||
/**
|
||||
* The key of the displayed recipe.
|
||||
*/
|
||||
@Getter
|
||||
private final NamespacedKey displayedKey;
|
||||
|
||||
/**
|
||||
* The recipe's output.
|
||||
@@ -38,7 +46,8 @@ public final class EcoShapedRecipe extends PluginDependent implements Registerab
|
||||
super(plugin);
|
||||
|
||||
this.parts = parts;
|
||||
this.key = key;
|
||||
this.key = plugin.getNamespacedKeyFactory().create(key);
|
||||
this.displayedKey = plugin.getNamespacedKeyFactory().create(key + "_displayed");
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
@@ -84,7 +93,7 @@ public final class EcoShapedRecipe extends PluginDependent implements Registerab
|
||||
*/
|
||||
@Override
|
||||
public void register() {
|
||||
this.getPlugin().getRecipeManager().register(this);
|
||||
Recipes.register(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -193,66 +202,4 @@ public final class EcoShapedRecipe extends PluginDependent implements Registerab
|
||||
return new EcoShapedRecipe(plugin, key.toLowerCase(), recipeParts, output);
|
||||
}
|
||||
}
|
||||
|
||||
public enum RecipePosition {
|
||||
/**
|
||||
* Top left of matrix.
|
||||
*/
|
||||
TOP_LEFT(0),
|
||||
|
||||
/**
|
||||
* Top middle of matrix.
|
||||
*/
|
||||
TOP_MIDDLE(1),
|
||||
|
||||
/**
|
||||
* Top right of matrix.
|
||||
*/
|
||||
TOP_RIGHT(2),
|
||||
|
||||
/**
|
||||
* Middle left of matrix.
|
||||
*/
|
||||
MIDDLE_LEFT(3),
|
||||
|
||||
/**
|
||||
* Middle of matrix.
|
||||
*/
|
||||
MIDDLE(4),
|
||||
|
||||
/**
|
||||
* Middle right of matrix.
|
||||
*/
|
||||
MIDDLE_RIGHT(5),
|
||||
|
||||
/**
|
||||
* Bottom left of matrix.
|
||||
*/
|
||||
BOTTOM_LEFT(6),
|
||||
|
||||
/**
|
||||
* Bottom middle of matrix.
|
||||
*/
|
||||
BOTTOM_MIDDLE(7),
|
||||
|
||||
/**
|
||||
* Bottom right of matrix.
|
||||
*/
|
||||
BOTTOM_RIGHT(8);
|
||||
|
||||
/**
|
||||
* The index within a crafting table matrix.
|
||||
*/
|
||||
@Getter
|
||||
private final int index;
|
||||
|
||||
/**
|
||||
* Recipe position with crafting table index.
|
||||
*
|
||||
* @param index The index.
|
||||
*/
|
||||
RecipePosition(final int index) {
|
||||
this.index = index;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.willfp.eco.util.recipe;
|
||||
package com.willfp.eco.util.recipe.recipes;
|
||||
|
||||
import com.willfp.eco.util.internal.PluginDependent;
|
||||
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||
import com.willfp.eco.util.recipe.Recipes;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
@@ -39,7 +40,7 @@ public class RecipeListener extends PluginDependent implements Listener {
|
||||
}
|
||||
|
||||
ItemStack[] matrix = event.getInventory().getMatrix();
|
||||
EcoShapedRecipe matched = this.getPlugin().getRecipeManager().getMatch(matrix);
|
||||
EcoShapedRecipe matched = Recipes.getMatch(matrix);
|
||||
|
||||
if (matched == null) {
|
||||
event.getInventory().setResult(new ItemStack(Material.AIR));
|
||||
@@ -71,7 +72,7 @@ public class RecipeListener extends PluginDependent implements Listener {
|
||||
}
|
||||
|
||||
ItemStack[] matrix = event.getInventory().getMatrix();
|
||||
EcoShapedRecipe matched = this.getPlugin().getRecipeManager().getMatch(matrix);
|
||||
EcoShapedRecipe matched = Recipes.getMatch(matrix);
|
||||
|
||||
if (matched == null) {
|
||||
event.getInventory().setResult(new ItemStack(Material.AIR));
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.willfp.eco.util.recipe.recipes;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
public enum RecipePosition {
|
||||
/**
|
||||
* Top left of matrix.
|
||||
*/
|
||||
TOP_LEFT(0),
|
||||
|
||||
/**
|
||||
* Top middle of matrix.
|
||||
*/
|
||||
TOP_MIDDLE(1),
|
||||
|
||||
/**
|
||||
* Top right of matrix.
|
||||
*/
|
||||
TOP_RIGHT(2),
|
||||
|
||||
/**
|
||||
* Middle left of matrix.
|
||||
*/
|
||||
MIDDLE_LEFT(3),
|
||||
|
||||
/**
|
||||
* Middle of matrix.
|
||||
*/
|
||||
MIDDLE(4),
|
||||
|
||||
/**
|
||||
* Middle right of matrix.
|
||||
*/
|
||||
MIDDLE_RIGHT(5),
|
||||
|
||||
/**
|
||||
* Bottom left of matrix.
|
||||
*/
|
||||
BOTTOM_LEFT(6),
|
||||
|
||||
/**
|
||||
* Bottom middle of matrix.
|
||||
*/
|
||||
BOTTOM_MIDDLE(7),
|
||||
|
||||
/**
|
||||
* Bottom right of matrix.
|
||||
*/
|
||||
BOTTOM_RIGHT(8);
|
||||
|
||||
/**
|
||||
* The index within a crafting table matrix.
|
||||
*/
|
||||
@Getter
|
||||
private final int index;
|
||||
|
||||
/**
|
||||
* Recipe position with crafting table index.
|
||||
*
|
||||
* @param index The index.
|
||||
*/
|
||||
RecipePosition(final int index) {
|
||||
this.index = index;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user