having errors(not errors nothing) with a block arraylist in a hashmap

Discussion in 'Plugin Development' started by welsar55, Feb 22, 2013.

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

    welsar55

    i have a block array list in a hashmap like so
    Code:
    public final HashMap<String, ArrayList<Block>> sponge = new HashMap<String, ArrayList<Block>>();
    and then i try to add a block like this
    Code:
    @EventHandler
        public void blockplace(BlockPlaceEvent event)
        {
            Player player = event.getPlayer();
            if(player.hasPermission("fbffa.build"))
            {
               
            }
            else
            {
                if(event.getBlock().getType().equals(Material.SPONGE))
                {
                    if(plugin.sponge.containsKey(player.getName()))
                    {
                        plugin.sponge.get(player.getName()).add(event.getBlock());
                    }
                    else
                    {
                        plugin.sponge.put(player.getName(), null);
                        plugin.sponge.get(player.getName()).add(event.getBlock());
                    }
                }
                else
                {
                    event.setCancelled(true);
                }
            }
        }
    and i try to remove the sponge first the insta break does not work
    Code:
    @EventHandler
        public void blockbreak(BlockBreakEvent event)
        {
            Player player = event.getPlayer();
            if(player.hasPermission("fbffa.build"))
            {
               
            }
            else
            {
                if(event.getBlock().getType().equals(Material.SPONGE))
                {
                    event.getBlock().setType(Material.AIR);
                    if(plugin.sponge.get(player.getName()).contains(event.getBlock()));
                    {
                        plugin.sponge.get(player.getName()).remove(event.getBlock());
                    }
                }
                else
                {
                    event.setCancelled(true);
                }
            }
        }
    and a for loop to remove all the sponge does not work
    Code:
        if(plugin.sponge.containsKey(player.getName()))
            {
                for(Block block : plugin.sponge.get(player.getName()))
                {
                    plugin.sponge.get(player.getName()).remove(block);
                    block.setType(Material.AIR);
                }
            }
    any clue why? i have been working with this for a while
     
  2. Offline

    Tirelessly

    plugin.sponge.put(player.getName(), null);
    plugin.sponge.get(player.getName()).add(event.getBlock());
    Nullpointer

    plugin.sponge.get(player.getName()).add(event.getBlock());
    You never put that new list back in the map
     
  3. Offline

    welsar55

    how would i fix this never have work with a arraylist in hashmap

    would it be like this

    Code:
    ArrayList test = sponge.get(player.getname());
    test.add(block);
    sponge.put(player.getname(), test);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  4. Offline

    Tirelessly

    Yes
     
  5. Offline

    welsar55

    now every time i do somthing with the array i get a NPE
    it looks at everything that has to do with the array sponge
    heres my code
    Code:
        if(event.getBlock().getType().equals(Material.SPONGE))
                {
                    if(plugin.sponge.containsKey(player.getName()))
                    {
                        ArrayList<Block> sponge = plugin.sponge.get(player.getName());
                        sponge.add(event.getBlock());
                        plugin.sponge.put(player.getName(), sponge);
                    }
                    else
                    {
                        plugin.sponge.put(player.getName(), null);
                        ArrayList<Block> sponge = plugin.sponge.get(player.getName());
                        sponge.add(event.getBlock());
                        plugin.sponge.put(player.getName(), sponge);
                    }
                }
    and
    Code:
        if(plugin.sponge.containsKey(player.getName()))
            {
                ArrayList<Block> sponge = plugin.sponge.get(player.getName());
                for(Block block : sponge)
                {
                    block.setType(Material.AIR);
                    sponge.remove(block);
                }
                plugin.sponge.remove(player.getName());
            }
     
  6. Offline

    Geekola

    Why are you storing an ArrayList of the sponge (which is always a sponge). Are you sure you just want to keep a list of only the sponge's they break?

    Here is the code:

    Code:java
    1.  
    2. @EventHandler
    3. public void blockplace(BlockPlaceEvent event)
    4. {
    5.  
    6. if(event.getBlock().getType().equals(Material.SPONGE))
    7. {
    8.  
    9. if ( plugin.sponge.containsKey( event.getPlayer().getName() ) ) {
    10. this.plugin.sponge.get( event.getPlayer().getName()).add( event.getBlock() ) ;
    11. } else {
    12. this.plugin.sponge.put( event.getPlayer().getName(), new ArrayList<Block>( Arrays.asList( event.getBlock() ) ) );
    13. }
    14. }
    15. }


    HTHs

    PS. Any particular reason you are you making your public hashmap final?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  7. Offline

    Tirelessly

    plugin.sponge.put(player.getName(), null);

    And you wonder why you get an NPE?
     
  8. Offline

    welsar55

    fix the null

    fixed the null

    so i can use it in more then one class

    now this only removes one sponge though

    Code:
        if(plugin.sponge.containsKey(player.getName()))
            {
                ArrayList<Block> sponge = plugin.sponge.get(player.getname())
                for(Block block : sponge)
                {
                    plugin.sponge.get(player.getName()).remove(block);
                    block.setType(Material.AIR);
                }
            }
     
  9. Offline

    Geekola

    My suggestion won't give you an NPE?
    You can use the class in different places without it being final, final just means the map is immutable. Shouldn't really be a problem unless you needed to change the structure, but was just curious.

    Maybe it would help if you could explain what you're trying to do. If you are trying to keep a running list of all sponge's... when do you want to clear the list?

    Code:
    if ( plugin.sponge.containsKey( player.getName() ) plugin.sponge.get( player.getName() ).clear();
    
    
     
  10. Offline

    welsar55

    know i want to set the type of all the sponges that a player placed to air
     
  11. Offline

    Geekola

    Are you just trying to prevent them from placing sponges?
     
  12. Offline

    welsar55

    Geekola
    know when they a place a sponge it adds it to a array in a hashmap with the players name and then when i want i get ride of all there sponge
     
  13. Offline

    welsar55

Thread Status:
Not open for further replies.

Share This Page