From 1f72cd865cfacc9a33e59a4a075c4db94e8a341e Mon Sep 17 00:00:00 2001 From: YuRaNnNzZZ Date: Thu, 26 Oct 2023 08:53:53 +0300 Subject: [PATCH] emit events and particles on chest upgrade --- .../ru/dbotthepony/mc/otm/item/ChestUpgraderItem.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/ChestUpgraderItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/ChestUpgraderItem.kt index f4aefddff..efba143a0 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/ChestUpgraderItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/ChestUpgraderItem.kt @@ -17,6 +17,7 @@ import net.minecraft.world.level.Level import net.minecraft.world.level.block.Block import net.minecraft.world.level.block.entity.BarrelBlockEntity import net.minecraft.world.level.block.entity.ChestBlockEntity +import net.minecraft.world.level.gameevent.GameEvent import net.minecraft.world.level.storage.loot.LootParams import net.minecraft.world.level.storage.loot.parameters.LootContextParams import ru.dbotthepony.mc.otm.OverdriveThatMatters.MOD_ID @@ -47,9 +48,14 @@ class ChestUpgraderItem : Item(Properties().stacksTo(1)) { if (container.containerSize >= 54) return super.onItemUseFirst(stack, context) - if (context.level is ServerLevel) { - val newState = block.getStateForPlacement(BlockPlaceContext(context)) ?: return InteractionResult.FAIL + val blockState = context.level.getBlockState(pos) + val newState = block.getStateForPlacement(BlockPlaceContext(context)) ?: return InteractionResult.FAIL + if (!newState.canSurvive(context.level, pos)) return InteractionResult.FAIL + context.level.gameEvent(GameEvent.BLOCK_DESTROY, pos, GameEvent.Context.of(player, blockState)) + context.level.levelEvent(player, 2001, pos, Block.getId(blockState)) + + if (context.level is ServerLevel) { val contents = Int2ObjectArrayMap(container.containerSize) for (i in 0 until container.containerSize) { contents.put(i, container.getItem(i)) @@ -58,7 +64,6 @@ class ChestUpgraderItem : Item(Properties().stacksTo(1)) { val level = context.level as ServerLevel - val blockState = level.getBlockState(pos) val lootparams = LootParams.Builder(level) .withParameter(LootContextParams.ORIGIN, Vector.atCenterOf(context.clickedPos)) .withParameter(LootContextParams.TOOL, stack) @@ -86,6 +91,8 @@ class ChestUpgraderItem : Item(Properties().stacksTo(1)) { } } + context.level.gameEvent(GameEvent.BLOCK_PLACE, pos, GameEvent.Context.of(player, newState)) + return InteractionResult.SUCCESS }