Solved Nearby block detection

Discussion in 'Plugin Development' started by _Error, Aug 8, 2015.

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

    _Error

    I have 2 kind of strange errors.

    This is not detecting fire for some reason. I might be doing something stupid but I'm not sure.
    Code:
    public static boolean nearFire(Player player){
    for (int x = player.getLocation().getBlockX() - 5; x > player.getLocation().getBlockX() + 5; x++) {
    for (int y = player.getLocation().getBlockY() - 5; y > player.getLocation().getBlockY() + 5; y++) {
    for (int z = player.getLocation().getBlockZ() - 5; z > player.getLocation().getBlockZ() + 5; z++) {
    World w = player.getWorld();
    if (w.getBlockAt(x, y, z).getType() == Material.FIRE) {
    return true;
    
    }
    }
    
    }
    }
    return false;
    }
    I am using the correct imports.
    Code:
    import org.bukkit.entity.Player;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.Bukkit;
    And this math.random() is always generating 0.
    Code:
    public static void runScheduler() {
    BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    scheduler.scheduleSyncDelayedTask((Plugin) FrostbiteMain.getPlugin(), new Runnable() {
    public void run() {
    if(State.isState(State.NORMAL)) {
    int random = (int) (Math.random() * 10 / 100);
    if (random <= 10) {
    FrostBite.Scheduler();
    Bukkit.broadcastMessage("" + random);
    }
    FrostBite.runScheduler();
    }
    }
    }, 20L);
    EDIT: That has been fixed. Thanks @Eos


    EDIT: Forgot to give you that.
    IS frost
    REACHED
    Not near fire
    are the only ones printing out.
    EVEN if I'm IN the fire it still does not say that.
    Code:
    public static void LooseHealth(){
    Bukkit.broadcastMessage("REACHED");
    BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    scheduler.scheduleSyncDelayedTask((Plugin) FrostbiteMain.getPlugin(), new Runnable() {
    public void run() {
    if(State.isState(State.FROST)){
    Bukkit.broadcastMessage("IS frost");
    FrostBite.LooseHealth();
    for(Player player : Bukkit.getOnlinePlayers()) {
    if (nearFire(player)){ Bukkit.broadcastMessage("Near fire"); }else{
    Bukkit.broadcastMessage("Not near fire");
    if(player.getHealth() != 1) {
    player.damage(1);
    }
    }
    }
    }
    }
    }, 20L * 10);
    }
     
    Last edited: Aug 8, 2015
  2. Offline

    Eos

    @_Error
    Code:
    int random = (int) (Math.random() * 10 / 100);
    //Should be
    double random = Math.floor(( Math.random() * 100 ) * 1); // Returns a number between 10 and 100
    
    This isn't how Math.random() is used of course it's going to return 0 everytime

    Code:
    if (random <= 10) {
    FrostBite.Scheduler();
    Bukkit.broadcastMessage("" + random);
    }
    should be this
    Code:
            if ( random >= 10 ) {
           FrostBite.Scheduler();
           Bukkit.broadcastMessage("" + random);
            }
    Code:
    w.getBlockAt(x, y, z).getType() == Material.FIRE
    should be
    Code:
    w.getBlockAt(x, y, z).getType().equals(Material.FIRE)

    Going to update the thread.
     
    Last edited: Aug 8, 2015
  3. Offline

    _Error

    w.getBlockAt(x, y, z).getType().equals(Material.FIRE)
    Did not fix anything. ):
    double random = Math.floor(( Math.random() * 100 ) * 1);
    This might have done? I'm trying to make percentages.


    EDIT:
    If you want you can give me an alternative method for detecting stuff surrounding the player.
     
    Last edited: Aug 8, 2015
  4. Offline

    BurnyDaKath

    Enums should use ==, not .equals().
     
    _Error likes this.
  5. Offline

    _Error

    I even tried this.
    Code:
    public static boolean isNearBlock(Material blockType, Location loc, int radius){
    World world = loc.getWorld();
    int x = (int) loc.getX(), y = (int) loc.getY(), z = (int) loc.getZ();
    for (int ox = 0; ox > -radius; ox--) {
    for (int oy = 0; oy > -radius; oy--) {
    for (int oz = 0; oz > -radius; oz--) {
    if (world.getBlockAt(x + ox, y + oy, z + oz).getType() == blockType) {
    return true;
    }
    }
    }
    }
    return false;
    }
    EDIT : All fixed.
     
    Last edited: Aug 8, 2015
    mine-care likes this.
Thread Status:
Not open for further replies.

Share This Page