8000 Force location queries to check center of block by Pugzy · Pull Request #1000 · PGMDev/PGM · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Force location queries to check center of block #1000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
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