下面列出了怎么用org.bukkit.inventory.ShapelessRecipe的API类实例代码及写法,或者点击链接到github查看源代码。
public Recipe parseShapelessRecipe(MapFactory factory, Element elRecipe)
throws InvalidXMLException {
ShapelessRecipe recipe = new ShapelessRecipe(parseRecipeResult(factory, elRecipe));
for (Element elIngredient : XMLUtils.getChildren(elRecipe, "ingredient", "i")) {
SingleMaterialMatcher item = XMLUtils.parseMaterialPattern(elIngredient);
int count = XMLUtils.parseNumber(elIngredient.getAttribute("amount"), Integer.class, 1);
if (item.dataMatters()) {
recipe.addIngredient(count, item.getMaterialData());
} else {
recipe.addIngredient(count, item.getMaterial());
}
}
if (recipe.getIngredientList().isEmpty()) {
throw new InvalidXMLException(
"Crafting recipe must have at least one ingredient", elRecipe);
}
return recipe;
}
public Recipe next() {
if (recipes.hasNext()) {
removeFrom = recipes;
IRecipe recipe = recipes.next();
if (recipe instanceof ShapedRecipe || recipe instanceof ShapelessRecipe) {
return recipe.toBukkitRecipe();
} else {
return new CraftCustomModRecipe(recipe);
}
} else {
net.minecraft.item.ItemStack item;
if (smeltingCustom.hasNext()) {
removeFrom = smeltingCustom;
item = smeltingCustom.next();
} else {
removeFrom = smeltingVanilla;
item = smeltingVanilla.next();
}
CraftItemStack stack = CraftItemStack.asCraftMirror(FurnaceRecipes.instance().getSmeltingResult(item));
return new CraftFurnaceRecipe(stack, CraftItemStack.asCraftMirror(item));
}
}
public Recipe parseShapelessRecipe(MapModuleContext context, Element elRecipe) throws InvalidXMLException {
ShapelessRecipe recipe = new ShapelessRecipe(parseRecipeResult(context, elRecipe));
for(Element elIngredient : XMLUtils.getChildren(elRecipe, "ingredient", "i")) {
MaterialPattern item = XMLUtils.parseMaterialPattern(elIngredient);
int count = XMLUtils.parseNumber(elIngredient.getAttribute("amount"), Integer.class, 1);
if(item.dataMatters()) {
recipe.addIngredient(count, item.getMaterialData());
} else {
recipe.addIngredient(count, item.getMaterial());
}
}
if(recipe.getIngredientList().isEmpty()) {
throw new InvalidXMLException("Crafting recipe must have at least one ingredient", elRecipe);
}
return recipe;
}
public void unregister(){
Iterator<org.bukkit.inventory.Recipe> it = ProRecipes.getPlugin().getServer().recipeIterator();
org.bukkit.inventory.Recipe recipe = null;
while(it.hasNext())
{
recipe = it.next();
if (recipe != null && recipe instanceof ShapelessRecipe)
{
ShapelessRecipe b = (ShapelessRecipe)recipe;
if(ingredientCheck(b.getIngredientList(), registerer.getIngredientList())){
//it.remove();
break;
}
}
recipe = null;
}
if(recipe != null){
ProRecipes.getPlugin().mv.getChecker().removeRecipe(it, ((ShapelessRecipe)recipe));
}
}
@Override
public boolean addRecipe(Recipe recipe) {
CraftRecipe toAdd;
if (recipe instanceof CraftRecipe) {
toAdd = (CraftRecipe) recipe;
} else {
if (recipe instanceof ShapedRecipe) {
toAdd = CraftShapedRecipe.fromBukkitRecipe((ShapedRecipe) recipe);
} else if (recipe instanceof ShapelessRecipe) {
toAdd = CraftShapelessRecipe.fromBukkitRecipe((ShapelessRecipe) recipe);
} else if (recipe instanceof FurnaceRecipe) {
toAdd = CraftFurnaceRecipe.fromBukkitRecipe((FurnaceRecipe) recipe);
} else {
return false;
}
}
toAdd.addToCraftingManager();
//net.minecraft.item.crafting.CraftingManager.getInstance().sort(); // Cauldron - mod recipes not necessarily sortable
return true;
}
/**
* Check if the recipe has the given material in it.
*
* @param recipe the recipe to check
* @param mat the material to look for
* @return true if found, false if not
*/
public static boolean hasRecipeGotMaterial(Recipe recipe, Material mat) {
Collection<ItemStack> ingredients = null;
if (recipe instanceof ShapedRecipe) {
ingredients = ((ShapedRecipe) recipe).getIngredientMap().values();
} else if (recipe instanceof ShapelessRecipe) {
ingredients = ((ShapelessRecipe) recipe).getIngredientList();
}
if (null == ingredients) return false;
for (final ItemStack stack : ingredients) {
if (stack.getType() == mat) return true;
}
return false;
}
@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 static CraftShapelessRecipe fromBukkitRecipe(ShapelessRecipe recipe) {
if (recipe instanceof CraftShapelessRecipe) {
return (CraftShapelessRecipe) recipe;
}
CraftShapelessRecipe ret = new CraftShapelessRecipe(recipe.getKey(), recipe.getResult());
for (ItemStack ingred : recipe.getIngredientList()) {
ret.addIngredient(ingred.getType(), ingred.getDurability());
}
return ret;
}
public ShapelessRecipe createShapelessRecipe(TransportPipes transportPipes, String recipeKey, ItemStack resultItem, Material... ingredients) {
ShapelessRecipe recipe = new ShapelessRecipe(new NamespacedKey(transportPipes, recipeKey), resultItem);
for (int i = 0; i < ingredients.length; i += 2) {
recipe.addIngredient(ingredients[i]);
}
return recipe;
}
public static CraftShapelessRecipe fromBukkitRecipe(ShapelessRecipe recipe) {
if (recipe instanceof CraftShapelessRecipe) {
return (CraftShapelessRecipe) recipe;
}
CraftShapelessRecipe ret = new CraftShapelessRecipe(recipe.getResult());
for (ItemStack ingred : recipe.getIngredientList()) {
ret.addIngredient(ingred.getType(), ingred.getDurability());
}
return ret;
}
public GlisteringMelonRecipeModule() {
setId("GlisteringMelonRecipe");
this.iconName = ICON_NAME;
this.icon.setType(Material.SPECKLED_MELON);
this.icon.setWeight(ModuleRegistry.CATEGORY_RECIPIES);
final ShapelessRecipe modified = new ShapelessRecipe(new ItemStack(Material.SPECKLED_MELON, 1))
.addIngredient(1, Material.GOLD_BLOCK)
.addIngredient(1, Material.MELON);
Bukkit.addRecipe(modified);
}
@Override
public ShapelessRecipe toBukkitRecipe(ItemStack result) {
return toBukkitRecipe(null, result);
}
public void addConflict(RecipeShapeless rec, ShapelessRecipe re){
conflictsShapeless.put(rec, re);
}
@Override
protected void setUp()
{
ShapelessRecipe recipe = new ShapelessRecipe(new ItemStack(Material.ARROW,3)).addIngredient(Material.FLINT).addIngredient(Material.STICK);
Bukkit.addRecipe(recipe);
}
private List<DyeColor> getColors(Recipe recipe) {
return recipe instanceof ShapedRecipe ? getColors(((ShapedRecipe) recipe).getIngredientMap().values())
: recipe instanceof ShapelessRecipe ? getColors(((ShapelessRecipe) recipe).getIngredientList())
: Lists.newArrayList();
}