#873: Add PlayerRecipeBookClickEvent

By: Jishuna <joshl5324@gmail.com>
This commit is contained in:
Bukkit/Spigot
2023-08-02 07:46:15 +10:00
parent 35bb07c269
commit cbeae89133
4 changed files with 196 additions and 156 deletions

View File

@@ -0,0 +1,89 @@
package org.bukkit.inventory;
import com.google.common.base.Preconditions;
import org.bukkit.Keyed;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.recipe.CraftingBookCategory;
import org.jetbrains.annotations.NotNull;
/**
* Represents a shaped or shapeless crafting recipe.
*/
public abstract class CraftingRecipe implements Recipe, Keyed {
private final NamespacedKey key;
private final ItemStack output;
private String group = "";
private CraftingBookCategory category = CraftingBookCategory.MISC;
protected CraftingRecipe(@NotNull NamespacedKey key, @NotNull ItemStack result) {
Preconditions.checkArgument(key != null, "key cannot be null");
Preconditions.checkArgument(result.getType() != Material.AIR, "Recipe must have non-AIR result.");
this.key = key;
this.output = new ItemStack(result);
}
@NotNull
@Override
public NamespacedKey getKey() {
return key;
}
/**
* Get the result of this recipe.
*
* @return The result stack.
*/
@Override
@NotNull
public ItemStack getResult() {
return output.clone();
}
/**
* Get the group of this recipe. Recipes with the same group may be grouped
* together when displayed in the client.
*
* @return recipe group. An empty string denotes no group. May not be null.
*/
@NotNull
public String getGroup() {
return group;
}
/**
* Set the group of this recipe. Recipes with the same group may be grouped
* together when displayed in the client.
*
* @param group recipe group. An empty string denotes no group. May not be
* null.
*/
public void setGroup(@NotNull String group) {
Preconditions.checkArgument(group != null, "group cannot be null");
this.group = group;
}
/**
* Gets the category which this recipe will appear in the recipe book under.
*
* Defaults to {@link CraftingBookCategory#MISC} if not set.
*
* @return recipe book category
*/
@NotNull
public CraftingBookCategory getCategory() {
return category;
}
/**
* Sets the category which this recipe will appear in the recipe book under.
*
* Defaults to {@link CraftingBookCategory#MISC} if not set.
*
* @param category recipe book category
*/
public void setCategory(@NotNull CraftingBookCategory category) {
Preconditions.checkArgument(category != null, "category cannot be null");
this.category = category;
}
}

View File

@@ -4,23 +4,17 @@ import com.google.common.base.Preconditions;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Keyed;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.recipe.CraftingBookCategory;
import org.bukkit.material.MaterialData;
import org.jetbrains.annotations.NotNull;
/**
* Represents a shaped (ie normal) crafting recipe.
*/
public class ShapedRecipe implements Recipe, Keyed {
private final NamespacedKey key;
private final ItemStack output;
public class ShapedRecipe extends CraftingRecipe {
private String[] rows;
private Map<Character, RecipeChoice> ingredients = new HashMap<>();
private String group = "";
private CraftingBookCategory category = CraftingBookCategory.MISC;
/**
* Create a shaped recipe to craft the specified ItemStack. The
@@ -38,9 +32,7 @@ public class ShapedRecipe implements Recipe, Keyed {
*/
@Deprecated
public ShapedRecipe(@NotNull ItemStack result) {
Preconditions.checkArgument(result.getType() != Material.AIR, "Recipe must have non-AIR result.");
this.key = NamespacedKey.randomKey();
this.output = new ItemStack(result);
super(NamespacedKey.randomKey(), result);
}
/**
@@ -57,11 +49,7 @@ public class ShapedRecipe implements Recipe, Keyed {
* @see ShapedRecipe#setIngredient(char, RecipeChoice)
*/
public ShapedRecipe(@NotNull NamespacedKey key, @NotNull ItemStack result) {
Preconditions.checkArgument(key != null, "key");
Preconditions.checkArgument(result.getType() != Material.AIR, "Recipe must have non-AIR result.");
this.key = key;
this.output = new ItemStack(result);
super(key, result);
}
/**
@@ -229,68 +217,4 @@ public class ShapedRecipe implements Recipe, Keyed {
public String[] getShape() {
return rows.clone();
}
/**
* Get the result.
*
* @return The result stack.
*/
@Override
@NotNull
public ItemStack getResult() {
return output.clone();
}
@NotNull
@Override
public NamespacedKey getKey() {
return key;
}
/**
* Get the group of this recipe. Recipes with the same group may be grouped
* together when displayed in the client.
*
* @return recipe group. An empty string denotes no group. May not be null.
*/
@NotNull
public String getGroup() {
return group;
}
/**
* Set the group of this recipe. Recipes with the same group may be grouped
* together when displayed in the client.
*
* @param group recipe group. An empty string denotes no group. May not be
* null.
*/
public void setGroup(@NotNull String group) {
Preconditions.checkArgument(group != null, "group cannot be null");
this.group = group;
}
/**
* Gets the category which this recipe will appear in the recipe book under.
*
* Defaults to {@link CraftingBookCategory#MISC} if not set.
*
* @return recipe book category
*/
@NotNull
public CraftingBookCategory getCategory() {
return category;
}
/**
* Sets the category which this recipe will appear in the recipe book under.
*
* Defaults to {@link CraftingBookCategory#MISC} if not set.
*
* @param category recipe book category
*/
public void setCategory(@NotNull CraftingBookCategory category) {
Preconditions.checkArgument(category != null, "category cannot be null");
this.category = category;
}
}

View File

@@ -5,10 +5,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.bukkit.Keyed;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.recipe.CraftingBookCategory;
import org.bukkit.material.MaterialData;
import org.jetbrains.annotations.NotNull;
@@ -16,18 +14,12 @@ import org.jetbrains.annotations.NotNull;
* Represents a shapeless recipe, where the arrangement of the ingredients on
* the crafting grid does not matter.
*/
public class ShapelessRecipe implements Recipe, Keyed {
private final NamespacedKey key;
private final ItemStack output;
public class ShapelessRecipe extends CraftingRecipe {
private final List<RecipeChoice> ingredients = new ArrayList<>();
private String group = "";
private CraftingBookCategory category = CraftingBookCategory.MISC;
@Deprecated
public ShapelessRecipe(@NotNull ItemStack result) {
Preconditions.checkArgument(result.getType() != Material.AIR, "Recipe must have non-AIR result.");
this.key = NamespacedKey.randomKey();
this.output = new ItemStack(result);
super(NamespacedKey.randomKey(), result);
}
/**
@@ -45,9 +37,7 @@ public class ShapelessRecipe implements Recipe, Keyed {
* @see ShapelessRecipe#addIngredient(int,Material,int)
*/
public ShapelessRecipe(@NotNull NamespacedKey key, @NotNull ItemStack result) {
Preconditions.checkArgument(result.getType() != Material.AIR, "Recipe must have non-AIR result.");
this.key = key;
this.output = new ItemStack(result);
super(key, result);
}
/**
@@ -251,17 +241,6 @@ public class ShapelessRecipe implements Recipe, Keyed {
return this;
}
/**
* Get the result of this recipe.
*
* @return The result stack.
*/
@Override
@NotNull
public ItemStack getResult() {
return output.clone();
}
/**
* Get the list of ingredients used for this recipe.
*
@@ -284,57 +263,4 @@ public class ShapelessRecipe implements Recipe, Keyed {
}
return result;
}
@NotNull
@Override
public NamespacedKey getKey() {
return key;
}
/**
* Get the group of this recipe. Recipes with the same group may be grouped
* together when displayed in the client.
*
* @return recipe group. An empty string denotes no group. May not be null.
*/
@NotNull
public String getGroup() {
return group;
}
/**
* Set the group of this recipe. Recipes with the same group may be grouped
* together when displayed in the client.
*
* @param group recipe group. An empty string denotes no group. May not be
* null.
*/
public void setGroup(@NotNull String group) {
Preconditions.checkArgument(group != null, "group cannot be null");
this.group = group;
}
/**
* Gets the category which this recipe will appear in the recipe book under.
*
* Defaults to {@link CraftingBookCategory#MISC} if not set.
*
* @return recipe book category
*/
@NotNull
public CraftingBookCategory getCategory() {
return category;
}
/**
* Sets the category which this recipe will appear in the recipe book under.
*
* Defaults to {@link CraftingBookCategory#MISC} if not set.
*
* @param category recipe book category
*/
public void setCategory(@NotNull CraftingBookCategory category) {
Preconditions.checkArgument(category != null, "category cannot be null");
this.category = category;
}
}