Flying sword...

Discussion in 'Spigot Plugin Development' started by Creator, Feb 16, 2019.

  1. Hello to all!
    I would like to make a flying sword like in a Murder Mystery mini-game.
    But I don't understand how can send a sword to air.
    Thanks for any help.

    Code (Text):

    new BukkitRunnable() {
        int t = 60;
        Location swordLocation = p.getLocation();

        @Override
        public void run() {
            if (t > 0) {
                ArmorStand swordHolo = (ArmorStand) p.getWorld().spawnEntity(swordLocation, EntityType.ARMOR_STAND);
                swordHolo.setArms(true);
                swordHolo.setBasePlate(false);
                swordHolo.setVisible(false);
                swordHolo.setGravity(false);
                swordHolo.setItemInHand(new ItemStack(Material.IRON_SWORD));
            }

            if (t > 0) {
                t--;
            }
        }
    }.runTaskTimer(Main.getPlugin(), 2L, 2L);
     
     
  2. Your code seems fine,what's the issue?
     
  3. I want the sword to fly forward through the gaze of the player.
     
  4. Your code will just create 60 armor stand at the same location with an iron sword in the hand, i'm not sure that's what you want
     
  5. I want the sword to fly forward through the gaze of the player.
     
  6. What's currently happening?
     
  7. I don't know how this can be done. Use vectors?
     
  8. With the code you've given us, what is currently happening?
     
  9. 60 swords appear in one location)
     
  10. Create an armor stand holding a diamond sword outside of the runnable and teleport the armor stand in front of the players, inside the runnable.
     
    • Like Like x 1
  11. So I want to do that.
    The problem is that I don't really understand vectors.
    Without knowledge of vectors, this is not possible.

    Suppose for 90, 180, 270, 360 degrees, you can either add for X plus or minus 1 or for Z plus or minus 1.

    But what to do if the degree for example 110?
     
  12. Try this:


    Code (Java):
    public void trowSword(final Location location) {
            final ArmorStand swordHolo = (ArmorStand) location.getWorld().spawnEntity(location, EntityType.ARMOR_STAND);
            swordHolo.setArms(true);
            swordHolo.setBasePlate(false);
            swordHolo.setVisible(false);
            swordHolo.setGravity(false);
            swordHolo.setItemInHand(new ItemStack(Material.IRON_SWORD));
         
            double xzLen = Math.cos(location.getPitch());
            double x = xzLen * Math.cos(location.getYaw());
            double y = Math.sin(location.getPitch());
            double z = xzLen * Math.sin(-location.getYaw());
            final Vector heading = new Vector(x, y, z);
         
            new BukkitRunnable() {
             
                private Location pos = location.clone();
                private int c = 0;
             
                @Override
                public void run() {
                    c++;
                    pos.add(heading);
                    swordHolo.teleport(pos);
                 
                    if (c > 1000) {
                        this.cancel();
                    }
                }
            }.runTaskTimer(plugin, 0, 10);
        }
    It creates the armorstand with its properties at the given location.
    Using the location's pitch and yaw to determine the direction.
    and moving 1 armorstand each half second till 1000 calls have past.

    Be sure to insert the player's location in the function. (so the location contains the player's heading direction (pitch and yaw)
     
    • Like Like x 1
    • Optimistic Optimistic x 1
  13. Same problem with vectors.
    The sword does not fly straight, then to the side, then diagonally.
     
  14. I did this a while ago. I'll send you the code when I get home.

    No need for Math cos and complicated stuff. It's as simple as getting a location in front of the player using locations and vectors/
     
  15. Thanks! I am wait)
     
  16. Code (Java):
        public static Location getFixedLocation(final Player player, final double far, final double high,
               final float offset) {

           final Location loc = player.getEyeLocation().clone();

           loc.setYaw(loc.getYaw() + offset);

           final Vector toAdd = loc.getDirection().normalize().multiply(far);
           final Location fixed = loc.add(toAdd);

           fixed.add(0, high, 0);

           return fixed;
       }
    This is the code I'm using in order to get a location in front of the player.
    with far being a default value of 3.5 and high 2.2 while offset 0. (offset, if negative the item will go left if positive the item will go right.)
     
  17. my theory give the armor stand some velocity. correct me if needed
     
  18. What velocity though? The OP wishes the sword to follow the player's movement (cursor). Thus, spawning an armor stand holding a sword in front of them (with the code above) and then creating a repeating task (every tick) to teleport the sword seems to be ideal. This is what I did in one of my plugins.
     
  19. ohh I thought he wants throwing swords at the enemy well if he wants a sword behaving like a pet then for smoother armor stand behavior he would have to sync this with player movement.
     
  20. I changed the code a bit and this is what happened:
    Code (Text):

    static void trowSword(Player p) {
        Location location = p.getLocation();
        ArmorStand swordHolo = (ArmorStand) location.getWorld().spawnEntity(location, EntityType.ARMOR_STAND);
        swordHolo.setArms(true);
        swordHolo.setBasePlate(false);
        swordHolo.setVisible(false);
        swordHolo.setGravity(false);
        swordHolo.setRightArmPose(new EulerAngle(Math.toRadians(351), Math.toRadians(-location.getPitch()), Math.toRadians(90.0D)));
        swordHolo.setItemInHand(new ItemStack(Material.IRON_SWORD));

        new BukkitRunnable() {

            private int c = 0;
            Location loc = getFixedLocation(location);

            @Override
            public void run() {
                c++;
                loc = getFixedLocation(loc);
                swordHolo.teleport(loc);

                if (c > 60) {
                    swordHolo.remove();
                    this.cancel();
                }
            }
        }.runTaskTimer(MurderMystery.getPlugin(), 1, 1);
    }

    private static Location getFixedLocation(Location location) {
        Location loc = location.clone();

        Vector toAdd = loc.getDirection().normalize().multiply(0.6);

        return loc.add(toAdd);
    }I changed the code a bit and this is what happened:
     
    Everything works fine, but the problem is that when ArmorStand appears, it cannot move to the right.
    This is due to the fact that you cannot spawn an ArmorStand at a player’s location.
    They are like mobs. Are moving.
    If you use gamemode 3, then everything works without a move.
     
    #20 Creator, Feb 17, 2019
    Last edited: Feb 17, 2019

Share This Page