From 9683e5311591e725558b742b3812ff3d37e3ccbc Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 6 Sep 2024 21:00:32 +0700 Subject: [PATCH] Make computer terminal behave like redstone button --- .../mc/otm/datagen/lang/English.kt | 3 + .../mc/otm/datagen/lang/Russian.kt | 3 + .../dbotthepony/mc/otm/block/MatteryBlock.kt | 4 +- .../mc/otm/block/RotatableMatteryBlock.kt | 2 - .../block/decorative/ComputerTerminalBlock.kt | 121 ++++++++++++++++++ .../kotlin/ru/dbotthepony/mc/otm/core/Ext.kt | 3 +- .../dbotthepony/mc/otm/registry/MRegistry.kt | 10 +- 7 files changed, 132 insertions(+), 14 deletions(-) create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/block/decorative/ComputerTerminalBlock.kt diff --git a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/English.kt b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/English.kt index 0c37da5c8..fe18d65b0 100644 --- a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/English.kt +++ b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/English.kt @@ -58,6 +58,9 @@ private fun decoratives(provider: MatteryLanguageProvider) { } with(provider.english) { + misc("computer_terminal_tooltip", "Can be used as Redstone button, except it sends signal to block behind it, not under") + misc("decorative", "Decorative") + add(MItems.CARGO_CRATE_MINECARTS[null]!!, "Minecart with Cargo Crate") add(MEntityTypes.CARGO_CRATE_MINECARTS[null]!!, "Minecart with Cargo Crate") diff --git a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/Russian.kt b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/Russian.kt index 8844d4bd6..ac0d76029 100644 --- a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/Russian.kt +++ b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/Russian.kt @@ -67,6 +67,9 @@ private fun decoratives(provider: MatteryLanguageProvider) { } with(provider.russian) { + misc("computer_terminal_tooltip", "Может быть использован как кнопка, с оговоркой что он посылает сигнал блоку сзади, а не под ним") + misc("decorative", "Элемент декора") + add(MItems.CARGO_CRATE_MINECARTS[null]!!, "Вагонетка с грузовым ящиком") add(MEntityTypes.CARGO_CRATE_MINECARTS[null]!!, "Вагонетка с грузовым ящиком") diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/MatteryBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/MatteryBlock.kt index b8d5f6549..399054a81 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/MatteryBlock.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/MatteryBlock.kt @@ -50,10 +50,10 @@ fun Block.getShapeForEachState(properties: List>, fn: (BlockState) - val builder = Object2ObjectArrayMap>() if (properties.isEmpty()) { - val shape = Util.backgroundExecutor().submit(Callable { fn(stateDefinition.possibleStates.first()) }) + val shape = Util.backgroundExecutor().submit(Callable { fn(stateDefinition.possibleStates.first()) }).asSupplier() for (state in stateDefinition.possibleStates) { - builder[state] = shape.asSupplier() + builder[state] = shape } } else { val cache = Object2ObjectArrayMap, Supplier>() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/RotatableMatteryBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/RotatableMatteryBlock.kt index b2ecb1775..0ada6df7d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/RotatableMatteryBlock.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/RotatableMatteryBlock.kt @@ -51,9 +51,7 @@ open class RotatableMatteryBlock(properties: Properties = DEFAULT_PROPERTIES) : } } - @Suppress("OVERRIDE_DEPRECATION") override fun rotate(blockState: BlockState, rotation: Rotation): BlockState { - @Suppress("DEPRECATION") return super.rotate(blockState, rotation).setValue(rotationProperty, blockState[rotationProperty].rotate(rotation)) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/decorative/ComputerTerminalBlock.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/decorative/ComputerTerminalBlock.kt new file mode 100644 index 000000000..8b966a79f --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/decorative/ComputerTerminalBlock.kt @@ -0,0 +1,121 @@ +package ru.dbotthepony.mc.otm.block.decorative + +import net.minecraft.ChatFormatting +import net.minecraft.core.BlockPos +import net.minecraft.core.Direction +import net.minecraft.server.level.ServerLevel +import net.minecraft.sounds.SoundEvent +import net.minecraft.sounds.SoundEvents +import net.minecraft.sounds.SoundSource +import net.minecraft.util.RandomSource +import net.minecraft.world.InteractionResult +import net.minecraft.world.entity.player.Player +import net.minecraft.world.item.DyeColor +import net.minecraft.world.item.context.BlockPlaceContext +import net.minecraft.world.level.BlockGetter +import net.minecraft.world.level.Level +import net.minecraft.world.level.block.Block +import net.minecraft.world.level.block.SoundType +import net.minecraft.world.level.block.state.BlockState +import net.minecraft.world.level.block.state.StateDefinition +import net.minecraft.world.level.block.state.properties.BlockStateProperties +import net.minecraft.world.level.gameevent.GameEvent +import net.minecraft.world.level.material.MapColor +import net.minecraft.world.phys.BlockHitResult +import net.minecraft.world.phys.shapes.CollisionContext +import net.minecraft.world.phys.shapes.VoxelShape +import ru.dbotthepony.mc.otm.block.RotatableMatteryBlock +import ru.dbotthepony.mc.otm.block.getShapeForEachState +import ru.dbotthepony.mc.otm.core.TranslatableComponent +import ru.dbotthepony.mc.otm.core.get +import ru.dbotthepony.mc.otm.core.math.plus +import ru.dbotthepony.mc.otm.core.set +import ru.dbotthepony.mc.otm.shapes.BlockShapes + +class ComputerTerminalBlock(val color: DyeColor?) : RotatableMatteryBlock( + Properties.of() + .mapColor(color?.mapColor ?: MapColor.COLOR_LIGHT_BLUE) + .sound(SoundType.METAL) + .explosionResistance(15f) + .destroyTime(1.5f) +) { + private val shapes = getShapeForEachState(rotationProperty) { BlockShapes.COMPUTER_TERMINAL.rotateFromNorth(it[rotationProperty]).computeShape() } + + init { + registerDefaultState(defaultBlockState().set(BlockStateProperties.POWERED, false)) + } + + override fun createBlockStateDefinition(builder: StateDefinition.Builder) { + super.createBlockStateDefinition(builder) + builder.add(BlockStateProperties.POWERED) + } + + override fun getSignal(p_60483_: BlockState, p_60484_: BlockGetter, p_60485_: BlockPos, p_60486_: Direction): Int { + return if (p_60483_[BlockStateProperties.POWERED]) 15 else 0 + } + + override fun getDirectSignal( + p_60559_: BlockState, + p_60560_: BlockGetter, + p_60561_: BlockPos, + p_60562_: Direction + ): Int { + // weird that p_60559_[rotationProperty].front should be used and not p_60559_[rotationProperty].back + // minecraft engine shenanigans + return if (p_60559_[BlockStateProperties.POWERED] && p_60559_[rotationProperty].front == p_60562_) 15 else 0 + } + + override fun isSignalSource(p_60571_: BlockState): Boolean { + return true + } + + private fun updateNeighbours(blockState: BlockState, blockPos: BlockPos, level: Level) { + level.updateNeighborsAt(blockPos, this) + level.updateNeighborsAt(blockPos + blockState[rotationProperty].back, this) + } + + override fun useWithoutItem( + blockState: BlockState, + level: Level, + blockPos: BlockPos, + ply: Player, + blockHitResult: BlockHitResult + ): InteractionResult { + if (blockState[BlockStateProperties.POWERED]) { + return InteractionResult.CONSUME + } else { + level.setBlock(blockPos, blockState.set(BlockStateProperties.POWERED, true), UPDATE_ALL) + updateNeighbours(blockState, blockPos, level) + level.scheduleTick(blockPos, this, 40) + level.playSound(ply, blockPos, SoundEvents.STONE_BUTTON_CLICK_ON, SoundSource.BLOCKS, 1f, 1f) + level.gameEvent(ply, GameEvent.BLOCK_ACTIVATE, blockPos) + return InteractionResult.sidedSuccess(level.isClientSide) + } + } + + override fun tick(blockState: BlockState, level: ServerLevel, blockPos: BlockPos, random: RandomSource) { + super.tick(blockState, level, blockPos, random) + + if (blockState[BlockStateProperties.POWERED]) { + level.setBlock(blockPos, blockState.set(BlockStateProperties.POWERED, false), UPDATE_ALL) + level.playSound(null, blockPos, SoundEvents.STONE_BUTTON_CLICK_OFF, SoundSource.BLOCKS, 1f, 1f) + level.gameEvent(null, GameEvent.BLOCK_DEACTIVATE, blockPos) + updateNeighbours(blockState, blockPos, level) + } + } + + init { + tooltips.add(TranslatableComponent("otm.decorative").withStyle(ChatFormatting.GRAY)) + tooltips.add(TranslatableComponent("otm.computer_terminal_tooltip").withStyle(ChatFormatting.GRAY)) + tooltips.painted(color) + } + + override fun getShape( + state: BlockState, + blockGetter: BlockGetter, + pos: BlockPos, + context: CollisionContext + ): VoxelShape { + return shapes[state]!! + } +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt index 4f1c85c5f..6b7da446c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt @@ -248,8 +248,7 @@ operator fun StateHolder<*, *>.get(property: BlockRotationFreedom): BlockRotatio } operator fun , T : Comparable> S.set(property: Property, value: T): S { - setValue(property, value) - return this + return setValue(property, value) as S } fun List.toImmutableList(): List { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRegistry.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRegistry.kt index cad7fa32b..7c21ea9af 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRegistry.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRegistry.kt @@ -32,6 +32,7 @@ import ru.dbotthepony.mc.otm.android.AndroidFeatureType import ru.dbotthepony.mc.otm.android.feature.EnderTeleporterFeature import ru.dbotthepony.mc.otm.android.feature.NanobotsArmorFeature import ru.dbotthepony.mc.otm.block.decorative.CargoCrateBlock +import ru.dbotthepony.mc.otm.block.decorative.ComputerTerminalBlock import ru.dbotthepony.mc.otm.block.decorative.StarChairBlock import ru.dbotthepony.mc.otm.block.decorative.TritaniumPressurePlate import ru.dbotthepony.mc.otm.capability.matteryEnergy @@ -103,14 +104,7 @@ object MRegistry : IBlockItemRegistryAcceptor { .destroyTime(2.5f) }.also { decorativeBlocks.add(it) } - val COMPUTER_TERMINAL = DecorativeBlock.rotatable("computer_terminal", BlockShapes.COMPUTER_TERMINAL, BlockRotationFreedom.HORIZONTAL) { - BlockBehaviour.Properties.of() - .mapColor(it?.mapColor ?: MapColor.COLOR_LIGHT_BLUE) - .sound(SoundType.METAL) - .explosionResistance(15f) - .destroyTime(1.5f) - }.also { decorativeBlocks.add(it) } - + val COMPUTER_TERMINAL = DecorativeBlock("computer_terminal", ::ComputerTerminalBlock).also { decorativeBlocks.add(it) } val STAR_CHAIR = DecorativeBlock("star_chair", ::StarChairBlock).also { decorativeBlocks.add(it) } val TRITANIUM_STAIRS = DecorativeBlock(MNames.TRITANIUM_STAIRS) {