Solved Help with HashMap

Discussion in 'Spigot Plugin Development' started by Dantesudante, May 31, 2017.

  1. Code (Text):
        public void spawnPet(Player player) {
           

           static HashMap<UUID, ArmorStand> armorstands;

            ArmorStand as  = (ArmorStand) player.getLocation().getWorld().spawn(player.getLocation().add(0,-2,0), ArmorStand.class);      
            as.setCustomName( player.getName()+" 's Pet");
            as.setCustomNameVisible(true);
            as.setInvulnerable(true);
            as.setHelmet(a);
            as.setVisible(false);
            as.setCanPickupItems(false);
           // Vector v = player.getLocation().getDirection().normalize();
           // as.setVelocity(v);
            //as.teleport(player.getLocation().add(-1,0,0));
            as.setVelocity(player.getLocation().toVector().subtract(as.getLocation().toVector()).multiply(0.2));
            // Add the armorstand to the HashMap
            armorstands.put(player.getUniqueId(), as);
    Code (Text):
        @EventHandler
       
        public void onMove(PlayerMoveEvent event) {
           
            //Pet pet = new Pet();  
           
            Player player = (Player)event.getPlayer();
            Location loc = player.getLocation();
            World world = loc.getWorld();
           
            if(Pet.armorstands.containsKey(player.getUniqueId())) {
               
                // Gets the armorstand from the HashMap
                ArmorStand as = Pet.armorstands.get(player.getUniqueId());
               
                // Do a Vector to player
                as.teleport(player);
                //as.setVelocity(player.getLocation().toVector().subtract(as.getLocation().toVector()).multiply(0.2));
        }


    [​IMG]

    [​IMG]

    [​IMG]

    Command and PlayerMovementEvent error (Console)
    Can someone help me to fix this Error?
     
    • Funny Funny x 1
  2. Choco

    Moderator

    How did your code even compile in the first place? You can't have a static modifier within a method... You really also shouldn't be using static as an access modifier if you don't understand what it does. At this point, reading up on a bit more Java would assist you in solving your own issues. You can start here
     
    • Agree Agree x 1
  3. First of you already should receive an error when creating a static hashmap inside your method.
    Also you haven't even initialized the map somewhere.
    Also how would you get access to your map when its inside a method? that doesn't even make sense at all? maybe just copy & pasted it from somewhere?
     
    • Agree Agree x 1
  4. Do you use eclipse/the eclipse compiler and just happen to ignore compile errors? Because I believe the eclipse compiler can still compile with errors, and the static HashMap is definitely an error. Anyway, move the HashMap out of the method, and it should work.
     
  5. Yeah sorry, my fault. I copied and pasted (for error, in this thread) the hashmap inside the method (in the code is outside)
     
  6. I use NetBeans; My fault, the hashmap was out of the method but I copied and pasted It from the Code without see that I put it inside the method (In this thread)
     
  7. you still havent initialized the hashmap
     
  8. Code (Java):
    public void myMethod() {
        new Object() {
            private static final String staticInMethod = "lelele";
        };
    }
     
  9. You can instantiate a map within a method.


    Sent from my iPhone using Tapatalk
     
  10. Choco

    Moderator

    D: well fine. If you wanna get into specifics, SUREEE