|
| 1 | +package net.caffeinemc.mods.lithium.mixin.world.block_entity_ticking.sleeping.crafter; |
| 2 | + |
| 3 | +import com.llamalad7.mixinextras.sugar.Local; |
| 4 | +import net.caffeinemc.mods.lithium.common.block.entity.SetChangedHandlingBlockEntity; |
| 5 | +import net.caffeinemc.mods.lithium.common.block.entity.SleepingBlockEntity; |
| 6 | +import net.caffeinemc.mods.lithium.mixin.world.block_entity_ticking.sleeping.WrappedBlockEntityTickInvokerAccessor; |
| 7 | +import net.minecraft.core.BlockPos; |
| 8 | +import net.minecraft.world.level.block.entity.BlockEntity; |
| 9 | +import net.minecraft.world.level.block.entity.BlockEntityType; |
| 10 | +import net.minecraft.world.level.block.entity.CrafterBlockEntity; |
| 11 | +import net.minecraft.world.level.block.entity.TickingBlockEntity; |
| 12 | +import net.minecraft.world.level.block.state.BlockState; |
| 13 | +import org.spongepowered.asm.mixin.Mixin; |
| 14 | +import org.spongepowered.asm.mixin.Shadow; |
| 15 | +import org.spongepowered.asm.mixin.Unique; |
| 16 | +import org.spongepowered.asm.mixin.injection.At; |
| 17 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 18 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 19 | + |
| 20 | +@Mixin(CrafterBlockEntity.class) |
| 21 | +public class CrafterBlockEntityMixin extends BlockEntity implements SleepingBlockEntity, SetChangedHandlingBlockEntity { |
| 22 | + |
| 23 | + @Shadow |
| 24 | + private int craftingTicksRemaining; |
| 25 | + @Unique |
| 26 | + private WrappedBlockEntityTickInvokerAccessor tickWrapper = null; |
| 27 | + @Unique |
| 28 | + private TickingBlockEntity sleepingTicker = null; |
| 29 | + |
| 30 | + public CrafterBlockEntityMixin(BlockEntityType<?> blockEntityType, BlockPos blockPos, BlockState blockState) { |
| 31 | + super(blockEntityType, blockPos, blockState); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public WrappedBlockEntityTickInvokerAccessor lithium$getTickWrapper() { |
| 36 | + return this.tickWrapper; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void lithium$setTickWrapper(WrappedBlockEntityTickInvokerAccessor tickWrapper) { |
| 41 | + this.tickWrapper = tickWrapper; |
| 42 | + this.lithium$setSleepingTicker(null); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public TickingBlockEntity lithium$getSleepingTicker() { |
| 47 | + return this.sleepingTicker; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void lithium$setSleepingTicker(TickingBlockEntity sleepingTicker) { |
| 52 | + this.sleepingTicker = sleepingTicker; |
| 53 | + } |
| 54 | + |
| 55 | + @Inject(method = "serverTick", at = @At("RETURN")) |
| 56 | + private static void checkSleep(CallbackInfo ci, @Local(argsOnly = true) CrafterBlockEntity crafterBlockEntity, @Local int remainingTicks) { |
| 57 | + if (remainingTicks < 0) { |
| 58 | + ((CrafterBlockEntityMixin) (Object) crafterBlockEntity).checkSleep(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Unique |
| 63 | + private void checkSleep() { |
| 64 | + if (this.craftingTicksRemaining == 0) { |
| 65 | + this.lithium$startSleeping(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Inject( |
| 70 | + method = { |
| 71 | + "loadAdditional(Lnet/minecraft/nbt/CompoundTag;Lnet/minecraft/core/HolderLookup$Provider;)V", |
| 72 | + "setCraftingTicksRemaining" |
| 73 | + }, |
| 74 | + at = @At("RETURN") |
| 75 | + ) |
| 76 | + private void wakeUpAfterRemainingTicksChanged(CallbackInfo ci) { |
| 77 | + if (this.isSleeping() && this.level != null && !this.level.isClientSide) { |
| 78 | + this.wakeUpNow(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void lithium$handleSetChanged() { |
| 84 | + if (this.isSleeping() && this.level != null && !this.level.isClientSide) { |
| 85 | + this.wakeUpNow(); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments