Skip to content
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

Implement cuboid variable! #1475

Merged
merged 1 commit into from
Jan 6, 2025
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
Expand Up @@ -9,16 +9,16 @@ public class ModuleLoadException extends RuntimeException {
private final @Nullable Class<? extends Module> key;

public ModuleLoadException(Class<? extends Module> key, String message, Throwable cause) {
super(message, cause);
super(getFullMessage(message, key), cause);
this.key = key;
}

public @Nullable Class<? extends Module> getModule() {
return key;
}

public String getFullMessage() {
return getMessage() + (key != null ? " @ " + key.getSimpleName() : "");
public static String getFullMessage(String message, Class<? extends Module> key) {
return message + (key != null ? " @ " + key.getSimpleName() : "");
}

public ModuleLoadException(Class<? extends Module> key, String message) {
Expand Down
100 changes: 60 additions & 40 deletions core/src/main/java/tc/oc/pgm/api/region/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,55 @@
import org.bukkit.event.Event;
import org.bukkit.util.BlockVector;
import org.bukkit.util.Vector;
import tc.oc.pgm.api.PGM;
import tc.oc.pgm.api.filter.query.LocationQuery;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.filters.matcher.TypedFilter;
import tc.oc.pgm.regions.Bounds;
import tc.oc.pgm.util.StreamUtils;
import tc.oc.pgm.util.block.BlockVectors;
import tc.oc.pgm.util.chunk.ChunkVector;
import tc.oc.pgm.util.event.PlayerCoarseMoveEvent;

/** Represents an arbitrary region in a Bukkit world. */
public interface Region extends TypedFilter<LocationQuery> {
/** Test if the region contains the given point */
boolean contains(Vector point);
/** Static regions are ones which do not change depending on the world/match */
interface Static extends Region {
/** Test if the region contains the given point */
boolean contains(Vector point);

/** Test if the region contains the given point */
default boolean contains(Location point) {
return this.contains(point.toVector());
}

/** Test if the region contains the given point */
default boolean contains(Location point) {
return this.contains(point.toVector());
}
/** Test if the region contains the center of the given block */
default boolean contains(BlockVector blockPos) {
return this.contains((Vector) BlockVectors.center(blockPos));
}

/** Test if the region contains the center of the given block */
default boolean contains(BlockVector blockPos) {
return this.contains((Vector) BlockVectors.center(blockPos));
/**
* Iterate over all the blocks inside this region.
*
* @throws UnsupportedOperationException if the region's blocks are not enumerable
*/
default Iterator<BlockVector> getBlockVectorIterator() {
return Iterators.filter(this.getBounds().getBlockIterator(), this::contains);
}

default Iterable<BlockVector> getBlockVectors() {
return this::getBlockVectorIterator;
}

@Override
default Static getStatic() {
if (isStatic()) return this;
throw new UnsupportedOperationException("Can't get static for non-static region");
}
}

/** Test if the region contains the given point */
boolean contains(Location point);

/** Test if the region contains the center of the given block */
default boolean contains(Block block) {
return this.contains(BlockVectors.center(block));
Expand All @@ -50,7 +76,7 @@ default boolean contains(BlockState block) {

/** Test if the region contains the given entity */
default boolean contains(Entity entity) {
return this.contains(entity.getLocation().toVector());
return this.contains(entity.getLocation());
}

/** Test if the region contains the queried location */
Expand All @@ -63,26 +89,37 @@ default boolean enters(Location from, Location to) {
return !this.contains(from) && this.contains(to);
}

/** Test if moving from the first point to the second crosses into the region */
default boolean enters(Vector from, Vector to) {
return !this.contains(from) && this.contains(to);
}

/** Test if moving from the first point to the second crosses out of the region */
default boolean exits(Location from, Location to) {
return this.contains(from) && !this.contains(to);
}

/** Test if moving from the first point to the second crosses out of the region */
default boolean exits(Vector from, Vector to) {
return this.contains(from) && !this.contains(to);
}

/** Can this region generate evenly distributed random points? */
default boolean canGetRandom() {
return false;
}

default boolean isStatic() {
return false;
}

default Static getStatic() {
if (this.isStatic() && this instanceof Static s) return s;
throw new UnsupportedOperationException("Can't get static for non-static region");
}

Static getStaticImpl(Match match);

default Static getStatic(Match match) {
if (isStatic()) return getStatic();
return getStaticImpl(match);
}

default Static getStatic(World world) {
if (isStatic()) return getStatic();
return getStaticImpl(PGM.get().getMatchManager().getMatch(world));
}

/**
* Gets a random point contained within this region.
*
Expand Down Expand Up @@ -126,26 +163,9 @@ default boolean matches(LocationQuery query) {
return contains(query);
}

/**
* Iterate over all the blocks inside this region.
*
* @throws UnsupportedOperationException if the region's blocks are not enumerable
*/
default Iterator<BlockVector> getBlockVectorIterator() {
return Iterators.filter(this.getBounds().getBlockIterator(), this::contains);
}

default Iterable<BlockVector> getBlockVectors() {
return this::getBlockVectorIterator;
}

default Stream<BlockVector> getBlockPositions() {
return StreamUtils.of(getBlockVectorIterator());
}

default Iterable<Block> getBlocks(World world) {
return () ->
Iterators.transform(getBlockVectorIterator(), pos -> BlockVectors.blockAt(world, pos));
return () -> Iterators.transform(
getStatic(world).getBlockVectorIterator(), pos -> BlockVectors.blockAt(world, pos));
}

default Stream<ChunkVector> getChunkPositions() {
Expand Down
28 changes: 27 additions & 1 deletion core/src/main/java/tc/oc/pgm/api/region/RegionDefinition.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
package tc.oc.pgm.api.region;

import org.bukkit.World;
import tc.oc.pgm.api.filter.FilterDefinition;
import tc.oc.pgm.api.match.Match;

/** A {@link tc.oc.pgm.api.region.Region} that is not a reference */
public interface RegionDefinition extends FilterDefinition, Region {}
public interface RegionDefinition extends FilterDefinition, Region {
interface Static extends RegionDefinition, Region.Static {}

interface HardStatic extends RegionDefinition.Static {
@Override
default boolean isStatic() {
return true;
}

@Override
default RegionDefinition.Static getStatic(Match match) {
return this;
}

@Override
default RegionDefinition.Static getStatic(World world) {
return this;
}

@Override
default RegionDefinition.Static getStaticImpl(Match match) {
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import org.jetbrains.annotations.Nullable;
import tc.oc.pgm.api.event.BlockTransformEvent;
import tc.oc.pgm.api.filter.query.Query;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.player.ParticipantState;
import tc.oc.pgm.api.region.Region;
import tc.oc.pgm.filters.matcher.StaticFilter;
import tc.oc.pgm.filters.query.Queries;
import tc.oc.pgm.kits.Kit;
Expand Down Expand Up @@ -46,18 +48,19 @@ public ImmutableList<BlockDropsRule> getRules() {
}

/** Return the subset of rules that may act on the given region */
public BlockDropsRuleSet subsetAffecting(FiniteBlockRegion region) {
public BlockDropsRuleSet subsetAffecting(Match match, FiniteBlockRegion region) {
ImmutableList.Builder<BlockDropsRule> subset = ImmutableList.builder();
for (BlockDropsRule rule : this.rules) {
if (rule.region == null) {
subset.add(rule);
continue;
}

if (Bounds.disjoint(rule.region.getBounds(), region.getBounds())) continue;
Region.Static staticReg = region.getStatic(match);
if (Bounds.disjoint(staticReg.getBounds(), region.getBounds())) continue;

for (BlockVector block : region.getBlockVectors()) {
if (rule.region.contains(block)) {
if (staticReg.contains(block)) {
subset.add(rule);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ public class RegionPlayerTracker implements Listener {
private final Set<MatchPlayer> players = Sets.newHashSet();

// The region to check against
private Region region;
private Region.Static region;
// A static filter players must match when entering
private @Nullable Filter staticFilter;
private final @Nullable Filter staticFilter;

public RegionPlayerTracker(Match match, Region region) {
this(match, region, null);
}

public RegionPlayerTracker(Match match, Region region, @Nullable Filter staticFilter) {
this.match = match;
this.region = region;
this.region = region.getStatic(match);
this.staticFilter = staticFilter;
}

public Set<MatchPlayer> getPlayers() {
return this.players;
}

public void setRegion(Region region) {
public void setRegion(Region.Static region) {
this.region = region;
for (MatchPlayer player : match.getPlayers()) {
handlePlayerMove(player.getBukkit(), player.getLocation().toVector());
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/tc/oc/pgm/core/CoreMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.util.Vector;
import tc.oc.pgm.api.event.BlockTransformEvent;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.match.MatchModule;
Expand Down Expand Up @@ -64,9 +63,10 @@ public void leakCheck(final BlockTransformEvent event) {
if (event.getWorld() != this.match.getWorld()) return;

if (Materials.isLava(event.getNewState().getType())) {
Vector blockVector = BlockVectors.center(event.getNewState()).toVector();
var blockVector = BlockVectors.center(event.getNewState());
// Vector ensuring it's inside leak region if it's above
Vector minVector = blockVector.clone().setY(0.5);
var minVector = blockVector.clone();
minVector.setY(0.5);
for (Core core : this.cores) {
if (core.hasLeaked() || !core.getLeakRegion().contains(minVector)) continue;

Expand All @@ -90,7 +90,7 @@ public void breakCheck(final BlockTransformEvent event) {
if (event.getWorld() != this.match.getWorld()) return;
ParticipantState player = ParticipantBlockTransformEvent.getPlayerState(event);

Vector blockVector = BlockVectors.center(event.getNewState()).toVector();
var blockVector = BlockVectors.center(event.getNewState());

for (Core core : this.cores) {
if (!core.hasLeaked() && core.getCasingRegion().contains(blockVector)) {
Expand Down Expand Up @@ -134,7 +134,7 @@ public void damageCheck(BlockDamageEvent event) {
Block block = event.getBlock();
if (block.getWorld() != this.match.getWorld()) return;
MatchPlayer player = this.match.getPlayer(event.getPlayer());
Vector center = BlockVectors.center(block).toVector();
var center = BlockVectors.center(block);

for (Core core : this.cores) {
if (!core.hasLeaked()
Expand All @@ -150,7 +150,7 @@ public void damageCheck(BlockDamageEvent event) {
public void lavaProtection(final BlockTransformEvent event) {
if (event.getWorld() != this.match.getWorld()) return;

Vector blockVector = BlockVectors.center(event.getNewState()).toVector();
var blockVector = BlockVectors.center(event.getNewState());
for (Core core : this.cores) {
if (core.getLavaRegion().contains(blockVector)) {
event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public Destroyable(DestroyableFactory definition, Match match) {

BlockDropsMatchModule bdmm = match.getModule(BlockDropsMatchModule.class);
if (bdmm != null) {
this.blockDropsRuleSet = bdmm.getRuleSet().subsetAffecting(this.blockRegion);
this.blockDropsRuleSet = bdmm.getRuleSet().subsetAffecting(match, this.blockRegion);
}

this.recalculateHealth();
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/tc/oc/pgm/flag/Flag.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ protected Flag(Match match, FlagDefinition definition, ImmutableSet<NetDefinitio
// always do this, but it does technically violate the contract of that method.
banner = toBanner(r.getPosition().toLocation(match.getWorld()).getBlock());
} else {
banner = StreamUtils.of(point.getRegion().getBlockVectors())
.map(pos -> toBanner(pos.toLocation(match.getWorld()).getBlock()))
banner = StreamUtils.of(point.getRegion().getBlocks(match.getWorld()))
.map(Flag::toBanner)
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/tc/oc/pgm/map/MapFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public MapContext load() throws MapException {
} catch (InvalidXMLException e) {
throw new MapException(source, info, e.getMessage(), e);
} catch (ModuleLoadException e) {
throw new MapException(source, info, e.getFullMessage(), e);
throw new MapException(source, info, e.getMessage(), e);
} catch (JDOMParseException e) {
// Set base uri so when error is displayed it shows what XML caused the issue
Document d = e.getPartialDocument();
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/tc/oc/pgm/payload/PayloadRegion.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import tc.oc.pgm.regions.Bounds;

/** This is a region that is not immutable. The origin point of the cylinder can move. */
public class PayloadRegion implements RegionDefinition {
public class PayloadRegion implements RegionDefinition.HardStatic {

private final Supplier<Vector> base;
private final double radius;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package tc.oc.pgm.portals;

import org.bukkit.util.Vector;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.region.Region;
import tc.oc.pgm.regions.TransformedRegion;

public class PortalExitRegion extends TransformedRegion {

PortalTransform portalTransform;
private final PortalTransform portalTransform;
private final PortalTransform inverseTransform;

public PortalExitRegion(Region entranceRegion, PortalTransform portalTransform) {
Expand All @@ -24,4 +25,9 @@ protected Vector transform(Vector point) {
protected Vector untransform(Vector point) {
return inverseTransform.apply(point);
}

@Override
public Region.Static getStaticImpl(Match match) {
return new PortalExitRegion(region.getStatic(match), portalTransform);
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/tc/oc/pgm/regions/BlockRegion.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.bukkit.util.Vector;
import tc.oc.pgm.api.region.RegionDefinition;

public class BlockRegion implements RegionDefinition {
public class BlockRegion implements RegionDefinition.HardStatic {
protected final Vector location;

public BlockRegion(Vector block) {
Expand Down
Loading
Loading