diff --git a/patches/minecraft/net/minecraft/world/level/Level.java.patch b/patches/minecraft/net/minecraft/world/level/Level.java.patch index de78cce4c5..40780ef4cb 100644 --- a/patches/minecraft/net/minecraft/world/level/Level.java.patch +++ b/patches/minecraft/net/minecraft/world/level/Level.java.patch @@ -71,9 +71,11 @@ if (blockstate1 == p_46606_) { if (blockstate != blockstate1) { this.m_6550_(p_46605_, blockstate, blockstate1); -@@ -216,8 +_,6 @@ +@@ -215,9 +_,8 @@ + } this.m_6559_(p_46605_, blockstate, blockstate1); ++ p_46606_.onBlockStateChange(this, p_46605_, blockstate); } - - return true; diff --git a/src/main/java/net/minecraftforge/common/extensions/IForgeBlock.java b/src/main/java/net/minecraftforge/common/extensions/IForgeBlock.java index 5718072395..e2aa48bf1e 100644 --- a/src/main/java/net/minecraftforge/common/extensions/IForgeBlock.java +++ b/src/main/java/net/minecraftforge/common/extensions/IForgeBlock.java @@ -832,4 +832,17 @@ public interface IForgeBlock } return true; } + + /** + * Called after the {@link BlockState} at the given {@link BlockPos} was changed and neighbors were updated. + * This method is called on the server and client side. + * Modifying the level is disallowed in this method. + * Useful for calculating additional data based on the new state and the neighbor's reactions to the state change. + * + * @param level The level the state was modified in + * @param pos The blocks position in the level + * @param oldState The previous state of the block at the given position, may be a different block than this one + * @param newState The new state of the block at the given position + */ + default void onBlockStateChange(LevelReader level, BlockPos pos, BlockState oldState, BlockState newState) { } } diff --git a/src/main/java/net/minecraftforge/common/extensions/IForgeBlockState.java b/src/main/java/net/minecraftforge/common/extensions/IForgeBlockState.java index d331e9109e..ea1af2e092 100644 --- a/src/main/java/net/minecraftforge/common/extensions/IForgeBlockState.java +++ b/src/main/java/net/minecraftforge/common/extensions/IForgeBlockState.java @@ -696,4 +696,19 @@ public interface IForgeBlockState { return self().getBlock().supportsExternalFaceHiding(self()); } + + /** + * Called after the {@link BlockState} at the given {@link BlockPos} was changed and neighbors were updated. + * This method is called on the server and client side. + * Modifying the level is disallowed in this method. + * Useful for calculating additional data based on the new state and the neighbor's reactions to the state change. + * + * @param level The level the state was modified in + * @param pos The blocks position in the level + * @param oldState The previous state of the block at the given position, may be a different block than this one + */ + default void onBlockStateChange(LevelReader level, BlockPos pos, BlockState oldState) + { + self().getBlock().onBlockStateChange(level, pos, oldState, self()); + } } diff --git a/src/test/java/net/minecraftforge/debug/block/BlockStateChangeTest.java b/src/test/java/net/minecraftforge/debug/block/BlockStateChangeTest.java new file mode 100644 index 0000000000..6a0ad97c6d --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/block/BlockStateChangeTest.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.debug.block; + +import com.mojang.logging.LogUtils; +import net.minecraft.core.BlockPos; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.item.CreativeModeTab; +import net.minecraft.world.item.Item; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.LevelReader; +import net.minecraft.world.level.block.Block; +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.material.Material; +import net.minecraft.world.phys.BlockHitResult; +import net.minecraftforge.eventbus.api.IEventBus; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.registries.DeferredRegister; +import net.minecraftforge.registries.ForgeRegistries; +import net.minecraftforge.registries.RegistryObject; +import org.slf4j.Logger; + +/** + * This test mod tests the blockstate update callback which is called after a block's state has changed and neighbors + * have been notified. + * + * To test the callback, place the "blockstate_change_test:test_block" and right click it twice. Each of these three + * actions must print two log lines, one on the server and one on the client side. Both lines must be equivalent execpt + * for the side. + * When placing, the log must show a change from "{minecraft:air}" to "{blockstate_change_test:test_block}[lit=false]". + * When interacting with the block, the log must show a change from "{blockstate_change_test:test_block}[lit=false]" to + * "{blockstate_change_test:test_block}[lit=true]" or viceversa in accordance with the change of the visual state of + * the block in-game. + */ +@Mod(BlockStateChangeTest.MOD_ID) +public class BlockStateChangeTest +{ + public static final String MOD_ID = "blockstate_change_test"; + + private static final Logger LOGGER = LogUtils.getLogger(); + private static final boolean ENABLED = true; + + private static final DeferredRegister BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MOD_ID); + private static final DeferredRegister ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MOD_ID); + + private static final RegistryObject TEST_BLOCK = BLOCKS.register("test_block", TestBlock::new); + private static final RegistryObject TEST_BLOCK_ITEM = ITEMS.register( + "test_block", + () -> new BlockItem(TEST_BLOCK.get(), new Item.Properties().tab(CreativeModeTab.TAB_MISC)) + ); + + public BlockStateChangeTest() + { + if (ENABLED) + { + IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus(); + BLOCKS.register(bus); + ITEMS.register(bus); + } + } + + private static class TestBlock extends Block + { + public TestBlock() + { + super(Properties.of(Material.STONE)); + registerDefaultState(defaultBlockState().setValue(BlockStateProperties.LIT, false)); + } + + @Override + protected void createBlockStateDefinition(StateDefinition.Builder builder) + { + builder.add(BlockStateProperties.LIT); + } + + @Override + public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) + { + if (!level.isClientSide()) + { + boolean lit = state.getValue(BlockStateProperties.LIT); + level.setBlockAndUpdate(pos, state.setValue(BlockStateProperties.LIT, !lit)); + } + return InteractionResult.sidedSuccess(level.isClientSide()); + } + + @Override + public void onBlockStateChange(LevelReader level, BlockPos pos, BlockState oldState, BlockState newState) + { + LOGGER.info("TestBlock at {} changed from {} to {} on side '{}'", pos, oldState, newState, level.isClientSide() ? "CLIENT" : "SERVER"); + } + } +} diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index 04ec601c9b..692741a286 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -230,5 +230,7 @@ license="LGPL v2.1" modId="banner_pattern_test" [[mods]] modId="mod_mismatch_test" +[[mods]] + modId="blockstate_change_test" # ADD ABOVE THIS LINE diff --git a/src/test/resources/assets/blockstate_change_test/blockstates/test_block.json b/src/test/resources/assets/blockstate_change_test/blockstates/test_block.json new file mode 100644 index 0000000000..bbd9d93559 --- /dev/null +++ b/src/test/resources/assets/blockstate_change_test/blockstates/test_block.json @@ -0,0 +1,10 @@ +{ + "variants": { + "lit=false": { + "model": "minecraft:block/redstone_lamp" + }, + "lit=true": { + "model": "minecraft:block/redstone_lamp_on" + } + } +} \ No newline at end of file diff --git a/src/test/resources/assets/blockstate_change_test/models/item/test_block.json b/src/test/resources/assets/blockstate_change_test/models/item/test_block.json new file mode 100644 index 0000000000..0a7c3db6af --- /dev/null +++ b/src/test/resources/assets/blockstate_change_test/models/item/test_block.json @@ -0,0 +1,3 @@ +{ + "parent": "minecraft:item/redstone_lamp" +} \ No newline at end of file