Jump to content

I need to pick up multiple loot from one NPC.


Mokrocaan

Recommended Posts

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 by Mokrocaan
Link to comment
Share on other sites

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.

  • Heart 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...