From 84f01de7f9590caf94c99ba570a4beebcec863cb Mon Sep 17 00:00:00 2001 From: Nedelosk Date: Mon, 6 Mar 2017 16:45:08 +0100 Subject: [PATCH] Improve cocoon positions --- .../api/lepidopterology/IButterflyCocoon.java | 7 -- .../api/lepidopterology/IButterflyRoot.java | 2 +- .../arboriculture/tiles/TileLeaves.java | 2 +- .../java/forestry/core/utils/BlockUtil.java | 5 -- .../forestry/core/utils/GeneticsUtil.java | 9 ++- .../genetics/ButterflyRoot.java | 65 ++++++++++++------- .../items/ItemButterflyGE.java | 32 ++++----- .../lepidopterology/tiles/TileCocoon.java | 37 ----------- 8 files changed, 63 insertions(+), 96 deletions(-) diff --git a/src/main/java/forestry/api/lepidopterology/IButterflyCocoon.java b/src/main/java/forestry/api/lepidopterology/IButterflyCocoon.java index a638db53c..6e669f0c0 100644 --- a/src/main/java/forestry/api/lepidopterology/IButterflyCocoon.java +++ b/src/main/java/forestry/api/lepidopterology/IButterflyCocoon.java @@ -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(); } diff --git a/src/main/java/forestry/api/lepidopterology/IButterflyRoot.java b/src/main/java/forestry/api/lepidopterology/IButterflyRoot.java index be5e749ef..2fb8fc002 100644 --- a/src/main/java/forestry/api/lepidopterology/IButterflyRoot.java +++ b/src/main/java/forestry/api/lepidopterology/IButterflyRoot.java @@ -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. diff --git a/src/main/java/forestry/arboriculture/tiles/TileLeaves.java b/src/main/java/forestry/arboriculture/tiles/TileLeaves.java index f777a7bfc..8d5beee26 100644 --- a/src/main/java/forestry/arboriculture/tiles/TileLeaves.java +++ b/src/main/java/forestry/arboriculture/tiles/TileLeaves.java @@ -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(); diff --git a/src/main/java/forestry/core/utils/BlockUtil.java b/src/main/java/forestry/core/utils/BlockUtil.java index 8393eb7f5..bdbf9ddc9 100644 --- a/src/main/java/forestry/core/utils/BlockUtil.java +++ b/src/main/java/forestry/core/utils/BlockUtil.java @@ -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); diff --git a/src/main/java/forestry/core/utils/GeneticsUtil.java b/src/main/java/forestry/core/utils/GeneticsUtil.java index 939d0d4f1..940284d37 100644 --- a/src/main/java/forestry/core/utils/GeneticsUtil.java +++ b/src/main/java/forestry/core/utils/GeneticsUtil.java @@ -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); diff --git a/src/main/java/forestry/lepidopterology/genetics/ButterflyRoot.java b/src/main/java/forestry/lepidopterology/genetics/ButterflyRoot.java index 3d05d3287..09757db6d 100644 --- a/src/main/java/forestry/lepidopterology/genetics/ButterflyRoot.java +++ b/src/main/java/forestry/lepidopterology/genetics/ButterflyRoot.java @@ -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 diff --git a/src/main/java/forestry/lepidopterology/items/ItemButterflyGE.java b/src/main/java/forestry/lepidopterology/items/ItemButterflyGE.java index 52d4d79e9..bc0181528 100755 --- a/src/main/java/forestry/lepidopterology/items/ItemButterflyGE.java +++ b/src/main/java/forestry/lepidopterology/items/ItemButterflyGE.java @@ -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; diff --git a/src/main/java/forestry/lepidopterology/tiles/TileCocoon.java b/src/main/java/forestry/lepidopterology/tiles/TileCocoon.java index 278501f1d..82a093cb3 100644 --- a/src/main/java/forestry/lepidopterology/tiles/TileCocoon.java +++ b/src/main/java/forestry/lepidopterology/tiles/TileCocoon.java @@ -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;