How to checking biome at player's location ?

Discussion in 'Spigot Plugin Development' started by adairh, Jun 6, 2017.

  1. Sorry about my english !
    I have an project like this:
    If players in Ice Plains biome, they will have some effect like Slow ( because cold ) and other biomes will have some effect
    But i tried this code to check biome, and it wasn't work
    Code (Text):
        public void onMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
           
            if (p.getWorld().getBiome(p.getLocation().getBlockX(), p.getLocation()
                    .getBlockZ()).equals(Biome.ICE_PLAINS)) {
                p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100000,1));
            }
    Is there anyone here can help me ?
     
  2. Well you have put solved, so not sure if you have fixed it in 5 minutes of posting it, but if not try changing .equals() to == they mean similar things as if you decompile .equals() if defers to ==. == is null safe so I would use that.

    You may also (when testing) may not be standing in an ICE_PLAINS biome so I would print out the biome that you are standing in to debug check to ensure you are actually standing in the ICE_PLAINS biome. (Just with System.out.println(players biome)
     
    #2 tarpers, Jun 6, 2017
    Last edited: Jun 6, 2017
  3. can you brief it ? :|
     
  4. i sure i am standing in the ICE_PLAINS biome
     
  5. Mas

    Mas

    Use player.getLocation().getBlock().getBiome() to get the Biome the player is in, then compare that biome using the == operator. Which part don't you understand?
     
  6. i tried :
    Code (Text):
    if (p.getPlayer().getLocation().getBlock().getBiome() == Biome.HELL) {
                p.getPlayer().addPotionEffect(new PotionEffect(
                        PotionEffectType.WITHER, 100000, 1));
            }
    and:
    Code (Text):
    if (player.getLocation().getBlock().getBiome() == Biome.HELL) {
                p.getPlayer().addPotionEffect(new PotionEffect(
                        PotionEffectType.WITHER, 100000, 1));
            }
    but it doesn't work :(
     
    #8 adairh, Jun 9, 2017
    Last edited: Jun 9, 2017
  7. Mas

    Mas

    You've set the thread to solved, however if your issue isn't solved:
    Your variable 'p' is already a player, no need to use getPlayer().
    Are you 100% sure you are in a hell biome?
    Is your listener definitely registered?
     
  8. This is my code and it doesn't work
    Code (Text):
    @EventHandler
        public void onMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            if (p.getLocation().getBlock().getBiome() == Biome.HELL) {
                p.addPotionEffect(new PotionEffect(
                        PotionEffectType.WITHER, 100000, 1));
            }
       
        }
    i am sure i am in a hell biome
     
  9. Please take a screenshot of your screen with the debug screen up with it (get debug screen by hitting f3 on the keyboard) and take another screenshot of your inventory.
     
  10. [​IMG] [​IMG]
     
  11. If you are looking for nether only then there is a faster way. Location#getBlock() is an extremely expensive call. Doing it on a player move event is not sane at all.

    Code (Text):
    public void onMove(PlayerMoveEvent event) {
        if (event.getTo().getWorld().getEnvironment() != Environment.NETHER) {
            return; // not in nether
        }

        event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 100000, 1));
    }
    Might also want to check if the potion effect is already applied and also check if the player actually moves a full block. This will help lower any lag this may produce.
     
  12. not working :((((
     
  13. Is your event registered? Debug. Does your event fire at all? Does what you think should fire actually fire?


    Sent from my iPhone using Tapatalk
     
  14. I am already register event:
    Code (Text):
    public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
        }
     
  15. What doesn't work. Specifics! Also, add debug (print statements) to your code.
     
  16. ??? add debug ???
     
  17. Show us your entire class. I have a feeling you're doing something wrong/not implementing Listener
     
  18. Code (Text):
    package me.adairh;

    import org.bukkit.World.Environment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class main extends JavaPlugin implements Listener{
       
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            if (e.getTo().getWorld().getEnvironment() == Environment.NETHER) {
                p.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 100000, 1));
            }
           
        }
    }