Exemple #1
0
 private boolean checkTransaction(
     ClickInventoryEvent event,
     SlotTransaction transaction,
     ItemStack origStack,
     ItemStack finalStack) {
   if (!transaction.getOriginal().equals(transaction.getFinal())) {
     if (ItemTypes.NONE.equals(
         transaction.getOriginal().getType())) // Putting Item in Top Inventory
     {
       if (hasBlockIn(event, origStack, finalStack)) return true;
     } else if (ItemTypes.NONE.equals(
         transaction.getFinal().getType())) // Taking Item out of Top Inventory
     {
       if (hasBlockOut(event, origStack, finalStack)) return true;
     } else // Swapping Item in Top Inventory
     {
       if (hasBlockIn(event, origStack, finalStack)) return true;
       if (hasBlockOut(event, origStack, finalStack)) return true;
     }
   }
   return false;
 }
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");
  }