Exemplo n.º 1
0
  @Listener(order = Order.POST)
  public void onAffectSlotEvent(AffectSlotEvent event, @Root Player player) {
    for (SlotTransaction transaction : event.getTransactions()) {
      ItemStack itemStack = transaction.getFinal().createStack();

      BigDecimal amount = WalletDrop.getValue(itemStack);

      if (amount.compareTo(BigDecimal.ZERO) == 0) {
        continue;
      }

      WorldSettings settings = WorldSettings.get(player.getWorld());

      if (player.gameMode().get().equals(GameModes.CREATIVE) && !settings.isCreativeModeAllowed()) {
        event.setCancelled(true);
        return;
      }

      if (settings.isUsePermissions()) {
        if (!player.hasPermission("walletdrop.enable")) {
          return;
        }
      }

      WalletPickupEvent moneyPickupEvent =
          new WalletPickupEvent(itemStack, amount, Cause.of(NamedCause.source(player)));

      if (!Sponge.getEventManager().post(moneyPickupEvent)) {
        Sponge.getScheduler()
            .createTaskBuilder()
            .delayTicks(2)
            .execute(
                c -> {
                  player.getInventory().query(itemStack).clear();
                })
            .submit(Main.getPlugin());

        WalletDrop.depositOrWithdraw(player, moneyPickupEvent.getAmount());

        WalletDrop.sendPickupMessage(player, amount);
      }
    }
  }
Exemplo n.º 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");
  }
Exemplo n.º 3
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;
 }