mirror of
https://github.com/BentoBoxWorld/Greenhouses.git
synced 2024-11-29 14:16:08 +01:00
Fixes bug where animals spawn too much in greenhouses.
Also tracks island deletion so greenhouses are removed when that happens.
This commit is contained in:
parent
2341d9e3eb
commit
085db69dff
@ -66,7 +66,7 @@ public class Greenhouses extends Addon {
|
||||
public void onDisable() {
|
||||
if (manager != null) {
|
||||
manager.saveGreenhouses();
|
||||
manager.getEcoMgr().cancel();
|
||||
if (manager.getEcoMgr() != null) manager.getEcoMgr().cancel();
|
||||
}
|
||||
if (settings != null) {
|
||||
new Config<>(this, Settings.class).saveConfigObject(settings);
|
||||
|
@ -0,0 +1,29 @@
|
||||
package world.bentobox.greenhouses.listeners;
|
||||
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import world.bentobox.bentobox.api.events.island.IslandEvent.IslandDeleteEvent;
|
||||
import world.bentobox.greenhouses.Greenhouses;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
public class IslandChangeEvents implements Listener {
|
||||
|
||||
private Greenhouses addon;
|
||||
|
||||
/**
|
||||
* @param addon
|
||||
*/
|
||||
public IslandChangeEvents(Greenhouses addon) {
|
||||
this.addon = addon;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled=true)
|
||||
public void onIslandDeleteEvent(IslandDeleteEvent e) {
|
||||
addon.getManager().removeGreenhouses(e.getIsland());
|
||||
}
|
||||
}
|
@ -13,6 +13,7 @@ import org.bukkit.block.Hopper;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import world.bentobox.bentobox.util.Pair;
|
||||
import world.bentobox.greenhouses.Greenhouses;
|
||||
import world.bentobox.greenhouses.data.Greenhouse;
|
||||
|
||||
@ -91,19 +92,33 @@ public class EcoSystemManager {
|
||||
return;
|
||||
}
|
||||
// Count mobs in greenhouse
|
||||
// Load chunks
|
||||
List<Pair<Integer, Integer>> chunks = new ArrayList<>();
|
||||
for (double blockX = gh.getBoundingBox().getMinX(); blockX < gh.getBoundingBox().getMaxX(); blockX+=16) {
|
||||
for (double blockZ = gh.getBoundingBox().getMinZ(); blockZ < gh.getBoundingBox().getMaxZ(); blockZ+=16) {
|
||||
int chunkX = (int)(blockX / 16);
|
||||
int chunkZ = (int)(blockZ / 16);
|
||||
if (!gh.getWorld().isChunkLoaded(chunkX, chunkZ)) {
|
||||
gh.getWorld().loadChunk(chunkX, chunkZ);
|
||||
chunks.add(new Pair<>(chunkX, chunkZ));
|
||||
}
|
||||
}
|
||||
}
|
||||
long sum = gh.getWorld().getEntities().stream()
|
||||
.filter(e -> gh.getBiomeRecipe().getMobTypes().contains(e.getType()))
|
||||
.filter(e -> gh.contains(e.getLocation())).count();
|
||||
// Get the blocks in the greenhouse where spawning could occur
|
||||
Iterator<Block> it = getAvailableBlocks(gh).iterator();
|
||||
// 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
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -12,11 +12,13 @@ import org.bukkit.event.Listener;
|
||||
|
||||
import world.bentobox.bentobox.api.events.BentoBoxReadyEvent;
|
||||
import world.bentobox.bentobox.database.Database;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.greenhouses.Greenhouses;
|
||||
import world.bentobox.greenhouses.data.Greenhouse;
|
||||
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
|
||||
import world.bentobox.greenhouses.listeners.GreenhouseEvents;
|
||||
import world.bentobox.greenhouses.listeners.GreenhouseGuard;
|
||||
import world.bentobox.greenhouses.listeners.IslandChangeEvents;
|
||||
import world.bentobox.greenhouses.listeners.SnowTracker;
|
||||
|
||||
public class GreenhouseManager implements Listener {
|
||||
@ -70,6 +72,7 @@ public class GreenhouseManager implements Listener {
|
||||
addon.registerListener(new SnowTracker(addon));
|
||||
addon.registerListener(new GreenhouseEvents(addon));
|
||||
addon.registerListener(new GreenhouseGuard(addon));
|
||||
addon.registerListener(new IslandChangeEvents(addon));
|
||||
}
|
||||
|
||||
public GreenhouseMap getMap() {
|
||||
@ -245,4 +248,14 @@ public class GreenhouseManager implements Listener {
|
||||
public EcoSystemManager getEcoMgr() {
|
||||
return ecoMgr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all greenhouses on island
|
||||
* @param island - island
|
||||
*/
|
||||
public void removeGreenhouses(Island island) {
|
||||
map.getGreenhouses(island).forEach(handler::deleteObject);
|
||||
map.removeGreenhouses(island);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package world.bentobox.greenhouses.managers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -100,11 +101,18 @@ public class GreenhouseMap {
|
||||
*/
|
||||
protected void removeGreenhouse(Greenhouse greenhouse) {
|
||||
addon.getIslands().getIslandAt(greenhouse.getLocation()).ifPresent(i -> {
|
||||
greenhouses.putIfAbsent(i, new ArrayList<>());
|
||||
greenhouses.get(i).remove(greenhouse);
|
||||
if (greenhouses.containsKey(i)) greenhouses.get(i).remove(greenhouse);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param island
|
||||
*/
|
||||
public void removeGreenhouses(Island island) {
|
||||
greenhouses.remove(island);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return a list of all the Greenhouses
|
||||
*/
|
||||
@ -112,6 +120,15 @@ public class GreenhouseMap {
|
||||
return greenhouses.values().stream().flatMap(List::stream).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all greenhouses on island
|
||||
* @param island - island
|
||||
* @return list of islands or empty list
|
||||
*/
|
||||
public List<Greenhouse> getGreenhouses(Island island) {
|
||||
return greenhouses.getOrDefault(island, Collections.emptyList());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return number of greenhouses loaded
|
||||
*/
|
||||
|
@ -178,7 +178,7 @@ biomes:
|
||||
TALL_GRASS: 10:GRASS_BLOCK
|
||||
mobs:
|
||||
HORSE: 10:GRASS_BLOCK
|
||||
moblimit: 9
|
||||
moblimit: 1
|
||||
RIVER:
|
||||
friendlyname: "Clay river"
|
||||
biome: RIVER
|
||||
|
Loading…
Reference in New Issue
Block a user