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

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

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

Inventory.setItem介绍

[英]Stores the ItemStack at the given index of the inventory.
[中]将ItemStack存储在给定的库存索引处。

代码示例

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

@Override
public void set(ItemStack itemStack) {
  if (direction == 0) {
    throw new IllegalStateException("Must call next or previous first");
  }
  int i = direction > 0 ? nextIndex - 1 : nextIndex;
  inventory.setItem(i, itemStack);
}

代码示例来源: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

/**
 * 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: GlowstoneMC/Glowstone

private void handleUnusedInputs() {
  for (int i = 0; i < getTopInventory().getSlots().size(); i++) {
    ItemStack itemStack = getOpenInventory().getItem(i);
    if (InventoryUtil.isEmpty(itemStack)) {
      continue;
    }
    if (isDroppableCraftingSlot(i)) {
      getOpenInventory().getBottomInventory().addItem(itemStack);
      getOpenInventory().getTopInventory().setItem(i, InventoryUtil.createEmptyStack());
    }
  }
}

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

@Override
public boolean dispense() {
  GlowBlock block = getBlock();
  int dispenseSlot = getDispenseSlot();
  if (dispenseSlot < 0) {
    block.getWorld().playEffect(block.getLocation(), Effect.CLICK1, 0);
    return false;
  }
  ItemStack origItems = getInventory().getItem(dispenseSlot);
  DispenseBehavior behavior = getDispenseBehavior(origItems.getType());
  ItemStack result = behavior.dispense(block, origItems);
  getInventory().setItem(dispenseSlot, result);
  return true;
}

代码示例来源:origin: stackoverflow.com

public Inventory inv;

public void openGUI(Player p){
  //format: null, size of inventory (must be divisible by 9), "GUI name"
  inv = Bukkit.createInventory(null, 9, "GUI Name");
  inv.setItem(0, new ItemStack(Material.DIAMOND);
  p.openInventory(inv);
}

代码示例来源: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: FunnyGuilds/FunnyGuilds

public void fillEmpty(final ItemStack itemStack) {
  Validate.notNull(itemStack, "ItemStack cannot be NULL!");
  for (int i = 0; i < inv.getSize(); i++) {
    if (inv.getItem(i) == null) {
      inv.setItem(i, itemStack);
    }
  }
}

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

private void clearSlot(Integer slot) {
  if (slot != null) {
    Inventory inventory = getInventory(slot);
    slot = getInventorySlot(slot);
    inventory.setItem(slot, null);
  }
}

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

public void setToNextFree(GuiItem item) {
  for (int i = 0; i < inv.getSize(); i++) {
    if (inv.getItem(i) == null) {
      this.items.put(i, item);
      this.inv.setItem(i, item.wrap());
      break;
    }
  }
}

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

public void fillEmpty(final Material material) {
  Validate.notNull(material, "Material cannot be NULL!");
  for (int i = 0; i < inv.getSize(); i++) {
    if (inv.getItem(i) == null) {
      inv.setItem(i, new ItemStack(material, 1, (short) 15));
    }
  }
}

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

public void setToNextFree(final GuiItem item, int start) {
  for (int i = start; i < inv.getSize(); i++) {
    if (inv.getItem(i) == null) {
      this.items.put(i, item);
      this.inv.setItem(i, item.wrap());
      break;
    }
  }
}

代码示例来源:origin: Dytanic/CloudNet

public Inventory create(MobConfig mobConfig, ServerMob mob)
{
  Inventory inventory = Bukkit.createInventory(null, mobConfig.getInventorySize(), ChatColor.translateAlternateColorCodes('&', mob.getDisplay() + NetworkUtils.SPACE_STRING));
  for (Map.Entry<Integer, MobItemLayout> mobItem : mobConfig.getDefaultItemInventory().entrySet())
    inventory.setItem(mobItem.getKey() - 1, transform(mobItem.getValue()));
  return inventory;
}

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

private Inventory inject(Location l, int[] slots) {
  int size = BlockStorage.getInventory(l).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: slots) {
    inv.setItem(slot, BlockStorage.getInventory(l).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;
}

代码示例来源:origin: Bkm016/TabooLib

public Inventory build() {
    Inventory inventory = Bukkit.createInventory(new MenuBuilderHolder(lock, items), rows, name);
    items.forEach((key, value) -> inventory.setItem(key, value.getItemStack()));
    return inventory;
  }
}

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

private Inventory inject(Location l) {
  int size = BlockStorage.getInventory(l).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(l).getItemInSlot(slot));
  }
  return inv;
}

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

private Inventory inject(Location l) {
  int size = BlockStorage.getInventory(l).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(l).getItemInSlot(slot));
  }
  return inv;
}

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

private static Inventory inject(Location l) {
  int size = BlockStorage.getInventory(l).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(l).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;
}

相关文章