Attempting a plugin that prevents block placement if another player is within a block radius

Discussion in 'Plugin Development' started by jakelauer, Sep 11, 2012.

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

    jakelauer

    Ideally, it would allow for any block type and any block radius, but at the moment I only really need to prevent players from placing lava if another player is within 5 blocks of them. Is this possible? I'm super new to this stuff, so some friendly direction/help would be awesome.
     
  2. Offline

    stelar7

    player.getNearbyEntities(5,5,5) check for player, cancel event
     
  3. Offline

    skore87

    There are different ways to do this, some will tell you to loop through all online players, others search the chunks nearby. I offer you a chunk based solution as this one is a little more tedious to code if you're new to it. If you do the looping of all online players, you just need to do the distance method on the Location object to another one.

    Radius is in blocks.

    Code:
    public static boolean isNearbyPlayers(Entity e, int radius){
    return isNearbyPlayers(e.getLocation(),radius);
    }
     
    public static boolean isNearbyPlayers(Location l, int radius){
    int chunkRadius = radius < 16 ? 1 : (radius - (radius % 16))/16;
    for (int chX = 0 -chunkRadius; chX <= chunkRadius; chX ++){
    for (int chZ = 0 -chunkRadius; chZ <= chunkRadius; chZ++){
    int x=(int) l.getX(),y=(int) l.getY(),z=(int) l.getZ();
    for (Entity e : new Location(l.getWorld(),x+(chX*16),y,z+(chZ*16)).getChunk().getEntities()){
    if (e instanceof Player){
    if (e.getLocation().distance(l) <= radius && e.getLocation().getBlock() != l.getBlock()) return true;
    }
    }
    }
    }
    return false;
    }
     
Thread Status:
Not open for further replies.

Share This Page