Solved Throwable Sword

Discussion in 'Plugin Development' started by OfficerDeveloper, Nov 20, 2017.

Thread Status:
Not open for further replies.
  1. Hello, so I've been coding Java for a while and recently I decided to re-learn the Bukkit API by coding a version of Hypixel's Murder Mystery.

    The game works and everything, but I can't figure out how to throw the sword. Would it be an arrow and then mask it with a iron sword skin?

    If you don't know what I'm talking about in the Hypixel game you can right click to "throw a sword" which will throw a sword and kill a player if hit.

    I'm stumped about how to do this so any help is appreciated, thanks!

    SOLUTION

    Credit (open)
    @Zombie_Striker gave idea of armor stand implementation and runnables, I added the cooldown in the class

    Note that a lot of this is my own class work, but with basic java understanding you can get the jist of what it means in terms to you


    Code:
    case IRON_SWORD:
                    if (swordCooldown)
                        e.setCancelled(true);
                    else {
                        Vector dir = p.getEyeLocation().getDirection();
    
                        ArmorStand as = p.getWorld().spawn(p.getLocation().add(dir), ArmorStand.class);
    
                        as.setHelmet(new ItemStack(Material.IRON_SWORD));
                        as.setVisible(false);
                        as.setCanPickupItems(false);
                        as.setGravity(false);
    
                        loopID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new BukkitRunnable() {
    
                            @Override
                            public void run() {
                                as.teleport(as.getLocation().add(dir));
    
                                if (as.getLocation().add(dir).getBlock().getType() != Material.AIR) {
    
                                    for (Player p1 : Bukkit.getOnlinePlayers()) {
    
                                        if (p1.getLocation().distance(as.getLocation()) <= 1D) {
    
                                            switch (rm.getRole(p1)) {
    
                                            case HERO:
                                                Death.makeSpectator(p1);
                                                if (rm.getSurvivors().size() == 0) {
    
                                                    checkForWin();
                                                    Game.stop();
    
                                                } else if (rm.getSurvivors().size() > 0) {
    
                                                    Death.chooseNewHero();
                                                    msg.msg(p, "You got a kill! Be sneaky...");
    
                                                }
    
                                                break;
    
                                            case SURVIVOR:
                                                Death.makeSpectator(p1, p);
                                                msg.msg(p, "You got a kill! Be sneaky...");
                                                break;
    
                                            default:
                                                break;
                                            }
    
                                            for (ArmorStand a : p.getWorld().getEntitiesByClass(ArmorStand.class)) {
                                                a.remove();
                                            }
                                            p.playSound(p.getLocation(), Sound.ENTITY_ITEM_BREAK, 1, 0);
                                            Bukkit.getScheduler().cancelTask(loopID);
    
                                        }
    
                                        p.playSound(p.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 0);
                                        for (ArmorStand a : p.getWorld().getEntitiesByClass(ArmorStand.class)) {
                                            a.remove();
                                        }
    
                                    }
    
                                } else {
    
                                    p.playSound(p.getLocation(), Sound.ENTITY_ITEM_BREAK, 1, 0);
                                    for (ArmorStand a : p.getWorld().getEntitiesByClass(ArmorStand.class)) {
                                        a.remove();
                                    }
    
                                }
    
                            }
    
                        }
    
                                , 1, 1);
    
                        swordCooldown = true;
                        swordTimer = waitTime;
                        swordTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin,
                                new ProjectileTimer(plugin, p, Material.IRON_SWORD), 1, 20);
    
                    }
    
                    break;
    
     
    Last edited: Nov 22, 2017
  2. Offline

    Zombie_Striker

    @OfficerDeveloper
    The easiest way to achieve this would be to spawn an armorstand. Set it to be invisible and give it the sword. Then, continuously teleport and set the velocity of the armorstand so it looks like it is moving. If the armorstand is close to a player, damage them.
     
  3. Hmm.. alright. I'll try that now!


    Edit:

    Code:
    Code:
    if (p.getItemInHand().getType() == Material.IRON_SWORD) {
    
                    ArmorStand as = p.getWorld().spawn(p.getLocation(), ArmorStand.class);
    
                    as.setItemInHand(new ItemStack(Material.IRON_SWORD));
                    as.setVisible(false);
                    as.setCanPickupItems(false);
                    as.setGravity(false);
                    as.setVelocity(p.getLocation().getDirection());
    
                    while (true) {
    
                        as.teleport(as.getLocation().add(as.getVelocity().multiply(0.1)));
    
                        /**
                         * if (as.getLocation().getBlock().getType() != Material.AIR) { as.remove();
                         * e.getPlayer().playSound(e.getPlayer().getLocation(), Sound.ENTITY_ITEM_BREAK,
                         * 100, 0); break; }
                         */
    
                        for (Player p1 : Bukkit.getOnlinePlayers()) {
                            if (as.getLocation() == p1.getLocation()) {
    
                                switch (RoleManager.getInstance().getRole(p1)) {
    
                                case HERO:
                                    if (RoleManager.getInstance().getSurvivors().size() - 1 == 0) {
    
                                        MessageUtil.getInstance().broadcast("&6&l---------------", false);
                                        MessageUtil.getInstance().broadcast("", false);
                                        MessageUtil.getInstance().broadcast("&a&lWINNER: &4&lKILLER", false);
                                        MessageUtil.getInstance().broadcast("", false);
                                        MessageUtil.getInstance().broadcast(
                                                "&4&lKILLER: " + RoleManager.getInstance().getKiller().getName(), false);
                                        MessageUtil.getInstance().broadcast(
                                                "&2&lHERO: " + RoleManager.getInstance().getHero().getName(), false);
                                        MessageUtil.getInstance().broadcast("", false);
                                        MessageUtil.getInstance().broadcast("&6&l---------------", false);
    
                                        Death.makeSpectator(p1);
    
                                        Game.stop();
    
                                    } else {
    
                                        Death.makeSpectator(p1, p);
                                        MessageUtil.getInstance().msg(p, "You got a kill! Be sneaky...");
                                        Death.chooseNewHero();
    
                                    }
    
                                case SURVIVOR:
                                    if (RoleManager.getInstance().getSurvivors().size() - 1 == 0) {
                                        MessageUtil.getInstance().broadcast("&6&l---------------", false);
                                        MessageUtil.getInstance().broadcast("", false);
                                        MessageUtil.getInstance().broadcast("&a&lWINNER: &4&lKILLER", false);
                                        MessageUtil.getInstance().broadcast("", false);
                                        MessageUtil.getInstance().broadcast(
                                                "&4&lKILLER: " + RoleManager.getInstance().getKiller().getName(), false);
                                        MessageUtil.getInstance().broadcast(
                                                "&2&lHERO: " + RoleManager.getInstance().getHero().getName(), false);
                                        MessageUtil.getInstance().broadcast("", false);
                                        MessageUtil.getInstance().broadcast("&6&l---------------", false);
    
                                        Death.makeSpectator(p);
    
                                        Game.stop();
    
                                    } else {
                                        Death.makeSpectator(p1, p);
                                        MessageUtil.getInstance().msg(p1, "You got a kill! Be sneaky...");
                                    }
    
                                case DEAD:
                                    return;
                                }
    
                                as.remove();
                                break;
    
                            }
                        }
                    }
    
                }
    All this does is spawn an armorstand (that's visible and not holding a sword) where the player is, can't figure out why it's not working since I have a teleport method inside.

    No errors in console.

    @Zombie_Striker or @Anyone who might have an idea
     
    Last edited: Nov 20, 2017
  4. Offline

    Unknown123

    Or use an item drop
     
  5. That wouldn't really work for my purposes, my friend dared me to code Hypixel's "Murder Mystery" where the throwable sword looks like this:

    [​IMG]

    and I'm trying to re-create that effect with no success
     
  6. Offline

    Zombie_Striker

  7. Thank you so much! It's late where I am so I wasn't thinking straight with the while loop, but I got it to work!

    It looks awesome ingame too and I added a delay, thanks so much for that armorstand idea! Brilliant!

    Final code at top
     
Thread Status:
Not open for further replies.

Share This Page