while standing on block

Discussion in 'Plugin Development' started by stamline, Dec 14, 2015.

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

    stamline

    Hi, this is my current code, but is not working. When i stand on a emerlandblock the server freeze and after a while it crash, i think its because the code take to much memory, Is there another way to make this working. I don want to make so the player has to move around on the block. When he stand on it, he can stay still.

    This is my code:
    Code:
        @EventHandler
          public void OnPlayerMove(PlayerMoveEvent event){
            Player player = event.getPlayer();
            if((player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.EMERALD_BLOCK) && !this.haseffect.contains(player)){
                check(player);
            }
          }
       
        public void check(Player player){
            while(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.EMERALD_BLOCK){
                player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 20, 1));
                this.haseffect.add(player);
            }
            if(this.haseffect.contains(player)){
                this.haseffect.remove(player);
                player.removePotionEffect(PotionEffectType.REGENERATION);
            }
        }
     
  2. Offline

    mythbusterma

    @stamline

    This has nothing to do with memory. This has to do with you creating an infinite loop on the server. Do you understand how event based programming works? Because this seems to be more of an issue with your Java knowledge than a Bukkit issue.

    You have to return control to the server after the event handler as soon as possible, otherwise you will cause the server to freeze, and eventually the heartbeat thread will crash it.
     
  3. Offline

    stamline

    How can i fix this than?
     
  4. Offline

    teej107

    Don't use while.
     
  5. Offline

    stamline

    What should i use?
     
  6. Offline

    mcdorli

    if
     
  7. Offline

    teej107

    @stamline
    And else

    You can also get rid of your hasEffect Collection. Bukkit has methods to tell if a LivingEntity has an effect.
     
  8. Offline

    stamline

    Code:
        @EventHandler
          public void OnPlayerMove(PlayerMoveEvent event){
            Player player = event.getPlayer();
            if((player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.EMERALD_BLOCK) && !haseffect.contains(player)){
                BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
                scheduler.scheduleSyncRepeatingTask((Plugin) this, new Runnable() {
                    @Override
                    public void run() {
                        if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.EMERALD_BLOCK){
                            player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 20, 1));
                            if(!haseffect.contains(player)){
                                haseffect.add(player);
                            }
                        }else{
                            player.removePotionEffect(PotionEffectType.REGENERATION);
                            haseffect.remove(player);
                        }
                    }
                }, 0, 10);
            }
          }
    Tried this, its not working, how can i fix this...
     
  9. Offline

    teej107

    @stamline start by getting rid of the scheduled task.
     
Thread Status:
Not open for further replies.

Share This Page