Comparing locations

Discussion in 'Plugin Development' started by Yurij, Feb 4, 2011.

Thread Status:
Not open for further replies.
  1. I am trying to compare two locations when I break a block,
    but for some reason I cant seem to compare them correctly

    I used craftbukkit build 240-241 and bukkit build 111

    Here I outputted some locations.
    The first location is the block destroyed and the other are those i am trying to compare with
    Code:
    Location{world=CraftWorldx=-24.0y=69.0z=3.0pitch=0.0yaw=0.0}
    Location{world=CraftWorldx=-29.0y=70.5z=9.0pitch=0.0yaw=0.0}
    Location{world=CraftWorldx=-24.0y=69.5z=3.0pitch=0.0yaw=0.0}

    Here I have two examples on how I tried it with.
    "BVSettings.vaults" is a hashtable with the locations
    Code:
    Block block = event.getBlock();
    if(BVSettings.vaults.contains(block.getLocation())){
        player.sendMessage("§4You cant destroy this block");
        event.setCancelled(true);
    }
    Code:
    Block block = event.getBlock();
    double blockX = block.getLocation().getX();
    double blockY = block.getLocation().getY();
    double blockZ = block.getLocation().getZ();
    for(Location loc : BVSettings.vaults.values()){
        log.info(loc.toString());
        double x = loc.getX();
        double y = loc.getY();
        double z = loc.getZ();
        if(x == blockX && y == blockY && z == blockZ){
            player.sendMessage("§4You cant destroy this block");
            event.setCancelled(true);
            break;
        }
    }
     
  2. Offline

    Redecouverte

    it most likely fails, because blocks always have round values for x/y/z like 2 or 3, but generic locations may have something like 2.5

    i'd use that:
    Code:
    Block block = event.getBlock();
    int blockX = block.getLocation().getBlockX();
    int blockY = block.getLocation().getBlockY();
    int blockZ = block.getLocation().getBlockZ();
    for(Location loc : BVSettings.vaults.values()){
        log.info(loc.toString());
        if(loc.getBlockX() == blockX && loc.getBlockY() == blockY && loc.getBlockZ() == blockZ){
            player.sendMessage("§4You cant destroy this block");
            event.setCancelled(true);
            break;
        }
    }
    Location{world=CraftWorldx=-24.0y=69.0z=3.0pitch=0.0yaw=0.0} Location{world=CraftWorldx=-29.0y=70.5z=9.0pitch=0.0yaw=0.0} Location{world=CraftWorldx=-24.0y=69.5z=3.0pitch=0.0yaw=0.0}
     
  3. Damn. I must be blind or something.
    Thanks for pointing that out.
     
Thread Status:
Not open for further replies.

Share This Page