Skip to content

Instantly share code, notes, and snippets.

@rmsy
Last active December 18, 2015 00:19
Show Gist options
  • Save rmsy/5695715 to your computer and use it in GitHub Desktop.
Save rmsy/5695715 to your computer and use it in GitHub Desktop.
package com.github.rmsy.litelog.impl.filter;
import com.github.rmsy.litelog.Change;
import com.github.rmsy.litelog.Filter;
import com.google.common.base.Preconditions;
import org.bukkit.Location;
import javax.annotation.Nonnull;
/**
* Filter to target only the blocks within the specified distance of the specified location.
*/
public class LocationRadiusFilter extends Filter {
/**
* This filter's reference location.
*/
@Nonnull
private final Location location;
/**
* This filter's reference distance (squared).
*/
private final double distance;
private LocationRadiusFilter() {
this.location = null;
this.distance = 0.0;
}
/**
* Creates a new location radius filter.
*
* @param location The filter's reference location.
* @param distance The filter's reference distance; must be >= <code>1.0</code>.
*/
public LocationRadiusFilter(@Nonnull final Location location, final double distance) {
this.location = Preconditions.checkNotNull(location, "location");
Preconditions.checkArgument(distance >= 1.0, "distance");
this.distance = distance * distance;
}
/**
* Checks if the specified change passes the filter.
*
* @param change The change to be checked.
* @return Whether or not the specified change passes the filter.
*/
@Override
public boolean check(@Nonnull Change change) {
return Preconditions.checkNotNull(change, "change").getBlock().getLocation().distanceSquared(this.location) <= this.distance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment