mirror of
https://github.com/BentoBoxWorld/Greenhouses.git
synced 2024-09-27 14:22:50 +02:00
Fixes overspawning in greenhouses. Makes spawn locs random.
This commit is contained in:
parent
f7796d7996
commit
e71e25f2bb
2
pom.xml
2
pom.xml
@ -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>
|
||||||
|
|
||||||
|
@ -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,25 +96,25 @@ 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();
|
||||||
|
Bukkit.getLogger().info("DEBUG: found " + sum);
|
||||||
// Get the blocks in the greenhouse where spawning could occur
|
// Get the blocks in the greenhouse where spawning could occur
|
||||||
Iterator<Block> it = getAvailableBlocks(gh).iterator();
|
List<Block> list = new ArrayList<>(getAvailableBlocks(gh));
|
||||||
|
Collections.shuffle(list, new Random(System.currentTimeMillis()));
|
||||||
|
Iterator<Block> it = list.iterator();
|
||||||
// Check if the greenhouse is full
|
// Check if the greenhouse is full
|
||||||
while (it.hasNext() && (sum == 0 || gh.getArea() / sum >= gh.getBiomeRecipe().getMobLimit())) {
|
while (it.hasNext() && (sum == 0 || gh.getArea() / sum >= gh.getBiomeRecipe().getMobLimit())) {
|
||||||
// Spawn something if chance says so
|
// Spawn something if chance says so
|
||||||
@ -121,9 +123,6 @@ public class EcoSystemManager {
|
|||||||
sum++;
|
sum++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Unload chunks again
|
|
||||||
chunks.forEach(p -> gh.getWorld().unloadChunk(p.x, p.z));
|
|
||||||
}, 20L);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user