예제 #1
0
 public static void debug(InventoryClickEvent event) {
   System.out.println("===== DEBUG START =====");
   System.out.println("event.getAction() " + event.getAction());
   System.out.println("event.isLeftClick() " + event.isLeftClick());
   System.out.println("event.isRightClick() " + event.isRightClick());
   System.out.println("event.isShiftClick() " + event.isShiftClick());
   System.out.println("event.getClick() " + event.getClick());
   System.out.println("event.getCurrentItem() " + event.getCurrentItem());
   System.out.println("event.getCursor() " + event.getCursor());
   System.out.println("event.getHotbarButton() " + event.getHotbarButton());
   System.out.println("getInventory().getType() " + event.getInventory().getType());
   System.out.println("event.getRawSlot() " + event.getRawSlot());
   System.out.println("event.getResult() " + event.getResult());
   System.out.println("event.getSlot() " + event.getSlot());
   System.out.println("event.getSlotType() " + event.getSlotType());
   System.out.println(
       "getView().getTopInventory().getType() " + event.getView().getTopInventory().getType());
   System.out.println("getView().getType() " + event.getView().getType());
   System.out.println(
       "getView().getBottomInventory().getType() "
           + event.getView().getBottomInventory().getType());
   System.out.println("event.getWhoClicked() " + event.getWhoClicked());
   System.out.println("-----");
   System.out.println("isOutside(event) " + isOutside(event));
   System.out.println("isTopInventory(event) " + isTopInventory(event));
   System.out.println("isBottomInventory(event) " + isBottomInventory(event));
   System.out.println("getAlter(event) " + getAlter(event));
   System.out.println("isAltering(event) " + isAltering(event));
   System.out.println("isEquipping(event) " + isEquipping(event));
   System.out.println("===== DEBUG END =====");
 }
예제 #2
0
  protected static List<Entry<ItemStack, Integer>> getChangesClick(InventoryClickEvent event) {
    // Create
    List<Entry<ItemStack, Integer>> ret = new MassiveList<>();

    // Fill
    final InventoryAlter alter = InventoryUtil.getAlter(event);
    final InventoryAction action = event.getAction();
    ItemStack item;
    int amount;

    // Give
    if (alter.isGiving()) {
      // Special > MOVE_TO_OTHER_INVENTORY
      if (action == InventoryAction.MOVE_TO_OTHER_INVENTORY) {
        item = event.getCurrentItem();

        ItemStack compare = item.clone();
        compare.setAmount(1);
        amount = InventoryUtil.roomLeft(event.getInventory(), compare, item.getAmount());
      }
      // Special > HOTBAR_SWAP
      else if (action == InventoryAction.HOTBAR_SWAP) {
        item = event.getView().getBottomInventory().getItem(event.getHotbarButton());

        amount = item.getAmount();
      }
      // Normal
      else {
        item = event.getCursor();

        amount = item.getAmount();
        if (action == InventoryAction.PLACE_ONE) {
          amount = 1;
        } else if (action == InventoryAction.PLACE_SOME) {
          int max = event.getCurrentItem().getType().getMaxStackSize();
          amount = max - event.getCurrentItem().getAmount();
        }
      }

      amount *= -1;
      ret.add(new SimpleEntry<ItemStack, Integer>(item, amount));
    }

    // Take
    if (alter.isTaking()) {
      item = event.getCurrentItem();

      amount = item.getAmount();
      if (action == InventoryAction.PICKUP_ONE) amount = 1;
      if (action == InventoryAction.PICKUP_HALF) amount = (int) Math.ceil(amount / 2.0);

      ret.add(new SimpleEntry<ItemStack, Integer>(item, amount));
    }

    // Return
    return ret;
  }
예제 #3
0
  public static InventoryAlter getAlter(InventoryClickEvent event) {
    if (isOutside(event)) return InventoryAlter.NONE;
    boolean topClicked = isTopInventory(event);
    InventoryAction action = event.getAction();

    if (topClicked) {
      switch (action) {
          // What is the best thing to do?
        case UNKNOWN:
          return InventoryAlter.BOTH;

          // Possibly both
        case HOTBAR_SWAP:
          ItemStack hotbar = event.getView().getBottomInventory().getItem(event.getHotbarButton());
          ItemStack current = event.getCurrentItem();
          boolean give = isSomething(hotbar);
          boolean take = isSomething(current);

          return getAlter(give, take);

          // Neither give nor take
        case NOTHING:
          return InventoryAlter.NONE;
        case CLONE_STACK:
          return InventoryAlter.NONE;
        case DROP_ALL_CURSOR:
          return InventoryAlter.NONE;
        case DROP_ONE_CURSOR:
          return InventoryAlter.NONE;

          // Take
        case PICKUP_ALL:
          return InventoryAlter.TAKE;
        case PICKUP_HALF:
          return InventoryAlter.TAKE;
        case PICKUP_ONE:
          return InventoryAlter.TAKE;
        case PICKUP_SOME:
          return InventoryAlter.TAKE;
        case MOVE_TO_OTHER_INVENTORY:
          return InventoryAlter.TAKE;
        case COLLECT_TO_CURSOR:
          return InventoryAlter.TAKE;
        case HOTBAR_MOVE_AND_READD:
          return InventoryAlter.TAKE;
        case DROP_ONE_SLOT:
          return InventoryAlter.TAKE;
        case DROP_ALL_SLOT:
          return InventoryAlter.TAKE;

          // Give
        case PLACE_ALL:
          return InventoryAlter.GIVE;
        case PLACE_ONE:
          return InventoryAlter.GIVE;
        case PLACE_SOME:
          return InventoryAlter.GIVE;
        case SWAP_WITH_CURSOR:
          return InventoryAlter.BOTH;
      }
      throw new RuntimeException("Unsupported action: " + action);
    } else {
      // What is the best thing to do?
      if (action == InventoryAction.UNKNOWN) return InventoryAlter.BOTH;

      if (action == InventoryAction.MOVE_TO_OTHER_INVENTORY) return InventoryAlter.GIVE;

      // This one will possibly take, but we cannot be 100% sure.
      // We will return TAKE for security reasons.
      if (action == InventoryAction.COLLECT_TO_CURSOR) return InventoryAlter.TAKE;

      return InventoryAlter.NONE;
    }
  }
  @SuppressWarnings("deprecation")
  @EventHandler
  public void onInventoryClick(InventoryClickEvent event) {
    // controller.getLogger().info("CLICK: " + event.getAction() + ", " + event.getClick() + " on "
    // + event.getSlotType() + " in "+ event.getInventory().getType() + " slots: " + event.getSlot()
    // + ":" + event.getRawSlot());

    if (event.isCancelled()) return;
    if (!(event.getWhoClicked() instanceof Player)) return;

    Player player = (Player) event.getWhoClicked();
    Mage apiMage = controller.getMage(player);

    if (!(apiMage instanceof com.elmakers.mine.bukkit.magic.Mage)) return;
    final com.elmakers.mine.bukkit.magic.Mage mage = (com.elmakers.mine.bukkit.magic.Mage) apiMage;

    GUIAction gui = mage.getActiveGUI();
    if (gui != null) {
      gui.clicked(event);
      return;
    }

    // Check for temporary items and skill items
    InventoryAction action = event.getAction();
    InventoryType inventoryType = event.getInventory().getType();
    ItemStack clickedItem = event.getCurrentItem();

    boolean isDrop =
        event.getClick() == ClickType.DROP || event.getClick() == ClickType.CONTROL_DROP;
    boolean isSkill = clickedItem != null && Wand.isSkill(clickedItem);

    // Preventing putting skills in containers
    if (isSkill && inventoryType != InventoryType.CRAFTING) {
      if (!isDrop) {
        event.setCancelled(true);
      }
      return;
    }

    // Check for right-click-to-use
    if (isSkill && action == InventoryAction.PICKUP_HALF) {
      Spell spell = mage.getSpell(Wand.getSpell(clickedItem));
      if (spell != null) {
        spell.cast();
      }
      player.closeInventory();
      event.setCancelled(true);
      return;
    }

    if (clickedItem != null && NMSUtils.isTemporary(clickedItem)) {
      String message = NMSUtils.getTemporaryMessage(clickedItem);
      if (message != null && message.length() > 1) {
        mage.sendMessage(message);
      }
      ItemStack replacement = NMSUtils.getReplacement(clickedItem);
      event.setCurrentItem(replacement);
      event.setCancelled(true);
      return;
    }

    // Check for wearing spells
    ItemStack heldItem = event.getCursor();
    if (heldItem != null && event.getSlotType() == InventoryType.SlotType.ARMOR) {
      if (Wand.isSpell(heldItem)) {
        event.setCancelled(true);
        return;
      }
      if (Wand.isWand(clickedItem) || Wand.isWand(heldItem)) {
        controller.onArmorUpdated(mage);
      }
    }
    boolean isHotbar =
        event.getAction() == InventoryAction.HOTBAR_SWAP
            || event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD;
    if (isHotbar && event.getSlotType() == InventoryType.SlotType.ARMOR) {
      int slot = event.getHotbarButton();
      ItemStack item = mage.getPlayer().getInventory().getItem(slot);
      if (item != null && Wand.isSpell(item)) {
        event.setCancelled(true);
        return;
      }
      controller.onArmorUpdated(mage);
    }

    Wand activeWand = mage.getActiveWand();

    boolean isChest =
        inventoryType == InventoryType.CHEST
            || inventoryType == InventoryType.HOPPER
            || inventoryType == InventoryType.DISPENSER
            || inventoryType == InventoryType.DROPPER;
    boolean clickedWand = Wand.isWand(clickedItem);
    boolean isContainerSlot = event.getSlot() == event.getRawSlot();

    if (activeWand != null && activeWand.isInventoryOpen()) {
      // Kind of a hack to avoid hotbar items with 0 amounts disappearing :(
      Inventory inventory = player.getInventory();
      for (int i = 0; i < 8; i++) {
        ItemStack item = inventory.getItem(i);
        if (item != null && item.getAmount() == 0) {
          item.setAmount(1);
        }
      }

      // Don't allow the offhand slot to be messed with while the spell inventory is open
      if (event.getRawSlot() == 45) {
        event.setCancelled(true);
        return;
      }

      if (Wand.isSpell(clickedItem) && clickedItem.getAmount() != 1) {
        clickedItem.setAmount(1);
      }
      if (clickedWand) {
        event.setCancelled(true);
        if (dropChangesPages) {
          activeWand.cycleInventory();
        } else {
          activeWand.cycleHotbar(1);
        }
        return;
      }

      // So many ways to try and move the wand around, that we have to watch for!
      if (isHotbar && Wand.isWand(player.getInventory().getItem(event.getHotbarButton()))) {
        event.setCancelled(true);
        return;
      }

      // Can't wear spells
      if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedItem != null) {
        if (controller.isWearable(clickedItem)) {
          event.setCancelled(true);
          return;
        }
      }

      // Safety check for something that ought not to be possible
      // but perhaps happens with lag?
      if (Wand.isWand(event.getCursor())) {
        activeWand.closeInventory();
        event.setCursor(null);
        event.setCancelled(true);
        return;
      }
    } else if (activeWand != null) {
      // Check for changes that could have been made to the active wand
      Integer activeSlot = player.getInventory().getHeldItemSlot();
      if (activeSlot != null
          && (event.getSlot() == activeSlot
              || (event.getAction() == InventoryAction.HOTBAR_SWAP
                  && event.getHotbarButton() == activeSlot))) {
        mage.checkWand();
        activeWand = mage.getActiveWand();
      }
    } else if (clickedWand
        && Wand.Undroppable
        && !player.hasPermission("Magic.wand.override_drop")
        && isChest
        && !isContainerSlot) {
      Wand wand = new Wand(controller, clickedItem);
      if (wand.isUndroppable()) {
        event.setCancelled(true);
        return;
      }
    }

    // Check for armor changing
    if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedItem != null) {
      if (controller.isWearable(clickedItem)) {
        controller.onArmorUpdated(mage);
      }
    }

    // Check for dropping items out of a wand's inventory
    // or dropping undroppable wands
    if (isDrop) {
      if (clickedWand) {
        Wand wand = new Wand(controller, clickedItem);
        if (wand.isUndroppable()) {
          event.setCancelled(true);
          if (activeWand != null) {
            if (activeWand.getHotbarCount() > 1) {
              activeWand.cycleHotbar(1);
            } else {
              activeWand.closeInventory();
            }
          }
          return;
        }
      }
      if (activeWand != null && activeWand.isInventoryOpen()) {

        ItemStack droppedItem = clickedItem;

        if (!Wand.isSpell(droppedItem)) {
          mage.giveItem(droppedItem);
          event.setCurrentItem(null);
          event.setCancelled(true);
          return;
        }

        // This is a hack to deal with spells on cooldown disappearing,
        // Since the event handler doesn't match the zero-count itemstacks
        Integer slot = event.getSlot();
        int heldSlot = player.getInventory().getHeldItemSlot();
        Inventory hotbar = activeWand.getHotbar();
        if (hotbar != null
            && slot >= 0
            && slot <= hotbar.getSize()
            && slot != heldSlot
            && activeWand.getMode() == WandMode.INVENTORY) {
          if (slot > heldSlot) slot--;
          if (slot < hotbar.getSize()) {
            droppedItem = hotbar.getItem(slot);
          } else {
            slot = null;
          }
        } else {
          slot = null;
        }

        if (!controller.isSpellDroppingEnabled()) {
          player.closeInventory();
          String spellName = Wand.getSpell(droppedItem);
          if (spellName != null && !activeWand.isManualQuickCastDisabled()) {
            Spell spell = mage.getSpell(spellName);
            if (spell != null) {
              activeWand.cast(spell);
              // Just in case a spell has levelled up... jeez!
              if (hotbar != null && slot != null) {
                droppedItem = hotbar.getItem(slot);
              }
            }
          }
          event.setCancelled(true);

          // This is needed to avoid spells on cooldown disappearing from the hotbar
          if (hotbar != null && slot != null && mage.getActiveGUI() == null) {
            player.getInventory().setItem(event.getSlot(), droppedItem);
            player.updateInventory();
          }

          return;
        }
        ItemStack newDrop = controller.removeItemFromWand(activeWand, droppedItem);

        if (newDrop != null) {
          Location location = player.getLocation();
          Item item = location.getWorld().dropItem(location, newDrop);
          item.setVelocity(location.getDirection().normalize());
        } else {
          event.setCancelled(true);
        }
      }
      return;
    }

    // Check for wand cycling with active inventory
    if (activeWand != null) {
      WandMode wandMode = activeWand.getMode();
      if ((wandMode == WandMode.INVENTORY && inventoryType == InventoryType.CRAFTING)
          || (wandMode == WandMode.CHEST && inventoryType == InventoryType.CHEST)) {
        if (activeWand.isInventoryOpen()) {
          if (event.getAction() == InventoryAction.NOTHING) {
            int direction = event.getClick() == ClickType.LEFT ? 1 : -1;
            activeWand.cycleInventory(direction);
            event.setCancelled(true);
            return;
          }

          if (event.getSlotType() == InventoryType.SlotType.ARMOR) {
            event.setCancelled(true);
            return;
          }

          // Chest mode falls back to selection from here.
          if (event.getAction() == InventoryAction.PICKUP_HALF || wandMode == WandMode.CHEST) {
            controller.onPlayerActivateIcon(mage, activeWand, clickedItem);
            player.closeInventory();
            event.setCancelled(true);
          }
        }
      }
    }
  }