下面列出了怎么用org.bukkit.inventory.RecipeChoice的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* This returns the shape of a given {@link Recipe}.
* For any shapeless {@link Recipe} the result will be equivalent to
* {@link RecipeSnapshot#getRecipeInput(Recipe)}.
* For a {@link ShapedRecipe} this method will fix the order so it matches a
* 3x3 crafting grid.
*
* @param recipe
* The {@link Recipe} to get the shape from
* @return An Array of {@link RecipeChoice} representing the shape of this {@link Recipe}
*/
public RecipeChoice[] getRecipeShape(Recipe recipe) {
Validate.notNull(recipe, "Recipe must not be null!");
if (recipe instanceof ShapedRecipe) {
List<RecipeChoice> choices = new LinkedList<>();
for (String row : ((ShapedRecipe) recipe).getShape()) {
int columns = row.toCharArray().length;
for (char key : row.toCharArray()) {
choices.add(((ShapedRecipe) recipe).getChoiceMap().get(key));
}
while (columns < 3) {
choices.add(null);
columns++;
}
}
return choices.toArray(new RecipeChoice[0]);
}
else {
return snapshot.getRecipeInput(recipe);
}
}
@Test
public void testShapelessRecipeShape() {
MinecraftRecipeService service = new MinecraftRecipeService(plugin);
Assertions.assertThrows(IllegalArgumentException.class, () -> service.getRecipeShape(null));
NamespacedKey key = new NamespacedKey(plugin, "shapeless_test");
ShapelessRecipe recipe = new ShapelessRecipe(key, new ItemStack(Material.TNT_MINECART));
MaterialChoice choice = new MaterialChoice(Material.TNT);
recipe.addIngredient(choice);
server.addRecipe(recipe);
service.refresh();
Assertions.assertArrayEquals(new RecipeChoice[] { choice }, service.getRecipeShape(recipe));
}
public ShapedRecipe createShapedRecipe(TransportPipes transportPipes, String recipeKey, ItemStack resultItem, String[] shape, Object... ingredientMap) {
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(transportPipes, recipeKey), resultItem);
recipe.shape(shape);
for (int i = 0; i < ingredientMap.length; i += 2) {
char c = (char) ingredientMap[i];
if (ingredientMap[i + 1] instanceof Material) {
recipe.setIngredient(c, (Material) ingredientMap[i + 1]);
} else {
recipe.setIngredient(c, new RecipeChoice.MaterialChoice(((Collection<Material>) ingredientMap[i + 1]).stream().collect(Collectors.toList())));
}
}
return recipe;
}
private ShapedRecipe getUpsideDownRecipeFor(ShapedRecipe recipe){
ShapedRecipe upsideDown = VersionUtils.getVersionUtils().createShapedRecipe(recipe.getResult(), UUID.randomUUID().toString());
upsideDown.shape(getUpsideDownShape(recipe.getShape()));
Map<Character, RecipeChoice> recipeChoiceMap = recipe.getChoiceMap();
for (char c : recipeChoiceMap.keySet()){
upsideDown.setIngredient(c, recipeChoiceMap.get(c));
}
return upsideDown;
}
private ShapedRecipe cloneRecipeWithResult(ShapedRecipe recipe, ItemStack result){
ShapedRecipe clone = VersionUtils.getVersionUtils().createShapedRecipe(result, UUID.randomUUID().toString());
clone.shape(recipe.getShape());
Map<Character, RecipeChoice> recipeChoiceMap = recipe.getChoiceMap();
for (char c : recipeChoiceMap.keySet()){
clone.setIngredient(c, recipeChoiceMap.get(c));
}
return clone;
}
@Override
public void registerDefaultRecipes() {
SlimefunPlugin.getMinecraftRecipeService().subscribe(snapshot -> {
for (FurnaceRecipe recipe : snapshot.getRecipes(FurnaceRecipe.class)) {
RecipeChoice choice = recipe.getInputChoice();
if (choice instanceof MaterialChoice) {
for (Material input : ((MaterialChoice) choice).getChoices()) {
registerRecipe(4, new ItemStack[] { new ItemStack(input) }, new ItemStack[] { recipe.getResult() });
}
}
}
});
}
private void showMinecraftRecipe(Recipe[] recipes, int index, ItemStack item, PlayerProfile profile, Player p, boolean addToHistory) {
Recipe recipe = recipes[index];
ItemStack[] recipeItems = new ItemStack[9];
RecipeType recipeType = RecipeType.NULL;
ItemStack result = null;
Optional<MinecraftRecipe<? super Recipe>> optional = MinecraftRecipe.of(recipe);
RecipeChoiceTask task = new RecipeChoiceTask();
if (optional.isPresent()) {
MinecraftRecipe<?> mcRecipe = optional.get();
RecipeChoice[] choices = SlimefunPlugin.getMinecraftRecipeService().getRecipeShape(recipe);
if (choices.length == 1 && choices[0] instanceof MaterialChoice) {
recipeItems[4] = new ItemStack(((MaterialChoice) choices[0]).getChoices().get(0));
if (((MaterialChoice) choices[0]).getChoices().size() > 1) {
task.add(recipeSlots[4], (MaterialChoice) choices[0]);
}
}
else {
for (int i = 0; i < choices.length; i++) {
if (choices[i] instanceof MaterialChoice) {
recipeItems[i] = new ItemStack(((MaterialChoice) choices[i]).getChoices().get(0));
if (((MaterialChoice) choices[i]).getChoices().size() > 1) {
task.add(recipeSlots[i], (MaterialChoice) choices[i]);
}
}
}
}
recipeType = new RecipeType(mcRecipe);
result = recipe.getResult();
}
else {
recipeItems = new ItemStack[] { null, null, null, null, new CustomItem(Material.BARRIER, "&4We are somehow unable to show you this Recipe :/"), null, null, null, null };
}
ChestMenu menu = create(p);
if (addToHistory) {
profile.getGuideHistory().add(item, index);
}
displayItem(menu, profile, p, item, result, recipeType, recipeItems, task);
if (recipes.length > 1) {
for (int i = 27; i < 36; i++) {
menu.addItem(i, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler());
}
menu.addItem(28, ChestMenuUtils.getPreviousButton(p, index + 1, recipes.length), (pl, slot, action, stack) -> {
if (index > 0) {
showMinecraftRecipe(recipes, index - 1, item, profile, p, true);
}
return false;
});
menu.addItem(34, ChestMenuUtils.getNextButton(p, index + 1, recipes.length), (pl, slot, action, stack) -> {
if (index < recipes.length - 1) {
showMinecraftRecipe(recipes, index + 1, item, profile, p, true);
}
return false;
});
}
menu.open(p);
if (!task.isEmpty()) {
task.start(menu.toInventory());
}
}