use of org.spongepowered.api.item.recipe.crafting.Ingredient in project LanternServer by LanternPowered.
the class LanternShapedCraftingRecipe method matchAt.
/**
* Matches this recipe at the given start x
* and y coordinates within the {@link CraftingMatrix}.
*
* @param craftingMatrix The crafting matrix
* @param startX The initial x coordinate
* @param startY The initial y coordinate
* @return Whether the recipe matches
*/
@Nullable
private Result matchAt(CraftingMatrix craftingMatrix, int startX, int startY, int flags, int[][] itemQuantities) {
// Clear the quantities
fill(itemQuantities, 0);
final int cw = craftingMatrix.width();
final int ch = craftingMatrix.height();
final int rw = getWidth();
final int rh = getHeight();
final int ew = startX + rw;
final int eh = startY + rh;
// The recipe no longer fits within the grid when starting from the coordinates
if (ew > cw || eh > ch) {
return null;
}
final boolean resultItem = (flags & Flags.RESULT_ITEM) != 0;
// 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;
int times = -1;
for (int y = 0; y < ch; y++) {
for (int x = 0; x < cw; x++) {
final ItemStack itemStack = craftingMatrix.get(x, y);
final int i = x - startX;
final int j = y - startY;
final IIngredient ingredient = i < 0 || i >= rw || j < 0 || j >= rh ? null : this.ingredients[i][j];
if (ingredient == null) {
if (itemStack.isEmpty()) {
continue;
}
return null;
}
final int quantity = ingredient.getQuantity(itemStack);
if (!ingredient.test(itemStack) || itemStack.getQuantity() < quantity) {
return null;
}
itemQuantities[x][y] = quantity;
final int times1 = itemStack.getQuantity() / quantity;
if (times == -1 || times1 < times) {
times = times1;
}
if (ingredientItems != null) {
ingredientItems.put(ingredient, itemStack);
}
}
}
// 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.");
}
// Generate a list with all the remaining items, doing this for every
// slot, even empty ones, empty ones are added as a empty remaining
// item.
List<ItemStackSnapshot> remainingItemsList = null;
if ((flags & Flags.REMAINING_ITEMS) != 0) {
final List<ItemStackSnapshot> builder = new ArrayList<>();
for (int i = 0; i < ch * cw; i++) {
builder.add(ItemStackSnapshot.NONE);
}
for (int j = 0; j < rh; j++) {
for (int i = 0; i < rw; i++) {
final IIngredient ingredient = this.ingredients[i][j];
if (ingredient != null) {
final Optional<ItemStack> remainingItem = ingredient.getRemainingItem(craftingMatrix.get(i, j));
if (remainingItem.isPresent()) {
builder.set((j + startY) * cw + (i + startX), LanternItemStack.toSnapshot(remainingItem.get()));
}
}
}
}
remainingItemsList = ImmutableList.copyOf(builder);
}
return new Result(resultItemStack, remainingItemsList, itemQuantities, times);
}
use of org.spongepowered.api.item.recipe.crafting.Ingredient in project SpongeCommon by SpongePowered.
the class SpongeShapedCraftingRecipeBuilder method from.
@Override
public ShapedCraftingRecipe.Builder from(ShapedCraftingRecipe value) {
this.aisle.clear();
this.ingredientMap.clear();
this.groupName = "";
if (value instanceof ShapedRecipes) {
this.groupName = ((ShapedRecipes) value).group;
}
if (value != null) {
for (int y = 0; y < value.getHeight(); y++) {
String row = "";
for (int x = 0; x < value.getWidth(); x++) {
char symbol = (char) ('a' + x + y * value.getWidth());
row += symbol;
Ingredient ingredient = value.getIngredient(x, y);
this.ingredientMap.put(symbol, ingredient);
}
this.aisle.add(row);
}
this.result = value.getExemplaryResult().createStack();
} else {
this.result = null;
}
return this;
}
use of org.spongepowered.api.item.recipe.crafting.Ingredient in project SpongeCommon by SpongePowered.
the class SpongeShapedCraftingRecipeBuilder 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.");
}
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++;
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 modules-extra by CubeEngine.
the class ItemDuctManager method setup.
public void setup(PluginContainer plugin, ItemductConfig config) {
if (!this.init) {
DataRegistration.<DuctData, ImmutableDuctData>builder().dataClass(DuctData.class).immutableClass(ImmutableDuctData.class).builder(new DuctDataBuilder()).manipulatorId("duct").dataName("CubeEngine ItemDuct Data").buildAndRegister(plugin);
DuctData.FILTERS.getQuery();
Ingredient hopper = Ingredient.of(ItemTypes.HOPPER);
activatorItem = ItemStack.of(ItemTypes.HOPPER, 1);
activatorItem.offer(Keys.ITEM_ENCHANTMENTS, singletonList(Enchantment.builder().type(EnchantmentTypes.LOOTING).level(1).build()));
activatorItem.offer(Keys.DISPLAY_NAME, Text.of(TextColors.GOLD, "ItemDuct Activator"));
activatorItem.offer(Keys.HIDE_ENCHANTMENTS, true);
activatorItem.offer(new DuctData(config.activatorUses));
activatorItem.offer(Keys.ITEM_LORE, Collections.singletonList(Text.of("Uses: ", config.activatorUses)));
this.recipe = CraftingRecipe.shapedBuilder().rows().row(hopper, hopper, hopper).row(hopper, Ingredient.of(ItemTypes.DIAMOND), hopper).row(hopper, hopper, hopper).result(activatorItem.copy()).build("ItemDuctActivator", plugin);
Sponge.getRegistry().getCraftingRecipeRegistry().register(this.recipe);
activatorItem.offer(new DuctData(config.superActivatorUses));
activatorItem.offer(Keys.ITEM_LORE, Collections.singletonList(Text.of("Uses: Infinite")));
activatorItem.offer(Keys.DISPLAY_NAME, Text.of(TextColors.GOLD, "ItemDuct Super Activator"));
this.superRecipe = CraftingRecipe.shapedBuilder().rows().row(hopper, hopper, hopper).row(hopper, Ingredient.of(ItemTypes.NETHER_STAR), hopper).row(hopper, hopper, hopper).result(activatorItem.copy()).build("ItemDuctSuperActivator", plugin);
Sponge.getRegistry().getCraftingRecipeRegistry().register(this.superRecipe);
}
this.reload(config);
this.init = true;
}
use of org.spongepowered.api.item.recipe.crafting.Ingredient in project LanternServer by LanternPowered.
the class LanternSmeltingRecipeBuilder method build.
@Override
public ISmeltingRecipe build() {
check();
// Attempt to generate a id for the smelting recipe
String ingredient = this.exemplaryIngredient.getType().getId();
ingredient = ingredient.substring(ingredient.indexOf(':') + 1);
final ItemStackSnapshot exemplaryResult = getResultProvider().get(this.exemplaryIngredient).getResult();
String result = exemplaryResult.getType().getId();
result = result.substring(result.indexOf(':') + 1);
final String id = ingredient + "_to_" + result;
final int count = idCounters.computeIfAbsent(id, s -> 0) + 1;
idCounters.put(id, count);
return build0(id + "_" + count, Lantern.getGame().getImplementationPlugin());
}
Aggregations