Mokrocaan Posted September 16, 2020 Share Posted September 16, 2020 (edited) Hello all, I don't know if I can ask everyday for help on the forums so if it's not allowed please let me know. I am currently working on a kill and loot script. Everything is working fine but the only problem is that I cannot find a solution to pickup multiple loot from one NPC. This is the function where Im having trouble with: ======================================================================== public void lootClosestItem(String[] Item_Name, Area Target_Area) throws InterruptedException{ Player player = myPlayer(); Inventory inven = getInventory(); final Area TARGET_AREA = Target_Area; Area playerArea = player.getArea(7); //GroundItem loot = groundItems.closest(l -> l.getName().equals(Item_Name) && playerArea.contains(l)); for(int i = 0; i < Item_Name.length; i++) { List<GroundItem> loot = groundItems.filter(l -> l.getName().equals(Item_Name[i]) && playerArea.contains(l)); if(!inven.isFull() && TARGET_AREA.contains(player)) { if (loot != null && loot.exists() &&loot.isVisible()) { loot.interact("Take"); sleep(random(500,1000)); } } else getCamera().toEntity(loot); } } ======================================================================== It works fine when I make a string variable for one item. But when I made a list of grounditems i did get an error in the condition to check for visibilty and existence. The GetCamera() function doesnt work either. What am I doing wrong? Edited September 16, 2020 by Mokrocaan Quote Link to comment Share on other sites More sharing options...
Chris Posted September 16, 2020 Share Posted September 16, 2020 learn java first pls 1 Quote Link to comment Share on other sites More sharing options...
Medusa Posted September 16, 2020 Share Posted September 16, 2020 26 minutes ago, Chris said: learn java first pls ngl I have to agree with chris Quote Link to comment Share on other sites More sharing options...
BravoTaco Posted September 16, 2020 Share Posted September 16, 2020 2 hours ago, Mokrocaan said: Hello all, I don't know if I can ask everyday for help on the forums so if it's not allowed please let me know. I am currently working on a kill and loot script. Everything is working fine but the only problem is that I cannot find a solution to pickup multiple loot from one NPC. This is the function where Im having trouble with: ======================================================================== public void lootClosestItem(String[] Item_Name, Area Target_Area) throws InterruptedException{ Player player = myPlayer(); Inventory inven = getInventory(); final Area TARGET_AREA = Target_Area; Area playerArea = player.getArea(7); //GroundItem loot = groundItems.closest(l -> l.getName().equals(Item_Name) && playerArea.contains(l)); for(int i = 0; i < Item_Name.length; i++) { List<GroundItem> loot = groundItems.filter(l -> l.getName().equals(Item_Name[i]) && playerArea.contains(l)); if(!inven.isFull() && TARGET_AREA.contains(player)) { if (loot != null && loot.exists() &&loot.isVisible()) { loot.interact("Take"); sleep(random(500,1000)); } } else getCamera().toEntity(loot); } } ======================================================================== It works fine when I make a string variable for one item. But when I made a list of grounditems i did get an error in the condition to check for visibilty and existence. The GetCamera() function doesnt work either. What am I doing wrong? Don't worry about asking to many questions, that's how you learn. As stated though, learning java and its fundamentals would help greatly with this, but I will try to give some information and maybe help to point you in the right direction. As the current code stands the logic is this 1st -> You declare some variables. The Player variable, setting it to myPlayer() is unneeded and you and should just use myPlayer() when you want to access that particular object. Also with calling myPlayer(), since it is more than just a variable call, and is actually a method call, it could be doing more than just returning the variable itself. So when you set a Player variable with myPlayer() you may actually be using an older version of that Player object when you later call upon that variable. The same from above also applies to the Inventory object. The TARGET_AREA does not need to be a variable as you can just use the one that is passed in via the method parameters since the area does not change from the initial parameter value to the end result. Setting the playerArea variable is actually okay here, and is probably the only variable that I would create in this method. 2nd -> You than move onto the looting-loop. Right off the bat you mess up the for-loop which is fine again, you just need to learn the fundamentals of java. We've all been there Here is a link to for-loops to better help explain them. I will try to explain how the current one is wrong, and some things you could do to fix it. for(int i = 0; i < Item_Name.length; i++) In the line of code above, you are starting a for loop with i is equal to 0, and you will than loop as long as i is less than the string variables length, and increment 'i' each loop by one. The problem with this line of code is your conditional, which is the second statement "i < Item_Name.length". With this you are essentially setting the loop to run according to how many characters/letters there are in the string variable, and not actually setting the loop to run equal to how much loot there is to pick-up from the ground. To fix this you could declare the available loot before entering the for-loop and using the length/count from that array/list, in the conditional statement of your for-loop. List<GroundItem> loot = groundItems.filter(l -> l.getName().equals(Item_Name[i]) && playerArea.contains(l)); In the line of code above, you set the ground items you want to pick-up which is correct, but you are doing it every time you loop in the for-loop. I recommend moving this above the for-loop and than create the for loop to iterate through it. Here is a link to looping through lists/arrays. if(!inven.isFull() && TARGET_AREA.contains(player)) Moving further down in the for-loop we come across the first condition check. Which states if the players inventory is not full and the TARGET_AREA contains the player than return true. The first check pertaining to the inventory is good, and needed in this situation. The second check pertaining to the area check, is not needed and also not logically correct. What you need to be checking is if the GroundItem you are wanting to loot is within the TARGET_AREA not the player, otherwise you will have cases where even though the player is within the area, the loot may not be, and that can cause the player to loot items outside of the specified range. if (loot != null && loot.exists() &&loot.isVisible()) Good job doing a null check, those can be easily forgotten. But the function completely falls apart after this check. Since as you are not referencing just one item when you call the loot variable, but are actually referencing an entire list/array of them. This will be fixed if you refactor the code and move declaring the available loot before the for-loop and looping through that, which will return a single item from the list each loop. loot.interact("Take"); sleep(random(500,1000)); Again the loot variable is not just a single item and so the interact() will not work on it. The sleep should be a ConditionalSleep here is a link to those look under the sleep section. Hope this helps to clear some things up, If I made you more confused I am sorry for that I'm not the best at explaining things through text. Let me know if you have any questions. 1 Quote Link to comment Share on other sites More sharing options...
Mokrocaan Posted September 17, 2020 Author Share Posted September 17, 2020 Thankyou for actually helping me out! This is great! I'm gonna exercise my Java programming skills more l, I see I have made some dumb mistakes. Thank you again! Quote Link to comment Share on other sites More sharing options...
BravoTaco Posted September 18, 2020 Share Posted September 18, 2020 23 hours ago, Mokrocaan said: Thankyou for actually helping me out! This is great! I'm gonna exercise my Java programming skills more l, I see I have made some dumb mistakes. Thank you again! Np. Let me know if ya have any questions. 1 Quote Link to comment Share on other sites More sharing options...