예제 #1
0
  private void handleMiddleClick(aue guiScreen) {

    if (Mouse.isButtonDown(2)) {

      if (!cfgManager.makeSureConfigurationIsLoaded()) {
        return;
      }
      InvTweaksConfig config = cfgManager.getConfig();

      // Check that middle click sorting is allowed
      if (config
          .getProperty(InvTweaksConfig.PROP_ENABLE_MIDDLE_CLICK)
          .equals(InvTweaksConfig.VALUE_TRUE)) {

        if (!chestAlgorithmButtonDown) {
          chestAlgorithmButtonDown = true;

          InvTweaksContainerManager containerMgr = new InvTweaksContainerManager(mc);
          containerMgr.setClickDelay(config.getClickDelay());
          sq slotAtMousePosition = containerMgr.getSlotAtMousePosition();
          InvTweaksContainerSection target = null;
          if (slotAtMousePosition != null) {
            target = containerMgr.getSlotSection(getSlotNumber(slotAtMousePosition));
          }

          if (isValidChest(guiScreen)) {

            // Check if the middle click target the chest or the inventory
            // (copied GuiContainer.getSlotAtPosition algorithm)
            auy guiContainer = asGuiContainer(guiScreen);

            if (InvTweaksContainerSection.CHEST.equals(target)) {

              // Play click
              playClick();

              long timestamp = System.currentTimeMillis();
              if (timestamp - chestAlgorithmClickTimestamp
                  > InvTweaksConst.CHEST_ALGORITHM_SWAP_MAX_INTERVAL) {
                chestAlgorithm = InvTweaksHandlerSorting.ALGORITHM_DEFAULT;
              }
              try {
                new InvTweaksHandlerSorting(
                        mc,
                        cfgManager.getConfig(),
                        InvTweaksContainerSection.CHEST,
                        chestAlgorithm,
                        getContainerRowSize(guiContainer))
                    .sort();
              } catch (Exception e) {
                logInGameError("invtweaks.sort.chest.error", e);
                e.printStackTrace();
              }
              chestAlgorithm = (chestAlgorithm + 1) % 3;
              chestAlgorithmClickTimestamp = timestamp;

            } else if (InvTweaksContainerSection.INVENTORY_HOTBAR.equals(target)
                || (InvTweaksContainerSection.INVENTORY_NOT_HOTBAR.equals(target))) {
              handleSorting(guiScreen);
            }

          } else if (isValidInventory(guiScreen)) {

            /*
                         	 // Crafting stacks evening (hook ready, TODO implement algorithm)
                         	 if (InvTweaksContainerSection.CRAFTING_IN.equals(target)) {
                                 try {
            	new InvTweaksHandlerSorting(mc, cfgManager.getConfig(),
            	        InvTweaksContainerSection.CRAFTING_IN,
            	        InvTweaksHandlerSorting.ALGORITHM_EVEN_STACKS,
            	        (containerMgr.getSize(target) == 9) ? 3 : 2).sort();
            } catch (Exception e) {
                                     logInGameError("invtweaks.sort.crafting.error", e);
                                     e.printStackTrace();
            }
                             }*/

            handleSorting(guiScreen);
          }
        }
      }
    } else {
      chestAlgorithmButtonDown = false;
    }
  }
예제 #2
0
  /**
   * To be called everytime a stack has been picked up. Moves the picked up item in another slot
   * that matches best the current configuration.
   */
  public void onItemPickup() {

    if (!cfgManager.makeSureConfigurationIsLoaded()) {
      return;
    }
    InvTweaksConfig config = cfgManager.getConfig();
    // Handle option to disable this feature
    if (cfgManager
        .getConfig()
        .getProperty(InvTweaksConfig.PROP_ENABLE_SORTING_ON_PICKUP)
        .equals("false")) {
      return;
    }

    try {
      InvTweaksContainerSectionManager containerMgr =
          new InvTweaksContainerSectionManager(mc, InvTweaksContainerSection.INVENTORY);
      containerMgr.setClickDelay(config.getClickDelay());

      // Find stack slot (look in hotbar only).
      // We're looking for a brand new stack in the hotbar
      // (not an existing stack whose amount has been increased)
      int currentSlot = -1;
      for (int i = 0; i < InvTweaksConst.INVENTORY_HOTBAR_SIZE; i++) {
        um currentHotbarStack = containerMgr.getItemStack(i + 27);
        // Don't move already started stacks
        if (currentHotbarStack != null
            && getAnimationsToGo(currentHotbarStack) == 5
            && hotbarClone[i] == null) {
          currentSlot = i + 27;
        }
      }

      if (currentSlot != -1) {
        itemPickupPending = false;

        // Find preffered slots
        List<Integer> prefferedPositions = new LinkedList<Integer>();
        InvTweaksItemTree tree = config.getTree();
        um stack = containerMgr.getItemStack(currentSlot);
        List<InvTweaksItemTreeItem> items = tree.getItems(getItemID(stack), getItemDamage(stack));
        for (InvTweaksConfigSortingRule rule : config.getRules()) {
          if (tree.matches(items, rule.getKeyword())) {
            for (int slot : rule.getPreferredSlots()) {
              prefferedPositions.add(slot);
            }
          }
        }

        // Find best slot for stack
        boolean hasToBeMoved = true;
        if (prefferedPositions != null) {
          for (int newSlot : prefferedPositions) {
            try {
              // Already in the best slot!
              if (newSlot == currentSlot) {
                hasToBeMoved = false;
                break;
              }
              // Is the slot available?
              else if (containerMgr.getItemStack(newSlot) == null) {
                // TODO: Check rule level before to move
                if (containerMgr.move(currentSlot, newSlot)) {
                  break;
                }
              }
            } catch (TimeoutException e) {
              logInGameError("Failed to move picked up stack", e);
            }
          }
        }

        // Else, put the slot anywhere
        if (hasToBeMoved) {
          for (int i = 0; i < containerMgr.getSize(); i++) {
            if (containerMgr.getItemStack(i) == null) {
              if (containerMgr.move(currentSlot, i)) {
                break;
              }
            }
          }
        }
      }

    } catch (Exception e) {
      logInGameError("Failed to move picked up stack", e);
    }
  }