Skip to content

Commit

Permalink
Fix Downgrade to 1.21.1
Browse files Browse the repository at this point in the history
  • Loading branch information
2No2Name committed Nov 15, 2024
1 parent 0fe3cfd commit 8d42900
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.caffeinemc.mods.lithium.common.block.entity;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.state.BlockState;

public interface ShapeUpdateHandlingBlockBehaviour {

default void lithium$handleShapeUpdate(LevelReader world, BlockState myBlockState, BlockPos myPos, BlockPos posFrom, BlockState newState) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.caffeinemc.mods.lithium.mixin.block.hopper;

import net.caffeinemc.mods.lithium.common.block.entity.ShapeUpdateHandlingBlockBehaviour;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(BlockBehaviour.class)
public class BlockBehaviourMixin implements ShapeUpdateHandlingBlockBehaviour {

@Inject(method = "updateShape(Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/core/Direction;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/LevelAccessor;Lnet/minecraft/core/BlockPos;Lnet/minecraft/core/BlockPos;)Lnet/minecraft/world/level/block/state/BlockState;", at = @At("HEAD"))
private void notifyOnShapeUpdate(BlockState myBlockState, Direction direction, BlockState newState, LevelAccessor world, BlockPos myPos, BlockPos posFrom, CallbackInfoReturnable<BlockState> cir) {
//Triggers when a shape update (= update that observers can detect) is sent
this.lithium$handleShapeUpdate(world, myBlockState, myPos, posFrom, newState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,24 @@
import net.minecraft.world.phys.Vec3;
import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.Unique;

@Mixin(ChestBoat.class)
@Mixin(value = ChestBoat.class, priority = 2000)
// Apply this mixin after other mixins, so the fallback is our Intrinsic not being applied
public abstract class ChestBoatMixin extends Entity {
public ChestBoatMixin(EntityType<?> type, Level world) {
super(type, world);
}

@Intrinsic
@Intrinsic()
// Intrinsic for mod compatibility: If this injection does not work, it is not critical, only a slight performance loss.
@Override
public void rideTick() {
super.rideTick();
this.tickRidingSummarizeMovementNotifications();
}

@SuppressWarnings({ "MixinAnnotationTarget", "UnresolvedMixinReference" })
@Redirect(
method = "rideTick()V",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;rideTick()V")
)
private void tickRidingSummarizeMovementNotifications(Entity entity) {
@Unique
private void tickRidingSummarizeMovementNotifications() {
EntityInLevelCallback changeListener = ((EntityAccessor) this).getChangeListener();
if (changeListener instanceof ToggleableMovementTracker toggleableMovementTracker) {
Vec3 beforeTickPos = this.position();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ private static boolean lithiumHopperIsEmpty(HopperBlockEntity hopperBlockEntity)
}
}

@Override
public void lithium$invalidateCacheOnUndirectedNeighborUpdate() {
if (this.extractionMode == HopperCachingState.BlockInventory.NO_BLOCK_INVENTORY || this.extractionMode == HopperCachingState.BlockInventory.BLOCK_STATE) {
this.invalidateBlockExtractionData();
}
if (this.insertionMode == HopperCachingState.BlockInventory.NO_BLOCK_INVENTORY || this.insertionMode == HopperCachingState.BlockInventory.BLOCK_STATE) {
this.invalidateBlockInsertionData();
}
}

@Override
public void lithium$invalidateCacheOnNeighborUpdate(Direction fromDirection) {
boolean fromAbove = fromDirection == Direction.UP;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,51 @@
package net.caffeinemc.mods.lithium.mixin.block.hopper;

import net.caffeinemc.mods.lithium.common.block.entity.ShapeUpdateHandlingBlockBehaviour;
import net.caffeinemc.mods.lithium.common.hopper.UpdateReceiver;
import net.caffeinemc.mods.lithium.common.world.blockentity.BlockEntityGetter;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.WorldlyContainerHolder;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.HopperBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(HopperBlock.class)
public abstract class HopperBlockMixin extends BaseEntityBlock {
public abstract class HopperBlockMixin extends BaseEntityBlock implements ShapeUpdateHandlingBlockBehaviour {

protected HopperBlockMixin(Properties settings) {
super(settings);
}

@Intrinsic
@Override
public BlockState updateShape(BlockState myBlockState, Direction direction, BlockState newState, LevelAccessor world, BlockPos myPos, BlockPos posFrom) {
return super.updateShape(myBlockState, direction, newState, world, myPos, posFrom);
}

@SuppressWarnings({ "MixinAnnotationTarget", "UnresolvedMixinReference" })
@Inject(method = "updateShape(Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/core/Direction;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/LevelAccessor;Lnet/minecraft/core/BlockPos;Lnet/minecraft/core/BlockPos;)Lnet/minecraft/world/level/block/state/BlockState;", at = @At("HEAD"))
private void notifyOnNeighborUpdate(BlockState myBlockState, Direction direction, BlockState newState, LevelAccessor world, BlockPos myPos, BlockPos posFrom, CallbackInfoReturnable<BlockState> ci) {
@Override
public void lithium$handleShapeUpdate(LevelReader levelReader, BlockState myBlockState, BlockPos myPos, BlockPos posFrom, BlockState newState) {
//invalidate cache when composters change state
if (!world.isClientSide() && newState.getBlock() instanceof WorldlyContainerHolder) {
this.updateHopper(world, myBlockState, myPos, posFrom);
if (!levelReader.isClientSide() && newState.getBlock() instanceof WorldlyContainerHolder) {
this.updateHopper(levelReader, myBlockState, myPos, posFrom);
}
}

@Inject(method = "neighborChanged", at = @At(value = "HEAD"))
private void updateBlockEntity(BlockState myBlockState, Level world, BlockPos myPos, Block block, BlockPos posFrom, boolean moved, CallbackInfo ci) {
private void updateBlockEntity(BlockState blockState, Level world, BlockPos myPos, Block block, BlockPos blockPos2, boolean bl, CallbackInfo ci) {
//invalidate cache when the block is replaced
if (!world.isClientSide()) {
this.updateHopper(world, myBlockState, myPos, posFrom);
BlockEntity hopper = ((BlockEntityGetter) world).lithium$getLoadedExistingBlockEntity(myPos);
if (hopper instanceof UpdateReceiver updateReceiver) {
updateReceiver.lithium$invalidateCacheOnUndirectedNeighborUpdate();
}
}
}

private void updateHopper(LevelAccessor world, BlockState myBlockState, BlockPos myPos, BlockPos posFrom) {
private void updateHopper(LevelReader world, BlockState myBlockState, BlockPos myPos, BlockPos posFrom) {
Direction facing = myBlockState.getValue(HopperBlock.FACING);
boolean above = posFrom.getY() == myPos.getY() + 1;
if (above || posFrom.getX() == myPos.getX() + facing.getStepX() && posFrom.getY() == myPos.getY() + facing.getStepY() && posFrom.getZ() == myPos.getZ() + facing.getStepZ()) {
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/resources/lithium.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
"block.flatten_states.FluidStateMixin",
"block.fluid.flow.FlowingFluidMixin",
"block.hopper.AbstractContainerMenuMixin",
"block.hopper.BlockBehaviourMixin",
"block.hopper.ChestBoatMixin",
"block.hopper.ChiseledBookShelfBlockEntityMixin",
"block.hopper.ClassInstanceMultiMapMixin",
"block.hopper.ComposterMixin$ComposterBlockComposterInventoryMixin",
Expand Down

0 comments on commit 8d42900

Please sign in to comment.