Help with Armor Stand

Discussion in 'Plugin Development' started by Mornov, Dec 7, 2020.

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

    Mornov

    Hi! im new here, so here i want to make a boomerang, but i don't know how to make it back to player, so i want to make the boomerang will going back to player after reach 8 blocks forward.

    this code only make the armor stand always move forward.

    My code:
    Code:
    @EventHandler
    public void BonemerangSpawn(PlayerInteractEvent e){
    Player p = e.getPlayer();
    if ((e.getAction() == Action.RIGHT_CLICK_AIR) && (p.getItemInHand().getType() == Material.BONE)) {
    Location loc = p.getLocation();ArmorStand bone = (ArmorStand) p.getWorld().spawnEntity(p.getLocation().add(0, 0, 0), EntityType.ARMOR_STAND);
    ItemStack skull = new ItemStack(Material.BONE, 1);
    bone.setItemInHand(skull);
    bone.setVisible(false);
    bone.setBasePlate(false);
    bone.setInvulnerable(true);
    bone.setGravity(false);
    new BukkitRunnable() {
    @Override
    public void run() {
    if (!p.isOnline()) {
    bone.remove();} else {
    Vector homingforward = p.getLocation().getDirection().normalize().multiply(1).add(new Vector(0, 0, 0));
    bone.teleport(bone.getLocation().add(homingforward));
    bone.setRightArmPose(bone.getRightArmPose().add(0, 0, 0.5));}
    
    }
    }.runTaskTimer(this, 1, 1);
    }
    }
    }
    
     
  2. Offline

    Chr0mosom3

    Save the original position of the boomerang, and every time you move the entity, check the distance of the current entity, and if it's over 8, cancel the timer, start a new timer going towards the player
     
  3. Offline

    Mornov

    can you give me the example?
     
  4. Offline

    Chr0mosom3

    Whenever you spawn a bomerang, put it in a hashmap along with it's orignal position <ArmorStand, Location>, along with it's velocity. Then in a Scheduler that runs every tick, get the armor stand's location from the HashMap, and use the #distance(Location loc) method to get the distance from the player, if over 8, remove from the hashmap, and add to another hashmap. Then later in the same scheduler, loop over everything in the second hashmap, set the velocity torwards the player, and if within 0.5 blocks of the player, delete the entity
     
Thread Status:
Not open for further replies.

Share This Page