PlayerMoveEvent and Runnables

Discussion in 'Plugin Development' started by GamerzKing, Nov 21, 2015.

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

    GamerzKing

    Hello everyone!

    Recently, I have been working on a game, and one of the abilities has been causing me trouble, and I am quite unsure why.


    Code:
    if(player.getLocation().getBlock().getType() == Material.VINE) {
    
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getPlugin(), new Runnable() {
    
                    @Override
                    public void run() {
    
                        player.sendMessage("Hello");
                    }
                }, 20L);
            }
    So what I am trying to create is this ability:

    So I created my wall, and it is fully functional, but when a player is on the vine, it spams you the message, while I thought it was supposed to delay the task by 20 ticks. I also tried moving the conditional inside the Runnable, etc, but I have found no solutions.

    [​IMG]

    If anyone could help, that would be greatly appreciated, thanks! :)
     
  2. Offline

    teej107

    @GamerzKing Use a "cooldown" instead of Runnables. It's more appropriate. OR create one Runnable only and check to see if the online players are climbing a vine.
     
  3. Offline

    TheNewTao

    @teej107

    The problem is that when he creates a runnable, the PlayerMoveEvent is triggered MANY times when a player is on a vine, so he couldn't have a runnable on the PlayerMoveEvent.

    This is what I came up with:
    Two variables at instance level, Player p; and int vine;
    Code:
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
    
            Player player = event.getPlayer();
    
            if (player.getLocation().getY() <= lowestY) {
    
                player.teleport(new Location(Bukkit.getWorld("Lobby"), teleportLocationX, teleportLocationY, teleportLocationZ));
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "The void doesn't accept trust falls.");
            }
            if(player.getLocation().getBlock().getType() == Material.VINE) {
                  vine = 1;
                n = event.getPlayer();
            }
            else{
                vine = 0;
            }
        }
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            new BukkitRunnable() {
                public void run() {
                    if (vine == 1) {
                        n.sendMessage(ChatColor.BOLD + "2.5 hearts have been removed!");
                        n.setHealth(n.getHealth()-5);
                    }
                }
            }.runTaskTimer(this, 0L, 20L);
        }
     
  4. Offline

    teej107

    I know. That's why I suggested
    And to much surprise, your spoonfed code won't work.
     
  5. Offline

    TheNewTao

    @teej107

    He is my teacher, he spoonfeeds me. Question though, is placing a BukkitRunnable on the PlayerJoinEvent a really bad thing? The code did work.
     
  6. Offline

    Scimiguy

    @TheNewTao
    Don't write code for people, you're just passing your own bad habits onto others and not helping them learn anything.

    Nothing wrong with scheduling tasks on PlayerJoinEvent
     
  7. Offline

    TheNewTao

    @Scimiguy

    We are working together on the code, he is a friend, but he just doesn't trust me with a runnable in PlayerJoinEvent.
     
  8. Offline

    Scimiguy

  9. Offline

    teej107

    No, it's not really bad. You'll just create a task per player but make sure you cancel it when they leave. Also you need to create "vine" variable per player too. I would recommend creating a class extending BukkitRunnable that has the vine variable and the player it is for. Also a boolean would be more appropriate than an int using 0 or 1.
     
Thread Status:
Not open for further replies.

Share This Page