Skip to content

Commit

Permalink
Force location queries to check center of block
Browse files Browse the repository at this point in the history
Signed-off-by: Pugzy <[email protected]>
  • Loading branch information
Pugzy committed May 11, 2022
1 parent ae18121 commit 2418728
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package tc.oc.pgm.api.filter.query;

import org.bukkit.Location;
import tc.oc.pgm.util.block.BlockVectors;

public interface LocationQuery extends MatchQuery {
Location getLocation();

default Location getBlockCenter() {
return BlockVectors.center(getLocation());
}
}
4 changes: 4 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/region/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.util.BlockVector;
import org.bukkit.util.Vector;
import tc.oc.pgm.api.filter.Filter;
import tc.oc.pgm.api.filter.query.LocationQuery;
import tc.oc.pgm.regions.Bounds;

/** Represents an arbitrary region in a Bukkit world. */
Expand All @@ -33,6 +34,9 @@ public interface Region extends Filter {
/** Test if the region contains the given entity */
boolean contains(Entity entity);

/** Test if the region contains the queried location */
boolean contains(LocationQuery query);

/** Test if moving from the first point to the second crosses into the region */
boolean enters(Location from, Location to);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ public void onPlayerMove(PlayerCoarseMoveEvent event) {
// We can't wait until the end of the tick because the player could move several
// more times by then (i.e. if we received multiple packets from them in the same
// tick) which would make region checks highly unreliable.
PGM.get().getMatchManager().getPlayer(event.getPlayer());
MatchPlayer player = match.getPlayer(event.getPlayer());

if (player != null) {
Expand Down
16 changes: 10 additions & 6 deletions core/src/main/java/tc/oc/pgm/filters/query/PlayerQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import javax.annotation.Nullable;
import org.bukkit.Location;
Expand All @@ -17,14 +16,14 @@
public class PlayerQuery extends Query implements tc.oc.pgm.api.filter.query.PlayerQuery {

private final MatchPlayer player;
private final Optional<Location> location;
private final Location location;
private final Location blockCenter;

// TODO: remove this implementation? only needed usage if for a query where the location is not
// the location of the player
public PlayerQuery(@Nullable Event event, MatchPlayer player, @Nullable Location location) {
super(event);
this.player = checkNotNull(player);
this.location = Optional.ofNullable(location);
this.location = location != null ? location : player.getBukkit().getLocation();
this.blockCenter = this.location.getBlock().getLocation();
}

public PlayerQuery(@Nullable Event event, MatchPlayer player) {
Expand Down Expand Up @@ -58,7 +57,12 @@ public Class<? extends Entity> getEntityType() {

@Override
public Location getLocation() {
return location.orElse(player.getBukkit().getLocation());
return location;
}

@Override
public Location getBlockCenter() {
return blockCenter;
}

@Override
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/java/tc/oc/pgm/regions/AbstractRegion.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public boolean contains(Entity entity) {
return this.contains(entity.getLocation().toVector());
}

@Override
public boolean contains(LocationQuery query) {
return this.contains(query.getBlockCenter());
}

@Override
public boolean enters(Location from, Location to) {
return !this.contains(from) && this.contains(to);
Expand Down Expand Up @@ -122,6 +127,6 @@ public Class<? extends LocationQuery> getQueryType() {

@Override
protected QueryResponse queryTyped(LocationQuery query) {
return QueryResponse.fromBoolean(contains(query.getLocation()));
return QueryResponse.fromBoolean(contains(query));
}
}
6 changes: 6 additions & 0 deletions core/src/main/java/tc/oc/pgm/regions/XMLRegionReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.bukkit.event.Event;
import org.bukkit.util.BlockVector;
import org.bukkit.util.Vector;
import tc.oc.pgm.api.filter.query.LocationQuery;
import tc.oc.pgm.api.filter.query.Query;
import tc.oc.pgm.api.region.Region;
import tc.oc.pgm.api.region.RegionDefinition;
Expand Down Expand Up @@ -65,6 +66,11 @@ public boolean contains(Entity entity) {
return get().contains(entity);
}

@Override
public boolean contains(LocationQuery query) {
return get().contains(query);
}

@Override
public boolean enters(Location from, Location to) {
return get().enters(from, to);
Expand Down

0 comments on commit 2418728

Please sign in to comment.