|
| 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 | +} |
0 commit comments