ikk Posted January 14, 2016 Share Posted January 14, 2016 I'm working on a script right now and I have a way that I can determine which loot the player should pick up but since I'm new with Java I'm sure it's not optimized. By default, the script will pick up one category of items I am giving the user the option to additionally pick up some valuable items that may come up Here is my current way of determining if they should pick up the valuables and merge the lists of items: public String[] getLoot() { if (!Config.enableLooting) { List<String> loot = new ArrayList<String>(Constants.LOOT_ARMOUR.length + Constants.LOOT_TOKEN.length); Collections.addAll(loot, Constants.LOOT_ARMOUR); Collections.addAll(loot, Constants.LOOT_TOKEN); return loot.toArray(new String[loot.size()]); } else { ArrayList<String> temp = new ArrayList<String>(); temp.addAll(Arrays.asList(Constants.LOOT_ARMOUR)); temp.addAll(Arrays.asList(Constants.LOOT_TOKEN)); temp.addAll(Arrays.asList(Constants.LOOT_VALUABLE)); return temp.toArray(new String[Constants.LOOT_ARMOUR.length + Constants.LOOT_TOKEN.length + Constants.LOOT_VALUABLE.length]); } } This way does seem to work but I would love to hear other ideas on how this might be done Quote Link to comment Share on other sites More sharing options...
Precise Posted January 14, 2016 Share Posted January 14, 2016 I'm working on a script right now and I have a way that I can determine which loot the player should pick up but since I'm new with Java I'm sure it's not optimized. By default, the script will pick up one category of items I am giving the user the option to additionally pick up some valuable items that may come up Here is my current way of determining if they should pick up the valuables and merge the lists of items: public String[] getLoot() { if (!Config.enableLooting) { List<String> loot = new ArrayList<String>(Constants.LOOT_ARMOUR.length + Constants.LOOT_TOKEN.length); Collections.addAll(loot, Constants.LOOT_ARMOUR); Collections.addAll(loot, Constants.LOOT_TOKEN); return loot.toArray(new String[loot.size()]); } else { ArrayList<String> temp = new ArrayList<String>(); temp.addAll(Arrays.asList(Constants.LOOT_ARMOUR)); temp.addAll(Arrays.asList(Constants.LOOT_TOKEN)); temp.addAll(Arrays.asList(Constants.LOOT_VALUABLE)); return temp.toArray(new String[Constants.LOOT_ARMOUR.length + Constants.LOOT_TOKEN.length + Constants.LOOT_VALUABLE.length]); } } This way does seem to work but I would love to hear other ideas on how this might be done just some things i would change: i dont think you need to set the parameter of a new arraylist the size of the 2 other arrays since arraylists are dynamic and not static like arrays. I dont know if you plan on adding any more arrays of loot but maybe make a 2d array of the loot and use a for loop and go through each one and add them? I would maybe think of a better way of storing your loot because the way you have currently done it should be ok but could be better if stored differently Quote Link to comment Share on other sites More sharing options...
LoudPacks Posted January 14, 2016 Share Posted January 14, 2016 (edited) I'm working on a script right now and I have a way that I can determine which loot the player should pick up but since I'm new with Java I'm sure it's not optimized. By default, the script will pick up one category of items I am giving the user the option to additionally pick up some valuable items that may come up Here is my current way of determining if they should pick up the valuables and merge the lists of items: code This way does seem to work but I would love to hear other ideas on how this might be done String[] lootItems = {"Blue partyhat", "White partyhat"}; for(GroundItem item : getGroundItems().getAll()){ if(item != null && Arrays.asList(lootItems).contains(item.getName())){ log("Looted " + item.getAmount() + " " + item.getName() + "."); item.interact("Take"); new ConditionalSleep(3000, 4000){ @Override public boolean condition(){ return !item.exists(); } }.sleep(); } } Maybe I misinterpreted what you meant. Anyhow, you could also check their osbuddy price and loot if the val > const. Edited January 14, 2016 by LoudPacks Quote Link to comment Share on other sites More sharing options...
FrostBug Posted January 14, 2016 Share Posted January 14, 2016 (edited) Your way of doing it is not really incorrect. Tho I find it highly strange that you do the same thing in 2 very different ways in those 2 halves of the if statement.. The else block has a bunch of unnecessary array/list conversions. You can avoid all the conversions using an approach like such: String[] itemArr = new String[Constants.ARMOR.length + Constants.TOKENS.length]; System.arrayCopy(Constants.ARMOR, 0, itemArr, 0, Constants.TOKENS.length); System.arrayCopy(Constants.TOKENS, 0, itemArr, Constants.ARMOR.length, Constants.TOKENS.length); return itemArr; (wrote that out in hand, mite have some typos). Do what you want tho, the difference in performance wont be noticable if its just gonna be used once Edited January 15, 2016 by FrostBug 1 Quote Link to comment Share on other sites More sharing options...
ikk Posted January 15, 2016 Author Share Posted January 15, 2016 String[] lootItems = {"Blue partyhat", "White partyhat"}; for(GroundItem item : getGroundItems().getAll()){ if(item != null && Arrays.asList(lootItems).contains(item.getName())){ log("Looted " + item.getAmount() + " " + item.getName() + "."); item.interact("Take"); new ConditionalSleep(3000, 4000){ @Override public boolean condition(){ return !item.exists(); } }.sleep(); } } Maybe I misinterpreted what you meant. Anyhow, you could also check their osbuddy price and loot if the val > const. Using osbuddy would be a nice thing to add, but wouldn't cover all the items that I might like to pick up as the monsters I am fighting also drop some worthwhile non-tradeable items. So in other words I'm trying to get the script to: -100% of the time pick up defenders and similar items -Give option for other items such as valuables and some other non-tradeables (Don't need to let them choose item-by-item) Your way of doing it is not really incorrect. Tho I find it highly strange that you do the same thing in 2 very different ways in those 2 halves of the if statement.. The else block has a bunch of unnecessary array/list conversions. You can avoid all the conversions using an approach like such:String[] itemArr = new String[Constants.ARMOR.length + Constants.TOKENS.length);System.arrayCopy(Constants.ARMOR, 0, itemArr, 0, Constants.TOKENS.length); System.arrayCopy(Constants.TOKENS, 0, itemArr, Constants.ARMOR.length, Constants.TOKENS.length); return itemArr; (wrote that out in hand, mite have some typos). Do what you want tho, the difference in performance wont be noticable if its just gonna be used once Yeah it kinda shows in my code that I'm not very comfortable with this yet. The "reason" for doing it in two different ways is because the method in the first if statement wouldn't allow me to enter in 3 parameters in the ArrayList so I tried getting it to work using the method in the else statement. Thanks a bunch for your input Quote Link to comment Share on other sites More sharing options...