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

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

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

Inventory.getSize介绍

[英]Returns the size of the inventory
[中]返回库存的大小

代码示例

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

@Override
public boolean hasNext() {
  return nextIndex < inventory.getSize();
}

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

/**
 * Check the total number of slots in this view, combining the upper and
 * lower inventories.
 * <p>
 * Note though that it's possible for this to be greater than the sum of
 * the two inventories if for example some slots are not being used.
 *
 * @return The total size
 */
public final int countSlots() {
  return getTopInventory().getSize() + getBottomInventory().getSize();
}

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

/**
 * Get a random slot index in an Inventory.
 *
 * @param random a Random instance
 * @param inventory the inventory
 * @param ignoreEmpty whether to skip empty items in the inventory
 * @return the index of a random slot in the inventory, -1 if no possible slot was found
 */
public static int getRandomSlot(Random random, Inventory inventory, boolean ignoreEmpty) {
  if (!ignoreEmpty) {
    return random.nextInt(inventory.getSize());
  }
  List<Integer> notEmpty = new ArrayList<>();
  for (int i = 0; i < inventory.getSize(); i++) {
    if (!isEmpty(inventory.getItem(i))) {
      notEmpty.add(i);
    }
  }
  if (notEmpty.isEmpty()) {
    return -1;
  }
  return notEmpty.get(random.nextInt(notEmpty.size()));
}

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

private ItemStack getFirstItem(ContainerEntity container) {
  Inventory inventory = container.getInventory();
  for (int i = 0; i < inventory.getSize(); i++) {
    if (inventory.getItem(i) == null || inventory.getItem(i).getType() == null) {
      continue;
    }
    return inventory.getItem(i);
  }
  return null;
}

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

@Override
public void load(GlowMinecart entity, CompoundTag tag) {
  super.load(entity, tag);
  if (entity instanceof InventoryHolder) {
    Inventory inventory = ((InventoryHolder) entity).getInventory();
    if (inventory != null) {
      tag.readCompoundList("Items",
        items -> inventory.setContents(NbtSerialization.readInventory(
          items, 0, inventory.getSize())));
    }
  }
  // todo
}

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

/**
 * Populates a container with random items.
 *
 * @param random the PRNG to use
 * @param state the block state for a container block
 * @param maxStacks the maximum number of slots to fill
 * @return true if successful (currently always true)
 */
public boolean fillContainer(Random random, BlockState state, int maxStacks) {
  if (state.getBlock().getState() instanceof InventoryHolder) {
    Inventory inventory = ((InventoryHolder) state.getBlock().getState()).getInventory();
    int size = inventory.getSize();
    for (int i = 0; i < maxStacks; i++) {
      RandomAmountItem item = getRandomItem(random);
      if (item != null) {
        for (ItemStack stack : item.getItemStacks(random)) {
          // slot can be overriden hence maxStacks can be less than what's expected
          inventory.setItem(random.nextInt(size), stack);
        }
      }
    }
  } else {
    throw new IllegalArgumentException("Bad container type");
  }
  return true;
}

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

/**
 * Converts a raw slot ID into its local slot ID into whichever of the two
 * inventories the slot points to.
 * <p>
 * If the raw slot refers to the upper inventory, it will be returned
 * unchanged and thus be suitable for getTopInventory().getItem(); if it
 * refers to the lower inventory, the output will differ from the input
 * and be suitable for getBottomInventory().getItem().
 *
 * @param rawSlot The raw slot ID.
 * @return The converted slot ID.
 */
public final int convertSlot(int rawSlot) {
  int numInTop = getTopInventory().getSize();
  if (rawSlot < numInTop) {
    return rawSlot;
  }
  int slot = rawSlot - numInTop;
  if (getPlayer().getGameMode() == GameMode.CREATIVE && getType() == InventoryType.PLAYER) {
    return slot;
  }
  if (getType() == InventoryType.CRAFTING) {
    if(slot < 4) return 39 - slot;
    else slot -= 4;
  }
  if (slot >= 27) slot -= 27;
  else slot += 9;
  return slot;
}

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

/**
 * Gets one item in this inventory view by its raw slot ID.
 *
 * @param slot The ID as returned by InventoryClickEvent.getRawSlot()
 * @return The item currently in the slot.
 */
public ItemStack getItem(int slot) {
  if (slot == OUTSIDE) {
    return null;
  }
  if (slot < getTopInventory().getSize()) {
    return getTopInventory().getItem(convertSlot(slot));
  } else {
    return getBottomInventory().getItem(convertSlot(slot));
  }
}

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

/**
 * Sets one item in this inventory view by its raw slot ID.
 * <p>
 * Note: If slot ID -999 is chosen, it may be expected that the item is
 * dropped on the ground. This is not required behaviour, however.
 *
 * @param slot The ID as returned by InventoryClickEvent.getRawSlot()
 * @param item The new item to put in the slot, or null to clear it.
 */
public void setItem(int slot, ItemStack item) {
  if (slot != OUTSIDE) {
    if (slot < getTopInventory().getSize()) {
      getTopInventory().setItem(convertSlot(slot),item);
    } else {
      getBottomInventory().setItem(convertSlot(slot),item);
    }
  } else {
    getPlayer().getWorld().dropItemNaturally(getPlayer().getLocation(), item);
  }
}

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

@Override
public void load(T entity, CompoundTag tag) {
  super.load(entity, tag);
  tag.readInt("XpSeed", entity::setXpSeed);
  tag.readInt("playerGameType", gameType -> {
    GlowServer server = (GlowServer) ServerProvider.getServer();
    if (!server.getConfig().getBoolean(ServerConfig.Key.FORCE_GAMEMODE)) {
      GameMode mode = GameMode.getByValue(gameType);
      if (mode != null) {
        entity.setGameMode(mode);
      }
    } else {
      entity.setGameMode(server.getDefaultGameMode());
    }
  });
  tag.readInt("SelectedItemSlot", entity.getInventory()::setHeldItemSlot);
  // Sleeping and SleepTimer are ignored on load.
  tag.readCompoundList("Inventory", items -> {
    PlayerInventory inventory = entity.getInventory();
    inventory.setStorageContents(
      NbtSerialization.readInventory(items, 0, inventory.getSize() - 5));
    inventory.setArmorContents(NbtSerialization.readInventory(items, 100, 4));
    inventory.setExtraContents(NbtSerialization.readInventory(items, -106, 1));
  });
  tag.readCompoundList("EnderItems", items -> {
    Inventory inventory = entity.getEnderChest();
    inventory.setContents(NbtSerialization.readInventory(items, 0, inventory.getSize()));
  });
}

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

.getTopInventory().getSize()
|| ((GlowInventory) view.getTopInventory()).getSlot(viewSlot).getType()
!= SlotType.RESULT) {

代码示例来源:origin: me.lucko/helper

public void fillWith(Item item) {
  Objects.requireNonNull(item, "item");
  for (int i = 0; i < this.inventory.getSize(); ++i) {
    setItem(i, item);
  }
}

代码示例来源:origin: lucko/helper

public Slot getSlot(int slot) {
  if (slot < 0 || slot >= this.inventory.getSize()) {
    throw new IllegalArgumentException("Invalid slot id: " + slot);
  }
  return this.slots.computeIfAbsent(slot, i -> new SimpleSlot(this, i));
}

代码示例来源:origin: jiongjionger/NeverLag

public GUIMaker fillItem(ItemStack item, int[]... pos) {
  for (int[] nowpos : pos) {
    if (nowpos.length == 2) {
      int slot = (nowpos[1] - 1) * 9 + nowpos[0] - 1;
      if (slot > this.inv.getSize() - 1) {
        return this;
      }
      this.inv.setItem(slot, item);
    }
  }
  return this;
}

代码示例来源:origin: jiongjionger/NeverLag

public GUIMaker fillItem(ItemStack item, int slot) {
  if (slot > this.inv.getSize() - 1) {
    return this;
  }
  this.inv.setItem(slot, item);
  return this;
}

代码示例来源:origin: jiongjionger/NeverLag

public GUIMaker fillItem(ItemStack item, int x, int y) {
  int slot = (y - 1) * 9 + x - 1;
  if (slot > this.inv.getSize() - 1) {
    return this;
  }
  this.inv.setItem(slot, item);
  return this;
}

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

protected Inventory getDisplayInventory() {
  if (displayInventory == null || displayInventory.getSize() != getInventorySize()) {
    displayInventory = CompatibilityUtils.createInventory(null, getInventorySize(), getInventoryTitle());
  }
  return displayInventory;
}

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

private void saveContents(Entity entity) {
  if (entity instanceof Player) {
    contents = ((Player) entity).getInventory().getContents();
    npc.getTrait(Equipment.class).setItemInHand(contents[0]);
  }
  if (view != null) {
    for (int i = 0; i < view.getSize(); i++) {
      view.setItem(i, contents[i]);
    }
  }
}

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

private Inventory inject(Block b) {
  int size = BlockStorage.getInventory(b).toInventory().getSize();
  Inventory inv = Bukkit.createInventory(null, size);
  for (int i = 0; i < size; i++) {
    inv.setItem(i, new CustomItem(Material.COMMAND_BLOCK, " &4ALL YOUR PLACEHOLDERS ARE BELONG TO US", 0));
  }
  for (int slot: getOutputSlots()) {
    inv.setItem(slot, BlockStorage.getInventory(b).getItemInSlot(slot));
  }
  return inv;
}

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

private Inventory inject(Block b) {
  int size = BlockStorage.getInventory(b).toInventory().getSize();
  Inventory inv = Bukkit.createInventory(null, size);
  for (int i = 0; i < size; i++) {
    inv.setItem(i, new CustomItem(Material.COMMAND_BLOCK, " &4ALL YOUR PLACEHOLDERS ARE BELONG TO US", 0));
  }
  for (int slot: getOutputSlots()) {
    inv.setItem(slot, BlockStorage.getInventory(b).getItemInSlot(slot));
  }
  return inv;
}

相关文章