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

Add a cylinder filter #55

Merged
merged 1 commit into from
Oct 22, 2024
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
24 changes: 24 additions & 0 deletions src/main/java/xyz/nucleoid/stimuli/filter/CylinderFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package xyz.nucleoid.stimuli.filter;

import net.minecraft.registry.RegistryKey;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import xyz.nucleoid.stimuli.EventSource;

public record CylinderFilter(RegistryKey<World> dimension, BlockPos center, int radius, int height) implements EventFilter {
@Override
public boolean accepts(EventSource source) {
var pos = source.getPos();
var dimension = source.getDimension();
return (dimension == null || dimension == this.dimension) && (pos == null || this.containsPos(pos));
}

private boolean containsPos(BlockPos pos) {
var center = this.center;
var radius = this.radius;
var height = this.height;
var dx = pos.getX() - center.getX();
var dz = pos.getZ() - center.getZ();
return dx * dx + dz * dz <= radius * radius && pos.getY() >= center.getY() && pos.getY() <= center.getY() + height;
}
}
13 changes: 13 additions & 0 deletions src/main/java/xyz/nucleoid/stimuli/filter/EventFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ static EventFilter box(RegistryKey<World> dimension, BlockPos min, BlockPos max)
return new BoxFilter(dimension, min, max);
}

/**
* Returns an event filter that accepts only events from the given dimension and cylinder bounds.
*
* @param dimension the dimension to filter for
* @param center the center of the cylinder at bottom
* @param radius the radius of the cylinder
* @param height the height of the cylinder
* @return the result event filter
*/
static EventFilter cylinder(RegistryKey<World> dimension, BlockPos center, int radius, int height) {
return new CylinderFilter(dimension, center, radius, height);
}

/**
* Returns an event filter that accepts events from any of the given filters.
*
Expand Down