From dea1fb144aec488855e56dc29cd9a11b0853ad4f Mon Sep 17 00:00:00 2001 From: mezz Date: Thu, 7 Sep 2017 23:41:59 -0700 Subject: [PATCH] Fix #1434 Fix #1551 Make hives spawn existing flowers only, up to 10 --- gradle.properties | 2 +- lang | 2 +- .../java/forestry/api/apiculture/IBee.java | 20 +++++++ .../api/genetics/IFlowerGrowthHelper.java | 16 +++++ .../api/genetics/IFlowerGrowthRule.java | 17 ++++++ .../api/genetics/IFlowerRegistry.java | 11 +++- .../forestry/apiculture/BeekeepingLogic.java | 20 +++++-- .../forestry/apiculture/HasFlowersCache.java | 59 +++++++++++++++++-- .../forestry/apiculture/PluginApiculture.java | 3 + .../apiculture/WorldgenBeekeepingLogic.java | 5 +- .../apiculture/flowers/FlowerRegistry.java | 58 ++++++++++++++---- .../flowers/GrowthRuleDirtGrass.java | 19 +++++- .../flowers/GrowthRuleFertilize.java | 6 ++ .../flowers/GrowthRuleMycelium.java | 18 +++++- .../apiculture/flowers/GrowthRuleNone.java | 7 +++ .../apiculture/flowers/GrowthRuleSnow.java | 19 +++++- .../forestry/apiculture/genetics/Bee.java | 20 +++++-- .../worldgen/VillageApiaristHouse.java | 4 +- .../core/utils/WeightedCollection.java | 36 +++++++++++ 19 files changed, 302 insertions(+), 40 deletions(-) create mode 100644 src/main/java/forestry/core/utils/WeightedCollection.java diff --git a/gradle.properties b/gradle.properties index 73787c2d9..c7421dc21 100755 --- a/gradle.properties +++ b/gradle.properties @@ -11,4 +11,4 @@ ic2_version=2.8.24-ex112 version_major=5 version_minor=5 -version_patch=3 +version_patch=4 diff --git a/lang b/lang index 192d6f858..774ccbb68 160000 --- a/lang +++ b/lang @@ -1 +1 @@ -Subproject commit 192d6f85845f6702e93fb234b56b72d945f5e2ab +Subproject commit 774ccbb6809e61954b6137f29d4cd79d908fab47 diff --git a/src/main/java/forestry/api/apiculture/IBee.java b/src/main/java/forestry/api/apiculture/IBee.java index d2218493f..ba70aa041 100644 --- a/src/main/java/forestry/api/apiculture/IBee.java +++ b/src/main/java/forestry/api/apiculture/IBee.java @@ -13,8 +13,10 @@ import forestry.api.core.IErrorState; import forestry.api.genetics.IEffectData; import forestry.api.genetics.IIndividual; import forestry.api.genetics.IIndividualLiving; +import net.minecraft.block.state.IBlockState; import net.minecraft.item.ItemStack; import net.minecraft.util.NonNullList; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.biome.Biome; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -85,8 +87,26 @@ public interface IBee extends IIndividualLiving { List spawnDrones(IBeeHousing housing); + /** + * @deprecated since Forestry 5.5.4. Use {@link #plantFlowerRandom(IBeeHousing, List)} + */ + @Deprecated void plantFlowerRandom(IBeeHousing housing); + /** + * Plants a random flower from the given list near the housing + * @param housing the location the bee is living + * @param potentialFlowers the flowers to choose from + * @return the position of the planted flower, or null of none were planted + * @since Forestry 5.5.4 + */ + @Nullable + default BlockPos plantFlowerRandom(IBeeHousing housing, List potentialFlowers) { + //noinspection deprecation + plantFlowerRandom(housing); + return null; + } + @Nullable IIndividual retrievePollen(IBeeHousing housing); diff --git a/src/main/java/forestry/api/genetics/IFlowerGrowthHelper.java b/src/main/java/forestry/api/genetics/IFlowerGrowthHelper.java index b3ca570cd..4ce74b564 100644 --- a/src/main/java/forestry/api/genetics/IFlowerGrowthHelper.java +++ b/src/main/java/forestry/api/genetics/IFlowerGrowthHelper.java @@ -5,6 +5,9 @@ ******************************************************************************/ package forestry.api.genetics; +import java.util.Collection; + +import net.minecraft.block.state.IBlockState; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -13,6 +16,19 @@ public interface IFlowerGrowthHelper { * Plants a random flower from flowerType at the specified position. * * @return true if successful, false if it could not be planted. + * @deprecated since Forestry 5.5.4. Use {@link #plantRandomFlower(String, World, BlockPos, Collection)} */ + @Deprecated boolean plantRandomFlower(String flowerType, World world, BlockPos pos); + + /** + * Plants a random flower from flowerType at the specified position. + * @param flowerType the flower type that can be planted + * @param world the world to plant flowers in + * @param pos the position to plant the flowers + * @param potentialFlowers the potential flowers that can be planted + * @return true if a flower was planted, false otherwise + * @since Forestry 5.5.4 + */ + boolean plantRandomFlower(String flowerType, World world, BlockPos pos, Collection potentialFlowers); } diff --git a/src/main/java/forestry/api/genetics/IFlowerGrowthRule.java b/src/main/java/forestry/api/genetics/IFlowerGrowthRule.java index 812fbb332..9dfefc65a 100644 --- a/src/main/java/forestry/api/genetics/IFlowerGrowthRule.java +++ b/src/main/java/forestry/api/genetics/IFlowerGrowthRule.java @@ -5,6 +5,9 @@ ******************************************************************************/ package forestry.api.genetics; +import java.util.Collection; + +import net.minecraft.block.state.IBlockState; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -18,6 +21,20 @@ public interface IFlowerGrowthRule { * For implementers, you can plant a random flower using IFlowerGrowthHelper.plantRandomFlower * * @since Forestry 4.0.8 + * @deprecated since Forestry 5.5.4. Use {@link #growFlower(IFlowerGrowthHelper, String, World, BlockPos, Collection)} */ + @Deprecated boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, BlockPos pos); + + /** + * Checks a position for suitability, and then plants a flower there. + * Returns true on success. + * For implementers, you can plant a random flower using IFlowerGrowthHelper.plantRandomFlower + * + * @since Forestry 5.5.4 + */ + default boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, BlockPos pos, Collection potentialFlowers) { + //noinspection deprecation + return growFlower(helper, flowerType, world, pos); + } } diff --git a/src/main/java/forestry/api/genetics/IFlowerRegistry.java b/src/main/java/forestry/api/genetics/IFlowerRegistry.java index ab574fa02..5fd133285 100644 --- a/src/main/java/forestry/api/genetics/IFlowerRegistry.java +++ b/src/main/java/forestry/api/genetics/IFlowerRegistry.java @@ -5,6 +5,7 @@ ******************************************************************************/ package forestry.api.genetics; +import java.util.Collection; import java.util.Iterator; import java.util.List; @@ -59,8 +60,9 @@ public interface IFlowerRegistry { /** * Calls the appropriate IFlowerGrowthRule to grow a flower at a specified position. + * @since Forestry 5.5.4 */ - boolean growFlower(String flowerType, World world, IIndividual individual, BlockPos pos); + boolean growFlower(String flowerType, World world, IIndividual individual, BlockPos pos, Collection potentialFlowers); /** * Gets an iterator over the area a bee can travel from its beeHousing. @@ -96,4 +98,11 @@ public interface IFlowerRegistry { */ @Deprecated boolean isAcceptedFlower(String flowerType, World world, BlockPos pos); + + /** + * Calls the appropriate IFlowerGrowthRule to grow a flower at a specified position. + * @deprecated since Forestry 5.5.4. Use {@link #growFlower(String, World, IIndividual, BlockPos, Collection)} + */ + @Deprecated + boolean growFlower(String flowerType, World world, IIndividual individual, BlockPos pos); } diff --git a/src/main/java/forestry/apiculture/BeekeepingLogic.java b/src/main/java/forestry/apiculture/BeekeepingLogic.java index fbbc42004..a679e05da 100644 --- a/src/main/java/forestry/apiculture/BeekeepingLogic.java +++ b/src/main/java/forestry/apiculture/BeekeepingLogic.java @@ -40,6 +40,7 @@ import forestry.core.config.Constants; import forestry.core.errors.EnumErrorCode; import forestry.core.utils.Log; import forestry.core.utils.NetworkUtil; +import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayerMP; @@ -202,11 +203,13 @@ public class BeekeepingLogic implements IBeekeepingLogic { if (this.queenStack != queenStack) { if (!queenStack.isEmpty()) { this.queen = BeeManager.beeRoot.getMember(queenStack); + if (this.queen != null) { + hasFlowersCache.onNewQueen(queen, housing); + } } else { this.queen = null; } this.queenStack = queenStack; - hasFlowersCache.clear(); queenCanWorkCache.clear(); } @@ -250,9 +253,11 @@ public class BeekeepingLogic implements IBeekeepingLogic { @Override public void clearCachedValues() { if (!housing.getWorldObj().isRemote) { - hasFlowersCache.clear(); queenCanWorkCache.clear(); canWork(); + if (queen != null) { + hasFlowersCache.forceLookForFlowers(queen, housing); + } } } @@ -272,14 +277,21 @@ public class BeekeepingLogic implements IBeekeepingLogic { queenWorkCycleThrottle = 0; doProduction(queen, housing, beeListener); - queen.plantFlowerRandom(housing); + World world = housing.getWorldObj(); + List flowers = hasFlowersCache.getFlowers(world); + if (flowers.size() < PluginApiculture.maxFlowersSpawnedPerHive) { + BlockPos blockPos = queen.plantFlowerRandom(housing, flowers); + if (blockPos != null) { + hasFlowersCache.addFlowerPos(blockPos); + } + } pollenHandler.doPollination(queen, housing, beeListener); // Age the queen IBeeGenome mate = queen.getMate(); Preconditions.checkState(mate != null); float lifespanModifier = beeModifier.getLifespanModifier(queen.getGenome(), mate, 1.0f); - queen.age(housing.getWorldObj(), lifespanModifier); + queen.age(world, lifespanModifier); // Write the changed queen back into the item stack. NBTTagCompound nbttagcompound = new NBTTagCompound(); diff --git a/src/main/java/forestry/apiculture/HasFlowersCache.java b/src/main/java/forestry/apiculture/HasFlowersCache.java index 79cf12076..c46c45db2 100644 --- a/src/main/java/forestry/apiculture/HasFlowersCache.java +++ b/src/main/java/forestry/apiculture/HasFlowersCache.java @@ -19,15 +19,18 @@ import java.util.List; import forestry.api.apiculture.FlowerManager; import forestry.api.apiculture.IBee; +import forestry.api.apiculture.IBeeGenome; import forestry.api.apiculture.IBeeHousing; import forestry.api.core.INbtReadable; import forestry.api.core.INbtWritable; import forestry.api.core.IBlockPosPredicate; import forestry.api.genetics.IFlowerProvider; import forestry.core.utils.TickHelper; +import net.minecraft.block.state.IBlockState; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.PacketBuffer; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3i; import net.minecraft.world.World; public class HasFlowersCache implements INbtWritable, INbtReadable { @@ -40,17 +43,20 @@ public class HasFlowersCache implements INbtWritable, INbtReadable { @Nullable private FlowerData flowerData; private final List flowerCoords = new ArrayList<>(); + private final List flowers = new ArrayList<>(); private boolean needsSync = false; private static class FlowerData { public final String flowerType; + public final Vec3i territory; public final IBlockPosPredicate flowerPredicate; public Iterator areaIterator; public FlowerData(IBee queen, IBeeHousing beeHousing) { IFlowerProvider flowerProvider = queen.getGenome().getFlowerProvider(); this.flowerType = flowerProvider.getFlowerType(); + this.territory = queen.getGenome().getTerritory(); this.flowerPredicate = FlowerManager.flowerRegistry.createAcceptedFlowerPredicate(flowerType); this.areaIterator = FlowerManager.flowerRegistry.getAreaIterator(beeHousing, queen); } @@ -64,6 +70,7 @@ public class HasFlowersCache implements INbtWritable, INbtReadable { if (flowerData == null) { this.flowerData = new FlowerData(queen, beeHousing); this.flowerCoords.clear(); + this.flowers.clear(); } World world = beeHousing.getWorldObj(); tickHelper.onTick(); @@ -74,6 +81,7 @@ public class HasFlowersCache implements INbtWritable, INbtReadable { BlockPos flowerPos = iterator.next(); if (!flowerData.flowerPredicate.test(world, flowerPos) && world.isBlockLoaded(flowerPos)) { iterator.remove(); + flowers.clear(); needsSync = true; } } @@ -86,8 +94,7 @@ public class HasFlowersCache implements INbtWritable, INbtReadable { if (flowerData.areaIterator.hasNext()) { BlockPos.MutableBlockPos blockPos = flowerData.areaIterator.next(); if (flowerData.flowerPredicate.test(world, blockPos)) { - flowerCoords.add(blockPos.toImmutable()); - needsSync = true; + addFlowerPos(blockPos.toImmutable()); } } else { flowerData.resetIterator(queen, beeHousing); @@ -105,15 +112,55 @@ public class HasFlowersCache implements INbtWritable, INbtReadable { return returnVal; } - public void clear() { - flowerCoords.clear(); - flowerData = null; + public void onNewQueen(IBee queen, IBeeHousing housing) { + if (this.flowerData != null) { + IBeeGenome genome = queen.getGenome(); + String flowerType = genome.getFlowerProvider().getFlowerType(); + if (!this.flowerData.flowerType.equals(flowerType) + || !this.flowerData.territory.equals(genome.getTerritory())) { + flowerData = new FlowerData(queen, housing); + flowerCoords.clear(); + flowers.clear(); + } + } } public List getFlowerCoords() { return Collections.unmodifiableList(flowerCoords); } + public List getFlowers(World world) { + if (flowers.isEmpty() && !flowerCoords.isEmpty()) { + flowers.clear(); + for (BlockPos flowerCoord : flowerCoords) { + IBlockState blockState = world.getBlockState(flowerCoord); + flowers.add(blockState); + } + } + return Collections.unmodifiableList(flowers); + } + + public void addFlowerPos(BlockPos blockPos) { + flowerCoords.add(blockPos); + flowers.clear(); + needsSync = true; + } + + public void forceLookForFlowers(IBee queen, IBeeHousing housing) { + if (flowerData != null) { + flowerCoords.clear(); + flowers.clear(); + flowerData.resetIterator(queen, housing); + World world = housing.getWorldObj(); + while (flowerData.areaIterator.hasNext()) { + BlockPos.MutableBlockPos blockPos = flowerData.areaIterator.next(); + if (flowerData.flowerPredicate.test(world, blockPos)) { + addFlowerPos(blockPos.toImmutable()); + } + } + } + } + @Override public void readFromNBT(NBTTagCompound nbttagcompound) { if (!nbttagcompound.hasKey(NBT_KEY)) { @@ -133,6 +180,7 @@ public class HasFlowersCache implements INbtWritable, INbtReadable { needsSync = true; } } + flowers.clear(); } @Override @@ -170,6 +218,7 @@ public class HasFlowersCache implements INbtWritable, INbtReadable { public void readData(PacketBuffer data) throws IOException { flowerCoords.clear(); + flowers.clear(); int size = data.readVarInt(); while (size > 0) { diff --git a/src/main/java/forestry/apiculture/PluginApiculture.java b/src/main/java/forestry/apiculture/PluginApiculture.java index 5a8c75881..1b634690a 100755 --- a/src/main/java/forestry/apiculture/PluginApiculture.java +++ b/src/main/java/forestry/apiculture/PluginApiculture.java @@ -151,6 +151,7 @@ public class PluginApiculture extends BlankForestryPlugin { public static String beekeepingMode = "NORMAL"; public static final int ticksPerBeeWorkCycle = 550; + public static int maxFlowersSpawnedPerHive = 20; @Nullable public static VillagerRegistry.VillagerProfession villagerApiarist; @@ -261,6 +262,8 @@ public class PluginApiculture extends BlankForestryPlugin { secondPrincessChance = config.getFloatLocalized("beekeeping", "second.princess", secondPrincessChance, 0.0f, 100.0f); + maxFlowersSpawnedPerHive = config.getIntLocalized("beekeeping", "flowers.spawn", 20, 0, 1000); + String[] blacklist = config.getStringListLocalized("species", "blacklist", Constants.EMPTY_STRINGS); parseBeeBlacklist(blacklist); diff --git a/src/main/java/forestry/apiculture/WorldgenBeekeepingLogic.java b/src/main/java/forestry/apiculture/WorldgenBeekeepingLogic.java index d1cd74d65..f83a5fde8 100644 --- a/src/main/java/forestry/apiculture/WorldgenBeekeepingLogic.java +++ b/src/main/java/forestry/apiculture/WorldgenBeekeepingLogic.java @@ -116,10 +116,7 @@ public class WorldgenBeekeepingLogic implements IBeekeepingLogic { @Override public void clearCachedValues() { - if (!housing.getWorldObj().isRemote) { - hasFlowersCache.clear(); - canWork(); - } + } /* CLIENT */ diff --git a/src/main/java/forestry/apiculture/flowers/FlowerRegistry.java b/src/main/java/forestry/apiculture/flowers/FlowerRegistry.java index 58e3c61ce..22dfcbe1d 100644 --- a/src/main/java/forestry/apiculture/flowers/FlowerRegistry.java +++ b/src/main/java/forestry/apiculture/flowers/FlowerRegistry.java @@ -10,14 +10,16 @@ ******************************************************************************/ package forestry.apiculture.flowers; +import javax.annotation.Nullable; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Set; -import java.util.TreeMap; import com.google.common.base.Preconditions; import com.google.common.collect.ArrayListMultimap; @@ -36,6 +38,7 @@ import forestry.api.genetics.IFlowerRegistry; import forestry.api.genetics.IIndividual; import forestry.core.utils.BlockStateSet; import forestry.core.utils.VectUtil; +import forestry.core.utils.WeightedCollection; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; @@ -50,7 +53,7 @@ public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelpe private final HashMultimap plantableFlowers; private final ArrayListMultimap growthRules; - private final Map> chances; + private final Map> chances; public FlowerRegistry() { this.registeredRules = HashMultimap.create(); @@ -197,6 +200,7 @@ public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelpe } @Override + @Deprecated public boolean growFlower(String flowerType, World world, IIndividual individual, BlockPos pos) { if (!this.growthRules.containsKey(flowerType)) { return false; @@ -211,6 +215,23 @@ public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelpe return false; } + @Override + public boolean growFlower(String flowerType, World world, IIndividual individual, BlockPos pos, Collection potentialFlowers) { + if (!this.growthRules.containsKey(flowerType)) { + return false; + } + + List growthRules = this.growthRules.get(flowerType); + Collections.shuffle(growthRules); + for (IFlowerGrowthRule rule : growthRules) { + if (rule.growFlower(this, flowerType, world, pos, potentialFlowers)) { + return true; + } + } + + return false; + } + @Override public void registerGrowthRule(IFlowerGrowthRule rule, String... flowerTypes) { Preconditions.checkNotNull(rule); @@ -220,20 +241,18 @@ public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelpe } } + @Nullable public Flower getRandomPlantableFlower(String flowerType, Random rand) { - TreeMap chancesMap = getChancesMap(flowerType); - double maxKey = chancesMap.lastKey() + 1.0; - return chancesMap.get(chancesMap.lowerKey(rand.nextDouble() * maxKey)); + WeightedCollection chancesMap = getFlowerChances(flowerType); + return chancesMap.getRandom(rand); } - private TreeMap getChancesMap(String flowerType) { + private WeightedCollection getFlowerChances(String flowerType) { if (!this.chances.containsKey(flowerType)) { - TreeMap flowerChances = new TreeMap<>(); - double count = 0.0; + WeightedCollection flowerChances = new WeightedCollection<>(); for (Flower flower : this.plantableFlowers.get(flowerType)) { if (flower.isPlantable()) { - flowerChances.put(count, flower); - count += flower.getWeight(); + flowerChances.put(flower.getWeight(), flower); } } this.chances.put(flowerType, flowerChances); @@ -253,7 +272,24 @@ public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelpe @Override public boolean plantRandomFlower(String flowerType, World world, BlockPos pos) { Flower flower = getRandomPlantableFlower(flowerType, world.rand); - return world.setBlockState(pos, flower.getBlockState()); + return flower != null && world.setBlockState(pos, flower.getBlockState()); + } + + @Override + public boolean plantRandomFlower(String flowerType, World world, BlockPos pos, Collection potentialFlowers) { + WeightedCollection chances = getFlowerChances(flowerType); + WeightedCollection potentialChances = new WeightedCollection<>(); + + for (Map.Entry entry : chances.entrySet()) { + Flower flower = entry.getValue(); + IBlockState blockState = flower.getBlockState(); + if (potentialFlowers.contains(blockState)) { + potentialChances.put(entry.getKey(), blockState); + } + } + + final IBlockState blockState = potentialChances.getRandom(world.rand); + return blockState != null && world.setBlockState(pos, blockState); } private static class AcceptedFlowerPredicate implements IBlockPosPredicate { diff --git a/src/main/java/forestry/apiculture/flowers/GrowthRuleDirtGrass.java b/src/main/java/forestry/apiculture/flowers/GrowthRuleDirtGrass.java index 61c384e62..2017506a7 100644 --- a/src/main/java/forestry/apiculture/flowers/GrowthRuleDirtGrass.java +++ b/src/main/java/forestry/apiculture/flowers/GrowthRuleDirtGrass.java @@ -10,9 +10,12 @@ ******************************************************************************/ package forestry.apiculture.flowers; +import java.util.Collection; + import forestry.api.genetics.IFlowerGrowthHelper; import forestry.api.genetics.IFlowerGrowthRule; import net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -20,14 +23,24 @@ import net.minecraft.world.World; public class GrowthRuleDirtGrass implements IFlowerGrowthRule { @Override + @Deprecated public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, BlockPos pos) { + return isValidSpot(world, pos) && + helper.plantRandomFlower(flowerType, world, pos); + } + + @Override + public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, BlockPos pos, Collection potentialFlowers) { + return isValidSpot(world, pos) && + helper.plantRandomFlower(flowerType, world, pos, potentialFlowers); + } + + private boolean isValidSpot(World world, BlockPos pos) { if (!world.isBlockLoaded(pos) || !world.isAirBlock(pos)) { return false; } Block ground = world.getBlockState(new BlockPos(pos.getX(), pos.getY() - 1, pos.getZ())).getBlock(); - return (ground == Blocks.DIRT || ground == Blocks.GRASS) && - helper.plantRandomFlower(flowerType, world, pos); + return (ground == Blocks.DIRT || ground == Blocks.GRASS); } - } diff --git a/src/main/java/forestry/apiculture/flowers/GrowthRuleFertilize.java b/src/main/java/forestry/apiculture/flowers/GrowthRuleFertilize.java index c99fdf8fa..83fefbeec 100644 --- a/src/main/java/forestry/apiculture/flowers/GrowthRuleFertilize.java +++ b/src/main/java/forestry/apiculture/flowers/GrowthRuleFertilize.java @@ -11,6 +11,7 @@ package forestry.apiculture.flowers; import java.util.Arrays; +import java.util.Collection; import java.util.List; import forestry.api.genetics.IFlowerGrowthHelper; @@ -33,6 +34,11 @@ public class GrowthRuleFertilize implements IFlowerGrowthRule { return growFlower(world, pos); } + @Override + public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, BlockPos pos, Collection potentialFlowers) { + return growFlower(world, pos); + } + private boolean growFlower(World world, BlockPos pos) { if (!world.isBlockLoaded(pos)) { return false; diff --git a/src/main/java/forestry/apiculture/flowers/GrowthRuleMycelium.java b/src/main/java/forestry/apiculture/flowers/GrowthRuleMycelium.java index e0dcc63ff..373f99fba 100644 --- a/src/main/java/forestry/apiculture/flowers/GrowthRuleMycelium.java +++ b/src/main/java/forestry/apiculture/flowers/GrowthRuleMycelium.java @@ -10,9 +10,12 @@ ******************************************************************************/ package forestry.apiculture.flowers; +import java.util.Collection; + import forestry.api.genetics.IFlowerGrowthHelper; import forestry.api.genetics.IFlowerGrowthRule; import net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -20,14 +23,25 @@ import net.minecraft.world.World; public class GrowthRuleMycelium implements IFlowerGrowthRule { @Override + @Deprecated public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, BlockPos pos) { + return isValidSpot(world, pos) && + helper.plantRandomFlower(flowerType, world, pos); + } + + @Override + public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, BlockPos pos, Collection potentialFlowers) { + return isValidSpot(world, pos) && + helper.plantRandomFlower(flowerType, world, pos, potentialFlowers); + } + + private boolean isValidSpot(World world, BlockPos pos) { if (!world.isBlockLoaded(pos) || !world.isAirBlock(pos)) { return false; } Block ground = world.getBlockState(new BlockPos(pos.getX(), pos.getY() - 1, pos.getZ())).getBlock(); - return ground == Blocks.MYCELIUM && - helper.plantRandomFlower(flowerType, world, pos); + return ground == Blocks.MYCELIUM; } } diff --git a/src/main/java/forestry/apiculture/flowers/GrowthRuleNone.java b/src/main/java/forestry/apiculture/flowers/GrowthRuleNone.java index 546e82a29..0ebf1394c 100644 --- a/src/main/java/forestry/apiculture/flowers/GrowthRuleNone.java +++ b/src/main/java/forestry/apiculture/flowers/GrowthRuleNone.java @@ -10,8 +10,11 @@ ******************************************************************************/ package forestry.apiculture.flowers; +import java.util.Collection; + import forestry.api.genetics.IFlowerGrowthHelper; import forestry.api.genetics.IFlowerGrowthRule; +import net.minecraft.block.state.IBlockState; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -22,4 +25,8 @@ public class GrowthRuleNone implements IFlowerGrowthRule { return true; } + @Override + public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, BlockPos pos, Collection potentialFlowers) { + return true; + } } diff --git a/src/main/java/forestry/apiculture/flowers/GrowthRuleSnow.java b/src/main/java/forestry/apiculture/flowers/GrowthRuleSnow.java index fbc66222a..07156804f 100644 --- a/src/main/java/forestry/apiculture/flowers/GrowthRuleSnow.java +++ b/src/main/java/forestry/apiculture/flowers/GrowthRuleSnow.java @@ -10,9 +10,12 @@ ******************************************************************************/ package forestry.apiculture.flowers; +import java.util.Collection; + import forestry.api.genetics.IFlowerGrowthHelper; import forestry.api.genetics.IFlowerGrowthRule; import net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -20,14 +23,24 @@ import net.minecraft.world.World; public class GrowthRuleSnow implements IFlowerGrowthRule { @Override + @Deprecated public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, BlockPos pos) { + return isValidSpot(world, pos) && + helper.plantRandomFlower(flowerType, world, pos); + } + + @Override + public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, BlockPos pos, Collection potentialFlowers) { + return isValidSpot(world, pos) && + helper.plantRandomFlower(flowerType, world, pos, potentialFlowers); + } + + private boolean isValidSpot(World world, BlockPos pos) { if (!world.isBlockLoaded(pos) || world.getBlockState(pos).getBlock() != Blocks.SNOW) { return false; } Block ground = world.getBlockState(new BlockPos(pos.getX(), pos.getY() - 1, pos.getZ())).getBlock(); - return (ground == Blocks.DIRT || ground == Blocks.GRASS) && - helper.plantRandomFlower(flowerType, world, pos); + return (ground == Blocks.DIRT || ground == Blocks.GRASS); } - } diff --git a/src/main/java/forestry/apiculture/genetics/Bee.java b/src/main/java/forestry/apiculture/genetics/Bee.java index 9c76c0370..e16313a0c 100755 --- a/src/main/java/forestry/apiculture/genetics/Bee.java +++ b/src/main/java/forestry/apiculture/genetics/Bee.java @@ -19,6 +19,7 @@ import java.util.Map; import java.util.Random; import java.util.Set; +import net.minecraft.block.state.IBlockState; import net.minecraft.item.EnumRarity; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -735,7 +736,11 @@ public class Bee extends IndividualLiving implements IBee { @Override public void plantFlowerRandom(IBeeHousing housing) { + plantFlowerRandom(housing, null); + } + @Override + public BlockPos plantFlowerRandom(IBeeHousing housing, @Nullable List potentialFlowers) { IBeeModifier beeModifier = BeeManager.beeRoot.createBeeHousingModifier(housing); int chance = Math.round(genome.getFlowering() * beeModifier.getFloweringModifier(getGenome(), 1f)); @@ -745,9 +750,8 @@ public class Bee extends IndividualLiving implements IBee { // Correct speed if (random.nextInt(100) >= chance) { - return; + return null; } - // Gather required info IFlowerProvider provider = genome.getFlowerProvider(); Vec3i area = getArea(genome, beeModifier); @@ -758,10 +762,18 @@ public class Bee extends IndividualLiving implements IBee { BlockPos randomPos = VectUtil.getRandomPositionInArea(random, area); BlockPos posBlock = VectUtil.add(housingPos, randomPos, offset); - if (FlowerManager.flowerRegistry.growFlower(provider.getFlowerType(), world, this, posBlock)) { - break; + if (potentialFlowers != null) { + if (FlowerManager.flowerRegistry.growFlower(provider.getFlowerType(), world, this, posBlock, potentialFlowers)) { + return posBlock; + } + } else { // legacy + //noinspection deprecation + if (FlowerManager.flowerRegistry.growFlower(provider.getFlowerType(), world, this, posBlock)) { + return posBlock; + } } } + return null; } private static Vec3i getArea(IBeeGenome genome, IBeeModifier beeModifier) { diff --git a/src/main/java/forestry/apiculture/worldgen/VillageApiaristHouse.java b/src/main/java/forestry/apiculture/worldgen/VillageApiaristHouse.java index 1d87fc85d..179bd0b9a 100755 --- a/src/main/java/forestry/apiculture/worldgen/VillageApiaristHouse.java +++ b/src/main/java/forestry/apiculture/worldgen/VillageApiaristHouse.java @@ -286,7 +286,9 @@ public class VillageApiaristHouse extends StructureVillagePieces.House1 { FlowerRegistry flowerRegistry = (FlowerRegistry) FlowerManager.flowerRegistry; Flower flower = flowerRegistry.getRandomPlantableFlower(FlowerManager.FlowerTypeVanilla, world.rand); - setBlockState(world, flower.getBlockState(), j, i, k, box); + if (flower != null) { + setBlockState(world, flower.getBlockState(), j, i, k, box); + } } } } diff --git a/src/main/java/forestry/core/utils/WeightedCollection.java b/src/main/java/forestry/core/utils/WeightedCollection.java new file mode 100644 index 000000000..c7a48510c --- /dev/null +++ b/src/main/java/forestry/core/utils/WeightedCollection.java @@ -0,0 +1,36 @@ +package forestry.core.utils; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Random; +import java.util.Set; +import java.util.TreeMap; + +import com.google.common.base.Preconditions; + +public class WeightedCollection { + private final NavigableMap map = new TreeMap<>(); + private double total = 0; + + public void put(double weight, T value) { + Preconditions.checkArgument(weight > 0); + total += weight; + map.put(total, value); + } + + @Nullable + public T getRandom(Random random) { + double value = random.nextDouble() * total; + Map.Entry higherEntry = map.higherEntry(value); + if (higherEntry == null) { + return null; + } + return higherEntry.getValue(); + } + + public Set> entrySet() { + return Collections.unmodifiableSet(map.entrySet()); + } +}