[Unsolved]Dropping only part of your inventory.

Discussion in 'Plugin Development' started by Sheepii, Nov 8, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Sheepii

    Code:
    HashMap<String, ItemStack[]> inventories = new HashMap<String, ItemStack[]>();
    @EventHandler
        public void onNoobDeath(PlayerDeathEvent event)
        {
            if(event.getEntity() instanceof Player)
            {
                Player p = event.getEntity();
                ItemStack[] isa = p.getInventory().getContents();
                Random rand = new Random();
                if(ConfigController.contains(p.getName())){
                    int number = rand.nextInt(10) + 1;
                    if(number <= 9)
                    {
                        inventories.put(p.getName(), isa);
                        event.getDrops().clear();
                    }
                    else if(number == 10)
                    {
                        if(ConfigController.contains(p.getName()))
                        {
                            List<ItemStack> isatolist = Arrays.asList();
                            Random rand2 = new Random();
                            int num = rand2.nextInt(isatolist.size());
     
                            ItemStack i = new ItemStack(isatolist.get(num));
                            int e = i.getMaxStackSize();
                            isatolist.remove(new ItemStack(i, e));
     
                            inventories.put(p.getName(), isatolist.toArray(new ItemStack[isatolist.size()]));
     
                            p.getLocation().getWorld().dropItemsNaturally(p.getLocation(), i, e);
                            event.getDrops().clear();
                        }
                    }
                }
            }
    Alright, my code is suppose to, on PlayerDeathEvent get a random number between 1-10, if it's 9, it saves their inventory and gives it back to them on PlayerRespawnEvent.

    Basically I want my code to drop the MaxStack of the item it gets from the saved inventory, to the ground, and remove the item from the istolist. But I don't really know how. Could anyone please help?
     
  2. Offline

    zeeveener

    Isn't there an object named PlayerInventory? Why not use that as the second type in the inventories HashMap instead. Then you can remove that object from its corresponding slot and save the inventory back into the HashMap?

    That's what I would attempt to do at least.
     
  3. Offline

    Sheepii

    k I'm trying this out, but I ran into a problem


    Code:
    HashMap<String, PlayerInventory> inventories = new HashMap<String, PlayerInventory>();
     
    List<PlayerInventory> isatolist = Arrays.asList();
     
    inventories.put(p.getName(), isatolist);
    PlayerInventory can't cast to List<PlayerInventory>. How do I fix this?

    Alright, I fixed the previous problem by saving the item that is going to be taken out to a hashmap and then taking it away once they respawn. However, how do I set their inventory from:
    Code:
    HashMap<String, PlayerInventory> inventories = new HashMap<String, PlayerInventory>();
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  4. Offline

    HamOmlet

    Just grab a key from the map and you should be able to get their inventory:
    Code:
    inventories.get(player.getName())...
     
  5. Offline

    Sheepii

    I didn't say how you get it. I said how to do you set it.
     
  6. Offline

    Liutenantpickle

Thread Status:
Not open for further replies.

Share This Page