From 6e3c28e07ebdd0e64a975e16aa447dea8ed5305a Mon Sep 17 00:00:00 2001 From: mezz Date: Fri, 4 Mar 2016 03:09:57 -0800 Subject: [PATCH] Clean up hive drops API --- .../forestry/api/apiculture/IHiveDrop.java | 23 ++++++--- .../forestry/apiculture/PluginApiculture.java | 28 +++++------ .../apiculture/blocks/BlockBeehives.java | 49 ++++++++++++------- .../apiculture/genetics/HiveDrop.java | 37 ++++++-------- .../core/gui/GuiNaturalistInventory.java | 4 +- .../storage/items/ItemBackpackNaturalist.java | 5 +- 6 files changed, 82 insertions(+), 64 deletions(-) diff --git a/src/main/java/forestry/api/apiculture/IHiveDrop.java b/src/main/java/forestry/api/apiculture/IHiveDrop.java index 9edaa8d92..3ebf22e0d 100644 --- a/src/main/java/forestry/api/apiculture/IHiveDrop.java +++ b/src/main/java/forestry/api/apiculture/IHiveDrop.java @@ -9,7 +9,7 @@ import java.util.Collection; import net.minecraft.item.ItemStack; import net.minecraft.util.BlockPos; -import net.minecraft.world.World; +import net.minecraft.world.IBlockAccess; /** * Bees can be seeded either as hive drops or as mutation results. @@ -20,18 +20,25 @@ import net.minecraft.world.World; */ public interface IHiveDrop { - ItemStack getPrincess(World world, BlockPos pos, int fortune); + IBee getBeeType(IBlockAccess world, BlockPos pos); - Collection getDrones(World world, BlockPos pos, int fortune); - - Collection getAdditional(World world, BlockPos pos, int fortune); + Collection getExtraItems(IBlockAccess world, BlockPos pos, int fortune); /** - * Chance to drop. Default drops have 80 (= 80 %). + * Chance to drop a bee or extra items. Default drops have 0.80 (= 80 %). * * @param world Minecraft world this is called for. * @param pos Coordinates of the broken hive. - * @return Chance for drop as an integer of 0 - 100. + * @return Chance for drop as a float of 0.0 - 1.0. */ - int getChance(World world, BlockPos pos); + double getChance(IBlockAccess world, BlockPos pos, int fortune); + + /** + * Chance for the princess to be ignoble. Default is around 0.4 to 0.7 (40% - 70%). + * + * @param world Minecraft world this is called for. + * @param pos Coordinates of the broken hive. + * @return Chance for ignoble as a float of 0.0 - 1.0. + */ + double getIgnobleChance(IBlockAccess world, BlockPos pos, int fortune); } diff --git a/src/main/java/forestry/apiculture/PluginApiculture.java b/src/main/java/forestry/apiculture/PluginApiculture.java index a59c980e9..087df83c6 100644 --- a/src/main/java/forestry/apiculture/PluginApiculture.java +++ b/src/main/java/forestry/apiculture/PluginApiculture.java @@ -747,43 +747,43 @@ public class PluginApiculture extends BlankForestryPlugin { private static void registerBeehiveDrops() { ItemStack honeyComb = items.beeComb.get(EnumHoneyComb.HONEY, 1); hiveRegistry.addDrops(HiveType.FOREST.getHiveName(), - new HiveDrop(80, BeeDefinition.FOREST, honeyComb).setIgnobleShare(0.7f), - new HiveDrop(8, BeeDefinition.FOREST.getRainResist(), honeyComb), - new HiveDrop(3, BeeDefinition.VALIANT, honeyComb) + new HiveDrop(0.80, BeeDefinition.FOREST, honeyComb).setIgnobleShare(0.7), + new HiveDrop(0.08, BeeDefinition.FOREST.getRainResist(), honeyComb), + new HiveDrop(0.03, BeeDefinition.VALIANT, honeyComb) ); hiveRegistry.addDrops(HiveType.MEADOWS.getHiveName(), - new HiveDrop(80, BeeDefinition.MEADOWS, honeyComb).setIgnobleShare(0.7f), - new HiveDrop(3, BeeDefinition.VALIANT, honeyComb) + new HiveDrop(0.80, BeeDefinition.MEADOWS, honeyComb).setIgnobleShare(0.7), + new HiveDrop(0.03, BeeDefinition.VALIANT, honeyComb) ); ItemStack parchedComb = items.beeComb.get(EnumHoneyComb.PARCHED, 1); hiveRegistry.addDrops(HiveType.DESERT.getHiveName(), - new HiveDrop(80, BeeDefinition.MODEST, parchedComb).setIgnobleShare(0.7f), - new HiveDrop(3, BeeDefinition.VALIANT, parchedComb) + new HiveDrop(0.80, BeeDefinition.MODEST, parchedComb).setIgnobleShare(0.7), + new HiveDrop(0.03, BeeDefinition.VALIANT, parchedComb) ); ItemStack silkyComb = items.beeComb.get(EnumHoneyComb.SILKY, 1); hiveRegistry.addDrops(HiveType.JUNGLE.getHiveName(), - new HiveDrop(80, BeeDefinition.TROPICAL, silkyComb).setIgnobleShare(0.7f), - new HiveDrop(3, BeeDefinition.VALIANT, silkyComb) + new HiveDrop(0.80, BeeDefinition.TROPICAL, silkyComb).setIgnobleShare(0.7), + new HiveDrop(0.03, BeeDefinition.VALIANT, silkyComb) ); ItemStack mysteriousComb = items.beeComb.get(EnumHoneyComb.MYSTERIOUS, 1); hiveRegistry.addDrops(HiveType.END.getHiveName(), - new HiveDrop(90, BeeDefinition.ENDED, mysteriousComb) + new HiveDrop(0.90, BeeDefinition.ENDED, mysteriousComb) ); ItemStack frozenComb = items.beeComb.get(EnumHoneyComb.FROZEN, 1); hiveRegistry.addDrops(HiveType.SNOW.getHiveName(), - new HiveDrop(80, BeeDefinition.WINTRY, frozenComb).setIgnobleShare(0.5f), - new HiveDrop(3, BeeDefinition.VALIANT, frozenComb) + new HiveDrop(0.80, BeeDefinition.WINTRY, frozenComb).setIgnobleShare(0.5), + new HiveDrop(0.03, BeeDefinition.VALIANT, frozenComb) ); ItemStack mossyComb = items.beeComb.get(EnumHoneyComb.MOSSY, 1); hiveRegistry.addDrops(HiveType.SWAMP.getHiveName(), - new HiveDrop(80, BeeDefinition.MARSHY, mossyComb).setIgnobleShare(0.4f), - new HiveDrop(3, BeeDefinition.VALIANT, mossyComb) + new HiveDrop(0.80, BeeDefinition.MARSHY, mossyComb).setIgnobleShare(0.4), + new HiveDrop(0.03, BeeDefinition.VALIANT, mossyComb) ); } diff --git a/src/main/java/forestry/apiculture/blocks/BlockBeehives.java b/src/main/java/forestry/apiculture/blocks/BlockBeehives.java index 3439b02bb..6dda23738 100644 --- a/src/main/java/forestry/apiculture/blocks/BlockBeehives.java +++ b/src/main/java/forestry/apiculture/blocks/BlockBeehives.java @@ -13,6 +13,7 @@ package forestry.apiculture.blocks; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Random; import net.minecraft.block.BlockContainer; import net.minecraft.block.properties.PropertyEnum; @@ -28,6 +29,9 @@ import net.minecraft.util.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; +import forestry.api.apiculture.BeeManager; +import forestry.api.apiculture.EnumBeeType; +import forestry.api.apiculture.IBee; import forestry.api.apiculture.IHiveDrop; import forestry.api.apiculture.hives.IHiveRegistry.HiveType; import forestry.api.core.IItemModelRegister; @@ -100,20 +104,28 @@ public class BlockBeehives extends BlockContainer implements IItemModelRegister @Override public List getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) { - ArrayList ret = new ArrayList<>(); + List drops = new ArrayList<>(); - List dropList = getDropsForHive(getMetaFromState(state)); + Random random = world instanceof World ? ((World) world).rand : RANDOM; + + List hiveDrops = getDropsForHive(getMetaFromState(state)); + Collections.shuffle(hiveDrops); - Collections.shuffle(dropList); // Grab a princess int tries = 0; boolean hasPrincess = false; while (tries <= 10 && !hasPrincess) { tries++; - for (IHiveDrop drop : dropList) { - if (RANDOM.nextInt(100) < drop.getChance((World) world, pos)) { - ret.add(drop.getPrincess((World) world, pos, fortune)); + for (IHiveDrop drop : hiveDrops) { + if (random.nextDouble() < drop.getChance(world, pos, fortune)) { + IBee bee = drop.getBeeType(world, pos); + if (random.nextFloat() < drop.getIgnobleChance(world, pos, fortune)) { + bee.setIsNatural(false); + } + + ItemStack princess = BeeManager.beeRoot.getMemberStack(bee, EnumBeeType.PRINCESS.ordinal()); + drops.add(princess); hasPrincess = true; break; } @@ -121,21 +133,24 @@ public class BlockBeehives extends BlockContainer implements IItemModelRegister } // Grab drones - for (IHiveDrop drop : dropList) { - if (RANDOM.nextInt(100) < drop.getChance((World) world, pos)) { - ret.addAll(drop.getDrones((World) world, pos, fortune)); - break; - } - } - // Grab anything else on offer - for (IHiveDrop drop : dropList) { - if (RANDOM.nextInt(100) < drop.getChance((World) world, pos)) { - ret.addAll(drop.getAdditional((World) world, pos, fortune)); + for (IHiveDrop drop : hiveDrops) { + if (random.nextDouble() < drop.getChance(world, pos, fortune)) { + IBee bee = drop.getBeeType(world, pos); + ItemStack drone = BeeManager.beeRoot.getMemberStack(bee, EnumBeeType.DRONE.ordinal()); + drops.add(drone); break; } } - return ret; + // Grab anything else on offer + for (IHiveDrop drop : hiveDrops) { + if (random.nextDouble() < drop.getChance(world, pos, fortune)) { + drops.addAll(drop.getExtraItems(world, pos, fortune)); + break; + } + } + + return drops; } // / CREATIVE INVENTORY diff --git a/src/main/java/forestry/apiculture/genetics/HiveDrop.java b/src/main/java/forestry/apiculture/genetics/HiveDrop.java index 19a5460d2..30ce297d6 100755 --- a/src/main/java/forestry/apiculture/genetics/HiveDrop.java +++ b/src/main/java/forestry/apiculture/genetics/HiveDrop.java @@ -16,10 +16,8 @@ import java.util.List; import net.minecraft.item.ItemStack; import net.minecraft.util.BlockPos; -import net.minecraft.world.World; +import net.minecraft.world.IBlockAccess; -import forestry.api.apiculture.BeeManager; -import forestry.api.apiculture.EnumBeeType; import forestry.api.apiculture.IBee; import forestry.api.apiculture.IHiveDrop; @@ -27,40 +25,29 @@ public class HiveDrop implements IHiveDrop { private final IBeeDefinition beeTemplate; private final ArrayList additional = new ArrayList<>(); - private final int chance; - private float ignobleShare = 0.0f; + private final double chance; + private double ignobleShare = 0.0; - public HiveDrop(int chance, IBeeDefinition beeTemplate, ItemStack... bonus) { + public HiveDrop(double chance, IBeeDefinition beeTemplate, ItemStack... bonus) { this.beeTemplate = beeTemplate; this.chance = chance; Collections.addAll(this.additional, bonus); } - public HiveDrop setIgnobleShare(float share) { + public HiveDrop setIgnobleShare(double share) { this.ignobleShare = share; return this; } @Override - public ItemStack getPrincess(World world, BlockPos pos, int fortune) { - IBee bee = beeTemplate.getIndividual(); - if (world.rand.nextFloat() < ignobleShare) { - bee.setIsNatural(false); - } - - return BeeManager.beeRoot.getMemberStack(bee, EnumBeeType.PRINCESS.ordinal()); + public IBee getBeeType(IBlockAccess world, BlockPos pos) { + return beeTemplate.getIndividual(); } @Override - public List getDrones(World world, BlockPos pos, int fortune) { - ItemStack drone = beeTemplate.getMemberStack(EnumBeeType.DRONE); - return Collections.singletonList(drone); - } - - @Override - public ArrayList getAdditional(World world, BlockPos pos, int fortune) { - ArrayList ret = new ArrayList<>(); + public List getExtraItems(IBlockAccess world, BlockPos pos, int fortune) { + List ret = new ArrayList<>(); for (ItemStack stack : additional) { ret.add(stack.copy()); } @@ -69,8 +56,12 @@ public class HiveDrop implements IHiveDrop { } @Override - public int getChance(World world, BlockPos pos) { + public double getChance(IBlockAccess world, BlockPos pos, int fortune) { return chance; } + @Override + public double getIgnobleChance(IBlockAccess world, BlockPos pos, int fortune) { + return ignobleShare; + } } diff --git a/src/main/java/forestry/core/gui/GuiNaturalistInventory.java b/src/main/java/forestry/core/gui/GuiNaturalistInventory.java index 303f42e3c..abc45ebaf 100755 --- a/src/main/java/forestry/core/gui/GuiNaturalistInventory.java +++ b/src/main/java/forestry/core/gui/GuiNaturalistInventory.java @@ -10,6 +10,7 @@ ******************************************************************************/ package forestry.core.gui; +import javax.annotation.Nonnull; import java.io.IOException; import java.util.HashMap; @@ -36,12 +37,13 @@ import forestry.core.proxy.Proxies; import forestry.core.utils.StringUtil; public class GuiNaturalistInventory extends GuiForestry { + @Nonnull private final ISpeciesRoot speciesRoot; private final IBreedingTracker breedingTracker; private final HashMap iconStacks = new HashMap<>(); private final int pageCurrent, pageMax; - public GuiNaturalistInventory(ISpeciesRoot speciesRoot, EntityPlayer player, Container container, IPagedInventory inventory, int page, int maxPages) { + public GuiNaturalistInventory(@Nonnull ISpeciesRoot speciesRoot, EntityPlayer player, Container container, IPagedInventory inventory, int page, int maxPages) { super(Constants.TEXTURE_PATH_GUI + "/apiaristinventory.png", container, inventory); this.speciesRoot = speciesRoot; diff --git a/src/main/java/forestry/storage/items/ItemBackpackNaturalist.java b/src/main/java/forestry/storage/items/ItemBackpackNaturalist.java index 7b8f7e18a..b5e95ce70 100644 --- a/src/main/java/forestry/storage/items/ItemBackpackNaturalist.java +++ b/src/main/java/forestry/storage/items/ItemBackpackNaturalist.java @@ -10,6 +10,8 @@ ******************************************************************************/ package forestry.storage.items; +import javax.annotation.Nonnull; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; @@ -23,9 +25,10 @@ import forestry.storage.gui.ContainerNaturalistBackpack; import forestry.storage.inventory.ItemInventoryBackpackPaged; public class ItemBackpackNaturalist extends ItemBackpack { + @Nonnull private final ISpeciesRoot speciesRoot; - public ItemBackpackNaturalist(ISpeciesRoot speciesRoot, IBackpackDefinition definition, EnumBackpackType type) { + public ItemBackpackNaturalist(@Nonnull ISpeciesRoot speciesRoot, IBackpackDefinition definition, EnumBackpackType type) { super(definition, type); this.speciesRoot = speciesRoot; }