Skip to content

Commit af7c0f1

Browse files
committed
Add sleeping CrafterBlockEntity
1 parent 7b96de9 commit af7c0f1

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@MixinConfigOption(description = "BlockEntity sleeping for inactive crafters"
2+
)
3+
package net.caffeinemc.mods.lithium.mixin.world.block_entity_ticking.sleeping.crafter;
4+
5+
import net.caffeinemc.gradle.MixinConfigOption;

common/src/main/resources/lithium.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
"world.block_entity_ticking.sleeping.campfire.CampfireBlockEntityMixin",
222222
"world.block_entity_ticking.sleeping.campfire.lit.CampfireBlockEntityMixin",
223223
"world.block_entity_ticking.sleeping.campfire.unlit.CampfireBlockEntityMixin",
224+
"world.block_entity_ticking.sleeping.crafter.CrafterBlockEntityMixin",
224225
"world.block_entity_ticking.sleeping.furnace.AbstractFurnaceBlockEntityMixin",
225226
"world.block_entity_ticking.sleeping.hopper.HopperBlockEntityMixin",
226227
"world.block_entity_ticking.sleeping.shulker_box.ShulkerBoxBlockEntityMixin",

lithium-fabric-mixin-config.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,10 @@ BlockEntity sleeping for inactive lit campfires
592592
(default: `true`)
593593
BlockEntity sleeping for inactive unlit campfires
594594

595+
### `mixin.world.block_entity_ticking.sleeping.crafter`
596+
(default: `true`)
597+
BlockEntity sleeping for inactive crafters
598+
595599
### `mixin.world.block_entity_ticking.sleeping.furnace`
596600
(default: `true`)
597601
BlockEntity sleeping for inactive furnaces

lithium-neoforge-mixin-config.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,10 @@ BlockEntity sleeping for inactive lit campfires
573573
(default: `true`)
574574
BlockEntity sleeping for inactive unlit campfires
575575

576+
### `mixin.world.block_entity_ticking.sleeping.crafter`
577+
(default: `true`)
578+
BlockEntity sleeping for inactive crafters
579+
576580
### `mixin.world.block_entity_ticking.sleeping.furnace`
577581
(default: `true`)
578582
BlockEntity sleeping for inactive furnaces

0 commit comments

Comments
 (0)