|
| 1 | +using JetBrains.Annotations; |
| 2 | +using Vintagestory.API.Common; |
| 3 | +using Vintagestory.API.MathTools; |
| 4 | +using Vintagestory.GameContent; |
| 5 | + |
| 6 | +namespace Common.Mod.Blocks; |
| 7 | + |
| 8 | +// TODO: Remove this class if https://github.com/anegostudios/vsessentialsmod/pull/31 gets merged |
| 9 | +[UsedImplicitly] |
| 10 | +public class MultiblockBlock : BlockMultiblock |
| 11 | +{ |
| 12 | + public const string RegistryId = "MultiblockBlock"; |
| 13 | + |
| 14 | + public override Cuboidf[] GetParticleCollisionBoxes(IBlockAccessor blockAccessor, BlockPos pos) |
| 15 | + { |
| 16 | + return Handle<Cuboidf[], IMultiBlockParticleColBoxes>( |
| 17 | + blockAccessor, |
| 18 | + pos.X + OffsetInv.X, pos.InternalY + OffsetInv.Y, pos.Z + OffsetInv.Z, |
| 19 | + (inf) => inf.MBGetParticleCollisionBoxes(blockAccessor, pos, OffsetInv), |
| 20 | + _ => [Cuboidf.Default()], |
| 21 | + block => block.GetParticleCollisionBoxes(blockAccessor, pos.AddCopy(OffsetInv)) |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + // This is copy-pasted from the `BlockMultiblock` class where it is private |
| 26 | + private static T Handle<T, TK>( |
| 27 | + IBlockAccessor blockAccessor, |
| 28 | + int x, |
| 29 | + int y, |
| 30 | + int z, |
| 31 | + BlockCallDelegateInterface<T, TK> onImplementsInterface, |
| 32 | + BlockCallDelegateBlock<T> onIsMultiblock, |
| 33 | + BlockCallDelegateBlock<T> onOtherwise |
| 34 | + ) |
| 35 | + where TK : class |
| 36 | + { |
| 37 | + var block = blockAccessor.GetBlock((new Vec3i(x, y, z)).ToBlockPos()); |
| 38 | + var blockInf = block as TK; |
| 39 | + |
| 40 | + if (blockInf == null) |
| 41 | + { |
| 42 | + blockInf = block.GetBehavior(typeof(TK), true) as TK; |
| 43 | + } |
| 44 | + |
| 45 | + if (blockInf != null) |
| 46 | + { |
| 47 | + return onImplementsInterface(blockInf); |
| 48 | + } |
| 49 | + |
| 50 | + // This is to prevent endless recursion in situations where blocks become incorrectly arranged |
| 51 | + if (block is BlockMultiblock) |
| 52 | + { |
| 53 | + return onIsMultiblock(block); |
| 54 | + } |
| 55 | + |
| 56 | + return onOtherwise(block); |
| 57 | + } |
| 58 | +} |
0 commit comments