/
BlockBeehive.cs
105 lines (80 loc) · 4.28 KB
/
BlockBeehive.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using Vintagestory.API.Common;
using Vintagestory.API.Common.Entities;
using Vintagestory.API.Config;
using Vintagestory.API.MathTools;
namespace Vintagestory.GameContent
{
public class BlockBeehive : Block
{
public override void OnBlockBroken(IWorldAccessor world, BlockPos pos, IPlayer byPlayer, float dropQuantityMultiplier = 1)
{
base.OnBlockBroken(world, pos, byPlayer, dropQuantityMultiplier);
if (world.Side == EnumAppSide.Client) return; // Only spawn the entity on the server side
EntityProperties type = world.GetEntityType(new AssetLocation("beemob"));
Entity entity = world.ClassRegistry.CreateEntity(type);
if (entity != null)
{
entity.ServerPos.X = pos.X + 0.5f;
entity.ServerPos.Y = pos.Y + 0.5f;
entity.ServerPos.Z = pos.Z + 0.5f;
entity.ServerPos.Yaw = (float)world.Rand.NextDouble() * 2 * GameMath.PI;
entity.Pos.SetFrom(entity.ServerPos);
entity.Attributes.SetString("origin", "brokenbeehive");
world.SpawnEntity(entity);
}
}
BlockPos atPos = new BlockPos();
Cuboidf[] nocoll = new Cuboidf[0];
public override Cuboidf[] GetParticleCollisionBoxes(IBlockAccessor blockAccessor, BlockPos pos)
{
return nocoll;
}
public override bool TryPlaceBlockForWorldGen(IBlockAccessor blockAccessor, BlockPos pos, BlockFacing onBlockFace, LCGRandom worldgenRand)
{
//blockAccessor.SetBlock(blockAccessor.GetBlock(new AssetLocation("creativeblock-60")).BlockId, pos);
for (int i = 1; i < 4; i++)
{
atPos.Set(pos.X, pos.Y - i, pos.Z);
Block aboveBlock = blockAccessor.GetBlock(atPos);
var abovemat = aboveBlock.GetBlockMaterial(blockAccessor, atPos);
if ((abovemat == EnumBlockMaterial.Wood || abovemat == EnumBlockMaterial.Leaves) && aboveBlock.SideSolid[BlockFacing.DOWN.Index])
{
atPos.Set(pos.X, pos.Y - i - 1, pos.Z);
Block block = blockAccessor.GetBlock(atPos);
var mat = block.GetBlockMaterial(blockAccessor, atPos);
BlockPos belowPos = new BlockPos(pos.X, pos.Y - i - 2, pos.Z);
if (
mat == EnumBlockMaterial.Wood &&
abovemat == EnumBlockMaterial.Wood &&
blockAccessor.GetBlock(belowPos).GetBlockMaterial(blockAccessor, belowPos) == EnumBlockMaterial.Wood &&
aboveBlock.LastCodePart() == "ud"
)
{
Block inlogblock = blockAccessor.GetBlock(new AssetLocation("wildbeehive-inlog-" + aboveBlock.Variant["wood"]));
blockAccessor.SetBlock(inlogblock.BlockId, atPos);
if (EntityClass != null)
{
blockAccessor.SpawnBlockEntity(EntityClass, atPos);
}
return true;
}
if (mat != EnumBlockMaterial.Leaves && mat != EnumBlockMaterial.Air) continue;
int dx = pos.X % GlobalConstants.ChunkSize;
int dz = pos.Z % GlobalConstants.ChunkSize;
int surfacey = blockAccessor.GetMapChunkAtBlockPos(atPos).WorldGenTerrainHeightMap[dz * GlobalConstants.ChunkSize + dx];
if (pos.Y - surfacey < 4) return false;
blockAccessor.SetBlock(BlockId, atPos);
if (EntityClass != null)
{
blockAccessor.SpawnBlockEntity(EntityClass, atPos);
}
//BlockPos test = pos.Copy();
//test.Y = 160;
//blockAccessor.SetBlock(blockAccessor.GetBlock(new AssetLocation("creativeblock-60")).BlockId, test);
return true;
}
}
return false;
}
}
}