use of net.minecraft.util.NonNullList in project MinecraftForge by MinecraftForge.
the class ShapelessOreRecipe method matches.
@SuppressWarnings("unchecked")
@Override
public boolean matches(InventoryCrafting var1, World world) {
NonNullList<Object> required = NonNullList.create();
required.addAll(input);
for (int x = 0; x < var1.getSizeInventory(); x++) {
ItemStack slot = var1.getStackInSlot(x);
if (!slot.isEmpty()) {
boolean inRecipe = false;
Iterator<Object> req = required.iterator();
while (req.hasNext()) {
boolean match = false;
Object next = req.next();
if (next instanceof ItemStack) {
match = OreDictionary.itemMatches((ItemStack) next, slot, false);
} else if (next instanceof List) {
Iterator<ItemStack> itr = ((List<ItemStack>) next).iterator();
while (itr.hasNext() && !match) {
match = OreDictionary.itemMatches(itr.next(), slot, false);
}
}
if (match) {
inRecipe = true;
required.remove(next);
break;
}
}
if (!inRecipe) {
return false;
}
}
}
return required.isEmpty();
}
use of net.minecraft.util.NonNullList 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 net.minecraft.util.NonNullList in project Charset by CharsetMC.
the class RecipeReplacement method replaceIngredient.
@Nullable
private Ingredient replaceIngredient(Ingredient ing) {
if (ing.getClass() == Ingredient.class || ing.getClass() == IngredientNBT.class) {
boolean checkNBT = ing.getClass() == IngredientNBT.class;
ItemStack[] matchingStacks = ing.getMatchingStacks();
ItemStack[] matchingStacksNew = null;
int replacementOreMatches = 0;
String replacementOre = null;
boolean dirty = false;
for (int j = 0; j < matchingStacks.length; j++) {
ItemStack stack = matchingStacks[j];
if (stack.isEmpty())
continue;
ItemStack newStack = null;
Object replacement = null;
for (int i : OreDictionary.getOreIDs(stack)) {
if (replaceableOres.containsKey(i)) {
replacement = replaceableOres.get(i);
break;
}
}
if (replacement == null) {
if (replacements.containsKey(stack)) {
replacement = replacements.get(stack);
} else if (!checkNBT && replaceableItems.containsKey(stack.getItem())) {
replacement = replaceableItems.get(stack.getItem());
}
}
if (replacement instanceof Item) {
newStack = new ItemStack((Item) replacement, stack.getCount(), stack.getItemDamage());
newStack.setTagCompound(stack.getTagCompound());
} else if (replacement instanceof ItemStack) {
newStack = ((ItemStack) replacements.get(stack)).copy();
newStack.setCount(stack.getCount());
} else if (replacement instanceof String) {
replacementOreMatches++;
replacementOre = (String) replacement;
}
if (newStack != null) {
if (!dirty) {
matchingStacksNew = new ItemStack[matchingStacks.length];
System.arraycopy(matchingStacks, 0, matchingStacksNew, 0, matchingStacks.length);
dirty = true;
}
matchingStacksNew[j] = newStack;
}
}
if (replacementOre != null && replacementOreMatches == matchingStacks.length) {
return new OreIngredient(replacementOre);
} else {
if (matchingStacksNew != null) {
return Ingredient.fromStacks(matchingStacksNew);
}
}
} else if (ing.getClass() == OreIngredient.class) {
try {
NonNullList<ItemStack> list = (NonNullList<ItemStack>) ORES_GETTER.invokeExact((OreIngredient) ing);
if (list.isEmpty()) {
return null;
}
TIntIterator it = replaceableOres.keySet().iterator();
while (it.hasNext()) {
int oreId = it.next();
NonNullList<ItemStack> oreList = OreDictionary.getOres(OreDictionary.getOreName(oreId));
if (!oreList.isEmpty() && list == oreList) {
Object replacement = replaceableOres.get(oreId);
if (replacement instanceof Item) {
return Ingredient.fromItem((Item) replacement);
} else if (replacement instanceof ItemStack) {
return Ingredient.fromStacks((ItemStack) replacement);
} else if (replacement instanceof String) {
return new OreIngredient((String) replacement);
}
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}
return null;
}
use of net.minecraft.util.NonNullList in project BaseMetals by MinecraftModDevelopmentMods.
the class ShieldUpgradeRecipe method getCraftingResult.
@Override
public ItemStack getCraftingResult(final InventoryCrafting inv) {
final Map<String, NonNullList<ItemStack>> plates = getPlates();
Pair<Map<Enchantment, Integer>, ItemStack> matchedBits = Pair.of(Collections.emptyMap(), ItemStack.EMPTY);
for (int i = 0; i < inv.getSizeInventory(); i++) {
final ItemStack curItem = inv.getStackInSlot(i);
if (curItem != null) {
matchedBits = matchAndFind(curItem, plates);
}
}
BaseMetals.logger.debug("Adding %d enchantments to output item", matchedBits.getLeft().size());
if (!(matchedBits.getRight().isEmpty())) {
EnchantmentHelper.setEnchantments(matchedBits.getLeft(), matchedBits.getRight());
}
return matchedBits.getRight();
}
use of net.minecraft.util.NonNullList in project BaseMetals by MinecraftModDevelopmentMods.
the class ShieldUpgradeRecipe method getPlates.
private Map<String, NonNullList<ItemStack>> getPlates() {
final Collection<MMDMaterial> allmats = Materials.getAllMaterials();
final int hardness = ((Float) Materials.getMaterialByName(materialName).getStat(MaterialStats.HARDNESS)).intValue();
final Map<String, NonNullList<ItemStack>> plates = new TreeMap<>();
for (final MMDMaterial mat : allmats) {
if (mat.getStat(MaterialStats.HARDNESS) >= hardness && (!mat.getName().equals(materialName))) {
final NonNullList<ItemStack> mats = OreDictionary.getOres(Oredicts.PLATE + mat.getCapitalizedName());
plates.put(mat.getName(), mats);
}
}
return plates;
}
Aggregations