Improve cocoon positions

This commit is contained in:
Nedelosk committed 2017-03-06 16:45:08 +01:00
1 parent ced6f3cbf2
commit 84f01de7f9
8 files changed
+63 -96

No files matched your search

@@ -10,8 +10,6 @@
******************************************************************************/
package forestry.api.lepidopterology;
import javax.annotation.Nullable;
import forestry.api.genetics.IHousing;
public interface IButterflyCocoon extends IHousing {
@@ -20,11 +18,6 @@ public interface IButterflyCocoon extends IHousing {
void setCaterpillar(IButterfly butterfly);
@Nullable
IButterflyNursery getNursery();
void setNursery(IButterflyNursery nursery);
boolean isSolid();
}
@@ -53,7 +53,7 @@ public interface IButterflyRoot extends ISpeciesRoot {
*/
EntityLiving spawnButterflyInWorld(World world, IButterfly butterfly, double x, double y, double z);
BlockPos plantCocoon(World world, IButterflyNursery nursery, GameProfile owner, int age);
BlockPos plantCocoon(World world, BlockPos pos, IButterfly caterpillar, GameProfile owner, int age, boolean createNursery);
/**
* @return true if passed item is mated.
@@ -469,7 +469,7 @@ public class TileLeaves extends TileTreeContainer implements IPollinatable, IFru
int caterpillarMatureTime = Math.round((float) caterpillarGenome.getLifespan() / (caterpillarGenome.getFertility() * 2));
if (maturationTime >= caterpillarMatureTime) {
ButterflyManager.butterflyRoot.plantCocoon(world, this, getOwnerHandler().getOwner(), 0);
ButterflyManager.butterflyRoot.plantCocoon(world, getCaterpillar(), getOwnerHandler().getOwner(), 0);
setCaterpillar(null);
} else if (!wasDestroyed && isDestroyed(tree, damage)) {
sendNetworkUpdate();
@@ -243,11 +243,6 @@ public abstract class BlockUtil {
!block.isWood(world, downPos);
}
// get to the ground
//final BlockPos topPos = world.getHeight(new BlockPos(x, 0, z));
//if (topPos.getY() == 0) {
//return null;
//}
public static BlockPos getNextReplaceableUpPos(World world, BlockPos pos) {
BlockPos topPos = world.getHeight(pos);
final BlockPos.MutableBlockPos newPos = new BlockPos.MutableBlockPos(pos);
@@ -120,7 +120,7 @@ public class GeneticsUtil {
return null;
}
public static IButterflyNursery getOrCreateNursery(World world, BlockPos pos, EntityPlayer player) {
public static IButterflyNursery getOrCreateNursery(World world, BlockPos pos, GameProfile gameProfile) {
IButterflyNursery nursery = getNursery(world, pos);
if (nursery == null) {
@@ -128,7 +128,7 @@ public class GeneticsUtil {
if (pollen != null && pollen instanceof ITree) {
ITree treeLeave = (ITree) pollen;
if (treeLeave.setLeaves(world, player.getGameProfile(), pos)) {
if (treeLeave.setLeaves(world, gameProfile, pos)) {
nursery = getNursery(world, pos);
}
}
@@ -136,6 +136,11 @@ public class GeneticsUtil {
return nursery;
}
public static boolean canCreateNursery(World world, BlockPos pos){
IIndividual pollen = GeneticsUtil.getPollen(world, pos);
return pollen != null && pollen instanceof ITree;
}
public static IButterflyNursery getNursery(World world, BlockPos pos) {
TileEntity tileEntity = world.getTileEntity(pos);
@@ -38,6 +38,7 @@ import forestry.api.lepidopterology.ILepidopteristTracker;
import forestry.core.genetics.SpeciesRoot;
import forestry.core.utils.BlockUtil;
import forestry.core.utils.EntityUtil;
import forestry.core.utils.GeneticsUtil;
import forestry.lepidopterology.PluginLepidopterology;
import forestry.lepidopterology.blocks.BlockRegistryLepidopterology;
import forestry.lepidopterology.entities.EntityButterfly;
@@ -181,14 +182,17 @@ public class ButterflyRoot extends SpeciesRoot implements IButterflyRoot {
}
@Override
public BlockPos plantCocoon(World world, IButterflyNursery nursery, GameProfile owner, int age) {
if (nursery.getCaterpillar() == null) {
public BlockPos plantCocoon(World world, BlockPos coordinates, IButterfly caterpillar, GameProfile owner, int age, boolean createNursery) {
if (caterpillar == null) {
return BlockPos.ORIGIN;
}
BlockRegistryLepidopterology blocks = PluginLepidopterology.getBlocks();
BlockPos pos = getNextValidPos(world, nursery.getCoordinates());
BlockPos pos = getValidCocoonPos(world, coordinates, caterpillar, owner);
if(pos == BlockPos.ORIGIN){
return pos;
}
IBlockState state = blocks.cocoon.getDefaultState();
boolean placed = world.setBlockState(pos, state);
if (!placed) {
@@ -207,35 +211,52 @@ public class ButterflyRoot extends SpeciesRoot implements IButterflyRoot {
}
TileCocoon cocoon = (TileCocoon) tile;
cocoon.setCaterpillar(nursery.getCaterpillar());
cocoon.setCaterpillar(caterpillar);
cocoon.getOwnerHandler().setOwner(owner);
cocoon.setNursery(nursery);
cocoon.setAge(age);
return pos;
}
private BlockPos getNextValidPos(World world, BlockPos pos) {
for(int x = -2;x < 2;x++){
for(int z = -2;z < 2;z++){
for(int y = 1;y < 7;y++){
BlockPos coordinate = pos.add(x, -y, z);
TileEntity tile = world.getTileEntity(coordinate);
IBlockState blockState = world.getBlockState(coordinate);
if(tile instanceof IButterflyNursery){
IButterflyNursery nursery = (IButterflyNursery) tile;
if(nursery.getCaterpillar() != null){
break;
}
}
if(BlockUtil.canReplace(blockState, world, coordinate)){
return coordinate;
}
private BlockPos getValidCocoonPos(World world, BlockPos pos, IButterfly caterpillar, GameProfile gameProfile) {
if(isPositionValid(world, pos.down(), caterpillar, gameProfile)){
return pos.down();
}
for(int tries = 0;tries < 3;tries++){
for(int y = 1;y < world.rand.nextInt(5);y++){
BlockPos coordinate = pos.add(world.rand.nextInt(6)-3, -y, world.rand.nextInt(6)-3);
if(isPositionValid(world, coordinate, caterpillar, gameProfile)){
return coordinate;
}
}
}
return pos;
return BlockPos.ORIGIN;
}
public boolean isPositionValid(World world, BlockPos pos, IButterfly caterpillar, GameProfile gameProfile){
IBlockState blockState = world.getBlockState(pos);
if(BlockUtil.canReplace(blockState, world, pos)){
BlockPos nurseryPos = pos.up();
IButterflyNursery nursery = GeneticsUtil.getNursery(world, nurseryPos);
if(isNurseryValid(nursery, caterpillar, gameProfile)){
return true;
}else if(GeneticsUtil.canCreateNursery(world, nurseryPos)){
nursery = GeneticsUtil.getOrCreateNursery(world, nurseryPos, gameProfile);
return isNurseryValid(nursery, caterpillar, gameProfile);
}
}
return false;
}
private boolean isNurseryValid(IButterflyNursery nursery, IButterfly caterpillar, GameProfile gameProfile){
if(nursery != null){
if(!nursery.canNurse(caterpillar)){
return false;
}
return true;
}
return false;
}
@Override
@@ -227,31 +227,21 @@ public class ItemButterflyGE extends ItemGE implements ISpriteRegister, IColored
}
BlockPos posDown = pos.add(0, -yShift, 0);
IButterflyNursery nursery = GeneticsUtil.getOrCreateNursery(world, pos, player);
if (nursery != null) {
if (nursery.canNurse(flutter)) {
nursery.setCaterpillar(flutter);
pos = ButterflyManager.butterflyRoot.plantCocoon(world, nursery, player.getGameProfile(), age);
if (pos != BlockPos.ORIGIN) {
PacketFXSignal packet = new PacketFXSignal(PacketFXSignal.SoundFXType.BLOCK_PLACE, pos,
world.getBlockState(pos));
NetworkUtil.sendNetworkPacket(packet, pos, world);
pos = ButterflyManager.butterflyRoot.plantCocoon(world, pos, flutter, player.getGameProfile(), age, true);
if (pos != BlockPos.ORIGIN) {
PacketFXSignal packet = new PacketFXSignal(PacketFXSignal.SoundFXType.BLOCK_PLACE, pos,
world.getBlockState(pos));
NetworkUtil.sendNetworkPacket(packet, pos, world);
if (!player.capabilities.isCreativeMode) {
stack.shrink(1);
}
nursery.setCaterpillar(null);
return EnumActionResult.SUCCESS;
} else {
nursery.setCaterpillar(null);
return EnumActionResult.PASS;
}
if (!player.capabilities.isCreativeMode) {
stack.shrink(1);
}
return EnumActionResult.SUCCESS;
} else {
return EnumActionResult.PASS;
}
return EnumActionResult.PASS;
} else if (type == EnumFlutterType.CATERPILLAR) {
IButterflyNursery nursery = GeneticsUtil.getOrCreateNursery(world, pos, player);
IButterflyNursery nursery = GeneticsUtil.getOrCreateNursery(world, pos, player.getGameProfile());
if (nursery != null) {
if (!nursery.canNurse(flutter)) {
return EnumActionResult.PASS;
@@ -20,7 +20,6 @@ import forestry.api.lepidopterology.ButterflyManager;
import forestry.api.lepidopterology.IButterfly;
import forestry.api.lepidopterology.IButterflyCocoon;
import forestry.api.lepidopterology.IButterflyGenome;
import forestry.api.lepidopterology.IButterflyNursery;
import forestry.api.multiblock.IGreenhouseComponent;
import forestry.api.multiblock.IGreenhouseComponent.ButterflyHatch;
import forestry.api.multiblock.IGreenhouseController;
@@ -57,8 +56,6 @@ public class TileCocoon extends TileEntity implements IStreamable, IOwnedTile, I
private int age;
private int maturationTime;
private IButterfly caterpillar = ButterflyDefinition.CabbageWhite.getIndividual();
@Nullable
private BlockPos nursery;
private boolean isSolid;
public TileCocoon() {
@@ -80,13 +77,6 @@ public class TileCocoon extends TileEntity implements IStreamable, IOwnedTile, I
caterpillar = new Butterfly(nbttagcompound.getCompoundTag("Caterpillar"));
}
ownerHandler.readFromNBT(nbttagcompound);
if (nbttagcompound.hasKey("nursery")) {
NBTTagCompound nbt = nbttagcompound.getCompoundTag("nursery");
int x = nbt.getInteger("x");
int y = nbt.getInteger("y");
int z = nbt.getInteger("z");
nursery = new BlockPos(x, y, z);
}
age = nbttagcompound.getInteger("Age");
maturationTime = nbttagcompound.getInteger("CATMAT");
isSolid = nbttagcompound.getBoolean("isSolid");
@@ -102,15 +92,6 @@ public class TileCocoon extends TileEntity implements IStreamable, IOwnedTile, I
ownerHandler.writeToNBT(nbttagcompound);
if (nursery != null) {
BlockPos pos = nursery;
NBTTagCompound nbt = new NBTTagCompound();
nbt.setInteger("x", pos.getX());
nbt.setInteger("y", pos.getY());
nbt.setInteger("z", pos.getZ());
nbttagcompound.setTag("nursery", nbt);
}
nbttagcompound.setInteger("Age", age);
nbttagcompound.setInteger("CATMAT", maturationTime);
nbttagcompound.setBoolean("isSolid", isSolid);
@@ -249,24 +230,6 @@ public class TileCocoon extends TileEntity implements IStreamable, IOwnedTile, I
return caterpillar;
}
@Override
@Nullable
public IButterflyNursery getNursery() {
if (this.nursery == null) {
return null;
}
TileEntity nursery = world.getTileEntity(this.nursery);
if (nursery instanceof IButterflyNursery) {
return (IButterflyNursery) nursery;
}
return null;
}
@Override
public void setNursery(IButterflyNursery nursery) {
this.nursery = nursery.getCoordinates();
}
@Override
public void setCaterpillar(IButterfly butterfly) {
this.caterpillar = butterfly;