Added permissions to recipes

This commit is contained in:
Auxilor
2021-12-12 12:38:04 +00:00
parent 9b83f6eab4
commit 1d9fca1fb8
4 changed files with 137 additions and 16 deletions

View File

@@ -79,7 +79,27 @@ public final class Recipes {
@NotNull final String key,
@NotNull final ItemStack output,
@NotNull final List<String> recipeStrings) {
ShapedCraftingRecipe.Builder builder = ShapedCraftingRecipe.builder(plugin, key).setOutput(output);
return createAndRegisterRecipe(plugin, key, output, recipeStrings, null);
}
/**
* Create and register recipe.
*
* @param plugin The plugin.
* @param key The key.
* @param output The output.
* @param recipeStrings The recipe.
* @param permission The permission.
* @return The recipe.
*/
public static CraftingRecipe createAndRegisterRecipe(@NotNull final EcoPlugin plugin,
@NotNull final String key,
@NotNull final ItemStack output,
@NotNull final List<String> recipeStrings,
@Nullable final String permission) {
ShapedCraftingRecipe.Builder builder = ShapedCraftingRecipe.builder(plugin, key)
.setOutput(output)
.setPermission(permission);
for (int i = 0; i < 9; i++) {
builder.setRecipePart(i, Items.lookup(recipeStrings.get(i)));

View File

@@ -4,6 +4,7 @@ import com.willfp.eco.core.items.TestableItem;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@@ -31,6 +32,7 @@ public interface CraftingRecipe {
*
* @return The parts.
*/
@NotNull
List<TestableItem> getParts();
/**
@@ -38,6 +40,7 @@ public interface CraftingRecipe {
*
* @return The key.
*/
@NotNull
NamespacedKey getKey();
/**
@@ -45,6 +48,7 @@ public interface CraftingRecipe {
*
* @return The key.
*/
@NotNull
NamespacedKey getDisplayedKey();
/**
@@ -52,5 +56,16 @@ public interface CraftingRecipe {
*
* @return The output.
*/
@NotNull
ItemStack getOutput();
/**
* Get the recipe permission.
*
* @return The permission.
*/
@Nullable
default String getPermission() {
return null;
}
}

View File

@@ -15,6 +15,7 @@ import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
@@ -44,16 +45,23 @@ public final class ShapedCraftingRecipe extends PluginDependent<EcoPlugin> imple
*/
private final ItemStack output;
/**
* The permission.
*/
private final String permission;
private ShapedCraftingRecipe(@NotNull final EcoPlugin plugin,
@NotNull final String key,
@NotNull final List<TestableItem> parts,
@NotNull final ItemStack output) {
@NotNull final ItemStack output,
@Nullable final String permission) {
super(plugin);
this.parts = parts;
this.key = plugin.getNamespacedKeyFactory().create(key);
this.displayedKey = plugin.getNamespacedKeyFactory().create(key + "_displayed");
this.output = output;
this.permission = permission;
}
@Override
@@ -140,6 +148,8 @@ public final class ShapedCraftingRecipe extends PluginDependent<EcoPlugin> imple
*
* @return The parts.
*/
@NotNull
@Override
public List<TestableItem> getParts() {
return this.parts;
}
@@ -149,6 +159,8 @@ public final class ShapedCraftingRecipe extends PluginDependent<EcoPlugin> imple
*
* @return The key.
*/
@NotNull
@Override
public NamespacedKey getKey() {
return this.key;
}
@@ -158,6 +170,8 @@ public final class ShapedCraftingRecipe extends PluginDependent<EcoPlugin> imple
*
* @return The displayed key.
*/
@NotNull
@Override
public NamespacedKey getDisplayedKey() {
return this.displayedKey;
}
@@ -167,10 +181,23 @@ public final class ShapedCraftingRecipe extends PluginDependent<EcoPlugin> imple
*
* @return The output.
*/
@NotNull
@Override
public ItemStack getOutput() {
return this.output;
}
/**
* Get the permission.
*
* @return The permission.
*/
@Nullable
@Override
public String getPermission() {
return permission;
}
/**
* Builder for recipes.
*/
@@ -185,6 +212,11 @@ public final class ShapedCraftingRecipe extends PluginDependent<EcoPlugin> imple
*/
private ItemStack output = null;
/**
* The permission for the recipe.
*/
private String permission = null;
/**
* The key of the recipe.
*/
@@ -244,6 +276,16 @@ public final class ShapedCraftingRecipe extends PluginDependent<EcoPlugin> imple
return this;
}
/**
* Set the permission required to craft the recipe.
*
* @param permission The permission.
*/
public Builder setPermission(@Nullable final String permission) {
this.permission = permission;
return this;
}
/**
* Build the recipe.
*
@@ -256,7 +298,7 @@ public final class ShapedCraftingRecipe extends PluginDependent<EcoPlugin> imple
}
}
return new ShapedCraftingRecipe(plugin, key.toLowerCase(), recipeParts, output);
return new ShapedCraftingRecipe(plugin, key.toLowerCase(), recipeParts, output, permission);
}
}
}