Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 494fd27

Browse files
Don't crash when a mods Block has a BlockEntity but doesn't extend BlockEntityProvider (#145)
1 parent 657afda commit 494fd27

File tree

1 file changed

+33
-0
lines changed
  • patchwork-extensions-block/src/main/java/net/patchworkmc/mixin/extensions/block/blockentity

1 file changed

+33
-0
lines changed

patchwork-extensions-block/src/main/java/net/patchworkmc/mixin/extensions/block/blockentity/MixinWorldChunk.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919

2020
package net.patchworkmc.mixin.extensions.block.blockentity;
2121

22+
import javax.annotation.Nullable;
23+
24+
import org.spongepowered.asm.mixin.Final;
2225
import org.spongepowered.asm.mixin.Mixin;
26+
import org.spongepowered.asm.mixin.Shadow;
2327
import org.spongepowered.asm.mixin.Unique;
2428
import org.spongepowered.asm.mixin.injection.At;
2529
import org.spongepowered.asm.mixin.injection.Constant;
@@ -36,6 +40,7 @@
3640
import net.minecraft.block.entity.BlockEntity;
3741
import net.minecraft.util.math.BlockPos;
3842
import net.minecraft.world.BlockView;
43+
import net.minecraft.world.World;
3944
import net.minecraft.world.chunk.ChunkSection;
4045
import net.minecraft.world.chunk.WorldChunk;
4146

@@ -44,6 +49,17 @@
4449

4550
@Mixin(WorldChunk.class)
4651
public abstract class MixinWorldChunk {
52+
@Shadow
53+
@Nullable
54+
public abstract BlockEntity getBlockEntity(BlockPos pos, WorldChunk.CreationType creationType);
55+
56+
@Shadow
57+
@Final
58+
private World world;
59+
60+
@Shadow
61+
private volatile boolean shouldSave;
62+
4763
/**
4864
* @param blockState
4965
* @return the blockEntity created by IForgeBlock.createTileEntity(BlockState, World)
@@ -155,4 +171,21 @@ private BlockEntity patchwork_setBlockState_createBlockEntity(BlockEntityProvide
155171
private Block patchwork_setBlockEntity_getBlock(BlockState blockState) {
156172
return BlockContext.hasBlockEntityBlockMarker(blockState);
157173
}
174+
175+
// Workaround in setBlockState for Forge Blocks with BEs that don't extend BlockEntityProvider
176+
@Inject(method = "setBlockState", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/chunk/WorldChunk;getBlockEntity(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/world/chunk/WorldChunk$CreationType;)Lnet/minecraft/block/entity/BlockEntity;", ordinal = 1, shift = At.Shift.AFTER), cancellable = true)
177+
private void postGetBlockEntity(BlockPos pos, BlockState state, boolean bl, CallbackInfoReturnable<BlockState> cir) {
178+
if (!(state.getBlock() instanceof BlockEntityProvider)) {
179+
BlockEntity blockEntity = getBlockEntity(pos, WorldChunk.CreationType.CHECK);
180+
181+
if (blockEntity == null) {
182+
IForgeBlockState forgeBlockState = (IForgeBlockState) state;
183+
BlockEntity tileEntity = forgeBlockState.createTileEntity(world);
184+
world.setBlockEntity(pos, tileEntity);
185+
186+
this.shouldSave = true;
187+
cir.setReturnValue(state);
188+
}
189+
}
190+
}
158191
}

0 commit comments

Comments
 (0)