Call variable on Java Help

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

  1. If in the class "Class1" I create this metod:
    Code (Text):
    public ItemStack a = createItem(new ItemStack(Material.SKULL_ITEM, 1, (byte) 3), "MHF_Chest");


    public void spawnPet(Player player) {

    int ab;
    ArmorStand as = (ArmorStand) player.getLocation().getWorld().spawn(player.getLocation().add(0,-2,0), ArmorStand.class);
    as.setCustomName( player.getName()+" 's ChestPet");
    as.setCustomNameVisible(true);
    as.setInvulnerable(true);
    as.setHelmet(a);
    as.setVisible(false);


    }
    And in the Class Event I want move the Armorstand:

    Code (Text):
    @EventHandler[/USER]

        public void onMove(PlayerMoveEvent event) {
       
            Class1 c1 = new Class1();;
       
            Player player = (Player)event.getPlayer();
            Location loc = player.getLocation();
            World world = loc.getWorld();
            //c1.as.setVelocity(etc);
        }
    How I have to do?
     
  2. put the player and the pet in a HashMap<UUID, ArmorStand>
     
  3. Can you explain me better how HashMap works? If you want
     
    • Agree Agree x 1
  4. Code (Text):
        public void spawnPet(Player player) {
           

           
            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));
        }
    I tried with this Code, but it give me an Error in Console when i execute the Command that call the Class spawnPet
     
  5. your hashmap should be static
     
    • Optimistic Optimistic x 2
    • Funny Funny x 1
    • Winner Winner x 1
  6. static HashMap<UUID, ArmorStand> armorstands;

    It still now working
     
    #7 Dantesudante, May 30, 2017
    Last edited: May 30, 2017
  7. [​IMG]

    [​IMG]

    [​IMG]

    [​IMG]

    Command and PlayerMovementEvent error (Console)

     
  8. no. no it shouldn't..

    you should keep one instance of the class that contains the hashmap and stored values and retrieve that instance when needing to access stuff from the map.
     
    • Agree Agree x 2
  9. Why do you even...
     
  10. I would actually create a custom pet object and add that to the hashmap. The rationale in this case being, more elaborate "pets" tend to stack ArmorStands and this way its "easier" track those armor stands. Additionally, A pet class gives, you more control as you can define and control the properties and behaviour. I.e Say you wanna add a path finding algorithm to the pet.

    This is not helpful in the slightest. Instead of insulting him, why don't you extend his knowledge by telling him why you think it's wrong.

    @ChefJava Wow I love your name
     
  11. Excuse me but im writing from the phone. Is the hashmap null? Have you set any values to it?
     
  12. Sorry if it sounded insulting, but when programming in an OOP language, I shouldn't have to explain why it's bad to not use OOP. It contradicts itself quite clearly, if you ask me. It's also been answered hundred, if not thousands, of times. I believe just directing the OP away from that statement is more than enough.

    But that's just my opinion, and I am a firm believer of having arguments and information as backup, but when it is this obvious...
     
  13. So, what I have to do?
     
  14. Learn Java.
    We are not here to teach you java, we're here exclusively to offer support for the Bukkit API.
     
  15. Any solution for my problem?
     
  16. Just stated it.
     
  17. So, I must don't use an hashmap? I have to find another solution?