|
19 | 19 |
|
20 | 20 | package net.patchworkmc.mixin.extensions.block.blockentity;
|
21 | 21 |
|
| 22 | +import javax.annotation.Nullable; |
| 23 | + |
| 24 | +import org.spongepowered.asm.mixin.Final; |
22 | 25 | import org.spongepowered.asm.mixin.Mixin;
|
| 26 | +import org.spongepowered.asm.mixin.Shadow; |
23 | 27 | import org.spongepowered.asm.mixin.Unique;
|
24 | 28 | import org.spongepowered.asm.mixin.injection.At;
|
25 | 29 | import org.spongepowered.asm.mixin.injection.Constant;
|
|
36 | 40 | import net.minecraft.block.entity.BlockEntity;
|
37 | 41 | import net.minecraft.util.math.BlockPos;
|
38 | 42 | import net.minecraft.world.BlockView;
|
| 43 | +import net.minecraft.world.World; |
39 | 44 | import net.minecraft.world.chunk.ChunkSection;
|
40 | 45 | import net.minecraft.world.chunk.WorldChunk;
|
41 | 46 |
|
|
44 | 49 |
|
45 | 50 | @Mixin(WorldChunk.class)
|
46 | 51 | 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 | + |
47 | 63 | /**
|
48 | 64 | * @param blockState
|
49 | 65 | * @return the blockEntity created by IForgeBlock.createTileEntity(BlockState, World)
|
@@ -155,4 +171,21 @@ private BlockEntity patchwork_setBlockState_createBlockEntity(BlockEntityProvide
|
155 | 171 | private Block patchwork_setBlockEntity_getBlock(BlockState blockState) {
|
156 | 172 | return BlockContext.hasBlockEntityBlockMarker(blockState);
|
157 | 173 | }
|
| 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 | + } |
158 | 191 | }
|
0 commit comments