diff --git a/src/main/java/forestry/apiculture/gadgets/BlockBeehives.java b/src/main/java/forestry/apiculture/gadgets/BlockBeehives.java index 98426097f..d4ac8c93d 100755 --- a/src/main/java/forestry/apiculture/gadgets/BlockBeehives.java +++ b/src/main/java/forestry/apiculture/gadgets/BlockBeehives.java @@ -12,7 +12,6 @@ package forestry.apiculture.gadgets; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; -import forestry.api.apiculture.BeeManager; import forestry.api.apiculture.IHiveDrop; import forestry.api.apiculture.hives.HiveManager; import forestry.api.core.Tabs; @@ -123,9 +122,6 @@ public class BlockBeehives extends BlockContainer { } private List getDropsForHive(int meta) { - if (meta == 8) //TODO: make a real hive for swarm - return BeeManager.hiveDrops[meta]; - String hiveName = getHiveNameForMeta(meta); if (hiveName == null) return Collections.emptyList(); @@ -177,7 +173,7 @@ public class BlockBeehives extends BlockContainer { @Override @SideOnly(Side.CLIENT) public IIcon getIcon(int i, int j) { - if (j == 0 || j >= 8) + if (j == 0 || j > 8) return null; if (i == 0 || i == 1) { diff --git a/src/main/java/forestry/apiculture/gadgets/TileAlvearySwarmer.java b/src/main/java/forestry/apiculture/gadgets/TileAlvearySwarmer.java index a4f1983e6..4ea11717a 100755 --- a/src/main/java/forestry/apiculture/gadgets/TileAlvearySwarmer.java +++ b/src/main/java/forestry/apiculture/gadgets/TileAlvearySwarmer.java @@ -10,17 +10,6 @@ ******************************************************************************/ package forestry.apiculture.gadgets; -import java.util.Map.Entry; -import java.util.Stack; - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.inventory.IInventory; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; - -import net.minecraftforge.common.util.ForgeDirection; - import forestry.api.apiculture.BeeManager; import forestry.api.apiculture.EnumBeeType; import forestry.api.apiculture.IAlvearyComponent; @@ -28,12 +17,23 @@ import forestry.api.apiculture.IBee; import forestry.api.apiculture.IBeeHousing; import forestry.api.core.ForestryAPI; import forestry.api.core.ISpecialInventory; -import forestry.apiculture.worldgen.WorldGenHive; -import forestry.apiculture.worldgen.WorldGenHiveSwamer; +import forestry.apiculture.worldgen.HiveDecorator; +import forestry.apiculture.worldgen.HiveSwarmer; import forestry.core.network.GuiId; +import forestry.core.network.PacketPayload; import forestry.core.proxy.Proxies; +import forestry.core.utils.StackUtils; import forestry.core.utils.TileInventoryAdapter; import forestry.plugins.PluginApiculture; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraftforge.common.util.ForgeDirection; + +import java.util.Map.Entry; +import java.util.Stack; public class TileAlvearySwarmer extends TileAlveary implements ISpecialInventory { @@ -42,6 +42,7 @@ public class TileAlvearySwarmer extends TileAlveary implements ISpecialInventory TileInventoryAdapter swarmerInventory; private final Stack pendingSpawns = new Stack(); + private boolean isActive; public TileAlvearySwarmer() { super(BLOCK_META); @@ -64,86 +65,103 @@ public class TileAlvearySwarmer extends TileAlveary implements ISpecialInventory protected void updateServerSide() { super.updateServerSide(); - if (worldObj.getTotalWorldTime() % 100 != 0) - if (pendingSpawns.size() > 0) + if (pendingSpawns.size() > 0) { + setIsActive(true); + if (worldObj.getTotalWorldTime() % 1000 == 0) trySpawnSwarm(); + } else { + setIsActive(false); + } if (worldObj.getTotalWorldTime() % 500 != 0) return; - if (!this.hasMaster()) + ItemStack princessStack = getPrincessStack(); + if (princessStack == null) return; - IAlvearyComponent master = (IAlvearyComponent) this.getCentralTE(); - if (!(master instanceof IBeeHousing)) + int chance = consumeInducerAndGetChance(); + if (chance == 0) return; - IBeeHousing housing = (IBeeHousing) master; - ItemStack queenstack = housing.getQueen(); - if (queenstack == null) - return; - if (!PluginApiculture.beeInterface.isMated(queenstack)) - return; - - // Calculate chance - int slot = getInducerSlot(); - if (slot < 0) - return; - int chance = getChanceFor(swarmerInventory.getStackInSlot(slot)); - - // Remove resource - swarmerInventory.decrStackSize(slot, 1); - // Try to spawn princess if (worldObj.rand.nextInt(1000) >= chance) return; // Queue swarm spawn - IBee spawn = PluginApiculture.beeInterface.getMember(queenstack); - spawn.setIsNatural(false); - pendingSpawns.push(PluginApiculture.beeInterface.getMemberStack(spawn, EnumBeeType.PRINCESS.ordinal())); - + IBee princess = PluginApiculture.beeInterface.getMember(princessStack); + princess.setIsNatural(false); + pendingSpawns.push(PluginApiculture.beeInterface.getMemberStack(princess, EnumBeeType.PRINCESS.ordinal())); } - private int getChanceFor(ItemStack stack) { - for (Entry entry : BeeManager.inducers.entrySet()) { - if (entry.getKey().isItemEqual(stack)) - return entry.getValue(); + private ItemStack getPrincessStack() { + if (!this.hasMaster()) + return null; + + IAlvearyComponent master = (IAlvearyComponent) this.getCentralTE(); + if (!(master instanceof IBeeHousing)) + return null; + + IBeeHousing housing = (IBeeHousing) master; + ItemStack princessStack = housing.getQueen(); + if (princessStack == null || !PluginApiculture.beeInterface.isMated(princessStack)) + return null; + + return princessStack; + } + + private int consumeInducerAndGetChance() { + if (swarmerInventory == null) + return 0; + + for (int i = 0; i < swarmerInventory.getSizeInventory(); i++) { + ItemStack stack = swarmerInventory.getStackInSlot(i); + for (Entry entry : BeeManager.inducers.entrySet()) { + if (StackUtils.isIdenticalItem(entry.getKey(), stack)) { + swarmerInventory.decrStackSize(i, 1); + return entry.getValue(); + } + } } return 0; } - private int getInducerSlot() { - for (int i = 0; i < swarmerInventory.getSizeInventory(); i++) { - if (swarmerInventory.getStackInSlot(i) == null) - continue; - for (Entry entry : BeeManager.inducers.entrySet()) { - if (entry.getKey().isItemEqual(swarmerInventory.getStackInSlot(i))) - return i; - } - } - - return -1; - } - private void trySpawnSwarm() { ItemStack toSpawn = pendingSpawns.peek(); - WorldGenHive generator = new WorldGenHiveSwamer(new ItemStack[] { toSpawn }); + HiveSwarmer hive = new HiveSwarmer(128, toSpawn); - int i = 0; - while (i < 10) { - i++; - int spawnX = xCoord + worldObj.rand.nextInt(40 * 2) - 40; - int spawnY = yCoord + worldObj.rand.nextInt(40); - int spawnZ = zCoord + worldObj.rand.nextInt(40 * 2) - 40; - if (generator.generate(worldObj, worldObj.rand, spawnX, spawnY, spawnZ)) { - pendingSpawns.pop(); - break; - } + int chunkX = (xCoord + worldObj.rand.nextInt(40 * 2) - 40) / 16; + int chunkZ = (zCoord + worldObj.rand.nextInt(40 * 2) - 40) / 16; + + if (HiveDecorator.instance().genHive(worldObj, worldObj.rand, chunkX, chunkZ, hive)) { + pendingSpawns.pop(); } + } + private void setIsActive(boolean isActive) { + if (this.isActive != isActive) { + this.isActive = isActive; + sendNetworkUpdate(); + } + } + + /* NETWORK */ + @Override + public void fromPacketPayload(PacketPayload payload) { + boolean isActive = payload.shortPayload[0] > 0; + if (this.isActive != isActive) { + this.isActive = isActive; + worldObj.func_147479_m(xCoord, yCoord, zCoord); + } + } + + @Override + public PacketPayload getPacketPayload() { + PacketPayload payload = new PacketPayload(0, 1); + payload.shortPayload[0] = (short) (isActive ? 1 : 0); + return payload; } @Override @@ -157,7 +175,7 @@ public class TileAlvearySwarmer extends TileAlveary implements ISpecialInventory if(side == 0 || side == 1) return BlockAlveary.BOTTOM; - if (pendingSpawns.size() > 0) + if (isActive) return BlockAlveary.ALVEARY_SWARMER_ON; else return BlockAlveary.ALVEARY_SWARMER_OFF; diff --git a/src/main/java/forestry/apiculture/worldgen/HiveDecorator.java b/src/main/java/forestry/apiculture/worldgen/HiveDecorator.java index 80c50a72d..65ea83051 100755 --- a/src/main/java/forestry/apiculture/worldgen/HiveDecorator.java +++ b/src/main/java/forestry/apiculture/worldgen/HiveDecorator.java @@ -52,34 +52,39 @@ public class HiveDecorator { if (!TerrainGen.populate(event.chunkProvider, event.world, event.rand, event.chunkX, event.chunkZ, event.hasVillageGenerated, EVENT_TYPE)) { return; } - decorateHives(event.world, event.rand, event.chunkX * 16, event.chunkZ * 16); + decorateHives(event.world, event.rand, event.chunkX, event.chunkZ); } - private void decorateHives(World world, Random rand, int worldX, int worldZ) { + private void decorateHives(World world, Random rand, int chunkX, int chunkZ) { for (IHive hive : HiveManager.getHives()) if (Config.generateBeehivesDebug) - genHiveDebug(world, worldX, worldZ, hive); + genHiveDebug(world, chunkX, chunkZ, hive); else - genHive(world, rand, worldX, worldZ, hive); + genHive(world, rand, chunkX, chunkZ, hive); } - private void genHive(World world, Random rand, int worldX, int worldZ, IHive hive) { + public boolean genHive(World world, Random rand, int chunkX, int chunkZ, IHive hive) { if (hive.genChance() < rand.nextFloat() * 128.0f) - return; + return false; + + int worldX = chunkX * 16; + int worldZ = chunkZ * 16; BiomeGenBase biome = world.getBiomeGenForCoords(worldX, worldZ); EnumHumidity humidity = EnumHumidity.getFromValue(biome.rainfall); if (!hive.isGoodBiome(biome) || !hive.isGoodHumidity(humidity)) - return; + return false; for (int tries = 0; tries < 4; tries ++) { int x = worldX + rand.nextInt(16); int z = worldZ + rand.nextInt(16); if (tryGenHive(world, x, z, hive)) - return; + return true; } + + return false; } private void genHiveDebug(World world, int worldX, int worldZ, IHive hive) { diff --git a/src/main/java/forestry/apiculture/worldgen/HiveEnd.java b/src/main/java/forestry/apiculture/worldgen/HiveEnd.java index b61d63b84..1a675700a 100644 --- a/src/main/java/forestry/apiculture/worldgen/HiveEnd.java +++ b/src/main/java/forestry/apiculture/worldgen/HiveEnd.java @@ -11,8 +11,6 @@ package forestry.apiculture.worldgen; import forestry.api.apiculture.hives.HiveGround; -import forestry.api.core.EnumHumidity; -import forestry.api.core.EnumTemperature; import forestry.core.config.ForestryBlock; import net.minecraft.init.Blocks; import net.minecraft.world.biome.BiomeGenBase; diff --git a/src/main/java/forestry/apiculture/worldgen/HiveForest.java b/src/main/java/forestry/apiculture/worldgen/HiveForest.java index 934efb26f..fdc25a5d7 100644 --- a/src/main/java/forestry/apiculture/worldgen/HiveForest.java +++ b/src/main/java/forestry/apiculture/worldgen/HiveForest.java @@ -14,7 +14,6 @@ import forestry.api.apiculture.hives.HiveTree; import forestry.api.core.EnumHumidity; import forestry.api.core.EnumTemperature; import forestry.core.config.ForestryBlock; -import net.minecraft.world.biome.BiomeGenBase; public class HiveForest extends HiveTree { diff --git a/src/main/java/forestry/apiculture/worldgen/HiveJungle.java b/src/main/java/forestry/apiculture/worldgen/HiveJungle.java index 471680714..59e1b341d 100644 --- a/src/main/java/forestry/apiculture/worldgen/HiveJungle.java +++ b/src/main/java/forestry/apiculture/worldgen/HiveJungle.java @@ -14,7 +14,6 @@ import forestry.api.apiculture.hives.HiveTree; import forestry.api.core.EnumHumidity; import forestry.api.core.EnumTemperature; import forestry.core.config.ForestryBlock; -import net.minecraft.world.biome.BiomeGenBase; public class HiveJungle extends HiveTree { diff --git a/src/main/java/forestry/apiculture/worldgen/HiveMeadows.java b/src/main/java/forestry/apiculture/worldgen/HiveMeadows.java index 2dec548aa..4917582cc 100644 --- a/src/main/java/forestry/apiculture/worldgen/HiveMeadows.java +++ b/src/main/java/forestry/apiculture/worldgen/HiveMeadows.java @@ -15,7 +15,6 @@ import forestry.api.core.EnumHumidity; import forestry.api.core.EnumTemperature; import forestry.core.config.ForestryBlock; import net.minecraft.init.Blocks; -import net.minecraft.world.biome.BiomeGenBase; public class HiveMeadows extends HiveGround { diff --git a/src/main/java/forestry/apiculture/worldgen/HiveSnow.java b/src/main/java/forestry/apiculture/worldgen/HiveSnow.java index 5b9a37a63..deafc8802 100644 --- a/src/main/java/forestry/apiculture/worldgen/HiveSnow.java +++ b/src/main/java/forestry/apiculture/worldgen/HiveSnow.java @@ -11,12 +11,10 @@ package forestry.apiculture.worldgen; import forestry.api.apiculture.hives.HiveGround; -import forestry.api.core.EnumHumidity; import forestry.api.core.EnumTemperature; import forestry.core.config.ForestryBlock; import net.minecraft.init.Blocks; import net.minecraft.world.World; -import net.minecraft.world.biome.BiomeGenBase; public class HiveSnow extends HiveGround { diff --git a/src/main/java/forestry/apiculture/worldgen/HiveSwamp.java b/src/main/java/forestry/apiculture/worldgen/HiveSwamp.java index e8c434b07..efa47f548 100644 --- a/src/main/java/forestry/apiculture/worldgen/HiveSwamp.java +++ b/src/main/java/forestry/apiculture/worldgen/HiveSwamp.java @@ -15,7 +15,6 @@ import forestry.api.core.EnumHumidity; import forestry.api.core.EnumTemperature; import forestry.core.config.ForestryBlock; import net.minecraft.init.Blocks; -import net.minecraft.world.biome.BiomeGenBase; public class HiveSwamp extends HiveGround { diff --git a/src/main/java/forestry/apiculture/worldgen/WorldGenHiveSwamer.java b/src/main/java/forestry/apiculture/worldgen/HiveSwarmer.java old mode 100755 new mode 100644 similarity index 61% rename from src/main/java/forestry/apiculture/worldgen/WorldGenHiveSwamer.java rename to src/main/java/forestry/apiculture/worldgen/HiveSwarmer.java index d5998d4e8..e06dca4bb --- a/src/main/java/forestry/apiculture/worldgen/WorldGenHiveSwamer.java +++ b/src/main/java/forestry/apiculture/worldgen/HiveSwarmer.java @@ -4,46 +4,34 @@ * are made available under the terms of the GNU Lesser Public License v3 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl-3.0.txt - * + * * Various Contributors including, but not limited to: * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges ******************************************************************************/ package forestry.apiculture.worldgen; -import java.util.Random; - +import forestry.api.apiculture.hives.HiveGround; +import forestry.apiculture.gadgets.TileSwarm; +import forestry.core.config.ForestryBlock; +import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; -import forestry.apiculture.gadgets.TileSwarm; +public class HiveSwarmer extends HiveGround { -/** - * - * @author CovertJaguar - */ -public class WorldGenHiveSwamer extends WorldGenHive { + private final ItemStack[] bees; - private ItemStack[] bees; - - public WorldGenHiveSwamer(ItemStack[] bees) { + public HiveSwarmer(float genChance, ItemStack... bees) { + super(ForestryBlock.beehives.block(), 8, genChance, Blocks.dirt, Blocks.grass); this.bees = bees; } @Override - public boolean generate(World world, Random random, int x, int y, int z) { - if (world.blockExists(x, y, z) && world.isAirBlock(x, y, z) && (!world.isAirBlock(x, y - 1, z) || !world.isAirBlock(x, y + 1, z))) { - setHive(world, x, y, z, 8); - return true; - } - - return false; - } - - @Override - protected void postGen(World world, int x, int y, int z, int meta) { + public void postGen(World world, int x, int y, int z) { TileEntity tile = world.getTileEntity(x, y, z); if (tile instanceof TileSwarm) ((TileSwarm) tile).setContained(bees); } + } diff --git a/src/main/java/forestry/apiculture/worldgen/WorldGenHive.java b/src/main/java/forestry/apiculture/worldgen/WorldGenHive.java deleted file mode 100755 index 91b8ee9a1..000000000 --- a/src/main/java/forestry/apiculture/worldgen/WorldGenHive.java +++ /dev/null @@ -1,43 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2011-2014 SirSengir. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the GNU Lesser Public License v3 - * which accompanies this distribution, and is available at - * http://www.gnu.org/licenses/lgpl-3.0.txt - * - * Various Contributors including, but not limited to: - * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges - ******************************************************************************/ -package forestry.apiculture.worldgen; - -import java.util.Random; - -import net.minecraft.world.World; -import net.minecraft.world.gen.feature.WorldGenerator; - -import forestry.core.config.Defaults; -import forestry.core.config.ForestryBlock; - -public abstract class WorldGenHive extends WorldGenerator { - - @Override - public abstract boolean generate(World world, Random random, int x, int y, int z); - - protected void setHive(World world, int x, int y, int z, int meta) { - boolean placed = world.setBlock(x, y, z, ForestryBlock.beehives.block(), meta, Defaults.FLAG_BLOCK_SYNCH); - if (!placed) - return; - - if (!ForestryBlock.beehives.isBlockEqual(world, x, y, z)) - return; - - ForestryBlock.beehives.block().onBlockAdded(world, x, y, z); - world.markBlockForUpdate(x, y, z); - - postGen(world, x, y, z, meta); - } - - protected void postGen(World world, int x, int y, int z, int meta) { - ForestryBlock.beehives.block().onBlockPlaced(world, x, y, z, 0, 0.0f, 0.0f, 0.0f, meta); - } -}