From c1615c50efeef1f96e468cfe2e40a67cfb6c178d Mon Sep 17 00:00:00 2001 From: SuperCoder79 <25208576+SuperCoder7979@users.noreply.github.com> Date: Thu, 6 May 2021 06:49:47 -0400 Subject: [PATCH] Optimize performance and memory usage (#1850) --- .../world/biome/BiomeFeatureHelper.java | 5 ++ .../gen/feature/BOPConfiguredFeatures.java | 8 +-- .../gen/feature/CragSplatterFeature.java | 43 +++++++------- .../gen/feature/tree/BigTreeFeature.java | 14 ++--- .../common/world/layer/LargeIslandLayer.java | 58 ++++++++++--------- 5 files changed, 70 insertions(+), 58 deletions(-) diff --git a/src/main/java/biomesoplenty/common/world/biome/BiomeFeatureHelper.java b/src/main/java/biomesoplenty/common/world/biome/BiomeFeatureHelper.java index 83dde9b2d..34b42e6cd 100644 --- a/src/main/java/biomesoplenty/common/world/biome/BiomeFeatureHelper.java +++ b/src/main/java/biomesoplenty/common/world/biome/BiomeFeatureHelper.java @@ -28,6 +28,11 @@ public class BiomeFeatureHelper return (new BlockClusterFeatureConfig.Builder(new SimpleBlockStateProvider(state), new DoublePlantBlockPlacer())).tries(64).noProjection().build(); } + public static BlockClusterFeatureConfig createClusterConfigurationDoubleProjects(BlockState state) + { + return (new BlockClusterFeatureConfig.Builder(new SimpleBlockStateProvider(state), new DoublePlantBlockPlacer())).tries(64).build(); + } + public static BlockClusterFeatureConfig createClusterConfigurationDoubleWater(BlockState state) { return (new BlockClusterFeatureConfig.Builder(new SimpleBlockStateProvider(state), new DoublePlantBlockPlacer())).canReplace().tries(64).noProjection().build(); diff --git a/src/main/java/biomesoplenty/common/world/gen/feature/BOPConfiguredFeatures.java b/src/main/java/biomesoplenty/common/world/gen/feature/BOPConfiguredFeatures.java index d0d462f77..4252ea38b 100644 --- a/src/main/java/biomesoplenty/common/world/gen/feature/BOPConfiguredFeatures.java +++ b/src/main/java/biomesoplenty/common/world/gen/feature/BOPConfiguredFeatures.java @@ -170,9 +170,9 @@ public class BOPConfiguredFeatures public static final ConfiguredFeature CHERRY_BLOSSOM_GROVE_FLOWERS = register("cherry_blossom_grove_flowers", Feature.FLOWER.configured(BOPFeatures.CHERRY_BLOSSOM_GROVE_FLOWER_CONFIG).decorated(Features.Placements.ADD_32).decorated(Features.Placements.HEIGHTMAP_SQUARE.count(5))); public static final ConfiguredFeature CONIFEROUS_FOREST_FLOWERS = register("coniferous_forest_flowers", Feature.FLOWER.configured(BOPFeatures.CONIFEROUS_FOREST_FLOWER_CONFIG).decorated(Features.Placements.ADD_32).decorated(Features.Placements.HEIGHTMAP_SQUARE.count(4))); public static final ConfiguredFeature LUSH_DESERT_FLOWERS = register("lush_desert_flowers", Feature.FLOWER.configured((new BlockClusterFeatureConfig.Builder(new SimpleBlockStateProvider(BOPBlocks.wildflower.defaultBlockState()), SimpleBlockPlacer.INSTANCE)).tries(64).build()).decorated(Features.Placements.ADD_32).decorated(Features.Placements.HEIGHTMAP_SQUARE.count(6))); - public static final ConfiguredFeature FLOWER_MEADOW_FLOWERS = register("flower_meadow_flowers", Feature.FLOWER.configured(BOPFeatures.FLOWER_MEADOW_FLOWER_CONFIG).decorated(Features.Placements.HEIGHTMAP_DOUBLE_SQUARE).decorated(FeatureUtil.denseCount(150))); + public static final ConfiguredFeature FLOWER_MEADOW_FLOWERS = register("flower_meadow_flowers", Feature.FLOWER.configured(BOPFeatures.FLOWER_MEADOW_FLOWER_CONFIG).decorated(Features.Placements.HEIGHTMAP_SQUARE).count(4)); public static final ConfiguredFeature GROVE_FLOWERS = register("grove_flowers", Feature.FLOWER.configured(BOPFeatures.EXTENDED_FLOWER_CONFIG).decorated(Features.Placements.ADD_32).decorated(Features.Placements.HEIGHTMAP_SQUARE.count(6))); - public static final ConfiguredFeature LAVENDER_FLOWERS = register("lavender_flowers", Feature.FLOWER.configured((new BlockClusterFeatureConfig.Builder(new SimpleBlockStateProvider(BOPBlocks.lavender.defaultBlockState()), SimpleBlockPlacer.INSTANCE)).tries(64).build()).decorated(Features.Placements.HEIGHTMAP_DOUBLE_SQUARE).decorated(FeatureUtil.denseCount(500))); + public static final ConfiguredFeature LAVENDER_FLOWERS = register("lavender_flowers", Feature.FLOWER.configured((new BlockClusterFeatureConfig.Builder(new SimpleBlockStateProvider(BOPBlocks.lavender.defaultBlockState()), SimpleBlockPlacer.INSTANCE)).tries(64).build()).decorated(Features.Placements.HEIGHTMAP_SQUARE).count(15)); public static final ConfiguredFeature MEADOW_FLOWERS = register("meadow_flowers", Feature.FLOWER.configured(BOPFeatures.MEADOW_FLOWER_CONFIG).decorated(Features.Placements.ADD_32).decorated(Features.Placements.HEIGHTMAP_SQUARE.count(6))); public static final ConfiguredFeature MEADOW_FLOWERS_2 = register("meadow_flowers_2", Feature.FLOWER.configured(BOPFeatures.FLOWER_MEADOW_FLOWER_CONFIG).decorated(Features.Placements.ADD_32).decorated(Features.Placements.HEIGHTMAP_SQUARE.count(3))); public static final ConfiguredFeature MOOR_FLOWERS = register("moor_flowers", Feature.FLOWER.configured(BOPFeatures.MOOR_FLOWER_CONFIG).decorated(Features.Placements.ADD_32).decorated(Features.Placements.HEIGHTMAP_SQUARE.count(5))); @@ -182,7 +182,7 @@ public class BOPConfiguredFeatures public static final ConfiguredFeature OMINOUS_WOODS_FLOWERS = register("ominous_woods_flowers", Feature.FLOWER.configured((new BlockClusterFeatureConfig.Builder(new SimpleBlockStateProvider(Blocks.WITHER_ROSE.defaultBlockState()), SimpleBlockPlacer.INSTANCE)).tries(64).build()).decorated(Features.Placements.ADD_32).decorated(Features.Placements.HEIGHTMAP_SQUARE.count(3))); public static final ConfiguredFeature ORCHARD_FLOWERS = register("orchard_flowers", Feature.FLOWER.configured(BOPFeatures.EXTENDED_FLOWER_CONFIG).decorated(Features.Placements.ADD_32).decorated(Features.Placements.HEIGHTMAP_SQUARE.count(15))); public static final ConfiguredFeature ORIGIN_FLOWERS = register("origin_flowers", Feature.FLOWER.configured(BOPFeatures.ORIGIN_VALLEY_FLOWER_CONFIG).decorated(Features.Placements.ADD_32).decorated(Features.Placements.HEIGHTMAP_SQUARE.count(4))); - public static final ConfiguredFeature POPPY_FLOWERS = register("poppy_flowers", Feature.FLOWER.configured((new BlockClusterFeatureConfig.Builder(new SimpleBlockStateProvider(Blocks.POPPY.defaultBlockState()), SimpleBlockPlacer.INSTANCE)).tries(64).build()).decorated(Features.Placements.HEIGHTMAP_DOUBLE_SQUARE).decorated(FeatureUtil.denseCount(500))); + public static final ConfiguredFeature POPPY_FLOWERS = register("poppy_flowers", Feature.FLOWER.configured((new BlockClusterFeatureConfig.Builder(new SimpleBlockStateProvider(Blocks.POPPY.defaultBlockState()), SimpleBlockPlacer.INSTANCE)).tries(64).build()).decorated(Features.Placements.HEIGHTMAP_SQUARE).count(15)); public static final ConfiguredFeature RAINBOW_HILLS_FLOWERS = register("rainbow_hills_flowers", Feature.FLOWER.configured(BOPFeatures.RAINBOW_HILLS_FLOWER_CONFIG).decorated(Features.Placements.ADD_32).decorated(Features.Placements.HEIGHTMAP_SQUARE.count(25))); public static final ConfiguredFeature RAINFOREST_FLOWERS = register("rainforest_flowers", Feature.FLOWER.configured(BOPFeatures.RAINFOREST_FLOWER_CONFIG).decorated(Features.Placements.ADD_32).decorated(Features.Placements.HEIGHTMAP_SQUARE.count(5))); public static final ConfiguredFeature SHRUBLAND_FLOWERS = register("shrubland_flowers", Feature.FLOWER.configured((new BlockClusterFeatureConfig.Builder(new SimpleBlockStateProvider(Blocks.ALLIUM.defaultBlockState()), SimpleBlockPlacer.INSTANCE)).tries(64).build()).decorated(Features.Placements.ADD_32).decorated(Features.Placements.HEIGHTMAP_SQUARE.count(3))); @@ -255,7 +255,7 @@ public class BOPConfiguredFeatures public static final ConfiguredFeature GLOWSHROOM_UNCOMMON = register("glowshroom_uncommon", Feature.RANDOM_PATCH.configured(BiomeFeatureHelper.createClusterConfiguration(BOPBlocks.glowshroom.defaultBlockState())).decorated(Features.Placements.HEIGHTMAP_DOUBLE_SQUARE.count(7))); public static final ConfiguredFeature GLOWSHROOM_NORMAL = register("glowshroom_normal", Feature.RANDOM_PATCH.configured(BiomeFeatureHelper.createClusterConfiguration(BOPBlocks.glowshroom.defaultBlockState())).decorated(Features.Placements.HEIGHTMAP_DOUBLE_SQUARE.count(20))); public static final ConfiguredFeature GLOWSHROOM_EXTRA = register("glowshroom_extra", Feature.RANDOM_PATCH.configured(BiomeFeatureHelper.createClusterConfiguration(BOPBlocks.glowshroom.defaultBlockState())).decorated(Features.Placements.HEIGHTMAP_DOUBLE_SQUARE.count(50))); - public static final ConfiguredFeature BARLEY = register("barley", Feature.RANDOM_PATCH.configured(BiomeFeatureHelper.createClusterConfigurationDouble(BOPBlocks.barley.defaultBlockState())).decorated(Features.Placements.HEIGHTMAP_DOUBLE_SQUARE).decorated(FeatureUtil.denseCount(500))); + public static final ConfiguredFeature BARLEY = register("barley", Feature.RANDOM_PATCH.configured(BiomeFeatureHelper.createClusterConfigurationDoubleProjects(BOPBlocks.barley.defaultBlockState())).decorated(Features.Placements.HEIGHTMAP_SQUARE).count(15)); // Water Plants public static final ConfiguredFeature REED_5 = register("reed_5", Feature.RANDOM_PATCH.configured(BiomeFeatureHelper.createClusterConfigurationDoubleWater(BOPBlocks.reed.defaultBlockState())).decorated(Features.Placements.HEIGHTMAP_DOUBLE_SQUARE.count(5))); diff --git a/src/main/java/biomesoplenty/common/world/gen/feature/CragSplatterFeature.java b/src/main/java/biomesoplenty/common/world/gen/feature/CragSplatterFeature.java index c3f789f94..e5080fe6e 100644 --- a/src/main/java/biomesoplenty/common/world/gen/feature/CragSplatterFeature.java +++ b/src/main/java/biomesoplenty/common/world/gen/feature/CragSplatterFeature.java @@ -1,9 +1,9 @@ package biomesoplenty.common.world.gen.feature; -import biomesoplenty.api.block.BOPBlocks; import com.mojang.serialization.Codec; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; +import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; import net.minecraft.world.ISeedReader; import net.minecraft.world.gen.ChunkGenerator; @@ -22,22 +22,24 @@ public class CragSplatterFeature extends Feature @Override public boolean place(ISeedReader worldIn, ChunkGenerator chunkGenerator, Random rand, BlockPos pos, NoFeatureConfig config) { - int i = 0; - int j = rand.nextInt(8 - 2) + 2; + BlockPos.Mutable mutable = pos.mutable(); + int placed = 0; + int radius = rand.nextInt(8 - 2) + 2; - for (int k = pos.getX() - j; k <= pos.getX() + j; ++k) + for (int x = pos.getX() - radius; x <= pos.getX() + radius; ++x) { - for (int l = pos.getZ() - j; l <= pos.getZ() + j; ++l) + for (int z = pos.getZ() - radius; z <= pos.getZ() + radius; ++z) { - int i1 = k - pos.getX(); - int j1 = l - pos.getZ(); - if (i1 * i1 + j1 * j1 <= j * j) + int dx = x - pos.getX(); + int dz = z - pos.getZ(); + if (dx * dx + dz * dz <= radius * radius) { for (int k1 = pos.getY() - 6; k1 <= pos.getY() + 6; ++k1) { - BlockPos blockpos = new BlockPos(k, k1, l); - BlockState blockstate = worldIn.getBlockState(blockpos); - BlockState blockstate1 = worldIn.getBlockState(blockpos.above()); + mutable.set(x, k1, z); + BlockState blockstate = worldIn.getBlockState(mutable); + BlockState blockstate1 = worldIn.getBlockState(mutable.move(Direction.UP)); + mutable.move(Direction.DOWN); if ((blockstate.getBlock() == Blocks.STONE || blockstate.getBlock() == Blocks.GRAVEL || blockstate.getBlock() == Blocks.ANDESITE || blockstate.getBlock() == Blocks.DIORITE || blockstate.getBlock() == Blocks.GRANITE || blockstate.getBlock() == Blocks.DIRT)) { @@ -47,36 +49,35 @@ public class CragSplatterFeature extends Feature case 0: case 1: case 2: - worldIn.setBlock(blockpos, Blocks.COBBLESTONE.defaultBlockState(), 2); + worldIn.setBlock(mutable, Blocks.COBBLESTONE.defaultBlockState(), 2); break; case 3: case 4: case 5: - worldIn.setBlock(blockpos, Blocks.GRAVEL.defaultBlockState(), 2); + worldIn.setBlock(mutable, Blocks.GRAVEL.defaultBlockState(), 2); break; case 6: case 7: - worldIn.setBlock(blockpos, Blocks.MOSSY_COBBLESTONE.defaultBlockState(), 2); + worldIn.setBlock(mutable, Blocks.MOSSY_COBBLESTONE.defaultBlockState(), 2); break; case 8: case 9: - worldIn.setBlock(blockpos, Blocks.ANDESITE.defaultBlockState(), 2); + worldIn.setBlock(mutable, Blocks.ANDESITE.defaultBlockState(), 2); break; case 10: - if (blockstate1.isAir(worldIn, blockpos.above())) + if (blockstate1.isAir(worldIn, mutable.move(Direction.UP))) { - worldIn.setBlock(blockpos, Blocks.GRASS_BLOCK.defaultBlockState(), 2); - worldIn.setBlock(blockpos.above(), Blocks.GRASS.defaultBlockState(), 2); - + worldIn.setBlock(mutable, Blocks.GRASS.defaultBlockState(), 2); + worldIn.setBlock(mutable.move(Direction.DOWN), Blocks.GRASS_BLOCK.defaultBlockState(), 2); } break; } - ++i; + ++placed; break; } } @@ -84,6 +85,6 @@ public class CragSplatterFeature extends Feature } } - return i > 0; + return placed > 0; } } \ No newline at end of file diff --git a/src/main/java/biomesoplenty/common/world/gen/feature/tree/BigTreeFeature.java b/src/main/java/biomesoplenty/common/world/gen/feature/tree/BigTreeFeature.java index cee73ac4a..d8e187b8d 100644 --- a/src/main/java/biomesoplenty/common/world/gen/feature/tree/BigTreeFeature.java +++ b/src/main/java/biomesoplenty/common/world/gen/feature/tree/BigTreeFeature.java @@ -79,7 +79,7 @@ public class BigTreeFeature extends TreeFeatureBase // radius is the radius of the section from the center // direction is the direction the cross section is pointed, 0 for x, 1 // for y, 2 for z material is the index number for the material to use - private void crossSection(IWorld world, BlockPos pos, float radius, MutableBoundingBox boundingBox, Set changedBlocks) + private void crossSection(IWorld world, BlockPos pos, float radius, Random random, MutableBoundingBox boundingBox, Set changedBlocks) { final int r = (int)((double)radius + trunkHeightScale); @@ -95,7 +95,7 @@ public class BigTreeFeature extends TreeFeatureBase // Mojang sets leaves via the method used for logs. Probably intentional? if (this.altLeaves != Blocks.AIR.defaultBlockState()) { - int rand = new Random().nextInt(4); + int rand = random.nextInt(4); if (rand == 0) { @@ -182,11 +182,11 @@ public class BigTreeFeature extends TreeFeatureBase // Generate a cluster of foliage, with the base at blockPos // The shape of the cluster is derived from foliageShape // crossection is called to make each level. - private void foliageCluster(IWorld world, BlockPos pos, MutableBoundingBox boundingBox, Set changedBlocks) + private void foliageCluster(IWorld world, BlockPos pos, Random random, MutableBoundingBox boundingBox, Set changedBlocks) { for (int y = 0; y < foliageHeight; y++) { - this.crossSection(world, pos.above(y), this.foliageShape(y), boundingBox, changedBlocks); + this.crossSection(world, pos.above(y), this.foliageShape(y), random, boundingBox, changedBlocks); } } @@ -268,13 +268,13 @@ public class BigTreeFeature extends TreeFeatureBase return axis; } - private void makeFoliage(IWorld worldIn, int height, BlockPos pos, List coordinates, MutableBoundingBox boundingBox, Set changedBlocks) + private void makeFoliage(IWorld worldIn, int height, BlockPos pos, List coordinates, Random random, MutableBoundingBox boundingBox, Set changedBlocks) { for (FoliageCoordinates coordinate : coordinates) { if (this.trimBranches(height, coordinate.getBranchBase() - pos.getY())) { - this.foliageCluster(worldIn, coordinate, boundingBox, changedBlocks); + this.foliageCluster(worldIn, coordinate, random, boundingBox, changedBlocks); } } } @@ -394,7 +394,7 @@ public class BigTreeFeature extends TreeFeatureBase } } - this.makeFoliage(world, height, pos, foliageCoords, boundingBox, changedLeaves); + this.makeFoliage(world, height, pos, foliageCoords, random, boundingBox, changedLeaves); this.makeTrunk(changedLogs, world, pos, trunkHeight, boundingBox); this.makeBranches(changedLogs, world, height, pos, foliageCoords, boundingBox); return true; diff --git a/src/main/java/biomesoplenty/common/world/layer/LargeIslandLayer.java b/src/main/java/biomesoplenty/common/world/layer/LargeIslandLayer.java index c81553ea4..a5504c69c 100644 --- a/src/main/java/biomesoplenty/common/world/layer/LargeIslandLayer.java +++ b/src/main/java/biomesoplenty/common/world/layer/LargeIslandLayer.java @@ -24,40 +24,46 @@ public enum LargeIslandLayer implements IAreaTransformer2, IDimOffset1Transforme @Override public int applyPixel(INoiseRandom context, IArea landSeaArea, IArea climateArea, int x, int z) { - int northVal = landSeaArea.get(x + 1, z + 0); - int eastVal = landSeaArea.get(x + 2, z + 1); - int southVal = landSeaArea.get(x + 1, z + 2); - int westVal = landSeaArea.get(x + 0, z + 1); int centerVal = landSeaArea.get(x + 1, z + 1); - int climateVal = climateArea.get(x, z); - BOPClimates climate; - try + if (context.nextRandom(50) == 0) { - climate = BOPClimates.lookup(climateVal); - } - catch (ArrayIndexOutOfBoundsException e) - { - // This shouldn't happen - but apparently it (rarely) does (https://github.com/Glitchfiend/BiomesOPlenty/issues/983) - // If it does it means that something weird happened with the climate layer / lookup - // Rethrow with hopefully a more useful message - String msg = "Climate lookup failed climateOrdinal: " + climateVal; - throw new RuntimeException(msg,e); - } + int northVal = landSeaArea.get(x + 1, z + 0); + int eastVal = landSeaArea.get(x + 2, z + 1); + int southVal = landSeaArea.get(x + 1, z + 2); + int westVal = landSeaArea.get(x + 0, z + 1); + int climateVal = climateArea.get(x, z); - if (centerVal == 0 && northVal == 0 && eastVal == 0 && southVal == 0 && westVal == 0 && context.nextRandom(50) == 0) - { - RegistryKey islandBiome = climate.getRandomIslandBiome(context, null); - - if (islandBiome == null) + BOPClimates climate; + try { - return centerVal; + climate = BOPClimates.lookup(climateVal); } - else + catch (ArrayIndexOutOfBoundsException e) { - return BiomeUtil.getBiomeId(islandBiome); + // This shouldn't happen - but apparently it (rarely) does (https://github.com/Glitchfiend/BiomesOPlenty/issues/983) + // If it does it means that something weird happened with the climate layer / lookup + // Rethrow with hopefully a more useful message + String msg = "Climate lookup failed climateOrdinal: " + climateVal; + throw new RuntimeException(msg,e); } + + if (centerVal == 0 && northVal == 0 && eastVal == 0 && southVal == 0 && westVal == 0) + { + RegistryKey islandBiome = climate.getRandomIslandBiome(context, null); + + if (islandBiome == null) + { + return centerVal; + } + else + { + return BiomeUtil.getBiomeId(islandBiome); + } + } + else return centerVal; + } else { + return centerVal; } - else return centerVal; } }