Solved Shoot an armorstand from the player holding a bone

Discussion in 'Spigot Plugin Development' started by JohanDev, Jun 21, 2021.

  1. Hello Spigot Developers!

    I am currently working on a Boomerang plugin and i cant figure out how i would shoot an armorstand from the player holding a bone.

    **I am not a very experienced developer so i wont know any advanced stuff

    Code (Java):
    package no.johandev.simpleboomerangs.events;

    import no.johandev.simpleboomerangs.utilities.ChatColor;
    import no.johandev.simpleboomerangs.utilities.ItemManager;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Particle;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;

    public class BoomerangEvents implements Listener {


        @EventHandler
        public void onRightClick(PlayerInteractEvent event){
            if (event.getAction() == Action.RIGHT_CLICK_AIR){
                if (event.getItem() != null){
                    if (event.getItem().getItemMeta().equals(ItemManager.boomerang.getItemMeta())){
                        Player p = event.getPlayer();
                        event.setCancelled(true);
                        p.getInventory().setItem(p.getInventory().getHeldItemSlot(), ItemManager.usedboomerang);
                        p.sendMessage(ChatColor.chat("&dWooosh!"));
                        p.getWorld().spawnParticle(Particle.CLOUD, p.getLocation(), 4);
                    }
                }
            }
            if (event.getAction() == Action.RIGHT_CLICK_AIR){
                if (event.getItem() != null && event.getItem().getItemMeta().equals(ItemManager.usedboomerang.getItemMeta())){
                    Player p = event.getPlayer();
                    event.setCancelled(true);
                    p.sendMessage(ChatColor.chat("&dWait for your boomerang to come back!"));
                    p.getInventory().setItem(p.getInventory().getHeldItemSlot(), ItemManager.boomerang);
                }
            }
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK){
                if (event.getItem() != null && event.getItem().getItemMeta().equals(ItemManager.boomerang.getItemMeta())){
                    Player p = event.getPlayer();
                    event.setCancelled(true);
                    p.sendMessage(ChatColor.chat("&dYou can only shoot the air!"));
                }
            }
        }

    }
     
     
  2. Before everything, please don't mark this as a resource. This isn't a resource. Select your Spigot version instead.
     
  3. Bump i need help
     
  4. i want to shoot a bone not an arrow or snowball
     
  5. You could shoot an invisible snowball and spawn an item of a bone and set it as a passenger of the snowball, alternatively, you could spawn an armorstand with a bone in its hand/head slot and set it's velocity.
     
  6. Do not bump twice in 8 minutes, it is a bit useless. You could spawn an invisible armorstand without gravity on the player's location, then get the player's looking direction with org.bukkit.LivingEntity#getEyeLocation#getDirection and create a bukkitrunnable where each tick you move the armorstand in the starting direction by your boomerang's speed. You can calculate where the next position will be with org.bukkit.Vector#add(org.bukkit.Vector), where the parameter is the unit movement vector of your boomerang. Your armorstand will have equipped in the main hand a bone. You might have to shift a bit the spawning location of the armorstand and/or play with org.bukkit.entity.ArmorStand#set*Pose(org.bukkit.util.EulerAngle)
     
  7. how would i do that im not any good at java
     
  8. You need to create a repeating task with with org.bukkit.Bukkit.scheduleSyncDelayedTask(Plugin, Runnable, long) where you update the position of your armorstand on each execution of your task. You spawn the armorstand outside the task (setting it invisible with an equipped bone), then use the armorstand's reference inside of the task
     
  9. Please DON'T do anything with a repeating task... It's a terrible practice and bad execution.
    Try this:
    Code (Java):
    Snowball projectile = player.launchProjectile(Snowball.class);
    projectile.setItem(new ItemStack(Material.BONE);
    This has NOT been tested - I just wrote this on my phone in 2 minutes so just try to work off of this.
    Also since this is going to be a boomerang I suggest you remove its gravity with setGravity(false) - it's what I would do, you don't have to, I just think it'll look more realistic.
    After you manage to work this and see that the bone is shot properly I suggest you work on returning the boomerang - a timed event (a few seconds after its launch) if it didn't hit anything, or when it does hit something (or something different - whatever vision you had for it).
     
    • Optimistic Optimistic x 1
  10. ok i will try this
     
  11. What? Who told you that? There's nothing wrong with a repeating task if used reasonably?

    If you disable gravity the entity ignores all velocity and has to be teleported around, with a repeating task.

    I found this video which apparently shows how to make a boomerang. I haven't watched it though.
     
  12. i'll watch the video
     
  13. I meant a repeating task every tick - my bad I didn't clarify.
    Also if you shoot something with no gravity it'll just continue going (why should it ignore velocity? exactly the opposite - it doesn't slow down on any axis) - that's why I said to do a delayed task to bring it back after a few seconds. So it doesn't continue forever. And there's no need for a repeating task with that.
     
  14. Still nothing wrong with it, if he wants the boomerang to follow a natural curve a repeating task which modifies the direction is perfectly fine. Perhaps he wants the boomerang to rotate in the air? He's likely going to need a repeating task. Why is it wrong?

    My bad, I thought we were talking about armorstands earlier, I got it confused. However, disabling the gravity does not mean the entity doesn't slow down. It will still have decreased speed over time.
     
  15. Nope, a projectile only slows down on the Y axis and that's due to gravity (unless you manually change it of course...) - tested