Skip to content

Commit

Permalink
Add new BoundingBox methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jun 30, 2020
1 parent 85acae1 commit 7dffa0b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.bukkit.Material;
import org.bukkit.World;

import net.citizensnpcs.api.util.BoundingBox;

public class AsyncChunkSnapshotBlockSource extends CachingChunkBlockSource<ChunkSnapshot> {
public AsyncChunkSnapshotBlockSource(Location location, float radius) {
super(location, radius);
Expand All @@ -33,6 +35,11 @@ protected ChunkSnapshot getChunkObject(int x, int z) {
return null;
}

@Override
protected BoundingBox getCollisionBox(ChunkSnapshot chunk, int x, int y, int z) {
return null; // TODO
}

@Override
protected int getLightLevel(ChunkSnapshot chunk, int x, int y, int z) {
return Math.min(15, chunk.getBlockSkyLight(x, y, z) + chunk.getBlockEmittedLight(x, y, z));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
import org.bukkit.World;
import org.bukkit.util.Vector;

import net.citizensnpcs.api.util.BoundingBox;

public abstract class BlockSource {
public abstract BoundingBox getCollisionBox(int x, int y, int z);

public BoundingBox getCollisionBox(Vector pos) {
return getCollisionBox(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ());
}

public abstract Material getMaterialAt(int x, int y, int z);

public Material getMaterialAt(Vector pos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import com.google.common.collect.Maps;

import net.citizensnpcs.api.util.BoundingBox;

public abstract class CachingChunkBlockSource<T> extends BlockSource {
private final Map<ChunkCoord, ChunkCache> chunkCache = Maps.newHashMap();
private final Object[][] chunks;
Expand Down Expand Up @@ -39,6 +41,19 @@ protected CachingChunkBlockSource(World world, int minX, int minZ, int maxX, int

protected abstract T getChunkObject(int x, int z);

@Override
public BoundingBox getCollisionBox(int x, int y, int z) {
if (y > 255) {
return new BoundingBox(-1, -1, -1, 1, 1, 1);
}
T chunk = getSpecific(x, z);
if (chunk != null)
return getCollisionBox(chunk, x & 15, y, z & 15);
return BoundingBox.convert(world.getBlockAt(x, y, z).getBoundingBox());
}

protected abstract BoundingBox getCollisionBox(T chunk, int x, int y, int z);

protected abstract int getLightLevel(T chunk, int x, int y, int z);

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.bukkit.Material;
import org.bukkit.World;

import net.citizensnpcs.api.util.BoundingBox;

public class ChunkBlockSource extends CachingChunkBlockSource<Chunk> {
public ChunkBlockSource(Location location, float radius) {
super(location, radius);
Expand All @@ -19,6 +21,11 @@ protected Chunk getChunkObject(int x, int z) {
return world.getChunkAt(x, z);
}

@Override
protected BoundingBox getCollisionBox(Chunk chunk, int x, int y, int z) {
return BoundingBox.convert(chunk.getBlock(x, y, z).getBoundingBox());
}

@Override
protected int getLightLevel(Chunk chunk, int x, int y, int z) {
return chunk.getBlock(x, y, z).getLightLevel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.bukkit.Material;
import org.bukkit.World;

import net.citizensnpcs.api.util.BoundingBox;

public class ChunkSnapshotBlockSource extends CachingChunkBlockSource<ChunkSnapshot> {
public ChunkSnapshotBlockSource(Location location, float radius) {
super(location, radius);
Expand All @@ -19,6 +21,11 @@ protected ChunkSnapshot getChunkObject(int x, int z) {
return world.getChunkAt(x, z).getChunkSnapshot(false, false, false);
}

@Override
protected BoundingBox getCollisionBox(ChunkSnapshot chunk, int x, int y, int z) {
return null; // TODO
}

@Override
protected int getLightLevel(ChunkSnapshot chunk, int x, int y, int z) {
return Math.min(15, chunk.getBlockSkyLight(x, y, z) + chunk.getBlockEmittedLight(x, y, z));
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/net/citizensnpcs/api/util/BoundingBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.citizensnpcs.api.util;

public class BoundingBox {
public final double maxX;
public final double maxY;
public final double maxZ;
public final double minX;
public final double minY;
public final double minZ;

public BoundingBox(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
this.minX = minX;
this.minY = minY;
this.minZ = minZ;
this.maxX = maxX;
this.maxY = maxY;
this.maxZ = maxZ;
}

public BoundingBox add(int x, int y, int z) {
return new BoundingBox(minX + x, minY + y, minZ + z, maxX + x, maxY + y, maxZ + z);
}

public static BoundingBox convert(org.bukkit.util.BoundingBox bukkit) {
return new BoundingBox(bukkit.getMinX(), bukkit.getMinY(), bukkit.getMinZ(), bukkit.getMaxX(), bukkit.getMaxY(),
bukkit.getMaxZ());
}
}

0 comments on commit 7dffa0b

Please sign in to comment.