mirror of
https://github.com/BentoBoxWorld/Greenhouses.git
synced 2025-02-01 04:41:20 +01:00
Code refactoring around BoundingBox
This commit is contained in:
parent
6e41588405
commit
5808c5933a
@ -171,7 +171,7 @@ public class Greenhouse implements DataObject {
|
||||
*/
|
||||
@NonNull
|
||||
public BoundingBox getBoundingBox() {
|
||||
return Objects.requireNonNullElseGet(boundingBox, BoundingBox::new);
|
||||
return Objects.requireNonNull(boundingBox);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -220,7 +220,9 @@ public class Greenhouse implements DataObject {
|
||||
* @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());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -262,12 +264,13 @@ public class Greenhouse implements DataObject {
|
||||
* @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
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,6 @@ import org.bukkit.entity.Entity;
|
||||
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;
|
||||
@ -367,9 +366,7 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {
|
||||
.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;
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import org.bukkit.event.block.BlockFormEvent;
|
||||
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;
|
||||
@ -58,9 +59,10 @@ public class SnowTracker implements Listener {
|
||||
}
|
||||
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)) {
|
||||
|
@ -15,6 +15,7 @@ import org.bukkit.block.BlockFace;
|
||||
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;
|
||||
@ -85,18 +86,20 @@ public class EcoSystemManager {
|
||||
}
|
||||
|
||||
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++) {
|
||||
@ -129,8 +132,9 @@ public class EcoSystemManager {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@ -138,8 +142,8 @@ public class EcoSystemManager {
|
||||
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)) {
|
||||
@ -170,8 +174,9 @@ public class EcoSystemManager {
|
||||
* @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;
|
||||
}
|
||||
@ -212,11 +217,13 @@ public class EcoSystemManager {
|
||||
* @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()) {
|
||||
|
@ -12,6 +12,7 @@ import org.bukkit.Location;
|
||||
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;
|
||||
@ -135,25 +136,26 @@ public class GreenhouseManager implements Listener {
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -250,9 +252,10 @@ public class GreenhouseManager implements Listener {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user