Solved Removing entities from a HashMap

Discussion in 'Spigot Plugin Development' started by MaxNatural, May 14, 2016.

Thread Status:
Not open for further replies.
  1. Hello all!

    I want to spawn entities using HashMaps, which I have. So all the spawning of the entities is perfect but when I remove the player from the HashMap it doesn't remove the old pet. I use a GUI. I have an enderman pet and a pig pet. If I click the pig the pig spawns but if I click the enderman the other pig leaves the HashMap but doesn't disappear, but the enderman follows me.


    This below is me clicking the item in GUI and choosing the pet.

    Code (Text):
            case PORK:
                if (player.hasPermission("ch.pets.pig")) {
                new Pet().createPet(player, EntityType.PIG);
                player.closeInventory();
                } else {
                    player.sendMessage(Hub.noperm);
                    player.closeInventory();
                }
             
                break;

            case ENDER_PEARL:
                if (PetMain.Pets.containsKey(player.getPlayer())) {
                    new Pet().removePet(player.getPlayer(), null);
                    PetMain.Pets.remove(null);
                }
             
                if (player.hasPermission("ch.pets.enderman")) {
                    new Pet().createPet(player, EntityType.ENDERMAN);
                    player.closeInventory();
                } else {
                    player.sendMessage(Hub.noperm);
                    player.closeInventory();

                }
                break;

            }
        }
    HashMap:
    Code (Text):
        public static HashMap<String,Entity> Pets = new HashMap<String,Entity>();
     
    #1 MaxNatural, May 14, 2016
    Last edited: May 14, 2016
  2. You are creating a new pet object...? new Pet().removePet(player.getPlayer(), null); Since you checked you need to remove the Pet that is in the HashMap.
    if (PetMain.Pets.containsKey(player.getPlayer())) {
    YOUR_HASHMAP.get(player).removePet
    YOUR_HASHMAP.remove(player.getPlayer());
    }
     
  3. Code (Text):
        public void removePet(Player player,EntityType type){
            Entity entity = (Entity)player.getWorld().spawnEntity(player.getLocation(), type);
            entity.remove();
            PetMain.Pets.put(player.getName(), entity);
        }
    This is the removePet. I don't know how to get it to actually remove the pet.
     
  4. Code (Text):
    if (PetMain.Pets.containsKey(player.getPlayer())) {
                    new Pet().removePet(player.getPlayer(), null);
                    PetMain.Pets.remove(null);
                }
    You are saying containsKey(player.getPlayer())) while your HashMap is <String, Entity>. Meaning you need to do player.getName() or player.getUniqueID().toString() (depending on which way you want to do it, or how to already set it up).
     
  5. Like so?
    Code (Text):
        public void removePet(Player player){
            if (PetMain.Pets.containsKey(player.getPlayer())) {
                new Pet().removePet(player.getPlayer());
            }
     
  6. No, by that I mean you can't do "player.getPlayer()", player is already a player, and the HashMap takes in a String and an Entity, not a Player and an Entity. You should store player.getName() instead of player.getPlayer() unless you want to make your HashMap take in a Player and an Entity.
     
  7. How would I do this?
     
  8. Basically you need to replace "player.getPlayer()" everywhere in your project that include Pets as a hashmap, with "player.getName()". Don't mean to approach you in the wrong direction, but you should learn Java before attempting this :p.
     
Thread Status:
Not open for further replies.