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

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

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

ItemStack.getData介绍

[英]Gets the MaterialData for this stack of items
[中]获取此项堆栈的MaterialData

代码示例

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

@Override
protected float getSaturation(ItemStack stack) {
  byte data = stack.getData().getData();
  switch (data) {
    case 0:
      return 6f;
    case 1:
      return 9.6f;
    default:
      throw new IllegalArgumentException("Cannot find fish(350) for data: " + data);
  }
}

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

@Override
  protected int getFoodLevel(ItemStack stack) {
    byte data = stack.getData().getData();
    switch (data) {
      case 0:
        return 5;
      case 1:
        return 6;
      default:
        throw new IllegalArgumentException("Cannot find fish(350) for data: " + data);
    }
  }
}

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

@Override
protected float getSaturation(ItemStack stack) {
  byte data = stack.getData().getData();
  switch (data) {
    case 0:
    case 1:
      return 0.4f;
    case 2:
    case 3:
      return 0.2f;
    default:
      throw new IllegalArgumentException("Cannot find fish(349) for data: " + data);
  }
}

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

@Override
protected int getFoodLevel(ItemStack stack) {
  byte data = stack.getData().getData();
  switch (data) {
    case 0:
    case 1:
      return 2;
    case 2:
    case 3:
      return 1;
    default:
      throw new IllegalArgumentException("Cannot find fish(349) for data: " + data);
  }
}

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

/**
 * Called when a block is placed to calculate what the block will become.
 *
 * @param player     the player who placed the block
 * @param state      the BlockState to edit
 * @param holding    the ItemStack that was being held
 * @param face       the face off which the block is being placed
 * @param clickedLoc where in the block the click occurred
 */
public void placeBlock(GlowPlayer player, GlowBlockState state, BlockFace face,
            ItemStack holding, Vector clickedLoc) {
  state.setType(holding.getType());
  state.setData(holding.getData());
}

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

private boolean matchingType(GlowBlock block, BlockFace face, ItemStack holding,
  boolean ignoreFace) {
  if (holding == null) {
    return false;
  }
  Material blockType = block.getType();
  byte blockData = block.getData();
  byte holdingData = holding.getData().getData();
  return (blockType == Material.STEP || blockType == Material.WOOD_STEP
          || blockType == Material.STONE_SLAB2 || blockType == Material.PURPUR_SLAB)
      && blockType == holding.getType()
      && (face == BlockFace.UP && blockData == holdingData
          || face == BlockFace.DOWN && blockData - 8 == holdingData
          || ignoreFace && blockData % 8 == holdingData);
}

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

/**
 * Creates a new item stack derived from the specified stack
 *
 * @param stack the stack to copy
 * @throws IllegalArgumentException if the specified stack is null or
 *     returns an item meta not created by the item factory
 */
public ItemStack(final ItemStack stack) throws IllegalArgumentException {
  Validate.notNull(stack, "Cannot copy null stack");
  this.type = stack.getTypeId();
  this.amount = stack.getAmount();
  this.durability = stack.getDurability();
  this.data = stack.getData();
  if (stack.hasItemMeta()) {
    setItemMeta0(stack.getItemMeta(), getType0());
  }
}

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

@Override
public boolean blockInteract(GlowPlayer player, GlowBlock block, BlockFace face,
  Vector clickedLoc) {
  GlowBlockState state = block.getState();
  MaterialData data = state.getData();
  if (!(data instanceof FlowerPot)) {
    warnMaterialData(FlowerPot.class, data);
    return false;
  }
  if (state instanceof GlowFlowerPot) {
    GlowFlowerPot pot = (GlowFlowerPot) state;
    ItemStack heldItem = player.getItemInHand();
    // Only change contents if there is none and if the held item is valid pot contents.
    if (pot.getContents() == null && heldItem != null && isValidContents(
      heldItem.getData())) {
      pot.setContents(heldItem.getData().clone()); // Null-check in isValidContents.
      return pot.update();
    }
  }
  return false;
}

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

@Override
@Deprecated
public int clear(int id, int data) {
  int numCleared = 0;
  for (int i = 0; i < getSize(); ++i) {
    ItemStack stack = getItem(i);
    if (stack != null && (id == -1 || stack.getTypeId() == id) && (data == -1
        || stack.getData().getData() == data)) {
      setItem(i, InventoryUtil.createEmptyStack());
      if (!InventoryUtil.isEmpty(stack)) {
        // never report AIR as removed - else will report all empty slots cleared
        numCleared += stack.getAmount(); // report # items, not # stacks removed
      }
    }
  }
  return numCleared;
}

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

Color color = ((Dye) item.getData()).getColor().getColor();
colors.add(color);
continue;

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

if (blockType == Material.STEP) {
  state.setType(Material.DOUBLE_STEP);
  state.setData(holding.getData());
  return;
} else if (blockType == Material.WOOD_STEP) {
  state.setType(Material.WOOD_DOUBLE_STEP);
  state.setData(holding.getData());
  return;
} else if (blockType == Material.STONE_SLAB2) {
  state.setType(Material.DOUBLE_STONE_SLAB2);
  state.setData(holding.getData());
  return;
} else if (blockType == Material.PURPUR_SLAB) {
  state.setType(Material.PURPUR_DOUBLE_SLAB);
  state.setData(holding.getData());
  return;

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

/**
 * Remove all matching items from the inventory.
 *
 * @param type the item to remove, or null to remove everything
 * @param data the data value to match on, or null to match all data values
 * @return the number of items (not stacks) removed
 */
public int clear(Material type, MaterialData data) {
  if (type == Material.AIR) {
    return 0;
  }
  int numCleared = 0;
  for (int i = 0; i < getSize(); ++i) {
    ItemStack stack = getItem(i);
    if (stack != null && (type == null || stack.getType() == type) && (data == null || stack
        .getData().equals(data))) {
      setItem(i, InventoryUtil.createEmptyStack());
      if (!InventoryUtil.isEmpty(stack)) {
        // never report AIR as removed - else will report all empty slots cleared
        numCleared += stack.getAmount(); // report # items, not # stacks removed
      }
    }
  }
  return numCleared;
}

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

@Override
public void afterPlace(GlowPlayer player, GlowBlock block, ItemStack holding,
  GlowBlockState oldState) {
  if (block.getType() == Material.BED_BLOCK) {
    GlowBed bed = (GlowBed) block.getState();
    bed.setColor(DyeColor.getByWoolData(holding.getData().getData()));
    bed.update(true);
    BlockFace direction = ((Bed) bed.getData()).getFacing();
    GlowBlock headBlock = block.getRelative(direction);
    headBlock.setType(Material.BED_BLOCK);
    GlowBed headBlockState = (GlowBed) headBlock.getState();
    headBlockState.setColor(DyeColor.getByWoolData(holding.getData().getData()));
    MaterialData data = headBlockState.getData();
    ((Bed) data).setHeadOfBed(true);
    ((Bed) data).setFacingDirection(direction);
    headBlockState.setData(data);
    headBlockState.update(true);
  }
}

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

@Override
public boolean eat(GlowPlayer player, ItemStack item) {
  if (!super.eat(player, item)) {
    return false;
  }
  if (item.getData().getData() == 3) {
    player.addPotionEffect(new PotionEffect(PotionEffectType.POISON,
        TickUtil.minutesToTicks(1), 3), true);
    player.addPotionEffect(new PotionEffect(PotionEffectType.HUNGER,
        TickUtil.secondsToTicks(15), 2), true);
    player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION,
        TickUtil.secondsToTicks(15), 1), true);
  }
  return true;
}

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

Dye dye = (Dye) item.getData();
colors.add(dye.getColor().getFireworkColor());
break;

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

Dye dye = (Dye) hand.getData();
DyeColor color = dye.getColor();
setCollarColor(color);

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

MaterialData data = holding.getData();
if (!(data instanceof Sponge)) {
  warnMaterialData(Sponge.class, data);

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

@Override
public void rightClickBlock(GlowPlayer player, GlowBlock target, BlockFace face,
  ItemStack holding, Vector clickedLoc, EquipmentSlot hand) {
  MaterialData data = holding.getData();
  if (data instanceof Dye) {
    Dye dye = (Dye) data;

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

@Override
  public boolean eat(GlowPlayer player, ItemStack item) {
    if (!super.eat(player, item)) {
      return false;
    }

    byte data = item.getData().getData();
    if (data == 0) {
      player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION,
              TickUtil.minutesToTicks(2), 0), true);
      player
        .addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION,
            TickUtil.secondsToTicks(5), 1), true);
    } else if (data == 1) {
      player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION,
              TickUtil.minutesToTicks(2), 3),true);
      player
        .addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION,
            TickUtil.secondsToTicks(20), 1), true);
      player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,
            TickUtil.minutesToTicks(5), 0), true);
      player
        .addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE,
                TickUtil.minutesToTicks(5), 0),true);
    }

    return true;
  }
}

相关文章