use of org.spongepowered.api.item.recipe.crafting.Ingredient in project SpongeCommon by SpongePowered.
the class SpongeShapedCraftingRecipeBuilder method build.
@Override
public ShapedCraftingRecipe build(String id, Object plugin) {
checkState(!this.aisle.isEmpty(), "aisle has not been set");
checkState(!this.ingredientMap.isEmpty(), "no ingredients set");
checkState(!this.result.isEmpty(), "no result set");
checkNotNull(id, "id");
checkNotNull(id, "plugin");
PluginContainer container = SpongeImpl.getPluginContainer(plugin);
if (!id.startsWith(container.getId() + ":")) {
id = container.getId() + ":" + id;
}
Iterator<String> aisleIterator = this.aisle.iterator();
String aisleRow = aisleIterator.next();
int width = aisleRow.length();
int height = 1;
checkState(width > 0, "The aisle cannot be empty.");
while (aisleIterator.hasNext()) {
height++;
aisleRow = aisleIterator.next();
checkState(aisleRow.length() == width, "The aisle has an inconsistent width.");
}
String[] keys = this.aisle.toArray(new String[this.aisle.size()]);
Map<String, net.minecraft.item.crafting.Ingredient> ingredientsMap = this.ingredientMap.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), e -> IngredientUtil.toNative(e.getValue())));
// Default space to Empty Ingredient
ingredientsMap.putIfAbsent(" ", net.minecraft.item.crafting.Ingredient.EMPTY);
// Throws JsonException when pattern is not complete or defines unused Ingredients
NonNullList<net.minecraft.item.crafting.Ingredient> ingredients = ShapedRecipes.deserializeIngredients(keys, ingredientsMap, width, height);
return ((ShapedCraftingRecipe) new SpongeShapedRecipe(id, this.groupName, width, height, ingredients, ItemStackUtil.toNative(this.result)));
}
use of org.spongepowered.api.item.recipe.crafting.Ingredient in project SpongeCommon by SpongePowered.
the class RecipeTest method onInit.
@Listener
public void onInit(GamePreInitializationEvent event) {
final Ingredient s = Ingredient.of(STONE);
final Ingredient b = Ingredient.of(BED, WOOL);
final ItemStack item = ItemStack.of(BEDROCK, 1);
final DataTransactionResult trans = item.offer(Keys.ITEM_ENCHANTMENTS, Collections.singletonList(Enchantment.of(EnchantmentTypes.UNBREAKING, 1)));
if (trans.getType() != DataTransactionResult.Type.SUCCESS) {
this.plugin.getLogger().error("Could not build recipe output!");
}
final ShapedCraftingRecipe recipe = CraftingRecipe.shapedBuilder().rows().row(s, s, s).row(s, b, s).row(s, s, s).result(item).build("bedrock", this.plugin);
Sponge.getRegistry().getCraftingRecipeRegistry().register(recipe);
}
use of org.spongepowered.api.item.recipe.crafting.Ingredient in project LanternServer by LanternPowered.
the class LanternShapedCraftingRecipeBuilder method from.
@Override
public IShapedCraftingRecipe.Builder from(ShapedCraftingRecipe value) {
this.aisle.clear();
this.ingredientMap.clear();
this.groupName = value.getGroup().orElse(null);
for (int y = 0; y < value.getHeight(); y++) {
final StringBuilder row = new StringBuilder();
for (int x = 0; x < value.getWidth(); x++) {
final char symbol = (char) ('a' + x + y * value.getWidth());
row.append(symbol);
final Ingredient ingredient = value.getIngredient(x, y);
this.ingredientMap.put(symbol, ingredient);
}
this.aisle.add(row.toString());
}
this.resultProvider = ((LanternShapedCraftingRecipe) value).resultProvider;
return this;
}
use of org.spongepowered.api.item.recipe.crafting.Ingredient in project LanternServer by LanternPowered.
the class LanternShapedCraftingRecipeBuilder method row.
@Override
public RowsStep.ResultStep row(int skip, Ingredient... ingredients) {
int columns = ingredients.length + skip;
if (!this.aisle.isEmpty()) {
checkState(this.aisle.get(0).length() == columns, "The rows have an inconsistent width.");
}
final StringBuilder row = new StringBuilder();
for (int i = 0; i < skip; i++) {
row.append(" ");
}
int key = 'a' + columns * this.aisle.size();
for (Ingredient ingredient : ingredients) {
key++;
final char character = (char) key;
row.append(character);
this.ingredientMap.put(character, ingredient);
}
this.aisle.add(row.toString());
return this;
}
use of org.spongepowered.api.item.recipe.crafting.Ingredient in project LanternServer by LanternPowered.
the class LanternShapelessCraftingRecipe method match.
@Override
public Optional<Result> match(CraftingMatrix craftingMatrix, @Nullable World world, int flags) {
final int w = craftingMatrix.width();
final int h = craftingMatrix.height();
final int s = w * h;
// Check if all the ingredients can fit in the crafting grid
if (this.ingredients.size() > s) {
return Optional.empty();
}
final boolean resultItem = (flags & Flags.RESULT_ITEM) != 0;
final boolean remainingItems = (flags & Flags.REMAINING_ITEMS) != 0;
final List<Ingredient> ingredients = new ArrayList<>(this.ingredients);
// Generate a ingredient map that can be useful to generate a result item
final Multimap<Ingredient, ItemStack> ingredientItems = resultItem && !(this.resultProvider instanceof ConstantCraftingResultProvider) ? HashMultimap.create() : null;
final ImmutableList.Builder<ItemStackSnapshot> remainingItemsBuilder = remainingItems ? ImmutableList.builder() : null;
int times = -1;
int[][] itemQuantities = remainingItems ? new int[w][h] : null;
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
final ItemStack itemStack = craftingMatrix.get(i, j);
// Don't check empty item stacks
if (itemStack.isEmpty()) {
remainingItemsBuilder.add(ItemStackSnapshot.NONE);
continue;
}
final Iterator<Ingredient> it = ingredients.iterator();
Optional<ItemStack> remainingItem = Optional.empty();
boolean success = false;
while (it.hasNext()) {
final IIngredient ingredient = (IIngredient) it.next();
if (!ingredient.test(itemStack)) {
continue;
}
final int quantity = ingredient.getQuantity(itemStack);
if (quantity < itemStack.getQuantity()) {
continue;
}
itemQuantities[i][j] = quantity;
final int times1 = itemStack.getQuantity() / quantity;
if (times == -1 || times1 < times) {
times = times1;
}
if (ingredientItems != null) {
ingredientItems.put(ingredient, itemStack);
}
if (remainingItemsBuilder != null) {
remainingItem = ingredient.getRemainingItem(itemStack);
}
it.remove();
success = true;
break;
}
// A faulty input ingredient was found
if (!success) {
return Optional.empty();
}
if (remainingItemsBuilder != null) {
remainingItemsBuilder.add(remainingItem.map(ItemStack::createSnapshot).orElse(ItemStackSnapshot.NONE));
}
}
}
// Not all the ingredients were found
if (!ingredients.isEmpty()) {
return Optional.empty();
}
// Generate the result item
ItemStackSnapshot resultItemStack = null;
if (resultItem) {
resultItemStack = this.resultProvider.getSnapshot(craftingMatrix, ingredientItems == null ? null : new SimpleIngredientList(ingredientItems));
checkNotNull(resultItemStack, "Something funky happened.");
}
return Optional.of(new Result(resultItemStack, remainingItemsBuilder == null ? null : remainingItemsBuilder.build(), itemQuantities, times));
}
Aggregations