Skip to content

Commit

Permalink
Code refactoring around BoundingBox
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Dec 31, 2021
1 parent 6e41588 commit 5808c59
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 43 deletions.
15 changes: 9 additions & 6 deletions src/main/java/world/bentobox/greenhouses/data/Greenhouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void setRoofHopperLocation(@Nullable Vector v) {
*/
@NonNull
public BoundingBox getBoundingBox() {
return Objects.requireNonNullElseGet(boundingBox, BoundingBox::new);
return Objects.requireNonNull(boundingBox);
}

/**
Expand Down Expand Up @@ -220,7 +220,9 @@ public World getWorld() {
* @return true if inside the greenhouse
*/
public boolean contains(Location location2) {
return location.getWorld() != null && location.getWorld().equals(location2.getWorld()) && boundingBox.contains(location2.toVector());
return getLocation().getWorld() != null
&& getLocation().getWorld().equals(location2.getWorld())
&& getBoundingBox().contains(location2.toVector());
}

/**
Expand Down Expand Up @@ -262,12 +264,13 @@ public Map<Material, Integer> getMissingBlocks() {
* @return true if wall or roof block
*/
public boolean isRoofOrWallBlock(Location l) {
final BoundingBox bb = getBoundingBox();
return (l.getBlockY() > this.getFloorHeight()
&& ((l.getBlockY() == getCeilingHeight() - 1)
|| l.getBlockX() == (int)getBoundingBox().getMinX()
|| l.getBlockX() == (int)getBoundingBox().getMaxX() - 1
|| l.getBlockZ() == (int)getBoundingBox().getMinZ()
|| l.getBlockZ() == (int)getBoundingBox().getMaxZ() - 1
|| l.getBlockX() == (int)bb.getMinX()
|| l.getBlockX() == (int)bb.getMaxX() - 1
|| l.getBlockZ() == (int)bb.getMinZ()
|| l.getBlockZ() == (int)bb.getMaxZ() - 1
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Hoglin;
import org.bukkit.entity.Piglin;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;

import com.google.common.base.Enums;
Expand Down Expand Up @@ -367,9 +366,7 @@ public boolean spawnMob(Block b) {
.getManager()
.getMap()
.getGreenhouse(b.getLocation()).map(gh -> {
BoundingBox interior = gh.getBoundingBox().clone();
interior.expand(-1, -1, -1);
if (!interior.contains(entity.getBoundingBox())) {
if (!gh.getInternalBoundingBox().contains(entity.getBoundingBox())) {
entity.remove();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.bukkit.event.weather.WeatherChangeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.BoundingBox;

import com.google.common.base.Enums;
import com.google.common.base.Optional;
Expand Down Expand Up @@ -58,9 +59,10 @@ private boolean getAirBlocks(Greenhouse gh) {
}
boolean createdSnow = false;
List<Block> waterBlocks = new ArrayList<>();
for (int x = (int)gh.getBoundingBox().getMinX() + 1; x < (int)gh.getBoundingBox().getMaxX() -1; x++) {
for (int z = (int)gh.getBoundingBox().getMinZ() + 1; z < (int)gh.getBoundingBox().getMaxZ() - 1; z++) {
for (int y = (int)gh.getBoundingBox().getMaxY() - 2; y >= (int)gh.getBoundingBox().getMinY(); y--) {
final BoundingBox bb = gh.getBoundingBox();
for (int x = (int)bb.getMinX() + 1; x < (int)bb.getMaxX() -1; x++) {
for (int z = (int)bb.getMinZ() + 1; z < (int)bb.getMaxZ() - 1; z++) {
for (int y = (int)bb.getMaxY() - 2; y >= (int)bb.getMinY(); y--) {
Block b = Objects.requireNonNull(gh.getLocation().getWorld()).getBlockAt(x, y, z);
Material type = b.getType();
if (type.equals(Material.AIR) || type.equals(Material.SNOW)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.bukkit.block.Hopper;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.NumberConversions;

import world.bentobox.greenhouses.Greenhouses;
Expand Down Expand Up @@ -85,18 +86,20 @@ void setup() {
}

private void convertBlocks(Greenhouse gh) {
World world = gh.getWorld();
final World world = gh.getWorld();
final BoundingBox bb = gh.getBoundingBox();
if(world == null || gh.getLocation() == null || gh.getLocation().getWorld() == null
|| !gh.getLocation().getWorld().isChunkLoaded(((int) gh.getBoundingBox().getMaxX()) >> 4, ((int) gh.getBoundingBox().getMaxZ()) >> 4)
|| !gh.getLocation().getWorld().isChunkLoaded(((int) gh.getBoundingBox().getMinX()) >> 4, ((int) gh.getBoundingBox().getMinZ()) >> 4)){
|| !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMaxX()) >> 4, ((int) bb.getMaxZ()) >> 4)
|| !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMinX()) >> 4, ((int) bb.getMinZ()) >> 4)){
return;
}
int gh_min_x = NumberConversions.floor(gh.getInternalBoundingBox().getMinX());
int gh_max_x = NumberConversions.floor(gh.getInternalBoundingBox().getMaxX());
final BoundingBox ibb = gh.getInternalBoundingBox();
int gh_min_x = NumberConversions.floor(ibb.getMinX());
int gh_max_x = NumberConversions.floor(ibb.getMaxX());
int gh_min_y = NumberConversions.floor(gh.getBoundingBox().getMinY()); // Note: this gets the floor
int gh_max_y = NumberConversions.floor(gh.getInternalBoundingBox().getMaxY());
int gh_min_z = NumberConversions.floor(gh.getInternalBoundingBox().getMinZ());
int gh_max_z = NumberConversions.floor(gh.getInternalBoundingBox().getMaxZ());
int gh_max_y = NumberConversions.floor(ibb.getMaxY());
int gh_min_z = NumberConversions.floor(ibb.getMinZ());
int gh_max_z = NumberConversions.floor(ibb.getMaxZ());
BiomeRecipe biomeRecipe = gh.getBiomeRecipe();

for (int x = gh_min_x; x < gh_max_x; x++) {
Expand Down Expand Up @@ -129,17 +132,18 @@ private void verify(Greenhouse gh) {
}

private void addMobs(Greenhouse gh) {
final BoundingBox bb = gh.getBoundingBox();
if(gh.getLocation() == null || gh.getLocation().getWorld() == null || gh.getWorld() == null
|| !gh.getLocation().getWorld().isChunkLoaded(((int) gh.getBoundingBox().getMaxX()) >> 4, ((int) gh.getBoundingBox().getMaxZ()) >> 4) || !gh.getLocation().getWorld().isChunkLoaded(((int) gh.getBoundingBox().getMinX()) >> 4, ((int) gh.getBoundingBox().getMinZ()) >> 4)){
|| !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMaxX()) >> 4, ((int) bb.getMaxZ()) >> 4) || !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMinX()) >> 4, ((int) bb.getMinZ()) >> 4)){
// Skipping addmobs for unloaded greenhouse
return;
}
if (gh.getBiomeRecipe().noMobs()) {
return;
}
// Check greenhouse chunks are loaded
for (double blockX = gh.getBoundingBox().getMinX(); blockX < gh.getBoundingBox().getMaxX(); blockX+=16) {
for (double blockZ = gh.getBoundingBox().getMinZ(); blockZ < gh.getBoundingBox().getMaxZ(); blockZ+=16) {
for (double blockX = bb.getMinX(); blockX < bb.getMaxX(); blockX+=16) {
for (double blockZ = bb.getMinZ(); blockZ < bb.getMaxZ(); blockZ+=16) {
int chunkX = (int)(blockX / 16);
int chunkZ = (int)(blockZ / 16);
if (!gh.getWorld().isChunkLoaded(chunkX, chunkZ)) {
Expand Down Expand Up @@ -170,8 +174,9 @@ private void addMobs(Greenhouse gh) {
* @param gh - greenhouse
*/
private void growPlants(Greenhouse gh) {
final BoundingBox bb = gh.getBoundingBox();
if (gh.getLocation() == null || gh.getLocation().getWorld() == null
|| !gh.getLocation().getWorld().isChunkLoaded(((int) gh.getBoundingBox().getMaxX()) >> 4, ((int) gh.getBoundingBox().getMaxZ()) >> 4) || !gh.getLocation().getWorld().isChunkLoaded(((int) gh.getBoundingBox().getMinX()) >> 4, ((int) gh.getBoundingBox().getMinZ()) >> 4)){
|| !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMaxX()) >> 4, ((int) bb.getMaxZ()) >> 4) || !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMinX()) >> 4, ((int) bb.getMinZ()) >> 4)){
//Skipping growplants for unloaded greenhouse
return;
}
Expand Down Expand Up @@ -212,11 +217,13 @@ private void setBoneMeal(Greenhouse gh, int value) {
* @return List of blocks
*/
protected List<GrowthBlock> getAvailableBlocks(Greenhouse gh, boolean ignoreLiquid) {
final BoundingBox bb = gh.getBoundingBox();
final BoundingBox ibb = gh.getInternalBoundingBox();
List<GrowthBlock> result = new ArrayList<>();
if (gh.getWorld() == null) return result;
for (double x = gh.getInternalBoundingBox().getMinX(); x < gh.getInternalBoundingBox().getMaxX(); x++) {
for (double z = gh.getInternalBoundingBox().getMinZ(); z < gh.getInternalBoundingBox().getMaxZ(); z++) {
for (double y = gh.getInternalBoundingBox().getMaxY() - 1; y >= gh.getBoundingBox().getMinY(); y--) {
for (double x = ibb.getMinX(); x < ibb.getMaxX(); x++) {
for (double z = ibb.getMinZ(); z < ibb.getMaxZ(); z++) {
for (double y = ibb.getMaxY() - 1; y >= bb.getMinY(); y--) {
Block b = gh.getWorld().getBlockAt(NumberConversions.floor(x), NumberConversions.floor(y), NumberConversions.floor(z));
// Check ceiling blocks
if (b.isEmpty() && !b.getRelative(BlockFace.UP).isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.bukkit.block.Biome;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.util.BoundingBox;

import world.bentobox.bentobox.api.events.BentoBoxReadyEvent;
import world.bentobox.bentobox.database.Database;
Expand Down Expand Up @@ -135,25 +136,26 @@ public void saveGreenhouses() {

/**
* Removes the greenhouse from the world and resets biomes
* @param g - greenhouse
* @param gh - greenhouse
*/
public void removeGreenhouse(Greenhouse g) {
handler.deleteObject(g);
map.removeGreenhouse(g);
if (g.getOriginalBiome() == null) {
addon.logError("Greenhouse had no original biome: " + g.getLocation());
public void removeGreenhouse(Greenhouse gh) {
handler.deleteObject(gh);
map.removeGreenhouse(gh);
if (gh.getOriginalBiome() == null) {
addon.logError("Greenhouse had no original biome: " + gh.getLocation());
return;
}
if (g.getLocation() == null || g.getLocation().getWorld() == null) {
if (gh.getLocation() == null || gh.getLocation().getWorld() == null) {
// Greenhouse is messed up. It's being deleted anyway.
return;
}
addon.log("Returning biome to original state: " + g.getOriginalBiome().toString());
for (int x = (int)g.getBoundingBox().getMinX(); x<= (int)g.getBoundingBox().getMaxX(); x+=4) {
for (int z = (int)g.getBoundingBox().getMinZ(); z<= (int)g.getBoundingBox().getMaxZ(); z+=4) {
for (int y = (int)g.getBoundingBox().getMinY(); y<= (int)g.getBoundingBox().getMaxY(); y+=4) {
addon.log("Returning biome to original state: " + gh.getOriginalBiome().toString());
final BoundingBox bb = gh.getBoundingBox();
for (int x = (int)bb.getMinX(); x<= (int)bb.getMaxX(); x+=4) {
for (int z = (int)bb.getMinZ(); z<= (int)bb.getMaxZ(); z+=4) {
for (int y = (int)bb.getMinY(); y<= (int)bb.getMaxY(); y+=4) {
// Set back to the original biome
g.getLocation().getWorld().setBiome(x, y, z, g.getOriginalBiome());
gh.getLocation().getWorld().setBiome(x, y, z, gh.getOriginalBiome());
}
}
}
Expand Down Expand Up @@ -250,9 +252,10 @@ private void activateGreenhouse(Greenhouse gh) {
addon.logError("Biome recipe error - no such biome for " + gh.getBiomeRecipe().getName());
return;
}
for (int x = (int)gh.getBoundingBox().getMinX(); x < gh.getBoundingBox().getMaxX(); x+=4) {
for (int z = (int)gh.getBoundingBox().getMinZ(); z < gh.getBoundingBox().getMaxZ(); z+=4) {
for (int y = (int)gh.getBoundingBox().getMinY(); y < gh.getBoundingBox().getMaxY(); y+=4) {
final BoundingBox bb = gh.getBoundingBox();
for (int x = (int)bb.getMinX(); x < bb.getMaxX(); x+=4) {
for (int z = (int)bb.getMinZ(); z < bb.getMaxZ(); z+=4) {
for (int y = (int)bb.getMinY(); y < bb.getMaxY(); y+=4) {
Objects.requireNonNull(gh.getWorld()).setBiome(x, y, z, ghBiome);
}
}
Expand Down

0 comments on commit 5808c59

Please sign in to comment.