Java Code Examples for net.minecraft.util.NonNullList#set()

The following examples show how to use net.minecraft.util.NonNullList#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InventoryUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a copy of the whole inventory and returns it in a new NonNullList.
 * @param inv
 * @return a NonNullList containing a copy of the entire inventory
 */
public static NonNullList<ItemStack> createInventorySnapshot(IItemHandler inv)
{
    final int invSize = inv.getSlots();
    NonNullList<ItemStack> items = NonNullList.withSize(invSize, ItemStack.EMPTY);

    for (int i = 0; i < invSize; i++)
    {
        ItemStack stack = inv.getStackInSlot(i);

        if (stack.isEmpty() == false)
        {
            items.set(i, stack.copy());
        }
    }

    return items;
}
 
Example 2
Source File: CraftShapedRecipe.java    From Kettle with GNU General Public License v3.0 6 votes vote down vote up
public void addToCraftingManager() {
    String[] shape = this.getShape();
    Map<Character, ItemStack> ingred = this.getIngredientMap();
    int width = shape[0].length();
    NonNullList<Ingredient> data = NonNullList.withSize(shape.length * width, Ingredient.EMPTY);

    for (int i = 0; i < shape.length; i++) {
        String row = shape[i];
        for (int j = 0; j < row.length(); j++) {
            data.set(i * width + j, Ingredient.fromStacks(new net.minecraft.item.ItemStack[]{CraftItemStack.asNMSCopy(ingred.get(row.charAt(j)))}));
        }
    }
    // TODO: Check if it's correct way to register recipes
    ForgeRegistries.RECIPES.register(new ShapedRecipes("", width, shape.length, data, CraftItemStack.asNMSCopy(this.getResult())));
    // CraftingManager.register(CraftNamespacedKey.toMinecraft(this.getKey()), new ShapedRecipes("", width, shape.length, data, CraftItemStack.asNMSCopy(this.getResult())));
}
 
Example 3
Source File: RecipeShapedFluid.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) {
	NonNullList<ItemStack> remains = ForgeHooks.defaultRecipeGetRemainingItems(inv);
	for (int i = 0; i < height * width; i++) {
		ItemStack stack = inv.getStackInSlot(i);
		NonNullList<Ingredient> matchedIngredients = this.input;
		if (matchedIngredients.get(i) instanceof IngredientFluidStack) {
			if (!stack.isEmpty()) {
				ItemStack copy = stack.copy();
				copy.setCount(1);
				remains.set(i, copy);
			}
			IFluidHandlerItem handler = FluidUtil.getFluidHandler(remains.get(i));
			if (handler != null) {
				FluidStack fluid = ((IngredientFluidStack) matchedIngredients.get(i)).getFluid();
				handler.drain(fluid.amount, true);
				remains.set(i, handler.getContainer());
			}
		}
	}
	return remains;
}
 
Example 4
Source File: RecipeCrudeHaloInfusion.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Nonnull
@Override
public NonNullList<ItemStack> getRemainingItems(@Nonnull InventoryCrafting inv) {
	NonNullList<ItemStack> remainingItems = ForgeHooks.defaultRecipeGetRemainingItems(inv);

	ItemStack gluestick;
	for (int i = 0; i < inv.getSizeInventory(); i++) {
		ItemStack stack = inv.getStackInSlot(i);
		if (stack.getItem() == Items.SLIME_BALL) {
			gluestick = stack.copy();
			remainingItems.set(i, gluestick);
			break;
		}
	}

	return remainingItems;
}
 
Example 5
Source File: RecipeJam.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Nonnull
@Override
public NonNullList<ItemStack> getRemainingItems(@Nonnull InventoryCrafting inv) {
	NonNullList<ItemStack> remainingItems = ForgeHooks.defaultRecipeGetRemainingItems(inv);

	ItemStack sword;
	for (int i = 0; i < inv.getSizeInventory(); i++) {
		ItemStack stack = inv.getStackInSlot(i);
		if (stack.getItem() == Items.GOLDEN_SWORD) {
			sword = stack.copy();
			sword.setItemDamage(sword.getItemDamage() + 1);
			if (sword.getItemDamage() > sword.getMaxDamage()) sword = null;
			if (sword != null) {
				remainingItems.set(i, sword);
			}
			break;
		}
	}

	return remainingItems;
}
 
Example 6
Source File: RecipeShapelessFluid.java    From Wizardry with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) {
	NonNullList<ItemStack> remains = super.getRemainingItems(inv);
	for (int i = 0; i < remains.size(); i++) {
		ItemStack stack = inv.getStackInSlot(i);
		ItemStack remain = remains.get(i);
		if (!stack.isEmpty() && remain.isEmpty() && stack.getItem() instanceof UniversalBucket) {
			ItemStack empty = ((UniversalBucket) stack.getItem()).getEmpty();
			if (!empty.isEmpty())
				remains.set(i, empty.copy());
		}
	}
	return remains;
}
 
Example 7
Source File: CustomRecipeBase.java    From OpenModsLib with MIT License 5 votes vote down vote up
@Override
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) {
	NonNullList<ItemStack> result = NonNullList.withSize(inv.getSizeInventory(), ItemStack.EMPTY);

	for (int i = 0; i < result.size(); ++i) {
		ItemStack itemstack = inv.getStackInSlot(i);
		result.set(i, ForgeHooks.getContainerItem(itemstack));
	}

	return result;
}
 
Example 8
Source File: NBTUtils.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Reads the stored items from the provided NBTTagCompound, from a NBTTagList by the name <b>tagName</b>
 * and writes them to the provided list of ItemStacks <b>items</b>.<br>
 * <b>NOTE:</b> The list should be initialized to be large enough for all the stacks to be read!
 * @param tag
 * @param items
 * @param tagName
 */
public static void readStoredItemsFromTag(@Nonnull NBTTagCompound nbt, NonNullList<ItemStack> items, @Nonnull String tagName)
{
    if (nbt.hasKey(tagName, Constants.NBT.TAG_LIST) == false)
    {
        return;
    }

    NBTTagList nbtTagList = nbt.getTagList(tagName, Constants.NBT.TAG_COMPOUND);
    int num = nbtTagList.tagCount();
    int listSize = items.size();

    for (int i = 0; i < num; ++i)
    {
        NBTTagCompound tag = nbtTagList.getCompoundTagAt(i);
        int slotNum = tag.getShort("Slot");

        if (slotNum >= 0 && slotNum < listSize)
        {
            items.set(slotNum, loadItemStackFromTag(tag));
        }
        /*else
        {
            EnderUtilities.logger.warn("Failed to read items from NBT, invalid slot: " + slotNum + " (max: " + (items.length - 1) + ")");
        }*/
    }
}
 
Example 9
Source File: ContainerEnderUtilities.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Syncs the locked status and the current template ItemStack in a lockable inventory.
 * The current values are cached into the provided boolean and ItemStack arrays.
 * The locked status is sent via a sendProgressBarUpdate, using the id <b>progressBarId</b>.
 * The value for that is in the form '(locked ? 0x8000 : 0) | slot'.
 *
 * @param inv the lockable inventory to sync the locked status and template stacks from
 * @param typeId The id that is used in the MessageSyncCustomSlot packet
 * @param progressBarId The id to use in the sendProgressBarUpdate for the locked status
 * @param lockedLast an array for caching the locked status
 * @param templateStacksLast a list for caching the template stacks
 */
protected void syncLockableSlots(ItemStackHandlerLockable inv, int typeId, int progressBarId,
        boolean[] lockedLast, NonNullList<ItemStack> templateStacksLast)
{
    final int numSlots = inv.getSlots();

    for (int slot = 0; slot < numSlots; slot++)
    {
        boolean locked = inv.isSlotLocked(slot);

        if (lockedLast[slot] != locked)
        {
            for (int i = 0; i < this.listeners.size(); i++)
            {
                this.listeners.get(i).sendWindowProperty(this, progressBarId, (locked ? 0x8000 : 0) | slot);
            }

            lockedLast[slot] = locked;
        }

        ItemStack templateStack = inv.getTemplateStackInSlot(slot);

        if (InventoryUtils.areItemStacksEqual(templateStacksLast.get(slot), templateStack) == false)
        {
            for (int i = 0; i < this.listeners.size(); i++)
            {
                IContainerListener listener = this.listeners.get(i);

                if (listener instanceof EntityPlayerMP)
                {
                    PacketHandler.INSTANCE.sendTo(
                        new MessageSyncCustomSlot(this.windowId, typeId, slot, templateStack), (EntityPlayerMP) listener);
                }
            }

            templateStacksLast.set(slot, templateStack.copy());
        }
    }
}
 
Example 10
Source File: Helper.java    From TFC2 with GNU General Public License v3.0 5 votes vote down vote up
public static NonNullList<ItemStack> readStackArrayFromNBTList(NBTTagList list, int size)
{
	NonNullList<ItemStack> out = NonNullList.<ItemStack>withSize(size, ItemStack.EMPTY);

	for(int i = 0; i < list.tagCount(); i++)
	{
		NBTTagCompound tag = list.getCompoundTagAt(i);
		byte byte0 = tag.getByte("Slot");
		if(byte0 >= 0 && byte0 < size)
			out.set(byte0, new ItemStack(tag));
	}
	return out;
}
 
Example 11
Source File: CreateRecipe.java    From EnderStorage with MIT License 5 votes vote down vote up
@Nullable
@Override
public CreateRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
    String s = buffer.readString(32767);
    NonNullList<Ingredient> ingredients = NonNullList.withSize(3 * 3, Ingredient.EMPTY);

    for (int k = 0; k < ingredients.size(); ++k) {
        ingredients.set(k, Ingredient.read(buffer));
    }

    ItemStack result = buffer.readItemStack();
    return new CreateRecipe(recipeId, s, result, ingredients);
}
 
Example 12
Source File: RecipeUnmountPearl.java    From Wizardry with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Nonnull
@Override
public NonNullList<ItemStack> getRemainingItems(@Nonnull InventoryCrafting inv) {
	NonNullList<ItemStack> remainingItems = ForgeHooks.defaultRecipeGetRemainingItems(inv);

	for (int i = 0; i < inv.getSizeInventory(); i++) {
		ItemStack stack = inv.getStackInSlot(i);
		if (stack.getItem() instanceof IPearlSwappable) {
			remainingItems.set(i, new ItemStack(stack.getItem()));
		}
	}

	return remainingItems;
}
 
Example 13
Source File: ItemUtils.java    From TofuCraftReload with MIT License 5 votes vote down vote up
public static void growOrSetInventoryItem(NonNullList<ItemStack> inventory, ItemStack toSet, int index) {
    ItemStack stack = inventory.get(index);
    if (stack.isEmpty()) {
        inventory.set(index, toSet);
    } else {
        stack.grow(toSet.getCount());
    }
}
 
Example 14
Source File: DamageableShapelessOreRecipe.java    From customstuff4 with GNU General Public License v3.0 5 votes vote down vote up
@Nonnull
@Override
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv)
{
    NonNullList<ItemStack> items = NonNullList.withSize(inv.getSizeInventory(), ItemStack.EMPTY);

    matches(inv, null);

    for (int i = 0; i < invSlots.length; i++)
    {
        int amount = damageAmounts[i];
        int invIndex = invSlots[i];
        if (amount > 0)
        {
            ItemStack stack = inv.getStackInSlot(invIndex).copy();
            stack.setItemDamage(stack.getItemDamage() + amount);
            if (stack.getItemDamage() > stack.getMaxDamage())
            {
                stack = ForgeHooks.getContainerItem(stack);
            }
            items.set(invIndex, stack);
        } else
        {
            items.set(invIndex, ForgeHooks.getContainerItem(inv.getStackInSlot(invIndex)));
        }
    }

    return items;
}
 
Example 15
Source File: DamageableShapedOreRecipe.java    From customstuff4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv)
{
    NonNullList<ItemStack> items = NonNullList.withSize(inv.getSizeInventory(), ItemStack.EMPTY);

    getMatch(inv);

    int[] amounts = getAmounts(wasMirrored);

    for (int col = 0; col < getWidth(); col++)
    {
        for (int row = 0; row < getHeight(); row++)
        {
            int amountIndex = col + row * getWidth();
            int invIndex = matchX + col + (row + matchY) * inv.getWidth();
            int amount = amounts[amountIndex];
            if (amount > 0)
            {
                ItemStack stack = inv.getStackInSlot(invIndex).copy();
                stack.setItemDamage(stack.getItemDamage() + amount);
                if (stack.getItemDamage() > stack.getMaxDamage())
                {
                    stack = ForgeHooks.getContainerItem(stack);
                }
                items.set(invIndex, stack);
            } else
            {
                items.set(invIndex, ForgeHooks.getContainerItem(inv.getStackInSlot(invIndex)));
            }
        }
    }

    return items;
}
 
Example 16
Source File: CraftShapelessRecipe.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public void addToCraftingManager() {
    List<ItemStack> ingred = this.getIngredientList();
    NonNullList<Ingredient> data = NonNullList.withSize(ingred.size(), Ingredient.EMPTY);
    for (int i = 0; i < ingred.size(); i++) {
        data.set(i, Ingredient.fromStacks(new net.minecraft.item.ItemStack[]{CraftItemStack.asNMSCopy(ingred.get(i))}));
    }
    // TODO: Check if it's correct way to register recipes
    ForgeRegistries.RECIPES.register(new ShapelessRecipes("", CraftItemStack.asNMSCopy(this.getResult()), data));
    // CraftingManager.a(CraftNamespacedKey.toMinecraft(this.getKey()), new ShapelessRecipes("", CraftItemStack.asNMSCopy(this.getResult()), data));
}
 
Example 17
Source File: GTContainerWorktable.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public boolean isCurrentListEqual() {
	NonNullList<ItemStack> copyList = NonNullList.<ItemStack>withSize(9, ItemStack.EMPTY);
	for (int i = 0; i < this.craftMatrix.getSizeInventory(); ++i) {
		ItemStack mSlot = this.craftMatrix.getStackInSlot(i);
		copyList.set(i, mSlot);
	}
	return copyList.equals(this.block.craftingInventory);
}
 
Example 18
Source File: CustomItemReturnShapedOreRecipeRecipe.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) {
    NonNullList<ItemStack> remainingItems = super.getRemainingItems(inv);
    for (int i = 0; i < remainingItems.size(); i++) {
        if (!remainingItems.get(i).isEmpty()) continue;
        ItemStack stackInSlot = inv.getStackInSlot(i);
        //if specified item should be returned back, copy it with amount 1 and add to remaining items
        if (shouldItemReturn(stackInSlot)) {
            ItemStack remainingItem = GTUtility.copyAmount(1, stackInSlot);
            remainingItems.set(i, remainingItem);
        }
    }
    return remainingItems;
}
 
Example 19
Source File: ArmorHooks.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void damageArmor(float damage, EntityLivingBase entity, NonNullList<ItemStack> inventory, DamageSource damageSource) {
    double armorDamage = Math.max(1.0F, damage / 4.0F);
    for (int i = 0; i < inventory.size(); i++) {
        ItemStack itemStack = inventory.get(i);
        if (itemStack.getItem() instanceof IArmorItem) {
            ((IArmorItem) itemStack.getItem()).damageArmor(entity, itemStack, damageSource, (int) armorDamage, i);
            if (inventory.get(i).getCount() == 0) {
                inventory.set(i, ItemStack.EMPTY);
            }
        }
    }
}