From 71aaf98bb1ae870ec8894ca47c3746606509d760 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 19 Jan 2022 08:51:58 +0700 Subject: [PATCH] Move gravitation stabilizer rotation on placement to block --- .../otm/block/BlockGravitationStabilizer.kt | 29 +++++++++++++++++++ .../BlockEntityGravitationStabilizer.kt | 25 ---------------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/BlockGravitationStabilizer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/BlockGravitationStabilizer.kt index d7289fa89..979b73e0e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/BlockGravitationStabilizer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/BlockGravitationStabilizer.kt @@ -1,6 +1,8 @@ package ru.dbotthepony.mc.otm.block import net.minecraft.core.BlockPos +import net.minecraft.core.SectionPos +import net.minecraft.world.item.context.BlockPlaceContext import net.minecraft.world.level.Level import net.minecraft.world.level.block.Block import net.minecraft.world.level.block.EntityBlock @@ -13,7 +15,10 @@ import net.minecraft.world.level.material.Material import net.minecraft.world.level.material.MaterialColor import ru.dbotthepony.mc.otm.Registry import ru.dbotthepony.mc.otm.block.entity.BlockEntityGravitationStabilizer +import ru.dbotthepony.mc.otm.block.entity.blackhole.BlockEntityBlackHole import ru.dbotthepony.mc.otm.block.entity.worker.WorkerState +import ru.dbotthepony.mc.otm.core.plus +import ru.dbotthepony.mc.otm.core.times class BlockGravitationStabilizer : BlockMatteryRotatable(Properties.of(Material.STONE, MaterialColor.COLOR_BLUE).requiresCorrectToolForDrops().strength(3f, 600.0f)), EntityBlock { override fun newBlockEntity(p_153215_: BlockPos, p_153216_: BlockState): BlockEntity { @@ -37,4 +42,28 @@ class BlockGravitationStabilizer : BlockMatteryRotatable(Properties.of(Material. super.createBlockStateDefinition(builder) builder.add(WorkerState.SEMI_WORKER_STATE) } + + override fun getStateForPlacement(context: BlockPlaceContext): BlockState? { + var state = super.getStateForPlacement(context) ?: return null + val blockPos = context.clickedPos + val level = context.level + + for (face in FACING_FULL.possibleValues) { + val dir = face.normal + + for (i in 1 ..BlockEntityGravitationStabilizer.RANGE) { + val pos = blockPos + dir * i + val chunk = level.chunkSource.getChunkNow(SectionPos.blockToSectionCoord(pos.x), SectionPos.blockToSectionCoord(pos.z)) ?: continue + val getState = chunk.getBlockState(pos) + + if (!getState.isAir) { + if (chunk.getBlockEntity(pos) is BlockEntityBlackHole) { + state = state.setValue(FACING_FULL, face) + } + } + } + } + + return state + } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BlockEntityGravitationStabilizer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BlockEntityGravitationStabilizer.kt index 5e366b045..a02b9577a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BlockEntityGravitationStabilizer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BlockEntityGravitationStabilizer.kt @@ -62,31 +62,6 @@ class BlockEntityGravitationStabilizer(p_155229_: BlockPos, p_155230_: BlockStat blackHole?.stabilizerDetached(this) } - override fun setLevel(level: Level) { - super.setLevel(level) - - if (!level.isClientSide) { - OverdriveThatMatters.tickOnce(level) { - for (face in BlockMatteryRotatable.FACING_FULL.possibleValues) { - val dir = face.normal - - for (i in 1 .. RANGE) { - val pos = blockPos + dir * i - val chunk = level.chunkSource.getChunkNow(SectionPos.blockToSectionCoord(pos.x), SectionPos.blockToSectionCoord(pos.z)) ?: continue - val state = chunk.getBlockState(pos) - - if (!state.isAir) { - if (chunk.getBlockEntity(pos) is BlockEntityBlackHole) { - level.setBlock(blockPos, blockState.setValue(BlockMatteryRotatable.FACING_FULL, face), Block.UPDATE_ALL) - return@tickOnce - } - } - } - } - } - } - } - companion object { const val RANGE = 64 private val NAME = TranslatableComponent("block.overdrive_that_matters.gravitation_stabilizer")