Fix Alveary Swarmer and Swarm Hive

This commit is contained in:
mezz committed 2014-12-09 01:09:10 -08:00
1 parent 1d2266a524
commit dbf6202a90
11 files changed
+110 -154

No files matched your search

@@ -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<IHiveDrop> 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) {
@@ -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<ItemStack> pendingSpawns = new Stack<ItemStack>();
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<ItemStack, Integer> 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<ItemStack, Integer> 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<ItemStack, Integer> 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;
@@ -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) {
@@ -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;
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 <http://www.railcraft.info/>
*/
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);
}
}
@@ -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);
}
}