runTaskTimer and player blocking

Discussion in 'Plugin Development' started by PHILLIPS_71, Jul 11, 2013.

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

    PHILLIPS_71

    I have made a class in my plugin that if a player is blocking with a sword with the display name "Wind Cutter" play the effects and sound effects but its really glitch when you are blocking it does nothing, You have to left click and break a block for it to do something then if you constantly left click it just plays the effects and sound faster and faster for some reason any help is much appreciated.

    Class here -

    Code:
    package Legendary;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
     
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class WindCutter extends JavaPlugin implements Listener {
     
        private Core.main plugin;
        public WindCutter(Core.main plugin) {
            this.plugin = plugin;
        }
     
        List<String> WC = new ArrayList<String>();
     
        @EventHandler
        public void DWindCutter (PlayerInteractEvent event) {
            final Player player = event.getPlayer();
     
            if(!player.isBlocking()) {
                return;
            }
     
            if(!player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "Wind Cutter")) {
                return;
            }
     
            if(player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "Wind Cutter") && player.isBlocking()) {
                WC.add(player.getName());
     
                Bukkit.getScheduler().runTaskTimer(plugin, new Runnable() {
                    public void run() {
                        if(WC.contains(player.getName())) {
                            player.playSound(player.getLocation(), Sound.DIG_SAND, 1.0F, 1.0F);
                            player.playEffect(player.getLocation(), Effect.STEP_SOUND, Material.WEB);
                           
                            if(!player.isBlocking()) {
                                WC.remove(player.getName());
                            }
                        }
                    }
                }, 30, 0);
            }
        }
    }
     
  2. Offline

    andf54

    You dont need if(player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "Wind Cutter") && player.isBlocking()) { because you already checked it.

    PlayerInteractEvent is fired when a player left clicks, right clicks, steps on a pressure plate. You need to check which action it was.

    My guess is that it's not working, because when you rclick, you are not in a isBlocking state yet.

    Schedule a delayed task when someone right clicks. After 1-X ticks the run method is called where you check the isBlocking state.

    You might be able to pull this off with 0 ticks. Try scheduling a task with runTask.
     
Thread Status:
Not open for further replies.

Share This Page