Skip to content

Commit

Permalink
Added border features to Area (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKaVu committed Jun 5, 2023
1 parent bd4c82c commit 9e454c5
Showing 1 changed file with 37 additions and 118 deletions.
155 changes: 37 additions & 118 deletions src/main/java/dev/kavu/gameapi/world/Area.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,160 +3,79 @@
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

import java.util.function.Predicate;

public class Area {

// Fields
private int dx;
private int dy;
private int dz;
private final Location originPos;

private boolean blockPlacement = true;
private boolean blockDestruction = true;
private boolean blockInteraction = true;
private AffectTarget target;

private Predicate<Material> blockFilter = (material) -> true;
private Predicate<Player> playerFilter = (player) -> true;
private double sizeX;
private double sizeY;
private double sizeZ;
private final Location center;
private final AreaShape shape;

// Constructors
public Area(Location originPos, int x, int y, int z, AffectTarget affectTarget, boolean center) {
public Area(Location center, double x, double y, double z, AreaShape shape) {
if(x < 0) throw new IllegalArgumentException("x was less than 0");
if(y < 0) throw new IllegalArgumentException("y was less than 0");
if(z < 0) throw new IllegalArgumentException("z was less than 0");
if(affectTarget == null){
throw new NullPointerException();
}
if(center) {
this.dx = 2 * x;
this.dy = 2 * y;
this.dz = 2 * z;
this.originPos = originPos.add(new Vector(-x, -y, -z));
} else {
this.dx = x;
this.dy = y;
this.dz = z;
this.originPos = originPos;
}
target = affectTarget;
}

public Area(Location corner1, Location corner2, AffectTarget affectTarget) {
if(affectTarget == null){
throw new NullPointerException();
}
dx = Math.abs(corner1.getBlockX() - corner2.getBlockX());
dy = Math.abs(corner1.getBlockY() - corner2.getBlockY());
dz = Math.abs(corner1.getBlockZ() - corner2.getBlockZ());

originPos = new Location(corner1.getWorld(),
Math.min(corner1.getBlockX(), corner2.getBlockX()),
Math.min(corner1.getBlockY(), corner2.getBlockY()),
Math.min(corner1.getBlockZ(), corner2.getBlockZ())
);
this.center = center;
this.sizeX = 2 * x;
this.sizeY = 2 * y;
this.sizeZ = 2 * z;
this.shape = shape;

target = affectTarget;
}

// Getters & Setters
public int getSizeX() {
return dx;
}

public int getSizeY() {
return dy;
}

public int getSizeZ() {
return dz;
}

public Location getOriginPos() {
return originPos;
}

public boolean allowBlockPlacement() {
return blockPlacement;
}

public void setBlockPlacement(boolean blockPlacement) {
this.blockPlacement = blockPlacement;
}

public boolean allowBlockDestruction() {
return blockDestruction;
public double getSizeX() {
return sizeX;
}

public void setBlockDestruction(boolean blockDestruction) {
this.blockDestruction = blockDestruction;
public double getSizeY() {
return sizeY;
}

public boolean allowBlockInteraction() {
return blockInteraction;
public double getSizeZ() {
return sizeZ;
}

public void setBlockInteraction(boolean blockInteraction) {
this.blockInteraction = blockInteraction;
public Location getCenter() {
return center;
}

public AffectTarget getTarget(){
return target;
public AreaShape getShape() {
return shape;
}

public void setTarget(AffectTarget target){
if(target == null){
throw new NullPointerException();
}
this.target = target;
}

public boolean filterBlock(Material material) {
return blockFilter.test(material);
}

public void setBlockFilter(Predicate<Material> blockFilter) {
if(blockFilter == null){
throw new NullPointerException();
}
this.blockFilter = blockFilter;
}

public boolean filterPlayer(Player player) {
return playerFilter.test(player);
// Functionality
public Area resize(int x, int y, int z){
this.sizeX = x;
this.sizeY = y;
this.sizeZ = z;
return this;
}

public void setPlayerFilter(Predicate<Player> playerFilter) {
if(playerFilter == null){
throw new NullPointerException();
}
this.playerFilter = playerFilter;
public Area scale(double scaleX, double scaleY, double scaleZ){
sizeX *= scaleX;
sizeY *= scaleY;
sizeZ *= scaleZ;
return this;
}

// Functionality
public Area resize(int x, int y, int z){
this.dx = x;
this.dy = y;
this.dz = z;
return this;
public Area scale(double scale){
return scale(scale, scale, scale);
}

public boolean hasLocation(Location location){
if(location == null){
throw new NullPointerException();
}
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();

if(x < originPos.getBlockX() || x > originPos.getBlockX() + dx) return false;
if(y < originPos.getBlockY() || y > originPos.getBlockY() + dy) return false;
if(z < originPos.getBlockZ() || z > originPos.getBlockZ() + dz) return false;
if(location.getWorld() != center.getWorld()) return false;

return true;
return shape.compute(this, center);
}

public boolean hasPlayer(Player player){
Expand All @@ -166,4 +85,4 @@ public boolean hasPlayer(Player player){
return hasLocation(player.getLocation()) || hasLocation(player.getEyeLocation());
}

}
}

0 comments on commit 9e454c5

Please sign in to comment.