Package net.minecraft.src

Examples of net.minecraft.src.ItemStack


        // Try to merge with existing slot
        if (!shortcut.forceEmptySlot) {
            int i = 0;
            for (Slot slot : container.getSlots(shortcut.toSection)) {
                if (hasStack(slot)) {
                    ItemStack stack = getStack(slot);
                    if (!hasDataTags(stack) && areItemsEqual(stack, shortcut.fromStack)
                            && getStackSize(stack) < getMaxStackSize(stack)) {
                        result = i;
                        break;
                    }
View Full Code Here


        this.rulePriority = new int[size];
        this.keywordOrder = new int[size];
        for (int i = 0; i < size; i++) {
            this.rulePriority[i] = -1;
            ItemStack stack = containerMgr.getItemStack(i);
            if (stack != null) {
                this.keywordOrder[i] = getItemOrder(stack);
            } else {
                this.keywordOrder[i] = -1;
            }
View Full Code Here

                log.info("Distributing items.");

                //item and slot counts for each unique item
                HashMap<List<Integer>, int[]> itemCounts = new HashMap<List<Integer>, int[]>();
                for(int i = 0; i < size; i++) {
                    ItemStack stack = containerMgr.getItemStack(i);
                    if(stack != null) {
                        List<Integer> item = Arrays.asList(getItemID(stack), getItemDamage(stack));
                        int[] count = itemCounts.get(item);
                        if(count == null) {
                            int[] newCount = {getStackSize(stack), 1};
                            itemCounts.put(item,newCount);
                        } else {
                            count[0] += getStackSize(stack); //amount of item
                            count[1]++;                      //slots with item
                        }
                    }
                }

                //handle each unique item separately
                for(Entry<List<Integer>, int[]> entry : itemCounts.entrySet()) {
                    List<Integer> item = entry.getKey();
                    int[] count = entry.getValue();
                    int numPerSlot = count[0]/count[1]//totalNumber/numberOfSlots

                    //skip hacked itemstacks that are larger than their max size
                    //no idea why they would be here, but may as well account for them anyway
                    if(numPerSlot <= getMaxStackSize(new ItemStack(item.get(0),1,0))) {

                        //linkedlists to store which stacks have too many/few items
                        LinkedList<Integer> smallStacks = new LinkedList<Integer>();
                        LinkedList<Integer> largeStacks = new LinkedList<Integer>();
                        for(int i = 0; i < size; i++) {
                            ItemStack stack = containerMgr.getItemStack(i);
                            if(stack != null && Arrays.asList(getItemID(stack),getItemDamage(stack)).equals(item)) {
                                int stackSize = getStackSize(stack);
                                if(stackSize > numPerSlot)
                                    largeStacks.offer(i);
                                else if(stackSize < numPerSlot)
                                    smallStacks.offer(i);
                            }
                        }

                        //move items from stacks with too many to those with too little
                        while((!smallStacks.isEmpty())) {
                            int largeIndex = (Integer)largeStacks.peek();
                            int largeSize = getStackSize(containerMgr.getItemStack(largeIndex));
                            int smallIndex = (Integer)smallStacks.peek();
                            int smallSize = getStackSize(containerMgr.getItemStack(smallIndex));
                            containerMgr.moveSome(largeIndex, smallIndex, Math.min(numPerSlot-smallSize,largeSize-numPerSlot));

                            //update stack lists
                            largeSize = getStackSize(containerMgr.getItemStack(largeIndex));
                            smallSize = getStackSize(containerMgr.getItemStack(smallIndex));
                            if(largeSize == numPerSlot)
                                largeStacks.remove();
                            if(smallSize == numPerSlot)
                                smallStacks.remove();
                        }

                        //put all leftover into one stack for easy removal
                        while(largeStacks.size() > 1) {
                            int largeIndex = (Integer)largeStacks.poll();
                            int largeSize = getStackSize(containerMgr.getItemStack(largeIndex));
                            containerMgr.moveSome(largeIndex,(Integer)largeStacks.peek(),largeSize-numPerSlot);
                        }
                    }
                }

                //mark all items as moved. (is there a better way?)
                for(int i=0;i<size;i++)
                    markAsMoved(i,1);

            } else if (algorithm == ALGORITHM_INVENTORY) {
                //// Move items out of the crafting slots
                log.info("Handling crafting slots.");
                if (globalContainer.hasSection(InvTweaksContainerSection.CRAFTING_IN)) {
                    List<Slot> craftingSlots = globalContainer.getSlots(InvTweaksContainerSection.CRAFTING_IN);
                    int emptyIndex = globalContainer.getFirstEmptyIndex(InvTweaksContainerSection.INVENTORY);
                    if (emptyIndex != -1) {
                        for (Slot craftingSlot : craftingSlots) {
                            if (hasStack(craftingSlot)) {
                                globalContainer.move(
                                        InvTweaksContainerSection.CRAFTING_IN,
                                        globalContainer.getSlotIndex(getSlotNumber(craftingSlot)),
                                        InvTweaksContainerSection.INVENTORY,
                                        emptyIndex);
                                emptyIndex = globalContainer.getFirstEmptyIndex(InvTweaksContainerSection.INVENTORY);
                                if(emptyIndex == -1) {
                                    break;
                                }
                            }
                        }
                    }
                }


                //// Merge stacks to fill the ones in locked slots
                //// + Move armor parts to the armor slots

                log.info("Merging stacks.");
                for (int i = size - 1; i >= 0; i--) {
                    ItemStack  from = containerMgr.getItemStack(i);
                    if (from != null) {

                        // Move armor parts
                      Item fromItem = getItem(from);
                      if (isDamageable(fromItem)) {
                          if (sortArmorParts) {
                               if (isItemArmor(fromItem)) {
                                 ItemArmor fromItemArmor = asItemArmor(fromItem);
                                   if (globalContainer.hasSection(InvTweaksContainerSection.ARMOR)) {
                                       List<Slot> armorSlots = globalContainer.getSlots(InvTweaksContainerSection.ARMOR);
                                       for (Slot slot : armorSlots) {
                                        boolean move = false;
                                        if (!hasStack(slot)) {
                                          move = true;
                                        }
                                        else {
                                                Item currentArmor = getItem(getStack(slot));
                                                if(isItemArmor(currentArmor)) {
                                                    int armorLevel = getArmorLevel(asItemArmor(currentArmor));
                                                    if (armorLevel < getArmorLevel(fromItemArmor)
                                                            || (armorLevel == getArmorLevel(fromItemArmor)
                                                                    && getItemDamage(getStack(slot)) < getItemDamage(from))) {
                                                        move = true;
                                                    }
                                                } else {
                                                    move = true;
                                                }
                                        }
                                          if (areSlotAndStackCompatible(slot, from) && move) {
                                              globalContainer.move(InvTweaksContainerSection.INVENTORY, i, InvTweaksContainerSection.ARMOR,
                                                      globalContainer.getSlotIndex(getSlotNumber(slot)));
                                          }
                                      }
                                   }
                               }
                          }
                      }

                        // Stackable objects are never damageable
                        else {
                            int j = 0;
                            for (Integer lockPriority : lockPriorities) {
                                if (lockPriority > 0) {
                                    ItemStack to = containerMgr.getItemStack(j);
                                    if (to != null && areItemsEqual(from, to)) {
                                        move(i, j, Integer.MAX_VALUE);
                                        markAsNotMoved(j);
                                        if (containerMgr.getItemStack(i) == null) {
                                            break;
                                        }
                                    }
                                }
                                j++;
                            }
                        }
                    }
                }

            }

            //// Apply rules
            log.info("Applying rules.");

            // Sorts rule by rule, themselves being already sorted by decreasing priority
            Iterator<InvTweaksConfigSortingRule> rulesIt = rules.iterator();
            while (rulesIt.hasNext()) {

                InvTweaksConfigSortingRule rule = rulesIt.next();
                int rulePriority = rule.getPriority();

                if (log.getLevel() == InvTweaksConst.DEBUG)
                    log.info("Rule : "+rule.getKeyword()+"("+rulePriority+")");

                // For every item in the inventory
                for (int i = 0; i < size; i++) {
                    ItemStack from = containerMgr.getItemStack(i);

                    // If the rule is strong enough to move the item and it matches the item, move it
                    if (hasToBeMoved(i) && lockPriorities[i] < rulePriority) {
                        List<InvTweaksItemTreeItem> fromItems = tree.getItems(
                                getItemID(from), getItemDamage(from));
View Full Code Here

     *      n if the j stack has been moved to the n slot.
     * @throws TimeoutException
     */
    private int move(int i, int j, int priority) throws TimeoutException {

        ItemStack from = containerMgr.getItemStack(i), to = containerMgr.getItemStack(j);

        if (from == null || frozenSlots[j] || frozenSlots[i]) {
            return -1;
        }

        //log.info("Moving " + i + " (" + from + ") to " + j + " (" + to + ") ");

        if (lockPriorities[i] <= priority) {

            if (i == j) {
                markAsMoved(i, priority);
                return j;
            }

            // Move to empty slot
            if (to == null && lockPriorities[j] <= priority && !frozenSlots[j]) {
                rulePriority[i] = -1;
                keywordOrder[i] = -1;
                rulePriority[j] = priority;
                keywordOrder[j] = getItemOrder(from);
                containerMgr.move(i, j);
                return j;
            }

            // Try to swap/merge
            else if (to != null) {

                boolean canBeSwappedOrMerged = false;

                // Can be swapped?
                if (lockPriorities[j] <= priority) {
                    if (rulePriority[j] < priority) {
                        canBeSwappedOrMerged = true;
                    } else if (rulePriority[j] == priority) {
                        if (isOrderedBefore(i, j)) {
                            canBeSwappedOrMerged = true;
                        }
                    }
                }

                if (!hasDataTags(from) && !hasDataTags(to) && areItemsEqual(from, to)) {
                    // Can be merged?
                    if (getStackSize(to) < getMaxStackSize(to)) {
                        canBeSwappedOrMerged = true;
                    }
                    // Safety check (for TooManyItems)
                    else if (getStackSize(from) > getMaxStackSize(from)) {
                        canBeSwappedOrMerged = false;
                    }
                }

                if (canBeSwappedOrMerged) {

                    keywordOrder[j] = keywordOrder[i];
                    rulePriority[j] = priority;
                    rulePriority[i] = -1;
                    containerMgr.move(i, j);

                    ItemStack remains = containerMgr.getItemStack(i);

                    if (remains != null) {
                        int dropSlot = i;
                        if (lockPriorities[j] > lockPriorities[i]) {
                            for (int k = 0; k < size; k++) {
View Full Code Here

                && rulePriority[slot] == -1;
    }

    private boolean isOrderedBefore(int i, int j) {

        ItemStack iStack = containerMgr.getItemStack(i), jStack = containerMgr.getItemStack(j);

        if (jStack == null) {
            return true;
        } else if (iStack == null || keywordOrder[i] == -1) {
            return false;
View Full Code Here

    private Map<InvTweaksItemTreeItem, Integer> computeContainerStats() {
        Map<InvTweaksItemTreeItem, Integer> stats = new HashMap<InvTweaksItemTreeItem, Integer>();
        Map<Integer, InvTweaksItemTreeItem> itemSearch = new HashMap<Integer, InvTweaksItemTreeItem>();

        for (int i = 0; i < size; i++) {
            ItemStack stack = containerMgr.getItemStack(i);
            if (stack != null) {
                int itemSearchKey = getItemID(stack)*100000 +
                        ((getMaxStackSize(stack) != 1) ? getItemDamage(stack) : 0);
                InvTweaksItemTreeItem item = itemSearch.get(itemSearchKey);
                if (item == null) {
View Full Code Here

     * @throws TimeoutException
     */
  public boolean move(InvTweaksContainerSection srcSection, int srcIndex,
            InvTweaksContainerSection destSection, int destIndex) {
      //System.out.println(srcSection + ":" + srcIndex + " to " + destSection + ":" + destIndex);
      ItemStack srcStack = getItemStack(srcSection, srcIndex);
      ItemStack destStack = getItemStack(destSection, destIndex);
     
        if (srcStack == null && destIndex != DROP_SLOT) {
            return false;
        }
        else if (srcSection == destSection && srcIndex == destIndex) {
View Full Code Here

   */
  public boolean moveSome(InvTweaksContainerSection srcSection, int srcIndex,
          InvTweaksContainerSection destSection, int destIndex,
          int amount) {

      ItemStack source = getItemStack(srcSection, srcIndex);
      if (source == null || srcSection == destSection && srcIndex == destIndex) {
            return true;
        }

      ItemStack destination = getItemStack(srcSection, srcIndex);
        int sourceSize = getStackSize(source);
        int movedAmount = Math.min(amount, sourceSize);
     
      if (source != null && (destination == null
              || areItemStacksEqual(source, destination))) {
View Full Code Here

     *
     * @return true unless the item could not be put down
     * @throws Exception
     */
    public boolean putHoldItemDown(InvTweaksContainerSection destSection, int destIndex) {
        ItemStack heldStack = getHeldStack();
        if (heldStack != null) {
          if (getItemStack(destSection, destIndex) == null) {
            click(destSection, destIndex, false);
                return true;
          }
View Full Code Here

     */
  public void autoRefillSlot(int slot, int wantedId, int wantedDamage) throws Exception {

    InvTweaksContainerSectionManager container = new InvTweaksContainerSectionManager(
            mc, InvTweaksContainerSection.INVENTORY);
    ItemStack candidateStack, replacementStack = null;
    int replacementStackSlot = -1;
        boolean refillBeforeBreak = config.getProperty(InvTweaksConfig.PROP_AUTO_REFILL_BEFORE_BREAK)
                .equals(InvTweaksConfig.VALUE_TRUE);
   
    List<InvTweaksConfigSortingRule> matchingRules = new ArrayList<InvTweaksConfigSortingRule>();
    List<InvTweaksConfigSortingRule> rules = config.getRules();
    InvTweaksItemTree tree = config.getTree();
   
    // Check that the item is in the tree
        if (!tree.isItemUnknown(wantedId, wantedDamage)) {
           
            //// Search replacement
           
        List<InvTweaksItemTreeItem> items = tree.getItems(wantedId, wantedDamage);
   
        // Find rules that match the slot
        for (InvTweaksItemTreeItem item : items) {
          // Since we search a matching item using rules,
            // create a fake one that matches the exact item first
          matchingRules.add(new InvTweaksConfigSortingRule(
              tree, "D"+(slot-27), item.getName(),
              InvTweaksConst.INVENTORY_SIZE, InvTweaksConst.INVENTORY_ROW_SIZE));
        }
        for (InvTweaksConfigSortingRule rule : rules) {
          if (rule.getType() == InvTweaksConfigSortingRuleType.SLOT
                  || rule.getType() == InvTweaksConfigSortingRuleType.COLUMN) {
            for (int preferredSlot : rule.getPreferredSlots()) {
              if (slot == preferredSlot) {
                matchingRules.add(rule);
                break;
              }
            }
          }
        }
   
        // Look only for a matching stack
        // First, look for the same item,
        // else one that matches the slot's rules
        for (InvTweaksConfigSortingRule rule : matchingRules) {
          for (int i = 0; i < InvTweaksConst.INVENTORY_SIZE; i++) {
            candidateStack = container.getItemStack(i);
            if (candidateStack != null) {
              List<InvTweaksItemTreeItem> candidateItems = tree.getItems(
                  getItemID(candidateStack),
                  getItemDamage(candidateStack));
              if (tree.matches(candidateItems, rule.getKeyword())) {
                            // Choose tool of highest damage value
                if (getMaxStackSize(candidateStack) == 1) {
                    if ((replacementStack == null || getItemDamage(candidateStack) > getItemDamage(replacementStack)) &&
                            (!refillBeforeBreak || getMaxDamage(getItem(candidateStack)) - getItemDamage(candidateStack)
                                    > InvTweaksConst.AUTO_REFILL_DAMAGE_TRESHOLD)) {
                        replacementStack = candidateStack;
                                  replacementStackSlot = i;
                    }
                }
                            // Choose stack of lowest size
                else if (replacementStack == null || getStackSize(candidateStack) < getStackSize(replacementStack)) {
                    replacementStack = candidateStack;
                                replacementStackSlot = i;
                }
              }
            }
          }
        }
        }
       
        // If item is unknown, look for exact same item
        else {
            for (int i = 0; i < InvTweaksConst.INVENTORY_SIZE; i++) {
                candidateStack = container.getItemStack(i);
                if (candidateStack != null &&
                        getItemID(candidateStack) == wantedId &&
                        getItemDamage(candidateStack) == wantedDamage) {
                    replacementStack = candidateStack;
                    replacementStackSlot = i;
                    break;
                }
            }
        }
   
    //// Proceed to replacement
 
    if (replacementStack != null || (refillBeforeBreak && getStack(container.getSlot(slot)) != null)) {
     
      log.info("Automatic stack replacement.");
     
        /*
         * This allows to have a short feedback
         * that the stack/tool is empty/broken.
         */
      new Thread(new Runnable() {

        private InvTweaksContainerSectionManager containerMgr;
        private int targetedSlot;
        private int i, expectedItemId;
        private boolean refillBeforeBreak;
       
        public Runnable init(Minecraft mc,
            int i, int currentItem, boolean refillBeforeBreak) throws Exception {
          this.containerMgr = new InvTweaksContainerSectionManager(
                  mc, InvTweaksContainerSection.INVENTORY);
          this.targetedSlot = currentItem;
          if (i != -1) {
                      this.i = i;
              this.expectedItemId = getItemID(containerMgr.getItemStack(i));
          }
          else {
                        this.i = containerMgr.getFirstEmptyIndex();
                        this.expectedItemId = -1;
          }
          this.refillBeforeBreak = refillBeforeBreak;
          return this;
        }
       
        public void run() {
         
          // Wait for the server to confirm that the
          // slot is now empty
          int pollingTime = 0;
          setHasInventoryChanged(false);
          while(getThePlayer() != null && !hasInventoryChanged()
              && pollingTime < InvTweaksConst.POLLING_TIMEOUT) {
            trySleep(InvTweaksConst.POLLING_DELAY);
          }
          if (getThePlayer() == null) {
              return; // Game closed
          }
          if (pollingTime < InvTweaksConst.AUTO_REFILL_DELAY)
            trySleep(InvTweaksConst.AUTO_REFILL_DELAY - pollingTime);
          if (pollingTime >= InvTweaksConst.POLLING_TIMEOUT)
            log.warning("Autoreplace timout");
         
          // In POLLING_DELAY ms, things might have changed
          try {
            ItemStack stack = containerMgr.getItemStack(i);
            if (stack != null && getItemID(stack) == expectedItemId || this.refillBeforeBreak) {
              if (containerMgr.move(targetedSlot, i) || containerMgr.move(i, targetedSlot)) {
                if (!config.getProperty(InvTweaksConfig.PROP_ENABLE_SOUNDS).equals(InvTweaksConfig.VALUE_FALSE)) {
                    playSound("mob.chickenplop", 1.4F, 0.5F);
                }
View Full Code Here

TOP

Related Classes of net.minecraft.src.ItemStack

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.