org.bukkit.inventory.Inventory.getContents()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(220)

本文整理了Java中org.bukkit.inventory.Inventory.getContents()方法的一些代码示例,展示了Inventory.getContents()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Inventory.getContents()方法的具体详情如下:
包路径:org.bukkit.inventory.Inventory
类名称:Inventory
方法名:getContents

Inventory.getContents介绍

[英]Returns all ItemStacks from the inventory
[中]返回库存中的所有ItemStack

代码示例

代码示例来源:origin: GlowstoneMC/Glowstone

private int countAllItems(Inventory inventory, Material material, int data, int maxCount) {
  if (material == null) {
    return Arrays.stream(inventory.getContents())
      .filter(stack -> !InventoryUtil.isEmpty(stack)).mapToInt(ItemStack::getAmount)
      .sum();
  }
  int count = 0;
  for (ItemStack stack : inventory.getContents()) {
    if (stack.getType() == material && (data == -1 || data == stack.getData().getData())
      && (maxCount == -1 || maxCount == 0 || count < maxCount)) {
      if (maxCount == -1 || maxCount == 0) {
        count += stack.getAmount();
      } else {
        for (int i = 0; i < stack.getAmount(); i++) {
          if (count < maxCount) {
            count++;
          } else {
            return count;
          }
        }
      }
    }
  }
  return count;
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
  public void save(GlowMinecart entity, CompoundTag tag) {
    super.save(entity, tag);
    if (entity instanceof InventoryHolder) {
      InventoryHolder inv = (InventoryHolder) entity;
      if (inv.getInventory() != null) {
        tag.putCompoundList("Items",
            NbtSerialization.writeInventory(inv.getInventory().getContents(), 0));
      }
    }
    // todo
  }
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
public boolean update(boolean force, boolean applyPhysics) {
  ItemStack[] contents = getInventory().getContents();
  boolean result = super.update(force, applyPhysics);
  if (result) {
    getBlockEntity().setContents(contents);
    getBlockEntity().updateInRange();
  }
  return result;
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
  public boolean update(boolean force, boolean applyPhysics) {
    ItemStack[] contents = getBlockInventory().getContents();

    boolean result = super.update(force, applyPhysics);

    if (result) {
      getBlockEntity().setContents(contents);
      getBlockEntity().updateInRange();
    }

    return result;
  }
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
public boolean entityInteract(GlowPlayer player, InteractEntityMessage message) {
  if (message.getAction() == InteractEntityMessage.Action.ATTACK.ordinal()) {
    // todo: damage points
    if (this instanceof InventoryHolder) {
      InventoryHolder inv = (InventoryHolder) this;
      if (inv.getInventory() != null) {
        for (ItemStack drop : inv.getInventory().getContents()) {
          if (drop == null || drop.getType() == Material.AIR
              || drop.getAmount() < 1) {
            continue;
          }
          GlowItem item = world.dropItemNaturally(getLocation(), drop);
          item.setPickupDelay(30);
          item.setBias(player);
        }
      }
    }
    remove();
  }
  return true;
}

代码示例来源:origin: CitizensDev/CitizensAPI

/**
 * Gets the contents of an NPC's inventory.
 *
 * @return ItemStack array of an NPC's inventory contents
 */
public ItemStack[] getContents() {
  if (view != null) {
    return view.getContents();
  }
  return contents;
}

代码示例来源:origin: eccentricdevotion/TARDIS

private void saveCurrentConsole(Inventory inv, String uuid) {
    String serialized = TARDISSerializeInventory.itemStacksToString(inv.getContents());
    HashMap<String, Object> set = new HashMap<>();
    set.put("console", serialized);
    HashMap<String, Object> where = new HashMap<>();
    where.put("uuid", uuid);
    new QueryFactory(plugin).doSyncUpdate("storage", set, where);
  }
}

代码示例来源:origin: filoghost/ChestCommands

public static boolean containsAtLeast(Inventory inv, Material material, int minAmount) {
  int contained = 0;
  
  for (ItemStack item : inv.getContents()) {
    if (item != null && item.getType() == material) {
      contained += item.getAmount();
    }
  }
  
  return contained >= minAmount;
}

代码示例来源:origin: FunnyGuilds/FunnyGuilds

public static int getItemAmount(ItemStack item, Inventory inv) {
  int amount = 0;
  for (ItemStack is : inv.getContents()) {
    if (item.isSimilar(is)) {
      amount += is.getAmount();
    }
  }
  return amount;
}

代码示例来源:origin: ChestShop-authors/ChestShop-3

private static ItemStack[] getStorageContents(Inventory inventory) {
  if (legacyContents == null) {
    try {
      inventory.getStorageContents();
      legacyContents = false;
    } catch (NoSuchMethodError e) {
      legacyContents = true;
    }
  }
  return legacyContents ? inventory.getContents() : inventory.getStorageContents();
}

代码示例来源:origin: eccentricdevotion/TARDIS

private List<ItemStack> getOtherItems(Inventory inv) {
    List<ItemStack> items = new ArrayList<>();
    for (ItemStack is : inv.getContents()) {
      if (is != null) {
        Material m = is.getType();
        if (!m.equals(Material.MUSIC_DISC_CAT) && !m.equals(Material.MUSIC_DISC_MALL) && !m.equals(Material.AIR)) {
          items.add(is);
        }
      }
    }
    return items;
  }
}

代码示例来源:origin: garbagemule/MobArena

public RepairableContainer(BlockState state, boolean clear) {
  super(state);
  // Grab the inventory of the block
  Inventory inv = ((InventoryHolder) state).getInventory();
  ItemStack[] stacks = inv.getContents();
  // Manual copy is necessary due to "reduce to 0" bug in Bukkit
  contents = new ItemStack[stacks.length];
  for (int i = 0; i < contents.length; i++) {
    contents[i] = (stacks[i] != null) ? stacks[i].clone() : null;
  }
  
  // Clear the inventory if prompted
  if (clear) inv.clear();
}

代码示例来源:origin: drtshock/PlayerVaults

public void saveVault(Player player, Inventory inventory) {
  if (plugin.getInVault().containsKey(player.getUniqueId().toString())) {
    Inventory inv = Bukkit.createInventory(null, 6 * 9);
    inv.setContents(inventory.getContents().clone());
    if (inventory.getViewers().size() == 1) {
      VaultViewInfo info = plugin.getInVault().get(player.getUniqueId().toString());
      vaultManager.saveVault(inv, info.getVaultName(), info.getNumber());
      plugin.getOpenInventories().remove(info.toString());
    }
    plugin.getInVault().remove(player.getUniqueId().toString());
  }
}

代码示例来源:origin: elBukkit/MagicPlugin

@Override
public double getBalance(Mage mage, CasterProperties caster) {
  double balance = 0;
  Inventory inventory = mage.getInventory();
  for (ItemStack item : inventory.getContents()) {
    if (item != null && mage.getController().itemsAreEqual(this.item, item)) {
      balance += item.getAmount();
    }
  }
  return balance;
}

代码示例来源:origin: TheBusyBiscuit/Slimefun4

@EventHandler
public void onAnvil(InventoryClickEvent e) {
  if (e.getRawSlot() == 2 && e.getWhoClicked() instanceof Player && e.getInventory().getType() == InventoryType.ANVIL) {
  if (SlimefunManager.isItemSimiliar(e.getInventory().getContents()[0], SlimefunItems.ELYTRA, true)) return;
    if (SlimefunItem.getByItem(e.getInventory().getContents()[0]) != null && !SlimefunItem.isDisabled(e.getInventory().getContents()[0])) {
      e.setCancelled(true);
      Messages.local.sendTranslation((Player) e.getWhoClicked(), "anvil.not-working", true);
    }
  }
}

代码示例来源:origin: ProSavage/SavageFactions

@Override
public void setChestSize(int chestSize) {
  ItemStack[] contents = this.getChestInventory().getContents();
  chest = Bukkit.createInventory(null, chestSize, SavageFactions.plugin.color(SavageFactions.plugin.getConfig().getString("fchest.Inventory-Title")));
  chest.setContents(contents);
}

代码示例来源:origin: ProSavage/SavageFactions

public boolean inventoryContains(Inventory inventory, ItemStack item) {
  int count = 0;
  ItemStack[] items = inventory.getContents();
  for (int i = 0; i < items.length; i++) {
    if (items[i] != null && items[i].getType() == item.getType() && items[i].getDurability() == item.getDurability()) {
      count += items[i].getAmount();
    }
    if (count >= item.getAmount()) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: io.github.bedwarsrel/BedwarsRel-Common

public boolean canContainItem(Inventory inv, ItemStack item) {
 int space = 0;
 for (ItemStack stack : inv.getContents()) {
  if (stack == null) {
   space += item.getMaxStackSize();
  } else if (stack.getType() == item.getType()
    && stack.getDurability() == item.getDurability()) {
   space += item.getMaxStackSize() - stack.getAmount();
  }
 }
 return space >= item.getAmount();
}

代码示例来源:origin: FunnyGuilds/FunnyGuilds

public void open(HumanEntity entity) {
  Inventory inv = Bukkit.createInventory(entity, this.wrap().getSize(), this.wrap().getTitle());
  inv.setContents(this.wrap().getContents());
  entity.openInventory(inv);
}

代码示例来源:origin: BedwarsRel/BedwarsRel

public boolean canContainItem(Inventory inv, ItemStack item) {
 int space = 0;
 for (ItemStack stack : inv.getContents()) {
  if (stack == null) {
   space += item.getMaxStackSize();
  } else if (stack.getType() == item.getType()
    && stack.getDurability() == item.getDurability()) {
   space += item.getMaxStackSize() - stack.getAmount();
  }
 }
 return space >= item.getAmount();
}

相关文章