mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-22 21:20:12 +00:00
Add callback after a BlockState was changed and the neighbors were updated (#8617)
This commit is contained in:
1 parent
dbf1c8f10f
commit
28d71761cd
7 files changed
+148
-1
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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) { }
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MOD_ID);
|
||||
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MOD_ID);
|
||||
|
||||
private static final RegistryObject<Block> TEST_BLOCK = BLOCKS.register("test_block", TestBlock::new);
|
||||
private static final RegistryObject<Item> 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<Block, BlockState> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"variants": {
|
||||
"lit=false": {
|
||||
"model": "minecraft:block/redstone_lamp"
|
||||
},
|
||||
"lit=true": {
|
||||
"model": "minecraft:block/redstone_lamp_on"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"parent": "minecraft:item/redstone_lamp"
|
||||
}
|
||||
Reference in new issue
Block a user