Skip to content

Commit 973e5f8

Browse files
authored
Add support for transmute recipes in /recipe (#6302)
Fixes #6297
1 parent 37e1054 commit 973e5f8

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

Essentials/src/main/java/com/earth2me/essentials/commands/Commandrecipe.java

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
import net.ess3.provider.InventoryViewProvider;
1212
import net.kyori.adventure.text.format.NamedTextColor;
1313
import org.bukkit.Material;
14+
import org.bukkit.NamespacedKey;
1415
import org.bukkit.Server;
1516
import org.bukkit.inventory.FurnaceRecipe;
1617
import org.bukkit.inventory.InventoryView;
1718
import org.bukkit.inventory.ItemStack;
1819
import org.bukkit.inventory.Recipe;
20+
import org.bukkit.inventory.RecipeChoice;
1921
import org.bukkit.inventory.ShapedRecipe;
2022
import org.bukkit.inventory.ShapelessRecipe;
23+
import org.bukkit.inventory.TransmuteRecipe;
2124

25+
import java.util.ArrayList;
2226
import java.util.Collections;
2327
import java.util.HashMap;
2428
import java.util.List;
@@ -62,7 +66,7 @@ public void run(final Server server, final CommandSource sender, final String co
6266
if (!sender.isPlayer()) {
6367
throw new TranslatableException("consoleCannotUseCommand");
6468
}
65-
69+
6670
itemType = Inventories.getItemInHand(sender.getPlayer());
6771
} else {
6872
itemType = ess.getItemDb().get(args[0]);
@@ -78,24 +82,42 @@ public void run(final Server server, final CommandSource sender, final String co
7882
}
7983
}
8084

81-
final List<Recipe> recipesOfType = ess.getServer().getRecipesFor(itemType);
82-
if (recipesOfType.size() < 1) {
85+
final List<Recipe> bukkitRecipes = ess.getServer().getRecipesFor(itemType);
86+
if (bukkitRecipes.isEmpty()) {
8387
throw new TranslatableException("recipeNone", getMaterialName(sender, itemType));
8488
}
8589

86-
if (recipeNo < 0 || recipeNo >= recipesOfType.size()) {
90+
final List<Recipe> recipes = new ArrayList<>();
91+
for (Recipe recipe : bukkitRecipes) {
92+
if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_21_3_R01) && recipe instanceof TransmuteRecipe) {
93+
final TransmuteRecipe transmuteRecipe = (TransmuteRecipe) recipe;
94+
95+
for (ItemStack inputChoice : toChoices(transmuteRecipe.getInput())) {
96+
for (ItemStack materialChoice : toChoices(transmuteRecipe.getMaterial())) {
97+
final ShapelessRecipe shapelessRecipe = new ShapelessRecipe(new NamespacedKey(ess, "transmute"), itemType);
98+
shapelessRecipe.addIngredient(inputChoice);
99+
shapelessRecipe.addIngredient(materialChoice);
100+
recipes.add(shapelessRecipe);
101+
}
102+
}
103+
} else {
104+
recipes.add(recipe);
105+
}
106+
}
107+
108+
if (recipeNo < 0 || recipeNo >= recipes.size()) {
87109
throw new TranslatableException("recipeBadIndex");
88110
}
89111

90-
final Recipe selectedRecipe = recipesOfType.get(recipeNo);
91-
sender.sendTl("recipe", getMaterialName(sender, itemType), recipeNo + 1, recipesOfType.size());
112+
final Recipe selectedRecipe = recipes.get(recipeNo);
113+
sender.sendTl("recipe", getMaterialName(sender, itemType), recipeNo + 1, recipes.size());
92114

93115
if (selectedRecipe instanceof FurnaceRecipe) {
94116
furnaceRecipe(sender, (FurnaceRecipe) selectedRecipe);
95117
} else if (selectedRecipe instanceof ShapedRecipe) {
96118
shapedRecipe(sender, (ShapedRecipe) selectedRecipe, sender.isPlayer());
97119
} else if (selectedRecipe instanceof ShapelessRecipe) {
98-
if (recipesOfType.size() == 1 && (itemType.getType() == FIREWORK_ROCKET)) {
120+
if (recipes.size() == 1 && itemType.getType() == FIREWORK_ROCKET) {
99121
final ShapelessRecipe shapelessRecipe = new ShapelessRecipe(itemType);
100122
shapelessRecipe.addIngredient(GUNPOWDER);
101123
shapelessRecipe.addIngredient(Material.PAPER);
@@ -106,11 +128,25 @@ public void run(final Server server, final CommandSource sender, final String co
106128
}
107129
}
108130

109-
if (recipesOfType.size() > 1 && args.length == 1) {
131+
if (recipes.size() > 1 && args.length == 1) {
110132
sender.sendTl("recipeMore", commandLabel, args[0], getMaterialName(sender, itemType));
111133
}
112134
}
113135

136+
private List<ItemStack> toChoices(final RecipeChoice choice) {
137+
if (choice instanceof RecipeChoice.MaterialChoice) {
138+
final List<ItemStack> stacks = new ArrayList<>();
139+
for (final Material material : ((RecipeChoice.MaterialChoice) choice).getChoices()) {
140+
stacks.add(new ItemStack(material, 1));
141+
}
142+
return stacks;
143+
} else if (choice instanceof RecipeChoice.ExactChoice) {
144+
return ((RecipeChoice.ExactChoice) choice).getChoices();
145+
} else {
146+
return Collections.emptyList();
147+
}
148+
}
149+
114150
public void furnaceRecipe(final CommandSource sender, final FurnaceRecipe recipe) {
115151
sender.sendTl("recipeFurnace", getMaterialName(sender, recipe.getInput()));
116152
}

0 commit comments

Comments
 (0)