public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException {
    if (src instanceof Player) {
      Player player = (Player) src;

      if (player.getItemInHand().isPresent()) {
        ItemStack itemInHand = player.getItemInHand().get();
        player.sendMessage(
            Text.of(
                TextColors.GOLD,
                "The ID of the item in your hand is: ",
                TextColors.GRAY,
                itemInHand.getItem().getName()));
        player.sendMessage(
            Text.of(
                TextColors.GOLD,
                "The meta of the item in your hand is: ",
                TextColors.GRAY,
                itemInHand.toContainer().get(DataQuery.of("UnsafeDamage")).get().toString()));
      } else {
        player.sendMessage(
            Text.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "You must hold an item."));
      }
    } else {
      src.sendMessage(
          Text.of(
              TextColors.DARK_RED,
              "Error! ",
              TextColors.RED,
              "You must be an in-game player to use this command."));
    }

    return CommandResult.success();
  }
Exemple #2
0
  @Listener
  public void onInventoryInteract(ClickInventoryEvent event) {
    if (!event.getTargetInventory().equals(this.container)
        && !event.getTargetInventory().first().parent().equals(this.inventory)) {
      return;
    }

    System.out.print("Event:\n");
    boolean cancel = false;
    for (SlotTransaction transaction : event.getTransactions()) {
      ItemStack origStack = transaction.getOriginal().createStack();
      ItemStack finalStack = transaction.getFinal().createStack();
      String origString =
          origStack.getItem().equals(ItemTypes.NONE)
              ? origStack.getItem().getId()
              : origStack.getItem().getId() + " " + origStack.getQuantity();
      String finalString =
          finalStack.getItem().equals(ItemTypes.NONE)
              ? finalStack.getItem().getId()
              : finalStack.getItem().getId() + " " + finalStack.getQuantity();
      System.out.print(origString + "->" + finalString + "\n");

      boolean upper =
          !(transaction.getSlot().parent()
              instanceof
              PlayerInventory); // TODO this will not work when viewing another playerinventory
      if (upper) {
        if (checkTransaction(event, transaction, origStack, finalStack)) {
          cancel = true;
        } else {
          runOnChange();
        }
      }
      // else lower inventory was affected ; actually we don't care (yet)
    }
    if (cancel) {
      System.out.print("Cancelled\n");
    }
    System.out.print("\n");
  }
 @Override
 @SuppressWarnings("deprecation")
 public int clear(int id, int data) {
   int removed = 0;
   for (Slot slot : this.getHandle().<Slot>slots()) {
     Optional<org.spongepowered.api.item.inventory.ItemStack> stackOptional = slot.peek();
     if (stackOptional.isPresent()) {
       org.spongepowered.api.item.inventory.ItemStack stack = stackOptional.get();
       if (id == -1 || stack.getItem() == MaterialConverter.asItem(Material.getMaterial(id))) {
         int damage = DurabilityConverter.getDamageValue(stack.getContainers());
         if (data == -1 || damage == data) {
           removed += stack.getQuantity();
           slot.clear();
         }
       }
     }
   }
   return removed;
 }