[Solved] Spawning bats with players items

Discussion in 'Spigot Plugin Development' started by TimeVisualSales, May 26, 2015.

Thread Status:
Not open for further replies.
  1. Hi there, I want to add a feature to my minigame where after 1 minute of the timer a few bats spawn around a player but get random items out of the players inventory for each bat. I'm guessing you'll set the item as the passenger of the bat but how would I get the item out of the players inventory? Many thanks

    Sorry, In a few minutes of posting this thread I might be asleep.
     
  2. Create a random variable and take a take a randomnumber from the invsize. Then get the index (your number).

    You have your item now, loop through his inventory and if he has that item remove all of it.
     
    #2 Svenna, May 26, 2015
    Last edited: May 26, 2015
    • Like Like x 1
  3. I cant test it atm, but i think you are looking for the following:

    Edit: Look at my next post for solution


    Every time you call this method a new Bat will get spawned with one random item :)
     
    #3 Friwi, May 26, 2015
    Last edited: May 26, 2015
  4. I'll see if this works ;)
     
  5. It does work thanks but, when the bat takes the item out of the players inventory they still have the item but the bat also has it. How can I fix this?
     
  6. Code (Text):
    public void takeOutRandomItem(Player p){
            //Count all items in inventory
            int items = 0;
            for(int i = 0; i<p.getInventory().getContents().length;i++){
                if(p.getInventory().getContents()[i]!=null)items++;
            }
            //Choose one Item randomly
            int random = (int) (Math.random()*(items-0.001));
            int index = -1;
            int found = 0;
            for(int i = 0; i<p.getInventory().getContents().length;i++){
                if(p.getInventory().getContents()[i]!=null){
                    if(found == random){
                        index = i;
                        break;
                    }
                    found++;
                }
            }
            if(index==-1)return;
            if(p.getInventory().getContents()[index]!=null){
                //Spawn the Bat & the ItemStack in a random position near the player
                ItemStack s = p.getInventory().getContents()[index];
                p.getInventory().remove(s);
                Bat bat = (Bat) p.getWorld().spawnEntity(p.getLocation().add(Math.random()*4-2, 1+Math.random()*1.5, Math.random()*4-2), EntityType.BAT);
                Item item = p.getWorld().dropItem(p.getLocation().add(0, 10, 0), s);
                //Make the item travel on the Bat
                bat.setPassenger(item);
                //Update the players inv
                p.updateInventory();
            }
        }
     
    • Like Like x 1
  7. Yeah, I did that but this occurs:
    [​IMG]
    This shows that when the bats have your items, there still in your inventory but glitch because when you right-click them, they just remove from your inventory.
     
  8. Ow works now. For got to update the players inventory.
     
  9. That was why i modified my code ;) i didn't repost the same code :D
     
    • Like Like x 1
  10. Nice, that was what I said but not with the bat spawning. Btw, spoonfeeding isn't good for people that want to learn. You can give him some code and let him figure out the rest. But yeah, that's my opinion.
     
    • Agree Agree x 1
  11. Yeah i know. I dont want to spoonfeed people, but I give them small portions of code which they have to integrate on their own. Thats how I learned programming :) Its still his job to call the function at the right time and run a timer etc. And i included comments, so he understands what i did
     
  12. I have the same problem.. The players items in their inventory don't disappear. But they are really gone.
     
  13. You need to update the players inventory.
     
  14. Is that something like:
    Code (Text):
    p.getInventory().update();
    EDIT: oh wait. It's p.updateInventory();
     
  15. It's
    Code (Text):
    player.updateInventory();
     
  16. Ja, ich habe es jetzt. Vielen Dank.
     
    • Like Like x 1
Thread Status:
Not open for further replies.