From 2ab3d9b8a3c575805412fc9763db4b752882ac71 Mon Sep 17 00:00:00 2001 From: Dennis C Date: Tue, 28 Jun 2022 05:21:34 +0200 Subject: [PATCH] Add callback after a BlockState was changed and the neighbors were updated (#8686) --- .../net/minecraft/world/level/Level.java.patch | 4 +++- .../common/extensions/IForgeBlock.java | 13 +++++++++++++ .../common/extensions/IForgeBlockState.java | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/patches/minecraft/net/minecraft/world/level/Level.java.patch b/patches/minecraft/net/minecraft/world/level/Level.java.patch index 1a012953ab..1e87872f2f 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); -@@ -226,8 +_,6 @@ +@@ -225,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 ee83e1ee7d..2a36b8d51e 100644 --- a/src/main/java/net/minecraftforge/common/extensions/IForgeBlock.java +++ b/src/main/java/net/minecraftforge/common/extensions/IForgeBlock.java @@ -845,6 +845,19 @@ 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) { } + /** * Returns whether the block can be hydrated by a fluid. * diff --git a/src/main/java/net/minecraftforge/common/extensions/IForgeBlockState.java b/src/main/java/net/minecraftforge/common/extensions/IForgeBlockState.java index 8897c0fce6..31d465afaa 100644 --- a/src/main/java/net/minecraftforge/common/extensions/IForgeBlockState.java +++ b/src/main/java/net/minecraftforge/common/extensions/IForgeBlockState.java @@ -688,6 +688,21 @@ 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()); + } + /** * Returns whether the block can be hydrated by a fluid. *