Thread: 614 Cleaner method to sort bank

Results 1 to 6 of 6
  1. #1 614 Cleaner method to sort bank 
    Registered Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks given
    0
    Thanks received
    0
    Rep Power
    0
    I've written a method to sort a players bank by the item id of each item. It works, that's not my problem. I'm just curious if there is a cleaner way of doing this?

    Code:
    public boolean sortBankByID() {
    		Item[] bankItems = new Item[bank.getItems().length];
    		Item[] items = new Item[32767];
    		for (int i = 0; i < bankItems.length; i++) {
    			if (bank.get(i) != null) {
    				bankItems[i] = bank.get(i);
    			}
    		}
    		for (int i = 0; i < bankItems.length; i++) {
    			if (bankItems[i] != null) {
    				int slot = bankItems[i].getId();
    				items[slot] = new Item(bankItems[i].getId(), bankItems[i].getAmount());
    				bank.removeAll(bankItems[i]);
    			}
    		}
    		int j = 0;
    		for (int i = 0; i < items.length; i++) {
    			if (items[i] != null) {
    				bank.set(j, items[i]);
    				j++;
    			}
    		}
    		refresh();
    		return true;
    	}
    Reply With Quote  
     

  2. #2  
    Registered Member
    Emily's Avatar
    Join Date
    Jul 2010
    Age
    30
    Posts
    622
    Thanks given
    67
    Thanks received
    215
    Rep Power
    271
    Code:
    public boolean sortBankByID() {
    		Item[] bankItems = new Item[bank.getItems().length];
    		Item[] items = new Item[32767];
    		for (int i = 0; i < bankItems.length; i++) {
                            if (bank.get(i) != null) {
    				bankItems[i] = bank.get(i);
    			}
    			if (bankItems[i] != null) {
    				int slot = bankItems[i].getId();
    				items[slot] = new Item(bankItems[i].getId(), bankItems[i].getAmount());
    				bank.removeAll(bankItems[i]);
    			}
    		}
    		for (int i = 0; i < items.length; i++) {
    			if (items[i] != null) {
    				bank.set(i, items[i]);
    			}
    		}
    		refresh();
    		return true;
    	}
    not sure, you are using the same loop (bankItems.length) so you should be able to combine those. & the int j = 0;, The value of int i is already 0, and since you are increase J by 1 each loop (which variable i does anyways) why not just i instead of j

    Anyways, that's the way I saw it. I might be wrong.
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks given
    0
    Thanks received
    0
    Rep Power
    0
    The reason I used a separate counter, j, is that 'i' will go from 0 to 32766 which is far beyond the bounds of the bank.
    Reply With Quote  
     

  4. #4  
    Registered Member
    Emily's Avatar
    Join Date
    Jul 2010
    Age
    30
    Posts
    622
    Thanks given
    67
    Thanks received
    215
    Rep Power
    271
    Quote Originally Posted by Growlith View Post
    The reason I used a separate counter, j, is that 'i' will go from 0 to 32766 which is far beyond the bounds of the bank.
    I see, but your J value would of done the same thing since it is increased each loop until i = 32767. Not entirely sure what you are doing tbh xD. Like the point of organizing by item id =P
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks given
    0
    Thanks received
    0
    Rep Power
    0
    This loop
    Code:
    for (int i = 0; i < items.length; i++) {
    			if (items[i] != null) {
    				bank.set(j, items[i]);
    				j++;
    			}
    		}
    cycles through items from 0 to items.length. For instance, if the first non-null item within items is coins, that index would be 995. Following that for instance, j will then increase because the item at that index was not null.
    Reply With Quote  
     

  6. #6  
    Registered Member
    Emily's Avatar
    Join Date
    Jul 2010
    Age
    30
    Posts
    622
    Thanks given
    67
    Thanks received
    215
    Rep Power
    271
    Quote Originally Posted by Growlith View Post
    This loop
    Code:
    for (int i = 0; i < items.length; i++) {
    			if (items[i] != null) {
    				bank.set(j, items[i]);
    				j++;
    			}
    		}
    cycles through items from 0 to items.length. For instance, if the first non-null item within items is coins, that index would be 995. Following that for instance, j will then increase because the item at that index was not null.
    Makes sense now that you put it that way. Well i guess you can only combine the 1st and 2nd loop then, re add your J snazz to what I posted.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [Impact/PI] Much cleaner Bank pin base
    By Armo in forum Snippets
    Replies: 99
    Last Post: 11-19-2012, 10:03 AM
  2. [Delta]Cleaner FreeSlots Method
    By TheElveAge in forum Snippets
    Replies: 5
    Last Post: 05-20-2010, 09:19 PM
  3. [Delta] - Npchit method redone - 150% cleaner
    By TheElveAge in forum Snippets
    Replies: 11
    Last Post: 04-15-2010, 03:18 AM
  4. ::bank Sort of command
    By profusion in forum Requests
    Replies: 0
    Last Post: 08-17-2009, 04:12 AM
  5. Cleaner RefreshSkills method
    By OG KingFox in forum Configuration
    Replies: 7
    Last Post: 09-12-2008, 09:49 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •