Skip to content

Commit 847afd3

Browse files
committed
add test multiblock to example mod (closes #3)
1 parent 8cfc20b commit 847afd3

5 files changed

Lines changed: 150 additions & 5 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using Common.Mod.Blocks;
2+
using Vintagestory.API.Common;
3+
using Vintagestory.API.MathTools;
4+
using Vintagestory.GameContent;
5+
6+
namespace Common.Mod.Example.BlockBehaviors;
7+
8+
public class TestMultiblockBlockBehavior : StrongBlockBehavior, IMultiBlockColSelBoxes, IMultiBlockParticleColBoxes
9+
{
10+
public const string RegistryId = "TestMultiblock";
11+
12+
public TestMultiblockBlockBehavior(Block block) : base(block)
13+
{
14+
}
15+
16+
public override bool CanPlaceBlock(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling, ref string failureCode)
17+
{
18+
var topBlockSel = blockSel.AddPosCopy(0, 1, 0);
19+
20+
var bottomBlockPlaceable = world.BlockAccessor.GetBlock(blockSel.Position).IsReplacableBy(block);
21+
var topBlockPlaceable = world.BlockAccessor.GetBlock(topBlockSel.Position).IsReplacableBy(block);
22+
23+
if (!bottomBlockPlaceable || !topBlockPlaceable)
24+
{
25+
handling = EnumHandling.PreventDefault;
26+
failureCode = "notenoughspace";
27+
return false;
28+
}
29+
30+
return base.CanPlaceBlock(world, byPlayer, blockSel, ref handling, ref failureCode);
31+
}
32+
33+
public override bool TryPlaceBlock(
34+
IWorldAccessor world,
35+
IPlayer byPlayer,
36+
ItemStack itemstack,
37+
BlockSelection blockSel,
38+
ref EnumHandling handling,
39+
ref string failureCode
40+
)
41+
{
42+
handling = EnumHandling.PreventDefault;
43+
44+
if (!block.CanPlaceBlock(world, byPlayer, blockSel, ref failureCode))
45+
{
46+
return false;
47+
}
48+
49+
world.BlockAccessor.SetBlock(block.Id, blockSel.Position);
50+
51+
if (world.Side is EnumAppSide.Server)
52+
{
53+
var assetLocation = new AssetLocation("common:multiblock-monolithic-0-p1-0");
54+
var topBlockSel = blockSel.AddPosCopy(0, 1, 0);
55+
world.BlockAccessor.SetBlock(world.GetBlock(assetLocation).Id, topBlockSel.Position);
56+
world.BlockAccessor.TriggerNeighbourBlockUpdate(topBlockSel.Position);
57+
}
58+
59+
return true;
60+
}
61+
62+
public override void OnBlockRemoved(IWorldAccessor world, BlockPos pos, ref EnumHandling handling)
63+
{
64+
if (world.Side is EnumAppSide.Client)
65+
{
66+
return;
67+
}
68+
69+
var topBlockPos = pos.Add(0, 1, 0);
70+
if (world.BlockAccessor.GetBlock(topBlockPos) is BlockMultiblock)
71+
{
72+
world.BlockAccessor.SetBlock(0, topBlockPos);
73+
if (world.Side is EnumAppSide.Server)
74+
{
75+
world.BlockAccessor.TriggerNeighbourBlockUpdate(topBlockPos);
76+
}
77+
}
78+
79+
base.OnBlockRemoved(world, pos, ref handling);
80+
}
81+
82+
public override Cuboidf[] GetCollisionBoxes(IBlockAccessor blockAccessor, BlockPos pos, ref EnumHandling handled)
83+
{
84+
handled = EnumHandling.PreventSubsequent;
85+
return block.SelectionBoxes;
86+
}
87+
88+
public override Cuboidf[] GetSelectionBoxes(IBlockAccessor blockAccessor, BlockPos pos, ref EnumHandling handled)
89+
{
90+
handled = EnumHandling.PreventSubsequent;
91+
return block.SelectionBoxes;
92+
}
93+
94+
public override Cuboidf[] GetParticleCollisionBoxes(IBlockAccessor blockAccessor, BlockPos pos, ref EnumHandling handled)
95+
{
96+
handled = EnumHandling.PreventSubsequent;
97+
return block.SelectionBoxes;
98+
}
99+
100+
public Cuboidf[] MBGetCollisionBoxes(IBlockAccessor blockAccessor, BlockPos pos, Vec3i offset)
101+
{
102+
return block.SelectionBoxes.Select(box => box.OffsetCopy(offset)).ToArray();
103+
}
104+
105+
public Cuboidf[] MBGetSelectionBoxes(IBlockAccessor blockAccessor, BlockPos pos, Vec3i offset)
106+
{
107+
return block.SelectionBoxes.Select(box => box.OffsetCopy(offset)).ToArray();
108+
}
109+
110+
public Cuboidf[] MBGetParticleCollisionBoxes(IBlockAccessor blockAccessor, BlockPos pos, Vec3i offset)
111+
{
112+
return block.SelectionBoxes.Select(box => box.OffsetCopy(offset)).ToArray();
113+
}
114+
}

Common.Mod.Example/Common.Mod.Example.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
<HintPath>../vendor/VintagestoryAPI.dll</HintPath>
1616
<Private>false</Private>
1717
</Reference>
18+
<Reference Include="VSEssentials">
19+
<HintPath>../vendor/VSEssentials.dll</HintPath>
20+
<Private>false</Private>
21+
</Reference>
1822

1923
<Reference Include="protobuf-net">
2024
<HintPath>../vendor/protobuf-net.dll</HintPath>

Common.Mod.Example/ExampleSystem.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Common.Mod.Common.Config;
2+
using Common.Mod.Example.BlockBehaviors;
23
using JetBrains.Annotations;
34
using Vintagestory.API.Common;
45

@@ -19,4 +20,10 @@ protected override void RegisterConfigs(IConfigSystem configSystem)
1920
configSystem.Register<ExampleServerConfig>();
2021
configSystem.Register<ExampleClientConfig>();
2122
}
23+
24+
protected override void RegisterClasses(ICoreAPI api)
25+
{
26+
base.RegisterClasses(api);
27+
RegisterBlockBehaviorClass<TestMultiblockBlockBehavior>(api, TestMultiblockBlockBehavior.RegistryId);
28+
}
2229
}

Common.Mod.Example/assets/example/blocktypes/test-multiblock.json

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"code": "test-multiblock",
33
"class": "BlockGeneric",
4+
"behaviors": [
5+
{
6+
"name": "example:TestMultiblock"
7+
}
8+
],
49
"creativeInventory": {
510
"general": [
611
"*"
@@ -12,5 +17,23 @@
1217
"drawType": "JSON",
1318
"shape": {
1419
"base": "example:block/test-multiblock"
15-
}
20+
},
21+
"selectionBoxes": [
22+
{
23+
"x1": 0,
24+
"y1": 0,
25+
"z1": 0,
26+
"x2": 1,
27+
"y2": 1,
28+
"z2": 1
29+
},
30+
{
31+
"x1": 0.0625,
32+
"y1": 1,
33+
"z1": 0.0625,
34+
"x2": 0.9375,
35+
"y2": 1.875,
36+
"z2": 0.9375
37+
}
38+
]
1639
}

Common.Mod/System.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,7 @@ protected virtual void ClientStartPre(ICoreClientAPI api)
217217
[UsedImplicitly]
218218
protected virtual void RegisterClasses(ICoreAPI api)
219219
{
220-
if (api.ClassRegistry.GetBlockClass($"common:{MultiblockBlock.RegistryId}") is null)
221-
{
222-
api.RegisterBlockClass($"common:{MultiblockBlock.RegistryId}", typeof(MultiblockBlock));
223-
}
220+
api.RegisterBlockClass($"common:{MultiblockBlock.RegistryId}", typeof(MultiblockBlock));
224221
}
225222

226223
#endregion Virtuals

0 commit comments

Comments
 (0)