Skip to content

Commit

Permalink
Fixes bug where animals spawn too much in greenhouses.
Browse files Browse the repository at this point in the history
Also tracks island deletion so greenhouses are removed when that
happens.
  • Loading branch information
tastybento committed Jul 8, 2019
1 parent 2341d9e commit 085db69
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/world/bentobox/greenhouses/Greenhouses.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void onEnable() {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;

Expand Down Expand Up @@ -91,19 +92,33 @@ private void addMobs(Greenhouse gh) {
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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@

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 {
Expand Down Expand Up @@ -70,6 +72,7 @@ public void startManager(BentoBoxReadyEvent e) {
addon.registerListener(new SnowTracker(addon));
addon.registerListener(new GreenhouseEvents(addon));
addon.registerListener(new GreenhouseGuard(addon));
addon.registerListener(new IslandChangeEvents(addon));
}

public GreenhouseMap getMap() {
Expand Down Expand Up @@ -245,4 +248,14 @@ public String toString() {
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);

}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -100,18 +101,34 @@ private boolean isOverlapping(Greenhouse greenhouse) {
*/
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
*/
public List<Greenhouse> getGreenhouses() {
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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/biomes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 085db69

Please sign in to comment.