Renamed RecipePart into CustomItem system
This commit is contained in:
@@ -3,7 +3,7 @@ package com.willfp.eco.core;
|
||||
import com.willfp.eco.core.command.AbstractCommand;
|
||||
import com.willfp.eco.core.config.configs.Config;
|
||||
import com.willfp.eco.core.config.configs.Lang;
|
||||
import com.willfp.eco.core.config.updating.ConfigHandler;
|
||||
import com.willfp.eco.internal.config.updating.ConfigHandler;
|
||||
import com.willfp.eco.core.display.Display;
|
||||
import com.willfp.eco.core.display.DisplayModule;
|
||||
import com.willfp.eco.core.events.EventManager;
|
||||
@@ -38,6 +38,7 @@ import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class EcoPlugin extends JavaPlugin {
|
||||
@@ -154,6 +155,11 @@ public abstract class EcoPlugin extends JavaPlugin {
|
||||
@Getter
|
||||
private DisplayModule displayModule;
|
||||
|
||||
/**
|
||||
* The logger for the plugin.
|
||||
*/
|
||||
private Logger logger;
|
||||
|
||||
/**
|
||||
* If the server is running an outdated version of the plugin.
|
||||
*/
|
||||
@@ -404,4 +410,10 @@ public abstract class EcoPlugin extends JavaPlugin {
|
||||
protected DisplayModule createDisplayModule() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Logger getLogger() {
|
||||
return logger;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.willfp.eco.core.recipe.parts;
|
||||
package com.willfp.eco.core.items;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -7,27 +7,27 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class ComplexRecipePart implements RecipePart {
|
||||
public class CustomItem implements TestableItem {
|
||||
/**
|
||||
* The test for itemstacks to pass.
|
||||
* The test for ItemStacks to pass.
|
||||
*/
|
||||
@Getter
|
||||
private final Predicate<ItemStack> predicate;
|
||||
|
||||
/**
|
||||
* Displayed itemstack: what the user should see.
|
||||
* Example Item: what the user should see.
|
||||
*/
|
||||
private final ItemStack displayed;
|
||||
private final ItemStack item;
|
||||
|
||||
/**
|
||||
* Create a new complex recipe part.
|
||||
* @param predicate The test.
|
||||
* @param displayed The example itemstack.
|
||||
* @param item The example ItemStacks.
|
||||
*/
|
||||
public ComplexRecipePart(@NotNull final Predicate<ItemStack> predicate,
|
||||
@NotNull final ItemStack displayed) {
|
||||
public CustomItem(@NotNull final Predicate<ItemStack> predicate,
|
||||
@NotNull final ItemStack item) {
|
||||
this.predicate = predicate;
|
||||
this.displayed = displayed;
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -36,7 +36,7 @@ public class ComplexRecipePart implements RecipePart {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getDisplayed() {
|
||||
return displayed;
|
||||
public ItemStack getItem() {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.willfp.eco.core.items;
|
||||
|
||||
import com.willfp.eco.core.recipe.parts.EmptyTestableItem;
|
||||
import com.willfp.eco.core.recipe.parts.MaterialTestableItem;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@UtilityClass
|
||||
@SuppressWarnings("deprecation")
|
||||
public final class CustomItems {
|
||||
/**
|
||||
* All recipe parts.
|
||||
*/
|
||||
private static final Map<NamespacedKey, TestableItem> REGISTRY = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Register a new recipe part.
|
||||
*
|
||||
* @param key The key of the recipe part.
|
||||
* @param part The recipe part.
|
||||
*/
|
||||
public void registerCustomItem(@NotNull final NamespacedKey key,
|
||||
@NotNull final TestableItem part) {
|
||||
REGISTRY.put(key, part);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookup item from string.
|
||||
* <p>
|
||||
* Used for recipes.
|
||||
*
|
||||
* @param key The string to test.
|
||||
* @return The found testable item, or null if not found.
|
||||
*/
|
||||
public TestableItem 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 EmptyTestableItem();
|
||||
}
|
||||
return new MaterialTestableItem(material);
|
||||
}
|
||||
|
||||
TestableItem part = REGISTRY.get(new NamespacedKey(split[0], split[1]));
|
||||
return part == null ? new EmptyTestableItem() : part;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if itemStack is a custom item.
|
||||
*
|
||||
* @param itemStack The itemStack to check.
|
||||
* @return If is recipe.
|
||||
*/
|
||||
public boolean isCustomItem(@NotNull final ItemStack itemStack) {
|
||||
return REGISTRY.values().stream().anyMatch(recipePart -> recipePart.matches(itemStack));
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.willfp.eco.core.recipe.parts;
|
||||
package com.willfp.eco.core.items;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface RecipePart {
|
||||
public interface TestableItem {
|
||||
/**
|
||||
* If an ItemStack matches the recipe part.
|
||||
*
|
||||
@@ -17,6 +17,5 @@ public interface RecipePart {
|
||||
*
|
||||
* @return The item, displayed.
|
||||
*/
|
||||
ItemStack getDisplayed();
|
||||
ItemStack getItem();
|
||||
}
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
package com.willfp.eco.core.recipe;
|
||||
|
||||
import com.willfp.eco.core.recipe.parts.EmptyRecipePart;
|
||||
import com.willfp.eco.core.recipe.parts.RecipePart;
|
||||
import com.willfp.eco.core.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 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.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
RecipePart part = PARTS.get(new NamespacedKey(split[0], split[1]));
|
||||
return part == null ? new EmptyRecipePart() : part;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,15 @@
|
||||
package com.willfp.eco.core.recipe.parts;
|
||||
|
||||
import com.willfp.eco.core.items.TestableItem;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class EmptyRecipePart implements RecipePart {
|
||||
public class EmptyTestableItem implements TestableItem {
|
||||
/**
|
||||
* Create a new empty recipe part.
|
||||
*/
|
||||
public EmptyRecipePart() {
|
||||
public EmptyTestableItem() {
|
||||
|
||||
}
|
||||
|
||||
@@ -24,7 +25,7 @@ public class EmptyRecipePart implements RecipePart {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getDisplayed() {
|
||||
public ItemStack getItem() {
|
||||
return new ItemStack(Material.AIR);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
package com.willfp.eco.core.recipe.parts;
|
||||
|
||||
import com.willfp.eco.core.items.TestableItem;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class SimpleRecipePart implements RecipePart {
|
||||
public class MaterialTestableItem implements TestableItem {
|
||||
/**
|
||||
* The material.
|
||||
*/
|
||||
@@ -18,7 +19,7 @@ public class SimpleRecipePart implements RecipePart {
|
||||
*
|
||||
* @param material The material.
|
||||
*/
|
||||
public SimpleRecipePart(@NotNull final Material material) {
|
||||
public MaterialTestableItem(@NotNull final Material material) {
|
||||
this.material = material;
|
||||
}
|
||||
|
||||
@@ -34,7 +35,7 @@ public class SimpleRecipePart implements RecipePart {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getDisplayed() {
|
||||
public ItemStack getItem() {
|
||||
return new ItemStack(material);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.willfp.eco.core.recipe.recipes;
|
||||
|
||||
import com.willfp.eco.core.recipe.parts.RecipePart;
|
||||
import com.willfp.eco.core.items.TestableItem;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -41,7 +41,7 @@ public interface CraftingRecipe {
|
||||
*
|
||||
* @return The parts.
|
||||
*/
|
||||
RecipePart[] getParts();
|
||||
TestableItem[] getParts();
|
||||
|
||||
/**
|
||||
* Get the recipe key.
|
||||
|
||||
@@ -2,9 +2,9 @@ package com.willfp.eco.core.recipe.recipes;
|
||||
|
||||
import com.willfp.eco.core.PluginDependent;
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import com.willfp.eco.core.items.TestableItem;
|
||||
import com.willfp.eco.core.recipe.Recipes;
|
||||
import com.willfp.eco.core.recipe.parts.EmptyRecipePart;
|
||||
import com.willfp.eco.core.recipe.parts.RecipePart;
|
||||
import com.willfp.eco.core.recipe.parts.EmptyTestableItem;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
@@ -18,7 +18,7 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft
|
||||
* Recipe parts.
|
||||
*/
|
||||
@Getter
|
||||
private final RecipePart[] parts;
|
||||
private final TestableItem[] parts;
|
||||
|
||||
/**
|
||||
* The key of the recipe.
|
||||
@@ -40,7 +40,7 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft
|
||||
|
||||
private ShapedCraftingRecipe(@NotNull final EcoPlugin plugin,
|
||||
@NotNull final String key,
|
||||
@NotNull final RecipePart[] parts,
|
||||
@NotNull final TestableItem[] parts,
|
||||
@NotNull final ItemStack output) {
|
||||
super(plugin);
|
||||
|
||||
@@ -52,12 +52,12 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft
|
||||
|
||||
@Override
|
||||
public Material getMaterialAtIndex(final int index) {
|
||||
return parts[index].getDisplayed().getType();
|
||||
return parts[index].getItem().getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getDisplayedAtIndex(final int index) {
|
||||
return parts[index].getDisplayed();
|
||||
return parts[index].getItem();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -102,7 +102,7 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft
|
||||
/**
|
||||
* The recipe parts.
|
||||
*/
|
||||
private final RecipePart[] recipeParts = new RecipePart[9];
|
||||
private final TestableItem[] recipeParts = new TestableItem[9];
|
||||
|
||||
/**
|
||||
* The output of the recipe.
|
||||
@@ -139,7 +139,7 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft
|
||||
* @return The builder.
|
||||
*/
|
||||
public Builder setRecipePart(@NotNull final RecipePosition position,
|
||||
@NotNull final RecipePart part) {
|
||||
@NotNull final TestableItem part) {
|
||||
this.recipeParts[position.getIndex()] = part;
|
||||
return this;
|
||||
}
|
||||
@@ -152,7 +152,7 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft
|
||||
* @return The builder.
|
||||
*/
|
||||
public Builder setRecipePart(final int position,
|
||||
@NotNull final RecipePart part) {
|
||||
@NotNull final TestableItem part) {
|
||||
this.recipeParts[position] = part;
|
||||
return this;
|
||||
}
|
||||
@@ -176,7 +176,7 @@ 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 EmptyRecipePart();
|
||||
recipeParts[i] = new EmptyTestableItem();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.willfp.eco.core.config.updating;
|
||||
package com.willfp.eco.internal.config.updating;
|
||||
|
||||
import com.willfp.eco.core.config.updating.annotations.ConfigUpdater;
|
||||
import com.willfp.eco.core.config.updating.exceptions.InvalidUpdatableClassException;
|
||||
import com.willfp.eco.core.config.updating.exceptions.InvalidUpdateMethodException;
|
||||
import com.willfp.eco.internal.config.updating.annotations.ConfigUpdater;
|
||||
import com.willfp.eco.internal.config.updating.exceptions.InvalidUpdatableClassException;
|
||||
import com.willfp.eco.internal.config.updating.exceptions.InvalidUpdateMethodException;
|
||||
import com.willfp.eco.core.PluginDependent;
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.willfp.eco.core.config.updating.annotations;
|
||||
package com.willfp.eco.internal.config.updating.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.willfp.eco.core.config.updating.exceptions;
|
||||
package com.willfp.eco.internal.config.updating.exceptions;
|
||||
|
||||
import com.willfp.eco.core.config.updating.ConfigHandler;
|
||||
import com.willfp.eco.internal.config.updating.ConfigHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class InvalidUpdatableClassException extends RuntimeException {
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.willfp.eco.core.config.updating.exceptions;
|
||||
package com.willfp.eco.internal.config.updating.exceptions;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.willfp.eco.internal.logging;
|
||||
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import com.willfp.eco.util.StringUtils;
|
||||
import org.bukkit.plugin.PluginLogger;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class EcoLogger extends PluginLogger {
|
||||
public EcoLogger(@NotNull final EcoPlugin context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(@NotNull final String msg) {
|
||||
super.info(StringUtils.translate(msg));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warning(@NotNull final String msg) {
|
||||
super.warning(StringUtils.translate(msg));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void severe(@NotNull final String msg) {
|
||||
super.severe(StringUtils.translate(msg));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user