Beispiel #1
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;
 }