From 4dfb35e118a6c23f100803bdcd48439406e7989a Mon Sep 17 00:00:00 2001 From: QPCrummer <66036033+QPCrummer@users.noreply.github.com> Date: Mon, 27 May 2024 16:41:28 -0400 Subject: [PATCH] Implement lithium block entity sleeping --- build.gradle | 2 + .../AutoCraftingTableBlockEntity.java | 4 -- .../LithiumBlockEntityOptimizations.java | 71 +++++++++++++++++++ .../mixin/LithiumSleepControlMixin.java | 29 ++++++++ src/main/resources/autocrafter.mixins.json | 5 +- src/main/resources/fabric.mod.json | 5 +- 6 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/github/tatercertified/fabricautocrafter/mixin/LithiumBlockEntityOptimizations.java create mode 100644 src/main/java/com/github/tatercertified/fabricautocrafter/mixin/LithiumSleepControlMixin.java diff --git a/build.gradle b/build.gradle index 3c5e148..3ada218 100644 --- a/build.gradle +++ b/build.gradle @@ -28,6 +28,8 @@ dependencies { modImplementation("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}") modImplementation include("eu.pb4:polymer-core:0.8.0-beta.8+1.20.6") modImplementation include("xyz.nucleoid:server-translations-api:2.3.0+1.20.5-rc2") + modImplementation "maven.modrinth:lithium:mc1.20.6-0.12.3" + include(implementation("com.moulberry:mixinconstraints:1.0.1")) } loom { diff --git a/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableBlockEntity.java b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableBlockEntity.java index 5e93bd6..ce6e55f 100644 --- a/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableBlockEntity.java +++ b/src/main/java/com/github/tatercertified/fabricautocrafter/AutoCraftingTableBlockEntity.java @@ -14,7 +14,6 @@ import net.minecraft.registry.RegistryWrapper; import net.minecraft.screen.ScreenHandler; import net.minecraft.text.Text; -import net.minecraft.util.Identifier; import net.minecraft.util.ItemScatterer; import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.math.BlockPos; @@ -183,8 +182,6 @@ public RecipeEntry getLastRecipe() { return lastRecipe; } - - @Override public void clear() { this.inventory.clear(); @@ -220,7 +217,6 @@ private Optional getCurrentRecipe() { private ItemStack craft() { if (this.world == null) return ItemStack.EMPTY; final Optional optionalRecipe = getCurrentRecipe(); - System.out.println(optionalRecipe.isEmpty()); if (optionalRecipe.isEmpty()) return ItemStack.EMPTY; final CraftingRecipe recipe = optionalRecipe.get(); diff --git a/src/main/java/com/github/tatercertified/fabricautocrafter/mixin/LithiumBlockEntityOptimizations.java b/src/main/java/com/github/tatercertified/fabricautocrafter/mixin/LithiumBlockEntityOptimizations.java new file mode 100644 index 0000000..df3182c --- /dev/null +++ b/src/main/java/com/github/tatercertified/fabricautocrafter/mixin/LithiumBlockEntityOptimizations.java @@ -0,0 +1,71 @@ +package com.github.tatercertified.fabricautocrafter.mixin; + +import com.github.tatercertified.fabricautocrafter.AutoCraftingTableBlockEntity; +import com.moulberry.mixinconstraints.annotations.IfModLoaded; +import me.jellysquid.mods.lithium.common.block.entity.SleepingBlockEntity; +import me.jellysquid.mods.lithium.mixin.world.block_entity_ticking.sleeping.WrappedBlockEntityTickInvokerAccessor; +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.block.entity.LockableContainerBlockEntity; +import net.minecraft.inventory.SidedInventory; +import net.minecraft.recipe.RecipeInputProvider; +import net.minecraft.recipe.RecipeUnlocker; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraft.world.chunk.BlockEntityTickInvoker; +import org.spongepowered.asm.mixin.Mixin; + +@IfModLoaded(value = "lithium") +@Mixin(AutoCraftingTableBlockEntity.class) +public abstract class LithiumBlockEntityOptimizations extends LockableContainerBlockEntity implements SidedInventory, RecipeUnlocker, RecipeInputProvider, SleepingBlockEntity { + + // Block Entity Sleeping + private WrappedBlockEntityTickInvokerAccessor tickWrapper = null; + private BlockEntityTickInvoker sleepingTicker = null; + + protected LithiumBlockEntityOptimizations(BlockEntityType blockEntityType, BlockPos blockPos, BlockState blockState) { + super(blockEntityType, blockPos, blockState); + } + + @Override + public WrappedBlockEntityTickInvokerAccessor lithium$getTickWrapper() { + return this.tickWrapper; + } + + @Override + public void lithium$setTickWrapper(WrappedBlockEntityTickInvokerAccessor tickWrapper) { + this.tickWrapper = tickWrapper; + this.lithium$setSleepingTicker(null); + } + + @Override + public BlockEntityTickInvoker lithium$getSleepingTicker() { + return this.sleepingTicker; + } + + @Override + public void lithium$setSleepingTicker(BlockEntityTickInvoker sleepingTicker) { + this.sleepingTicker = sleepingTicker; + } + + @Override + public boolean lithium$startSleeping() { + if (this.isSleeping()) { + return false; + } + + WrappedBlockEntityTickInvokerAccessor tickWrapper = this.lithium$getTickWrapper(); + if (tickWrapper != null) { + this.lithium$setSleepingTicker(tickWrapper.getWrapped()); + tickWrapper.callSetWrapped(SleepingBlockEntity.SLEEPING_BLOCK_ENTITY_TICKER); + return true; + } + return false; + } + + @Override + public void setWorld(World world) { + super.setWorld(world); + lithium$startSleeping(); + } +} diff --git a/src/main/java/com/github/tatercertified/fabricautocrafter/mixin/LithiumSleepControlMixin.java b/src/main/java/com/github/tatercertified/fabricautocrafter/mixin/LithiumSleepControlMixin.java new file mode 100644 index 0000000..6399af2 --- /dev/null +++ b/src/main/java/com/github/tatercertified/fabricautocrafter/mixin/LithiumSleepControlMixin.java @@ -0,0 +1,29 @@ +package com.github.tatercertified.fabricautocrafter.mixin; + +import com.github.tatercertified.fabricautocrafter.AutoCraftingTableBlockEntity; +import com.github.tatercertified.fabricautocrafter.AutoCraftingTableContainer; +import com.moulberry.mixinconstraints.annotations.IfModLoaded; +import me.jellysquid.mods.lithium.common.block.entity.SleepingBlockEntity; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@IfModLoaded(value = "lithium") +@Mixin(AutoCraftingTableContainer.class) +public class LithiumSleepControlMixin { + + @Shadow + AutoCraftingTableBlockEntity blockEntity; + + @Inject(method = "onContentChanged", at = @At("TAIL")) + public void injectSleep(CallbackInfo ci) { + ((SleepingBlockEntity)blockEntity).lithium$startSleeping(); + } + + @Inject(method = "onContentChanged", at = @At("HEAD")) + public void injectWakeUp(CallbackInfo ci) { + ((SleepingBlockEntity)blockEntity).wakeUpNow(); + } +} diff --git a/src/main/resources/autocrafter.mixins.json b/src/main/resources/autocrafter.mixins.json index a7a7989..5368fa6 100644 --- a/src/main/resources/autocrafter.mixins.json +++ b/src/main/resources/autocrafter.mixins.json @@ -5,9 +5,12 @@ "mixins": [ "AccessorScreenHandler", "CraftingInventoryMixin", + "LithiumBlockEntityOptimizations", + "LithiumSleepControlMixin", "MixinHopperBlockEntity" ], "injectors": { "defaultRequire": 1 - } + }, + "plugin": "com.moulberry.mixinconstraints.ConstraintsMixinPlugin" } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index fd9675f..8d7fd31 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -25,6 +25,9 @@ "fabricloader": "*", "fabric": "*", "minecraft": ">=1.20.5", - "java": ">=17" + "java": ">=21" + }, + "recommends": { + "lithium": "*" } }