下面列出了net.minecraft.util.NonNullList#size ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static MachineFuel findMatchingFuel(ResourceLocation list, NonNullList<ItemStack> input)
{
if (list.toString().equals("minecraft:vanilla"))
{
if (input.size() == 1 && !input.get(0).isEmpty())
{
ItemStack stack = input.get(0);
int burnTime = TileEntityFurnace.getItemBurnTime(stack);
if (burnTime > 0)
return new VanillaFurnaceFuel(stack, burnTime);
}
return MachineFuel.EMPTY;
}
return findMatchingFuel(getInstance(list).fuels, input);
}
/**
* Inspect a player's inventory to see whether they have enough items to form the supplied list of ItemStacks.<br>
* The ingredients list MUST be amalgamated such that no two ItemStacks contain the same type of item.
*
* @param player
* @param ingredients an amalgamated list of ingredients
* @return true if the player's inventory contains sufficient quantities of all the required items.
*/
public static boolean playerHasIngredients(EntityPlayerMP player, List<ItemStack> ingredients) {
NonNullList<ItemStack> main = player.inventory.mainInventory;
NonNullList<ItemStack> arm = player.inventory.armorInventory;
for (ItemStack isIngredient : ingredients) {
int target = isIngredient.getCount();
for (int i = 0; i < main.size() + arm.size() && target > 0; i++) {
ItemStack isPlayer = (i >= main.size()) ? arm.get(i - main.size()) : main.get(i);
if (isPlayer != null && isIngredient != null && itemStackIngredientsMatch(isPlayer, isIngredient))
target -= isPlayer.getCount();
}
if (target > 0)
return false; // Don't have enough of this.
}
return true;
}
/**
* Inspect a player's inventory to see whether they have enough items to form the supplied list of ItemStacks.<br>
* The ingredients list MUST be amalgamated such that no two ItemStacks contain the same type of item.
*
* @param player
* @param ingredients an amalgamated list of ingredients
* @return true if the player's inventory contains sufficient quantities of all the required items.
*/
public static boolean playerHasIngredients(EntityPlayerSP player, List<ItemStack> ingredients) {
NonNullList<ItemStack> main = player.inventory.mainInventory;
NonNullList<ItemStack> arm = player.inventory.armorInventory;
for (ItemStack isIngredient : ingredients) {
int target = isIngredient.getCount();
for (int i = 0; i < main.size() + arm.size() && target > 0; i++) {
ItemStack isPlayer = (i >= main.size()) ? arm.get(i - main.size()) : main.get(i);
if (isPlayer != null && isIngredient != null && itemStackIngredientsMatch(isPlayer, isIngredient))
target -= isPlayer.getCount();
}
if (target > 0)
return false; // Don't have enough of this.
}
return true;
}
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);
}
}
}
}
@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;
}
/**
* Checks if there is a matching ItemStack in the provided array of stacks
*/
public static boolean matchingStackFoundOnList(NonNullList<ItemStack> list,
@Nonnull ItemStack stackTemplate, boolean ignoreMeta, boolean ignoreNbt)
{
Item item = stackTemplate.getItem();
int meta = stackTemplate.getMetadata();
final int size = list.size();
for (int i = 0; i < size; i++)
{
ItemStack stackTmp = list.get(i);
if (stackTmp.isEmpty() || stackTmp.getItem() != item)
{
continue;
}
if (ignoreMeta == false && (meta != OreDictionary.WILDCARD_VALUE && stackTmp.getMetadata() != meta))
{
continue;
}
if (ignoreNbt == false && ItemStack.areItemStackTagsEqual(stackTemplate, stackTmp) == false)
{
continue;
}
return true;
}
return false;
}
public static MachineRecipe findMatchingRecipe(ResourceLocation list, NonNullList<ItemStack> input, List<FluidStack> inputFluid, World worldIn)
{
if (list.toString().equals("minecraft:vanilla"))
{
if (input.size() == 1 && !input.get(0).isEmpty())
{
return new VanillaFurnaceRecipe(FurnaceRecipes.instance().getSmeltingResult(input.get(0)));
} else
{
return MachineRecipe.EMPTY;
}
}
return findMatchingRecipe(getRecipes(list), input, inputFluid, worldIn);
}
public static MachineRecipe findMatchingRecipe(List<MachineRecipe> recipes, NonNullList<ItemStack> input, List<FluidStack> inputFluid, World worldIn)
{
for (MachineRecipe recipe : recipes)
{
if (input.size() == recipe.getInputStacks()
&& inputFluid.size() == recipe.getFluidStacks()
&& recipe.matches(input, inputFluid, worldIn))
{
return recipe;
}
}
return MachineRecipe.EMPTY;
}
public static MachineFuel findMatchingFuel(List<MachineFuel> fuels, NonNullList<ItemStack> input)
{
for (MachineFuel fuel : fuels)
{
if (input.size() == fuel.getFuelInput().size() && fuel.matches(input))
{
return fuel;
}
}
return MachineFuel.EMPTY;
}
@Override
public IRecipe parse(JsonContext context, JsonObject json)
{
String group = JsonUtils.getString(json, "group", "");
NonNullList<Ingredient> ings = NonNullList.create();
for (JsonElement ele : JsonUtils.getJsonArray(json, "ingredients"))
ings.add(CraftingHelper.getIngredient(ele, context));
if (ings.isEmpty())
throw new JsonParseException("No ingredients for shapeless recipe");
ItemStack itemstack = CraftingHelper.getItemStack(JsonUtils.getJsonObject(json, "result"), context);
int[] damage = new int[ings.size()];
if (JsonUtils.hasField(json, "damage"))
{
JsonArray array = JsonUtils.getJsonArray(json, "damage");
if (array.size() > damage.length)
throw new JsonParseException("Too many values for damage array: got " + array.size() + ", expected " + damage.length);
for (int i = 0; i < array.size(); i++)
{
JsonElement element = array.get(i);
if (!element.isJsonPrimitive() || !element.getAsJsonPrimitive().isNumber())
throw new JsonSyntaxException("Entry in damage array is not a number, got " + element);
damage[i] = element.getAsJsonPrimitive().getAsInt();
}
}
return new DamageableShapelessOreRecipe(group.isEmpty() ? null : new ResourceLocation(group), damage, ings, itemstack);
}
@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;
}
@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);
}
/**
* Manually attempt to remove ingredients from the player's inventory.<br>
*
* @param player
* @param ingredients
*/
public static void removeIngredientsFromPlayer(EntityPlayerMP player, List<ItemStack> ingredients) {
NonNullList<ItemStack> main = player.inventory.mainInventory;
NonNullList<ItemStack> arm = player.inventory.armorInventory;
for (ItemStack isIngredient : ingredients) {
int target = isIngredient.getCount();
for (int i = 0; i < main.size() + arm.size() && target > 0; i++) {
ItemStack isPlayer = (i >= main.size()) ? arm.get(i - main.size()) : main.get(i);
if (itemStackIngredientsMatch(isPlayer, isIngredient)) {
if (target >= isPlayer.getCount()) {
// Consume this stack:
target -= isPlayer.getCount();
if (i >= main.size())
arm.get(i - main.size()).setCount(0);
else
main.get(i).setCount(0);
} else {
isPlayer.setCount(isPlayer.getCount() - target);
target = 0;
}
}
}
ItemStack resultForReward = isIngredient.copy();
RewardForDiscardingItemImplementation.LoseItemEvent event = new RewardForDiscardingItemImplementation.LoseItemEvent(resultForReward);
MinecraftForge.EVENT_BUS.post(event);
}
}
@Override
public void consumeItems(CraftingGrid craftingGrid) {
NonNullList<ItemStack> remainder = recipe.getRemainingItems(new NovaCraftingGrid(craftingGrid));
for (int i = 0; i < remainder.size(); i++) {
Optional<Item> result = Optional.of(remainder.get(i)).filter(item -> !item.isEmpty()).map(ItemConverter.instance()::toNova);
if (!result.isPresent()) {
result = craftingGrid.getCrafting(i).filter(item -> item.count() > 1).map(item -> item.withAmount(item.count() - 1));
}
craftingGrid.setCrafting(i, result);
}
}
@Override
public ItemStack onTake(EntityPlayer thePlayer, ItemStack stack)
{
this.onCrafting(stack);
net.minecraftforge.common.ForgeHooks.setCraftingPlayer(thePlayer);
NonNullList<ItemStack> nonnulllist = CraftingManagerTFC.getInstance().getRemainingItems(this.craftMatrix, thePlayer.world);
net.minecraftforge.common.ForgeHooks.setCraftingPlayer(null);
for (int i = 0; i < nonnulllist.size(); ++i)
{
ItemStack itemstack = this.craftMatrix.getStackInSlot(i);
ItemStack itemstack1 = (ItemStack)nonnulllist.get(i);
if (!itemstack.isEmpty())
{
this.craftMatrix.decrStackSize(i, 1);
itemstack = this.craftMatrix.getStackInSlot(i);
}
if (!itemstack1.isEmpty())
{
if (itemstack.isEmpty())
{
this.craftMatrix.setInventorySlotContents(i, itemstack1);
}
else if (ItemStack.areItemsEqual(itemstack, itemstack1) && ItemStack.areItemStackTagsEqual(itemstack, itemstack1))
{
itemstack1.grow(itemstack.getCount());
this.craftMatrix.setInventorySlotContents(i, itemstack1);
}
else if (!this.player.inventory.addItemStackToInventory(itemstack1))
{
this.player.dropItem(itemstack1, false);
}
}
}
return stack;
}
/**
* Writes the ItemStacks in <b>items</b> to the NBTTagCompound <b>nbt</b>
* in a NBTTagList by the name <b>tagName</b>.
* @param nbt
* @param items
* @param tagName the NBTTagList tag name where the items will be written to
* @param keepExtraSlots set to true to append existing items in slots that are outside of the currently written slot range
*/
@Nonnull
public static NBTTagCompound writeItemsToTag(@Nonnull NBTTagCompound nbt, NonNullList<ItemStack> items,
@Nonnull String tagName, boolean keepExtraSlots)
{
int invSlots = items.size();
NBTTagList nbtTagList = createTagListForItems(items);
if (keepExtraSlots && nbt.hasKey(tagName, Constants.NBT.TAG_LIST))
{
// Read the old items and append any existing items that are outside the current written slot range
NBTTagList nbtTagListExisting = nbt.getTagList(tagName, Constants.NBT.TAG_COMPOUND);
final int count = nbtTagListExisting.tagCount();
for (int i = 0; i < count; i++)
{
NBTTagCompound tag = nbtTagListExisting.getCompoundTagAt(i);
int slotNum = tag.getShort("Slot");
if (slotNum >= invSlots)
{
nbtTagList.appendTag(tag);
}
}
}
// Write the items to the compound tag
if (nbtTagList.tagCount() > 0)
{
nbt.setTag(tagName, nbtTagList);
}
else
{
nbt.removeTag(tagName);
}
return nbt;
}
public static ItemStack[][] getFullRecipeInput(IRecipe recipe) {
final NonNullList<Ingredient> ingredients = recipe.getIngredients();
final int ingredientCount = ingredients.size();
final ItemStack[][] result = new ItemStack[ingredientCount][];
for (int i = 0; i < ingredientCount; i++)
result[i] = ingredients.get(i).getMatchingStacks();
return result;
}
/**
* 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) + ")");
}*/
}
}
@Listener
public void onRenderTooltip(EventRenderTooltip event) {
if (event.getItemStack() == null)
return;
final Minecraft mc = Minecraft.getMinecraft();
if (event.getItemStack().getItem() instanceof ItemShulkerBox) {
ItemStack shulker = event.getItemStack();
NBTTagCompound tagCompound = shulker.getTagCompound();
if (tagCompound != null && tagCompound.hasKey("BlockEntityTag", 10)) {
NBTTagCompound blockEntityTag = tagCompound.getCompoundTag("BlockEntityTag");
if (blockEntityTag.hasKey("Items", 9)) {
event.setCanceled(true); // cancel rendering the old tooltip
NonNullList<ItemStack> nonnulllist = NonNullList.<ItemStack>withSize(27, ItemStack.EMPTY);
ItemStackHelper.loadAllItems(blockEntityTag, nonnulllist); // load the itemstacks from the tag to the list
// store mouse/event coords
int x = event.getX();
int y = event.getY();
// translate to mouse x, y
GlStateManager.translate(x + 10, y - 5, 0);
GlStateManager.disableLighting();
GlStateManager.disableDepth();
// background
RenderUtil.drawRect(-3, -mc.fontRenderer.FONT_HEIGHT - 4, 9 * 16 + 3, 3 * 16 + 3, 0x99101010);
RenderUtil.drawRect(-2, -mc.fontRenderer.FONT_HEIGHT - 3, 9 * 16 + 2, 3 * 16 + 2, 0xFF202020);
RenderUtil.drawRect(0, 0, 9 * 16, 3 * 16, 0xFF101010);
// text
mc.fontRenderer.drawStringWithShadow(shulker.getDisplayName(), 0, -mc.fontRenderer.FONT_HEIGHT - 1, 0xFFFFFFFF);
GlStateManager.enableDepth();
mc.getRenderItem().zLevel = 150.0F;
RenderHelper.enableGUIStandardItemLighting();
// loop through items in shulker inventory
for (int i = 0; i < nonnulllist.size(); i++) {
ItemStack itemStack = nonnulllist.get(i);
int offsetX = (i % 9) * 16;
int offsetY = (i / 9) * 16;
mc.getRenderItem().renderItemAndEffectIntoGUI(itemStack, offsetX, offsetY);
mc.getRenderItem().renderItemOverlayIntoGUI(mc.fontRenderer, itemStack, offsetX, offsetY, null);
}
RenderHelper.disableStandardItemLighting();
mc.getRenderItem().zLevel = 0.0F;
GlStateManager.enableLighting();
// reverse the translate
GlStateManager.translate(-(x + 10), -(y - 5), 0);
}
}
if(this.middleClick.getValue()) {
if (Mouse.isButtonDown(2)) {
if (!this.clicked) {
final BlockShulkerBox shulkerBox = (BlockShulkerBox) Block.getBlockFromItem(shulker.getItem());
if (shulkerBox != null) {
final NBTTagCompound tag = shulker.getTagCompound();
if (tag != null && tag.hasKey("BlockEntityTag", 10)) {
final NBTTagCompound entityTag = tag.getCompoundTag("BlockEntityTag");
final TileEntityShulkerBox te = new TileEntityShulkerBox();
te.setWorld(mc.world);
te.readFromNBT(entityTag);
mc.displayGuiScreen(new GuiShulkerBox(mc.player.inventory, te));
}
}
}
this.clicked = true;
} else {
this.clicked = false;
}
}
}
}
/**
* Used to check if a recipe matches current crafting inventory
*/
@Override
public boolean matches(NonNullList<ItemStack> var1, World world)
{
ArrayList<Object> required = new ArrayList<Object>(input);
for (int x = 0; x < var1.size(); x++)
{
ItemStack slot = var1.get(x);
if (slot != ItemStack.EMPTY)
{
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)
{
if(!tempMatch(slot))
{
break;
}
inRecipe = true;
required.remove(next);
break;
}
}
if (!inRecipe)
{
return false;
}
}
}
return required.isEmpty();
}