Null checks.

This commit is contained in:
tastybento 2021-08-01 16:17:12 -07:00
parent 0b9019fbfa
commit 818f6fc925
11 changed files with 77 additions and 24 deletions

View File

@ -1,5 +1,6 @@
package world.bentobox.greenhouses.data;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
@ -158,7 +159,11 @@ public class Greenhouse implements DataObject {
* @param v the roofHopperLocation to set
*/
public void setRoofHopperLocation(@Nullable Vector v) {
this.roofHopperLocation = v == null ? null : v.toLocation(getWorld());
if (v == null || getWorld() == null) {
this.roofHopperLocation = null;
} else {
this.roofHopperLocation = v.toLocation(getWorld());
}
}
/**
@ -206,7 +211,7 @@ public class Greenhouse implements DataObject {
*/
@Nullable
public World getWorld() {
return this.getLocation().getWorld();
return this.getLocation() == null ? null : this.getLocation().getWorld();
}
/**
@ -246,9 +251,9 @@ public class Greenhouse implements DataObject {
/**
* @return the missingBlocks
*/
@Nullable
@NonNull
public Map<Material, Integer> getMissingBlocks() {
return missingBlocks;
return Objects.requireNonNullElseGet(missingBlocks, () -> new HashMap<>());
}
/**

View File

@ -108,7 +108,15 @@ public class GreenhouseEvents implements Listener {
handleTransition(User.getInstance(e.getPlayer()), e.getTo(), e.getFrom());
}
/**
* @param user user
* @param toLoc to location
* @param fromLoc from location
*/
private void handleTransition(User user, Location toLoc, Location fromLoc) {
if (user == null) {
return;
}
Optional<Greenhouse> to = addon.getManager().getMap().getGreenhouse(toLoc);
Optional<Greenhouse> from = addon.getManager().getMap().getGreenhouse(fromLoc);
if (to.isEmpty() && from.isEmpty()) {

View File

@ -41,7 +41,15 @@ public class SnowTracker implements Listener {
}
/**
* @param gh - greenhouse
* @return true if snow was create, false if not.
*/
private boolean getAirBlocks(Greenhouse gh) {
if (gh.getLocation() == null) {
// Greenhouse does not have a location for some reason.
return false;
}
boolean createdSnow = false;
List<Block> waterBlocks = new ArrayList<>();
for (int x = (int)gh.getBoundingBox().getMinX() + 1; x < (int)gh.getBoundingBox().getMaxX() -1; x++) {
@ -108,7 +116,7 @@ public class SnowTracker implements Listener {
private void removeWaterBucketAndShake(Greenhouse g) {
// Scatter snow
if (getAirBlocks(g)) {
if (getAirBlocks(g) && g.getRoofHopperLocation() != null) {
Hopper h = ((Hopper)g.getRoofHopperLocation().getBlock().getState());
h.getInventory().removeItem(new ItemStack(Material.WATER_BUCKET));
h.getInventory().addItem(new ItemStack(Material.BUCKET));

View File

@ -85,7 +85,10 @@ public class EcoSystemManager {
}
private void convertBlocks(Greenhouse gh) {
if(!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)){
World world = gh.getWorld();
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)){
return;
}
@ -95,7 +98,6 @@ public class EcoSystemManager {
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());
World world = gh.getWorld();
BiomeRecipe biomeRecipe = gh.getBiomeRecipe();
for (int x = gh_min_x; x < gh_max_x; x++) {
@ -112,8 +114,10 @@ public class EcoSystemManager {
}
private void verify(Greenhouse gh) {
if(!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)){
//addon.log("Skipping verify for unloaded greenhouse at " + gh.getLocation());
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)){
// Skipping verify for unloaded greenhouse
return;
}
gh.getBiomeRecipe().checkRecipe(gh).thenAccept(rs -> {
@ -126,8 +130,9 @@ public class EcoSystemManager {
}
private void addMobs(Greenhouse gh) {
if(!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)){
//addon.log("Skipping addmobs for unloaded greenhouse at " + gh.getLocation());
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)){
// Skipping addmobs for unloaded greenhouse
return;
}
if (gh.getBiomeRecipe().noMobs()) {
@ -166,8 +171,9 @@ public class EcoSystemManager {
* @param gh - greenhouse
*/
private void growPlants(Greenhouse gh) {
if(!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)){
//addon.log("Skipping growplants for unloaded greenhouse at " + gh.getLocation());
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)){
//Skipping growplants for unloaded greenhouse
return;
}
int bonemeal = getBoneMeal(gh);
@ -207,6 +213,7 @@ public class EcoSystemManager {
*/
public List<Block> getAvailableBlocks(Greenhouse gh, boolean ignoreLiquid) {
List<Block> 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--) {
@ -234,12 +241,14 @@ public class EcoSystemManager {
.mapToInt(ItemStack::getAmount).sum();
}
/**
* Get the hopper
* @param gh greenhouse
* @return hopper block or null if it does not exist
*/
private Hopper getHopper(Greenhouse gh) {
if (gh.getRoofHopperLocation() == null) {
return null;
}
// Check if the hopper block is still a hopper
if (gh.getRoofHopperLocation().getBlock().getType() != Material.HOPPER) {
if (gh.getRoofHopperLocation() == null || !gh.getRoofHopperLocation().getBlock().getType().equals(Material.HOPPER)) {
gh.setRoofHopperLocation(null);
return null;
}

View File

@ -139,6 +139,14 @@ public class GreenhouseManager implements Listener {
public void removeGreenhouse(Greenhouse g) {
handler.deleteObject(g);
map.removeGreenhouse(g);
if (g.getOriginalBiome() == null) {
addon.logError("Greenhouse had no original biome: " + g.getLocation());
return;
}
if (g.getLocation() == null || g.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) {

View File

@ -34,7 +34,7 @@ public class GreenhouseMap {
*/
public GreenhouseResult addGreenhouse(Greenhouse greenhouse) {
// Validation checks
if (greenhouse.getBiomeRecipe() == null) {
if (greenhouse.getBiomeRecipe().getBiome() == null) {
return GreenhouseResult.FAIL_UNKNOWN_RECIPE;
}
if (greenhouse.getWorld() == null) {
@ -105,7 +105,7 @@ public class GreenhouseMap {
}
private boolean isOverlapping(Greenhouse greenhouse) {
return addon.getIslands().getIslandAt(greenhouse.getLocation()).map(i -> {
return greenhouse.getLocation() != null && addon.getIslands().getIslandAt(greenhouse.getLocation()).map(i -> {
greenhouses.putIfAbsent(i, new ArrayList<>());
return greenhouses.get(i).stream().anyMatch(g -> g.getBoundingBox().overlaps(greenhouse.getBoundingBox()));
}).orElse(false);
@ -117,9 +117,11 @@ public class GreenhouseMap {
* @param greenhouse - greenhouse
*/
protected void removeGreenhouse(Greenhouse greenhouse) {
addon.getIslands().getIslandAt(greenhouse.getLocation()).ifPresent(i -> {
if (greenhouses.containsKey(i)) greenhouses.get(i).remove(greenhouse);
});
if (greenhouse.getLocation() != null) {
addon.getIslands().getIslandAt(greenhouse.getLocation()).ifPresent(i -> {
if (greenhouses.containsKey(i)) greenhouses.get(i).remove(greenhouse);
});
}
}
/**

View File

@ -43,6 +43,10 @@ public class PanelClick implements ClickHandler {
}
private boolean makeGreenhouse(User user, BiomeRecipe br) {
if (user.getLocation() == null) {
addon.logError("User has no location : " + user.getName());
return false;
}
// Check flag
if (!addon.getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) {
user.sendMessage("greenhouses.errors.no-rank");

View File

@ -92,6 +92,10 @@ class MakeCommand extends CompositeCommand {
* @return true if successful
*/
private boolean makeGreenhouse(User user, BiomeRecipe br) {
if (user.getLocation() == null) {
getAddon().logError("User had no location");
return false;
}
// Check flag
if (!getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) {
user.sendMessage("greenhouses.errors.no-rank");

View File

@ -35,6 +35,10 @@ class RemoveCommand extends CompositeCommand {
*/
@Override
public boolean execute(User user, String label, List<String> args) {
if (user.getLocation() == null) {
getAddon().logError("User had no location");
return false;
}
// Check flag
if (!getIslands().getIslandAt(user.getLocation()).map(i -> i.isAllowed(user, Greenhouses.GREENHOUSES)).orElse(false)) {
user.sendMessage("greenhouses.errors.no-rank");

View File

@ -2,6 +2,7 @@ package world.bentobox.greenhouses.world;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@ -101,7 +102,7 @@ public class AsyncWorldCache {
int xx = x >= 0 ? x % 16 : (16 + (x % 16)) % 16;
int zz = z >= 0 ? z % 16 : (16 + (z % 16)) % 16;
try {
return getSnap(x,z).getBlockType(xx, y, zz);
return Objects.requireNonNull(getSnap(x, z)).getBlockType(xx, y, zz);
} catch (InterruptedException | ExecutionException e) {
Greenhouses.getInstance().logError("Chunk could not be obtained async! " + e);
// Restore interrupted state...

View File

@ -293,7 +293,7 @@ public class GreenhouseTest {
*/
@Test
public void testGetMissingBlocks() {
assertNull(gh.getMissingBlocks());
assertNotNull(gh.getMissingBlocks());
}
}