public static void TryPlaceBlock(BlockAngledGears __instance, IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref string failureCode, ref bool __result) { if (CheckAppSideAnywhere.Side == EnumAppSide.Server) { return; } __result = Fix(__instance, world, byPlayer, blockSel, ref failureCode); }
public static bool Fix(BlockAngledGears bAg, IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref string failureCode) { Block blockExisting = world.BlockAccessor.GetBlock(blockSel.Position); if (!bAg.CanPlaceBlock(world, byPlayer, blockSel, ref failureCode, blockExisting)) { return(false); } BlockFacing firstFace = null; BlockFacing secondFace = null; BlockMPMultiblockGear largeGearEdge = blockExisting as BlockMPMultiblockGear; bool validLargeGear = false; if (largeGearEdge != null) { BEMPMultiblock be = world.BlockAccessor.GetBlockEntity(blockSel.Position) as BEMPMultiblock; if (be != null) { validLargeGear = be.Centre != null; } } foreach (BlockFacing face in BlockFacing.ALLFACES) { if (validLargeGear && (face == BlockFacing.UP || face == BlockFacing.DOWN)) { continue; } BlockPos pos = blockSel.Position.AddCopy(face); IMechanicalPowerBlock block = world.BlockAccessor.GetBlock(pos) as IMechanicalPowerBlock; if (block != null && block.HasMechPowerConnectorAt(world, pos, face.Opposite)) { if (firstFace == null) { firstFace = face; } else { if (face.IsAdjacent(firstFace)) { secondFace = face; break; } } } } if (firstFace != null) { BlockPos firstPos = blockSel.Position.AddCopy(firstFace); BlockEntity be = world.BlockAccessor.GetBlockEntity(firstPos); IMechanicalPowerBlock neighbour = be?.Block as IMechanicalPowerBlock; BEBehaviorMPAxle bempaxle = be?.GetBehavior <BEBehaviorMPAxle>(); if (bempaxle != null && !BEBehaviorMPAxle.IsAttachedToBlock(world.BlockAccessor, neighbour as Block, firstPos)) { failureCode = "axlemusthavesupport"; return(false); } BlockEntity largeGearBE = validLargeGear ? largeGearEdge.GearPlaced(world, blockSel.Position) : null; Block toPlaceBlock = bAg.getGearBlock(world, validLargeGear, firstFace, secondFace); //world.BlockAccessor.RemoveBlockEntity(blockSel.Position); //## needed in 1.12, but not with new chunk BlockEntity Dictionary in 1.13 world.BlockAccessor.SetBlock(toPlaceBlock.BlockId, blockSel.Position); if (secondFace != null) { BlockPos secondPos = blockSel.Position.AddCopy(secondFace); IMechanicalPowerBlock neighbour2 = world.BlockAccessor.GetBlock(secondPos) as IMechanicalPowerBlock; neighbour2?.DidConnectAt(world, secondPos, secondFace.Opposite); } BEBehaviorMPAngledGears beAngledGear = world.BlockAccessor.GetBlockEntity(blockSel.Position)?.GetBehavior <BEBehaviorMPAngledGears>(); if (largeGearBE?.GetBehavior <BEBehaviorMPBase>() is BEBehaviorMPLargeGear3m largeGear) { beAngledGear.AddToLargeGearNetwork(largeGear, firstFace); } //do this last even for the first face so that both neighbours are correctly set neighbour?.DidConnectAt(world, firstPos, firstFace.Opposite); if (beAngledGear != null) { beAngledGear.newlyPlaced = true; if (!beAngledGear.tryConnect(firstFace) && secondFace != null) { beAngledGear.tryConnect(secondFace); } beAngledGear.newlyPlaced = false; } return(true); } failureCode = "requiresaxle"; return(false); }