From 11fa7bbf75e6a19ebe0104c7212f3ad502b31e75 Mon Sep 17 00:00:00 2001 From: mezz Date: Sun, 11 Oct 2015 06:10:59 -0700 Subject: [PATCH] Add improvements to the flower API --- api/forestry | 2 +- .../apiculture/flowers/FlowerRegistry.java | 85 ++++++++++++++----- ...owthRule.java => GrowthRuleDirtGrass.java} | 17 +++- ...owthRule.java => GrowthRuleFertilize.java} | 14 ++- ...owthRule.java => GrowthRuleFlowerPot.java} | 11 ++- ...rowthRule.java => GrowthRuleMycelium.java} | 17 +++- ...ultGrowthRule.java => GrowthRuleNone.java} | 8 +- ...nowGrowthRule.java => GrowthRuleSnow.java} | 17 +++- .../forestry/apiculture/genetics/Bee.java | 3 +- 9 files changed, 145 insertions(+), 29 deletions(-) rename src/main/java/forestry/apiculture/flowers/{VanillaDirtGrassGrowthRule.java => GrowthRuleDirtGrass.java} (75%) rename src/main/java/forestry/apiculture/flowers/{VanillaFertilizeGrowthRule.java => GrowthRuleFertilize.java} (78%) rename src/main/java/forestry/apiculture/flowers/{VanillaFlowerPotGrowthRule.java => GrowthRuleFlowerPot.java} (84%) rename src/main/java/forestry/apiculture/flowers/{VanillaMyceliumGrowthRule.java => GrowthRuleMycelium.java} (75%) rename src/main/java/forestry/apiculture/flowers/{VanillaDefaultGrowthRule.java => GrowthRuleNone.java} (79%) rename src/main/java/forestry/apiculture/flowers/{VanillaSnowGrowthRule.java => GrowthRuleSnow.java} (74%) diff --git a/api/forestry b/api/forestry index d0444f3ca..826b666cc 160000 --- a/api/forestry +++ b/api/forestry @@ -1 +1 @@ -Subproject commit d0444f3ca9372e591129421d01b02881709bcd53 +Subproject commit 826b666cc07d5c95d6cfd4abfe43285889df261c diff --git a/src/main/java/forestry/apiculture/flowers/FlowerRegistry.java b/src/main/java/forestry/apiculture/flowers/FlowerRegistry.java index 7b16b479d..c18120df0 100644 --- a/src/main/java/forestry/apiculture/flowers/FlowerRegistry.java +++ b/src/main/java/forestry/apiculture/flowers/FlowerRegistry.java @@ -46,14 +46,18 @@ import forestry.api.apiculture.IBeeGenome; import forestry.api.apiculture.IBeeHousing; import forestry.api.apiculture.IBeeModifier; import forestry.api.genetics.IFlower; +import forestry.api.genetics.IFlowerAcceptableRule; +import forestry.api.genetics.IFlowerGrowthHelper; import forestry.api.genetics.IFlowerGrowthRule; import forestry.api.genetics.IFlowerRegistry; import forestry.api.genetics.IIndividual; +import forestry.core.config.Constants; +import forestry.core.utils.Log; import forestry.core.utils.vect.MutableVect; import forestry.core.utils.vect.Vect; import forestry.plugins.PluginManager; -public final class FlowerRegistry implements IFlowerRegistry { +public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelper { private final Set defaultFlowerTypes = ImmutableSet.of( FlowerManager.FlowerTypeVanilla, @@ -67,13 +71,15 @@ public final class FlowerRegistry implements IFlowerRegistry { FlowerManager.FlowerTypeGourd ); + private final HashMultimap registeredRules; // custom check private final HashMultimap registeredBlocks; // quick first check - private final HashMultimap registeredFlowers; // full check + private final HashMultimap registeredFlowers; // full check private final ArrayListMultimap growthRules; - private final Map> chances; + private final Map> chances; public FlowerRegistry() { + this.registeredRules = HashMultimap.create(); this.registeredBlocks = HashMultimap.create(); this.registeredFlowers = HashMultimap.create(); this.growthRules = ArrayListMultimap.create(); @@ -92,6 +98,17 @@ public final class FlowerRegistry implements IFlowerRegistry { registerFlower(block, meta, 0.0, flowerTypes); } + @Override + public void registerAcceptableFlowerRule(IFlowerAcceptableRule acceptableFlower, String... flowerTypes) { + for (String flowerType : flowerTypes) { + if (defaultFlowerTypes.contains(flowerType)) { + Log.severe("IFlowerAcceptableRules are too slow to be applied to Forestry's built-in flower type: " + flowerType + "."); + } else { + registeredRules.put(flowerType, acceptableFlower); + } + } + } + @Override public void registerPlantableFlower(Block block, int meta, double weight, String... flowerTypes) { registerFlower(block, meta, weight, flowerTypes); @@ -115,7 +132,7 @@ public final class FlowerRegistry implements IFlowerRegistry { throw new NullPointerException("Tried to register flower with null type. " + block); } - Set flowers = this.registeredFlowers.get(flowerType); + Set flowers = this.registeredFlowers.get(flowerType); flowers.add(newFlower); Set blocks = this.registeredBlocks.get(flowerType); @@ -139,8 +156,9 @@ public final class FlowerRegistry implements IFlowerRegistry { return null; } + Set acceptableRules = this.registeredRules.get(flowerType); Set acceptedBlocks = this.registeredBlocks.get(flowerType); - Set acceptedFlowers = this.registeredFlowers.get(flowerType); + Set acceptedFlowers = this.registeredFlowers.get(flowerType); World world = beeHousing.getWorld(); IBeeModifier beeModifier = BeeManager.beeRoot.createBeeHousingModifier(beeHousing); @@ -153,6 +171,12 @@ public final class FlowerRegistry implements IFlowerRegistry { Vect posBlock = Vect.add(housingPos, posCurrent); + for (IFlowerAcceptableRule acceptableRule : acceptableRules) { + if (acceptableRule.isAcceptableFlower(flowerType, world, posBlock.x, posBlock.y, posBlock.z)) { + return new ChunkCoordinates(posBlock.x, posBlock.y, posBlock.z); + } + } + if (isAcceptedFlower(flowerType, acceptedBlocks, acceptedFlowers, world, posBlock.x, posBlock.y, posBlock.z)) { return new ChunkCoordinates(posBlock.x, posBlock.y, posBlock.z); } @@ -167,13 +191,20 @@ public final class FlowerRegistry implements IFlowerRegistry { return false; } + Set acceptedCustom = this.registeredRules.get(flowerType); + for (IFlowerAcceptableRule acceptableFlower : acceptedCustom) { + if (acceptableFlower.isAcceptableFlower(flowerType, world, x, y, z)) { + return true; + } + } + Set acceptedBlocks = this.registeredBlocks.get(flowerType); - Set acceptedFlowers = this.registeredFlowers.get(flowerType); + Set acceptedFlowers = this.registeredFlowers.get(flowerType); return isAcceptedFlower(flowerType, acceptedBlocks, acceptedFlowers, world, x, y, z); } - private static boolean isAcceptedFlower(String flowerType, Set acceptedBlocks, Set acceptedFlowers, World world, int x, int y, int z) { + private static boolean isAcceptedFlower(String flowerType, Set acceptedBlocks, Set acceptedFlowers, World world, int x, int y, int z) { Block block = world.getBlock(x, y, z); final int meta; @@ -221,7 +252,15 @@ public final class FlowerRegistry implements IFlowerRegistry { } for (IFlowerGrowthRule rule : this.growthRules.get(flowerType)) { - if (rule.growFlower(this, flowerType, world, individual, x, y, z)) { + boolean success; + try { + success = rule.growFlower(this, flowerType, world, x, y, z); + } catch (Throwable ignored) { + // legacy method + success = rule.growFlower(this, flowerType, world, individual, x, y, z); + } + + if (success) { return true; } } @@ -231,7 +270,7 @@ public final class FlowerRegistry implements IFlowerRegistry { @Override public Set getAcceptableFlowers(String flowerType) { - return this.registeredFlowers.get(flowerType); + return ImmutableSet.copyOf(this.registeredFlowers.get(flowerType)); } @Override @@ -247,7 +286,7 @@ public final class FlowerRegistry implements IFlowerRegistry { @Override public IFlower getRandomPlantableFlower(String flowerType, Random rand) { - TreeMap chancesMap = getChancesMap(flowerType); + TreeMap chancesMap = getChancesMap(flowerType); double maxKey = chancesMap.lastKey() + 1.0; return chancesMap.get(chancesMap.lowerKey(rand.nextDouble() * maxKey)); } @@ -257,11 +296,11 @@ public final class FlowerRegistry implements IFlowerRegistry { return new ArrayList<>(Sets.union(defaultFlowerTypes, registeredFlowers.keySet())); } - private TreeMap getChancesMap(String flowerType) { + private TreeMap getChancesMap(String flowerType) { if (!this.chances.containsKey(flowerType)) { - TreeMap flowerChances = new TreeMap<>(); + TreeMap flowerChances = new TreeMap<>(); double count = 0.0; - for (IFlower flower : this.registeredFlowers.get(flowerType)) { + for (Flower flower : this.registeredFlowers.get(flowerType)) { if (flower.isPlantable()) { flowerChances.put(count, flower); count += flower.getWeight(); @@ -273,13 +312,19 @@ public final class FlowerRegistry implements IFlowerRegistry { } private void registerVanillaGrowthRules() { - registerGrowthRule(new VanillaDirtGrassGrowthRule(), FlowerManager.FlowerTypeVanilla, FlowerManager.FlowerTypeSnow); - registerGrowthRule(new VanillaSnowGrowthRule(), FlowerManager.FlowerTypeSnow); - registerGrowthRule(new VanillaFlowerPotGrowthRule(), FlowerManager.FlowerTypeVanilla, FlowerManager.FlowerTypeSnow, FlowerManager.FlowerTypeMushrooms, FlowerManager.FlowerTypeCacti, + registerGrowthRule(new GrowthRuleDirtGrass(), FlowerManager.FlowerTypeVanilla, FlowerManager.FlowerTypeSnow); + registerGrowthRule(new GrowthRuleSnow(), FlowerManager.FlowerTypeSnow); + registerGrowthRule(new GrowthRuleFlowerPot(), FlowerManager.FlowerTypeVanilla, FlowerManager.FlowerTypeSnow, FlowerManager.FlowerTypeMushrooms, FlowerManager.FlowerTypeCacti, FlowerManager.FlowerTypeJungle); - registerGrowthRule(new VanillaMyceliumGrowthRule(), FlowerManager.FlowerTypeMushrooms); - registerGrowthRule(new VanillaDefaultGrowthRule(), FlowerManager.FlowerTypeEnd); - registerGrowthRule(new VanillaFertilizeGrowthRule(Blocks.melon_stem, Blocks.pumpkin_stem), FlowerManager.FlowerTypeGourd); - registerGrowthRule(new VanillaFertilizeGrowthRule(Blocks.wheat), FlowerManager.FlowerTypeWheat); + registerGrowthRule(new GrowthRuleMycelium(), FlowerManager.FlowerTypeMushrooms); + registerGrowthRule(new GrowthRuleNone(), FlowerManager.FlowerTypeEnd); + registerGrowthRule(new GrowthRuleFertilize(Blocks.melon_stem, Blocks.pumpkin_stem), FlowerManager.FlowerTypeGourd); + registerGrowthRule(new GrowthRuleFertilize(Blocks.wheat), FlowerManager.FlowerTypeWheat); + } + + @Override + public boolean plantRandomFlower(String flowerType, World world, int x, int y, int z) { + IFlower flower = getRandomPlantableFlower(flowerType, world.rand); + return world.setBlock(x, y, z, flower.getBlock(), flower.getMeta(), Constants.FLAG_BLOCK_SYNCH); } } diff --git a/src/main/java/forestry/apiculture/flowers/VanillaDirtGrassGrowthRule.java b/src/main/java/forestry/apiculture/flowers/GrowthRuleDirtGrass.java similarity index 75% rename from src/main/java/forestry/apiculture/flowers/VanillaDirtGrassGrowthRule.java rename to src/main/java/forestry/apiculture/flowers/GrowthRuleDirtGrass.java index df86818b5..079a9c6af 100644 --- a/src/main/java/forestry/apiculture/flowers/VanillaDirtGrassGrowthRule.java +++ b/src/main/java/forestry/apiculture/flowers/GrowthRuleDirtGrass.java @@ -15,12 +15,13 @@ import net.minecraft.init.Blocks; import net.minecraft.world.World; import forestry.api.genetics.IFlower; +import forestry.api.genetics.IFlowerGrowthHelper; import forestry.api.genetics.IFlowerGrowthRule; import forestry.api.genetics.IFlowerRegistry; import forestry.api.genetics.IIndividual; import forestry.core.config.Constants; -public class VanillaDirtGrassGrowthRule implements IFlowerGrowthRule { +public class GrowthRuleDirtGrass implements IFlowerGrowthRule { @Override public boolean growFlower(IFlowerRegistry flowerRegistry, String flowerType, World world, IIndividual individual, int x, int y, int z) { @@ -37,4 +38,18 @@ public class VanillaDirtGrassGrowthRule implements IFlowerGrowthRule { return world.setBlock(x, y, z, flower.getBlock(), flower.getMeta(), Constants.FLAG_BLOCK_SYNCH); } + @Override + public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, int x, int y, int z) { + if (!world.isAirBlock(x, y, z)) { + return false; + } + + Block ground = world.getBlock(x, y - 1, z); + if (ground != Blocks.dirt && ground != Blocks.grass) { + return false; + } + + return helper.plantRandomFlower(flowerType, world, x, y, z); + } + } diff --git a/src/main/java/forestry/apiculture/flowers/VanillaFertilizeGrowthRule.java b/src/main/java/forestry/apiculture/flowers/GrowthRuleFertilize.java similarity index 78% rename from src/main/java/forestry/apiculture/flowers/VanillaFertilizeGrowthRule.java rename to src/main/java/forestry/apiculture/flowers/GrowthRuleFertilize.java index 5b814f345..9df48fab8 100644 --- a/src/main/java/forestry/apiculture/flowers/VanillaFertilizeGrowthRule.java +++ b/src/main/java/forestry/apiculture/flowers/GrowthRuleFertilize.java @@ -16,20 +16,30 @@ import java.util.List; import net.minecraft.block.Block; import net.minecraft.world.World; +import forestry.api.genetics.IFlowerGrowthHelper; import forestry.api.genetics.IFlowerGrowthRule; import forestry.api.genetics.IFlowerRegistry; import forestry.api.genetics.IIndividual; -public class VanillaFertilizeGrowthRule implements IFlowerGrowthRule { +public class GrowthRuleFertilize implements IFlowerGrowthRule { private final List allowedItems; - public VanillaFertilizeGrowthRule(Block... allowedItems) { + public GrowthRuleFertilize(Block... allowedItems) { this.allowedItems = Arrays.asList(allowedItems); } @Override public boolean growFlower(IFlowerRegistry fr, String flowerType, World world, IIndividual individual, int x, int y, int z) { + return growFlower(world, x, y, z); + } + + @Override + public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, int x, int y, int z) { + return growFlower(world, x, y, z); + } + + private boolean growFlower(World world, int x, int y, int z) { Block ground = world.getBlock(x, y, z); int groundMeta; for (Block b : this.allowedItems) { diff --git a/src/main/java/forestry/apiculture/flowers/VanillaFlowerPotGrowthRule.java b/src/main/java/forestry/apiculture/flowers/GrowthRuleFlowerPot.java similarity index 84% rename from src/main/java/forestry/apiculture/flowers/VanillaFlowerPotGrowthRule.java rename to src/main/java/forestry/apiculture/flowers/GrowthRuleFlowerPot.java index a99691043..5344a2ae7 100644 --- a/src/main/java/forestry/apiculture/flowers/VanillaFlowerPotGrowthRule.java +++ b/src/main/java/forestry/apiculture/flowers/GrowthRuleFlowerPot.java @@ -17,15 +17,24 @@ import net.minecraft.tileentity.TileEntityFlowerPot; import net.minecraft.world.World; import forestry.api.apiculture.FlowerManager; +import forestry.api.genetics.IFlowerGrowthHelper; import forestry.api.genetics.IFlowerGrowthRule; import forestry.api.genetics.IFlowerRegistry; import forestry.api.genetics.IIndividual; -public class VanillaFlowerPotGrowthRule implements IFlowerGrowthRule { +public class GrowthRuleFlowerPot implements IFlowerGrowthRule { @Override public boolean growFlower(IFlowerRegistry fr, String flowerType, World world, IIndividual individual, int x, int y, int z) { + return growFlower(flowerType, world, x, y, z); + } + @Override + public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, int x, int y, int z) { + return growFlower(flowerType, world, x, y, z); + } + + private static boolean growFlower(String flowerType, World world, int x, int y, int z) { TileEntity tile = world.getTileEntity(x, y, z); if (!(tile instanceof TileEntityFlowerPot)) { return false; diff --git a/src/main/java/forestry/apiculture/flowers/VanillaMyceliumGrowthRule.java b/src/main/java/forestry/apiculture/flowers/GrowthRuleMycelium.java similarity index 75% rename from src/main/java/forestry/apiculture/flowers/VanillaMyceliumGrowthRule.java rename to src/main/java/forestry/apiculture/flowers/GrowthRuleMycelium.java index 5da9a259d..31e5466a5 100644 --- a/src/main/java/forestry/apiculture/flowers/VanillaMyceliumGrowthRule.java +++ b/src/main/java/forestry/apiculture/flowers/GrowthRuleMycelium.java @@ -15,12 +15,13 @@ import net.minecraft.init.Blocks; import net.minecraft.world.World; import forestry.api.genetics.IFlower; +import forestry.api.genetics.IFlowerGrowthHelper; import forestry.api.genetics.IFlowerGrowthRule; import forestry.api.genetics.IFlowerRegistry; import forestry.api.genetics.IIndividual; import forestry.core.config.Constants; -public class VanillaMyceliumGrowthRule implements IFlowerGrowthRule { +public class GrowthRuleMycelium implements IFlowerGrowthRule { @Override public boolean growFlower(IFlowerRegistry fr, String flowerType, World world, IIndividual individual, int x, int y, int z) { @@ -37,4 +38,18 @@ public class VanillaMyceliumGrowthRule implements IFlowerGrowthRule { return world.setBlock(x, y, z, flower.getBlock(), flower.getMeta(), Constants.FLAG_BLOCK_SYNCH); } + @Override + public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, int x, int y, int z) { + if (!world.isAirBlock(x, y, z)) { + return false; + } + + Block ground = world.getBlock(x, y - 1, z); + if (ground != Blocks.mycelium) { + return false; + } + + return helper.plantRandomFlower(flowerType, world, x, y, z); + } + } diff --git a/src/main/java/forestry/apiculture/flowers/VanillaDefaultGrowthRule.java b/src/main/java/forestry/apiculture/flowers/GrowthRuleNone.java similarity index 79% rename from src/main/java/forestry/apiculture/flowers/VanillaDefaultGrowthRule.java rename to src/main/java/forestry/apiculture/flowers/GrowthRuleNone.java index 9dbda19fc..e04297cbe 100644 --- a/src/main/java/forestry/apiculture/flowers/VanillaDefaultGrowthRule.java +++ b/src/main/java/forestry/apiculture/flowers/GrowthRuleNone.java @@ -12,15 +12,21 @@ package forestry.apiculture.flowers; import net.minecraft.world.World; +import forestry.api.genetics.IFlowerGrowthHelper; import forestry.api.genetics.IFlowerGrowthRule; import forestry.api.genetics.IFlowerRegistry; import forestry.api.genetics.IIndividual; -public class VanillaDefaultGrowthRule implements IFlowerGrowthRule { +public class GrowthRuleNone implements IFlowerGrowthRule { @Override public boolean growFlower(IFlowerRegistry fr, String flowerType, World world, IIndividual individual, int x, int y, int z) { return true; } + @Override + public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, int x, int y, int z) { + return true; + } + } diff --git a/src/main/java/forestry/apiculture/flowers/VanillaSnowGrowthRule.java b/src/main/java/forestry/apiculture/flowers/GrowthRuleSnow.java similarity index 74% rename from src/main/java/forestry/apiculture/flowers/VanillaSnowGrowthRule.java rename to src/main/java/forestry/apiculture/flowers/GrowthRuleSnow.java index 0db4cb106..b35e973e9 100644 --- a/src/main/java/forestry/apiculture/flowers/VanillaSnowGrowthRule.java +++ b/src/main/java/forestry/apiculture/flowers/GrowthRuleSnow.java @@ -15,12 +15,13 @@ import net.minecraft.init.Blocks; import net.minecraft.world.World; import forestry.api.genetics.IFlower; +import forestry.api.genetics.IFlowerGrowthHelper; import forestry.api.genetics.IFlowerGrowthRule; import forestry.api.genetics.IFlowerRegistry; import forestry.api.genetics.IIndividual; import forestry.core.config.Constants; -public class VanillaSnowGrowthRule implements IFlowerGrowthRule { +public class GrowthRuleSnow implements IFlowerGrowthRule { @Override public boolean growFlower(IFlowerRegistry fr, String flowerType, World world, IIndividual individual, int x, int y, int z) { @@ -37,4 +38,18 @@ public class VanillaSnowGrowthRule implements IFlowerGrowthRule { return world.setBlock(x, y, z, flower.getBlock(), flower.getMeta(), Constants.FLAG_BLOCK_SYNCH); } + @Override + public boolean growFlower(IFlowerGrowthHelper helper, String flowerType, World world, int x, int y, int z) { + if (world.getBlock(x, y, z) != Blocks.snow) { + return false; + } + + Block ground = world.getBlock(x, y - 1, z); + if (ground != Blocks.dirt && ground != Blocks.grass) { + return false; + } + + return helper.plantRandomFlower(flowerType, world, x, y, z); + } + } diff --git a/src/main/java/forestry/apiculture/genetics/Bee.java b/src/main/java/forestry/apiculture/genetics/Bee.java index 45f14fd64..a9d8cdae1 100644 --- a/src/main/java/forestry/apiculture/genetics/Bee.java +++ b/src/main/java/forestry/apiculture/genetics/Bee.java @@ -30,6 +30,7 @@ import com.mojang.authlib.GameProfile; import forestry.api.apiculture.BeeManager; import forestry.api.apiculture.EnumBeeChromosome; +import forestry.api.apiculture.FlowerManager; import forestry.api.apiculture.IAlleleBeeEffect; import forestry.api.apiculture.IAlleleBeeSpecies; import forestry.api.apiculture.IApiaristTracker; @@ -741,7 +742,7 @@ public class Bee extends IndividualLiving implements IBee { Vect randomPos = Vect.getRandomPositionInArea(random, area); Vect posBlock = Vect.add(housingPos, randomPos, offset); - if (provider.growFlower(world, this, posBlock.x, posBlock.y, posBlock.z)) { + if (FlowerManager.flowerRegistry.growFlower(provider.getFlowerType(), world, this, posBlock.x, posBlock.y, posBlock.z)) { break; } }