mirror of
https://github.com/BentoBoxWorld/Greenhouses.git
synced 2024-11-28 21:56:17 +01:00
Optimize convertBlocks and convertBlock methods by reducing the amoun… (#78)
This commit is contained in:
parent
7154877f14
commit
db3054ab0c
@ -5,12 +5,12 @@ import java.util.EnumMap;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
@ -107,7 +107,7 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mobType - entity type
|
* @param mobType - entity type
|
||||||
* @param mobProbability - reltive probability
|
* @param mobProbability - relative probability
|
||||||
* @param mobSpawnOn - material to spawn on
|
* @param mobSpawnOn - material to spawn on
|
||||||
* @return true if add is successful
|
* @return true if add is successful
|
||||||
*/
|
*/
|
||||||
@ -236,22 +236,30 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if block should be converted
|
* Check if block should be converted
|
||||||
* @param gh - greenhouse
|
|
||||||
* @param b - block to check
|
* @param b - block to check
|
||||||
*/
|
*/
|
||||||
public void convertBlock(Greenhouse gh, Block b) {
|
public void convertBlock(Block b) {
|
||||||
conversionBlocks.get(b.getType()).stream().filter(Objects::nonNull)
|
Material bType = b.getType();
|
||||||
.filter(bc -> random.nextDouble() < bc.getProbability())
|
// Check if there is a block conversion for this block, as while the rest of the method wont do anything if .get() returns nothing anyway it still seems to be quite expensive
|
||||||
.forEach(bc -> {
|
if(conversionBlocks.keySet().contains(bType)) {
|
||||||
// Check if the block is in the right area, up, down, n,s,e,w
|
for(GreenhouseBlockConversions conversion_option : conversionBlocks.get(bType)) {
|
||||||
if (ADJ_BLOCKS.stream().map(b::getRelative)
|
|
||||||
.filter(r -> gh.contains(r.getLocation()))
|
// Roll the dice before bothering with checking the surrounding block as I think it's more common for greenhouses to be filled with convertable blocks and thus this dice roll wont be "wasted"
|
||||||
.map(Block::getType)
|
if(ThreadLocalRandom.current().nextDouble() < conversion_option.getProbability()) {
|
||||||
.anyMatch(m -> bc.getLocalMaterial() == null || m == bc.getLocalMaterial())) {
|
// Check if any of the adjacent blocks matches the required LocalMaterial, if there are any required LocalMaterials
|
||||||
// Convert!
|
if(conversion_option.getLocalMaterial() != null) {
|
||||||
b.setType(bc.getNewMaterial());
|
for(BlockFace adjacent_block : ADJ_BLOCKS) {
|
||||||
|
if(b.getRelative(adjacent_block).getType() == conversion_option.getLocalMaterial()) {
|
||||||
|
b.setType(conversion_option.getNewMaterial());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
b.setType(conversion_option.getNewMaterial());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,14 +1,7 @@
|
|||||||
package world.bentobox.greenhouses.managers;
|
package world.bentobox.greenhouses.managers;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.World;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.BlockFace;
|
import org.bukkit.block.BlockFace;
|
||||||
import org.bukkit.block.Hopper;
|
import org.bukkit.block.Hopper;
|
||||||
@ -18,6 +11,15 @@ import org.bukkit.util.NumberConversions;
|
|||||||
|
|
||||||
import world.bentobox.greenhouses.Greenhouses;
|
import world.bentobox.greenhouses.Greenhouses;
|
||||||
import world.bentobox.greenhouses.data.Greenhouse;
|
import world.bentobox.greenhouses.data.Greenhouse;
|
||||||
|
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs the ecosystem for a greenhouse
|
* Runs the ecosystem for a greenhouse
|
||||||
@ -54,7 +56,7 @@ public class EcoSystemManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Kick block conversion growing
|
// Kick block conversion growing
|
||||||
long blockTick = addon.getSettings().getBlockTick() * 60 * 20l; // In minutes
|
long blockTick = addon.getSettings().getBlockTick() * 60 * 20L; // In minutes
|
||||||
|
|
||||||
if (blockTick > 0) {
|
if (blockTick > 0) {
|
||||||
addon.log("Kicking off block conversion scheduler every " + addon.getSettings().getBlockTick() + MINUTES);
|
addon.log("Kicking off block conversion scheduler every " + addon.getSettings().getBlockTick() + MINUTES);
|
||||||
@ -86,11 +88,24 @@ public class EcoSystemManager {
|
|||||||
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)){
|
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)){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (double x = gh.getInternalBoundingBox().getMinX(); x < gh.getInternalBoundingBox().getMaxX(); x++) {
|
|
||||||
for (double z = gh.getInternalBoundingBox().getMinZ(); z < gh.getInternalBoundingBox().getMaxZ(); z++) {
|
int gh_min_x = NumberConversions.floor(gh.getInternalBoundingBox().getMinX());
|
||||||
for (double y = gh.getInternalBoundingBox().getMaxY() - 1; y >= gh.getBoundingBox().getMinY() && y > 0; y--) {
|
int gh_max_x = NumberConversions.floor(gh.getInternalBoundingBox().getMaxX());
|
||||||
Block b = gh.getWorld().getBlockAt(NumberConversions.floor(x), NumberConversions.floor(y), NumberConversions.floor(z)).getRelative(BlockFace.DOWN);
|
int gh_min_y = NumberConversions.floor(gh.getInternalBoundingBox().getMinY());
|
||||||
if (!b.isEmpty()) gh.getBiomeRecipe().convertBlock(gh, b);
|
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++) {
|
||||||
|
for (int z = gh_min_z; z < gh_max_z; z++) {
|
||||||
|
for (int y = gh_min_y; y < gh_max_y; y++) {
|
||||||
|
Block b = world.getBlockAt(x, y, z);
|
||||||
|
|
||||||
|
if(!b.isEmpty()) {
|
||||||
|
biomeRecipe.convertBlock(b);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -187,7 +202,7 @@ public class EcoSystemManager {
|
|||||||
* Get a list of the lowest level blocks inside the greenhouse. May be air, liquid or plants.
|
* Get a list of the lowest level blocks inside the greenhouse. May be air, liquid or plants.
|
||||||
* These blocks sit just above solid blocks
|
* These blocks sit just above solid blocks
|
||||||
* @param gh - greenhouse
|
* @param gh - greenhouse
|
||||||
* @param ignoreliquid - true if liquid blocks should be treated like air blocks
|
* @param ignoreLiquid - true if liquid blocks should be treated like air blocks
|
||||||
* @return List of blocks
|
* @return List of blocks
|
||||||
*/
|
*/
|
||||||
public List<Block> getAvailableBlocks(Greenhouse gh, boolean ignoreLiquid) {
|
public List<Block> getAvailableBlocks(Greenhouse gh, boolean ignoreLiquid) {
|
||||||
|
@ -263,7 +263,7 @@ public class BiomeRecipeTest {
|
|||||||
Block ab = mock(Block.class);
|
Block ab = mock(Block.class);
|
||||||
when(ab.getType()).thenReturn(Material.WATER);
|
when(ab.getType()).thenReturn(Material.WATER);
|
||||||
when(b.getRelative(any())).thenReturn(ab);
|
when(b.getRelative(any())).thenReturn(ab);
|
||||||
br.convertBlock(gh, b);
|
br.convertBlock(b);
|
||||||
verify(b).setType(Material.CLAY);
|
verify(b).setType(Material.CLAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -282,7 +282,7 @@ public class BiomeRecipeTest {
|
|||||||
when(b.getRelative(any())).thenReturn(ab);
|
when(b.getRelative(any())).thenReturn(ab);
|
||||||
when(ab.getLocation()).thenReturn(location);
|
when(ab.getLocation()).thenReturn(location);
|
||||||
when(gh.contains(any())).thenReturn(false);
|
when(gh.contains(any())).thenReturn(false);
|
||||||
br.convertBlock(gh, b);
|
br.convertBlock(b);
|
||||||
verify(b, never()).setType(any());
|
verify(b, never()).setType(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -299,7 +299,7 @@ public class BiomeRecipeTest {
|
|||||||
Block ab = mock(Block.class);
|
Block ab = mock(Block.class);
|
||||||
when(ab.getType()).thenReturn(Material.SAND);
|
when(ab.getType()).thenReturn(Material.SAND);
|
||||||
when(b.getRelative(any())).thenReturn(ab);
|
when(b.getRelative(any())).thenReturn(ab);
|
||||||
br.convertBlock(gh, b);
|
br.convertBlock(b);
|
||||||
verify(b, never()).setType(Material.CLAY);
|
verify(b, never()).setType(Material.CLAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,7 +311,7 @@ public class BiomeRecipeTest {
|
|||||||
// Mock
|
// Mock
|
||||||
Block b = mock(Block.class);
|
Block b = mock(Block.class);
|
||||||
when(b.getType()).thenReturn(Material.SAND);
|
when(b.getType()).thenReturn(Material.SAND);
|
||||||
br.convertBlock(gh, b);
|
br.convertBlock(b);
|
||||||
verify(b, never()).setType(Material.CLAY);
|
verify(b, never()).setType(Material.CLAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -333,7 +333,7 @@ public class BiomeRecipeTest {
|
|||||||
Block ab = mock(Block.class);
|
Block ab = mock(Block.class);
|
||||||
when(ab.getType()).thenReturn(Material.WATER);
|
when(ab.getType()).thenReturn(Material.WATER);
|
||||||
when(b.getRelative(any())).thenReturn(ab);
|
when(b.getRelative(any())).thenReturn(ab);
|
||||||
br.convertBlock(gh, b);
|
br.convertBlock(b);
|
||||||
verify(b, never()).setType(Material.CLAY);
|
verify(b, never()).setType(Material.CLAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user