PlayerMoveEvent drops TPS

Discussion in 'Plugin Requests' started by wertolebus, Aug 27, 2023.

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

    wertolebus

    Even if only one player on server TPS drops to 2-3.

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class locationEvents implements Listener {
        Main plugin = com.wertolebus.religion.Main.getPlugin(Main.class);
        String[] blacklistBiomesSilvaism = {
                "BADLANDS",
                "CAVES",
                "HILLS",
        };
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent ev){
            Player player = ev.getPlayer();
            Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
                if(player.getLocation().getY() == 52 && new GetReligion().get(player.getUniqueId()) == 6)
                    player.damage(2.0);
                if((player.getLocation().getY() == 57 || player.getLocation().getY() == 76) && new GetReligion().get(player.getUniqueId()) == 6)
                    player.addPotionEffect(PotionEffectType.POISON.createEffect(3, 0));
                if(player.getWorld().getBiome(player.getLocation()).name().contains("FOREST") && new GetReligion().get(player.getUniqueId()) == 7){
                    player.addPotionEffect(PotionEffectType.REGENERATION.createEffect(3, 0));
                    player.addPotionEffect(PotionEffectType.SATURATION.createEffect(3, 0));
                }
                for(int i=0; i<blacklistBiomesSilvaism.length; i++) {
                    if (player.getWorld().getBiome(player.getLocation()).name().contains(blacklistBiomesSilvaism[i]) && new GetReligion().get(player.getUniqueId()) == 7) {
                        player.addPotionEffect(PotionEffectType.HUNGER.createEffect(3, 4));
                    }
                }
            }, 20L, 20L); // 20 ticks = 1 second
        }
    }
    
     
  2. Offline

    timtower Administrator Administrator Moderator

    @wertolebus Then why do you start a new REPEATING task every time a player moves?
     
  3. Offline

    wertolebus

    But then what is the best way to do?
     
  4. Offline

    timtower Administrator Administrator Moderator

    @wertolebus What do you want to do? What is the goal?
     
  5. Offline

    wertolebus

    How do this without tasks or smth?
     
  6. Offline

    timtower Administrator Administrator Moderator

    What is the goal of this method? What does it need to do?
     
  7. Offline

    wertolebus

    This method checks certain y coordinates and some biomes for players who have some numbers from method what's read database and return number.
    P.S. Method, what using to return number from database dont load TPS and successfully using in others classes.
     
  8. Offline

    timtower Administrator Administrator Moderator

    But why is it doing that? I can read the code just fine.
    I can't figure out the end goal of this.

    Just setting a potion effect based on the values?
    If so: start a single runnable in the onEnable of the plugin, that checks ALL players

    And consider caching the values from the database, database calls take a lot of time, certainly if the amount of them increases with every move like it is now.
     
  9. Offline

    wertolebus

    Okay, i think i got it, ty!
     
Thread Status:
Not open for further replies.

Share This Page