public override void FromTreeAttributes(ITreeAttribute tree, IWorldAccessor worldForResolving) { base.FromTreeAttributes(tree, worldForResolving); totalHoursTillGrowth = tree.GetDouble("totalHoursTillGrowth", 0); stage = (EnumTreeGrowthStage)tree.GetInt("growthStage", 1); plantedFromSeed = tree.GetBool("plantedFromSeed"); }
private void CheckGrow(float dt) { if (Api.World.Calendar.TotalHours < totalHoursTillGrowth) { return; } ClimateCondition conds = Api.World.BlockAccessor.GetClimateAt(Pos, EnumGetClimateMode.NowValues); if (conds == null || conds.Temperature < 5) { return; } if (conds.Temperature < 0) { totalHoursTillGrowth = Api.World.Calendar.TotalHours + (float)Api.World.Rand.NextDouble() * 72 * GrowthRateMod; return; } if (stage == EnumTreeGrowthStage.Seed) { stage = EnumTreeGrowthStage.Sapling; totalHoursTillGrowth = Api.World.Calendar.TotalHours + nextStageDaysRnd.nextFloat(1, Api.World.Rand) * 24 * GrowthRateMod; MarkDirty(true); return; } int chunksize = Api.World.BlockAccessor.ChunkSize; foreach (BlockFacing facing in BlockFacing.HORIZONTALS) { Vec3i dir = facing.Normali; int x = Pos.X + dir.X * chunksize; int z = Pos.Z + dir.Z * chunksize; // Not at world edge and chunk is not loaded? We must be at the edge of loaded chunks. Wait until more chunks are generated if (Api.World.BlockAccessor.IsValidPos(x, Pos.Y, z) && Api.World.BlockAccessor.GetChunkAtBlockPos(x, Pos.Y, z) == null) { return; } } Block block = Api.World.BlockAccessor.GetBlock(Pos); string treeGenCode = block.Attributes?["treeGen"].AsString(null); if (treeGenCode == null) { Api.Event.UnregisterGameTickListener(growListenerId); return; } AssetLocation code = new AssetLocation(treeGenCode); ICoreServerAPI sapi = Api as ICoreServerAPI; ITreeGenerator gen; if (!sapi.World.TreeGenerators.TryGetValue(code, out gen)) { Api.Event.UnregisterGameTickListener(growListenerId); return; } Api.World.BlockAccessor.SetBlock(0, Pos); Api.World.BulkBlockAccessor.ReadFromStagedByDefault = true; float size = 0.6f + (float)Api.World.Rand.NextDouble() * 0.5f; sapi.World.TreeGenerators[code].GrowTree(Api.World.BulkBlockAccessor, Pos.DownCopy(), true, size, 0, 0); Api.World.BulkBlockAccessor.Commit(); }
public override void OnBlockPlaced(ItemStack byItemStack = null) { stage = byItemStack?.Collectible is ItemTreeSeed ? EnumTreeGrowthStage.Seed : EnumTreeGrowthStage.Sapling; plantedFromSeed = stage == EnumTreeGrowthStage.Seed; totalHoursTillGrowth = Api.World.Calendar.TotalHours + nextStageDaysRnd.nextFloat(1, Api.World.Rand) * 24 * GrowthRateMod; }