Unable to add ArmorStand to list

Discussion in 'Spigot Plugin Development' started by craterhater, May 14, 2017.

  1. Hey there, I am trying to make an armorstand follow the player. I made this very basic thing but it doesn't work at all:

    Code (Text):
    public void owlr(Location loc, Player p){
            ArmorStand a = (ArmorStand) loc.getWorld().spawnEntity(loc, EntityType.ARMOR_STAND);
            ItemStack is = new ItemStack(Material.SKULL_ITEM,1,(byte)3);
            SkullMeta km = (SkullMeta) is.getItemMeta();
            km.setOwner("xBenji118x");
            is.setItemMeta(km);
            a.setHelmet(is);
         
             owlsx.add(a);
            owlOwner.put(a, p.getName());
         
            p.sendMessage("stored");
        }

        ArrayList<ArmorStand> owlsx = new ArrayList<ArmorStand>();
        HashMap<ArmorStand,String> owlOwner = new HashMap<ArmorStand,String>();  
     
        public void tick(){
            System.out.print(owlsx);
            for(ArmorStand a : owlsx){
                Player p = Main.getPlayer(owlOwner.get(a));
                if(p != null){
                 
                    Location loc = new Location(a.getWorld(),a.getLocation().getX() + calcX(a,p),a.getLocation().getY(),a.getLocation().getZ() + calcZ(a,p));
                    a.teleport(loc);
                }else{
                    owlsx.remove(a);
                    owlOwner.remove(a);
                    a.remove();
                }
            }
        }
    Debug:

    "stored"
    The console spams: "[]"

    Tick gets run, every tick.

    It doesn't get past: "for(ArmorStand a : owlsx){"

    Thanks!
     
  2. Hey,
    is the ArmorStand spawning? Try to cast the ArmorStand to an Entity and save the id in the lists.
     
  3. Try not to store entities and players in lists because it can cause serious performance issues.
    Instead store their unique id's, as every single different entity and player has their own id, they can never be the same :)