Skip to content

Commit

Permalink
Implement lithium block entity sleeping
Browse files Browse the repository at this point in the history
  • Loading branch information
QPCrummer committed May 27, 2024
1 parent 4ee804d commit 4dfb35e
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 6 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -183,8 +182,6 @@ public RecipeEntry<?> getLastRecipe() {
return lastRecipe;
}



@Override
public void clear() {
this.inventory.clear();
Expand Down Expand Up @@ -220,7 +217,6 @@ private Optional<CraftingRecipe> getCurrentRecipe() {
private ItemStack craft() {
if (this.world == null) return ItemStack.EMPTY;
final Optional<CraftingRecipe> optionalRecipe = getCurrentRecipe();
System.out.println(optionalRecipe.isEmpty());
if (optionalRecipe.isEmpty()) return ItemStack.EMPTY;

final CraftingRecipe recipe = optionalRecipe.get();
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
5 changes: 4 additions & 1 deletion src/main/resources/autocrafter.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
"mixins": [
"AccessorScreenHandler",
"CraftingInventoryMixin",
"LithiumBlockEntityOptimizations",
"LithiumSleepControlMixin",
"MixinHopperBlockEntity"
],
"injectors": {
"defaultRequire": 1
}
},
"plugin": "com.moulberry.mixinconstraints.ConstraintsMixinPlugin"
}
5 changes: 4 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"fabricloader": "*",
"fabric": "*",
"minecraft": ">=1.20.5",
"java": ">=17"
"java": ">=21"
},
"recommends": {
"lithium": "*"
}
}

0 comments on commit 4dfb35e

Please sign in to comment.