Fixes overspawning in greenhouses. Makes spawn locs random.

This commit is contained in:
tastybento 2019-11-01 14:50:20 -07:00
parent f7796d7996
commit e71e25f2bb
2 changed files with 21 additions and 22 deletions

View File

@ -51,7 +51,7 @@
<!-- Revision variable removes warning about dynamic version --> <!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision> <revision>${build.version}-SNAPSHOT</revision>
<!-- This allows to change between versions and snapshots. --> <!-- This allows to change between versions and snapshots. -->
<build.version>0.4.2</build.version> <build.version>0.4.5</build.version>
<build.number>-LOCAL</build.number> <build.number>-LOCAL</build.number>
</properties> </properties>

View File

@ -2,9 +2,11 @@ package world.bentobox.greenhouses.managers;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Random;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
@ -94,36 +96,33 @@ public class EcoSystemManager {
if (gh.getBiomeRecipe().noMobs()) { if (gh.getBiomeRecipe().noMobs()) {
return; return;
} }
// Count mobs in greenhouse // Check greenhouse chunks are loaded
// Load chunks
List<Pair<Integer, Integer>> chunks = new ArrayList<>();
for (double blockX = gh.getBoundingBox().getMinX(); blockX < gh.getBoundingBox().getMaxX(); blockX+=16) { 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 blockZ = gh.getBoundingBox().getMinZ(); blockZ < gh.getBoundingBox().getMaxZ(); blockZ+=16) {
int chunkX = (int)(blockX / 16); int chunkX = (int)(blockX / 16);
int chunkZ = (int)(blockZ / 16); int chunkZ = (int)(blockZ / 16);
if (!gh.getWorld().isChunkLoaded(chunkX, chunkZ)) { if (!gh.getWorld().isChunkLoaded(chunkX, chunkZ)) {
gh.getWorld().loadChunk(chunkX, chunkZ); return;
chunks.add(new Pair<>(chunkX, chunkZ));
} }
} }
} }
Bukkit.getScheduler().runTaskLater(addon.getPlugin(), () -> { // Count the entities in the greenhouse
long sum = gh.getWorld().getEntities().stream() long sum = gh.getWorld().getEntities().stream()
.filter(e -> gh.getBiomeRecipe().getMobTypes().contains(e.getType())) .filter(e -> gh.getBiomeRecipe().getMobTypes().contains(e.getType()))
.filter(e -> gh.contains(e.getLocation())).count(); .filter(e -> gh.contains(e.getLocation())).count();
// Get the blocks in the greenhouse where spawning could occur Bukkit.getLogger().info("DEBUG: found " + sum);
Iterator<Block> it = getAvailableBlocks(gh).iterator(); // Get the blocks in the greenhouse where spawning could occur
// Check if the greenhouse is full List<Block> list = new ArrayList<>(getAvailableBlocks(gh));
while (it.hasNext() && (sum == 0 || gh.getArea() / sum >= gh.getBiomeRecipe().getMobLimit())) { Collections.shuffle(list, new Random(System.currentTimeMillis()));
// Spawn something if chance says so Iterator<Block> it = list.iterator();
if (gh.getBiomeRecipe().spawnMob(it.next())) { // Check if the greenhouse is full
// Add a mob to the sum in the greenhouse while (it.hasNext() && (sum == 0 || gh.getArea() / sum >= gh.getBiomeRecipe().getMobLimit())) {
sum++; // Spawn something if chance says so
} if (gh.getBiomeRecipe().spawnMob(it.next())) {
// Add a mob to the sum in the greenhouse
sum++;
} }
// Unload chunks again }
chunks.forEach(p -> gh.getWorld().unloadChunk(p.x, p.z));
}, 20L);
} }
/** /**