Skip to content

Commit

Permalink
First attempt at fixing block state flags and path node caching
Browse files Browse the repository at this point in the history
  • Loading branch information
2No2Name committed Nov 8, 2024
1 parent 0f9bce1 commit 658f5a8
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public interface BlockStatePathingCache {
PathType lithium$getPathNodeType();

PathType lithium$getNeighborPathNodeType();

void lithium$initializePathNodeTypeCache();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

public interface BlockStateFlagHolder {
int lithium$getAllFlags();

void lithium$initializeFlags();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@
import org.apache.commons.lang3.Validate;
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;

@Mixin(BlockBehaviour.BlockStateBase.class)
public abstract class BlockStateBaseMixin implements BlockStatePathingCache {
private PathType pathNodeType = null;
private PathType pathNodeTypeNeighbor = null;

//TODO fix injection point
@Inject(method = "initCache()V", at = @At("RETURN"))
private void init(CallbackInfo ci) {
@Override
public void lithium$initializePathNodeTypeCache() {
// Reset the cached path node types, to ensure they are re-calculated.
this.pathNodeType = null;
this.pathNodeTypeNeighbor = null;
Expand All @@ -41,7 +37,9 @@ private void init(CallbackInfo ci) {
try {
//Passing null as previous node type to the method signals to other lithium mixins that we only want the neighbor behavior of this block and not its neighbors
//Using exceptions for control flow, but this way we do not need to copy the code for the cache initialization, reducing required maintenance and improving mod compatibility
//noinspection DataFlowIssue
this.pathNodeTypeNeighbor = (WalkNodeEvaluator.checkNeighbourBlocks(new PathfindingContext(singleBlockBlockView, null), 1, 1, 1, null));
//noinspection ConstantValue
if (this.pathNodeTypeNeighbor == null) {
this.pathNodeTypeNeighbor = PathType.OPEN;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.caffeinemc.mods.lithium.mixin.ai.pathing;

import net.caffeinemc.mods.lithium.common.ai.pathing.BlockStatePathingCache;
import net.minecraft.server.Bootstrap;
import net.minecraft.world.level.block.Block;
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.CallbackInfo;

@Mixin(Bootstrap.class)
public class BootstrapMixin {
@Inject(
method = "bootStrap",
at = @At("RETURN")
)
private static void afterBootstrap(CallbackInfo ci) {
for (BlockState blockState : Block.BLOCK_STATE_REGISTRY) {
((BlockStatePathingCache) blockState).lithium$initializePathNodeTypeCache();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
""",
depends = @MixinConfigDependency(
dependencyPath = "mixin.util.chunk_access"
), enabled = false // TODO: Broken by 1.21.2 or 1.21.3 Update due to initialization order change. Needs new injection points for cache initialization.
),
enabled = false // TODO handle data pack tag changes etc. Just injecting into Bootstrap is not enough.
)
package net.caffeinemc.mods.lithium.mixin.ai.pathing;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,14 @@
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(value = BlockBehaviour.BlockStateBase.class, priority = 1010)
public class BlockStateBaseMixin implements BlockStateFlagHolder {
@Unique
private int flags;

@Inject(method = "initCache()V", at = @At("RETURN"))
private void init(CallbackInfo ci) {
this.initFlags();
}

@Unique
private void initFlags() {
@Override
public void lithium$initializeFlags() {
TrackedBlockStatePredicate.FULLY_INITIALIZED.set(true);

int flags = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.caffeinemc.mods.lithium.mixin.util.block_tracking;

import net.caffeinemc.mods.lithium.common.block.BlockStateFlagHolder;
import net.minecraft.server.Bootstrap;
import net.minecraft.world.level.block.Block;
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.CallbackInfo;

@Mixin(value = Bootstrap.class, priority = 1010)
public class BootstrapMixin {
@Inject(
method = "bootStrap",
at = @At("RETURN")
)
private static void afterBootstrap(CallbackInfo ci) {
for (BlockState blockState : Block.BLOCK_STATE_REGISTRY) {
((BlockStateFlagHolder) blockState).lithium$initializeFlags();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
depends = {
@MixinConfigDependency(dependencyPath = "mixin.util.data_storage"),
@MixinConfigDependency(dependencyPath = "mixin.util.chunk_status_tracking")
}, enabled = false // TODO inject point bad
},
enabled = false // TODO handle data pack tag changes etc. Just injecting into Bootstrap is not enough.
)
package net.caffeinemc.mods.lithium.mixin.util.block_tracking;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
@MixinConfigOption(description = "Reduces indirection by inlining world height access methods", enabled = false) //TODO find out why this crashes the render thread
@MixinConfigOption(
description = "Reduces indirection by inlining world height access methods",
enabled = false //TODO find out why this crashes the render thread
)
package net.caffeinemc.mods.lithium.mixin.world.inline_height;

import net.caffeinemc.gradle.MixinConfigOption;
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 @@ -8,6 +8,7 @@
},
"mixins" : [
"ai.pathing.BlockStateBaseMixin",
"ai.pathing.BootstrapMixin",
"ai.pathing.FlyNodeEvaluatorMixin",
"ai.pathing.PathfindingContextAccessor",
"ai.pathing.PathfindingContextMixin",
Expand Down Expand Up @@ -172,6 +173,7 @@
"util.accessors.ServerLevelAccessor",
"util.block_entity_retrieval.LevelMixin",
"util.block_tracking.BlockStateBaseMixin",
"util.block_tracking.BootstrapMixin",
"util.block_tracking.LevelChunkSectionMixin",
"util.chunk_access.LevelReaderMixin",
"util.chunk_access.PathNavigationRegionMixin",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@MixinConfigOption(
description = "Allow hoppers to check whether a transfer-api inventory is present to avoid sleeping, which would prevent the hopper-inventory interaction. Enabled automatically when the transfer-api is present.",
enabled = false
enabled = false // Enabled automatically when the transfer-api is present
)
package net.caffeinemc.mods.lithium.fabric.mixin.compat.transfer_api;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
description = "Send updates to hoppers when adding inventory block entities to chunks when world edit is loaded. " +
"Fixes the issue of hoppers not noticing when inventories are placed using worldedit without any block updates. Enabled automatically when worldedit is present.",
depends = @MixinConfigDependency(dependencyPath = "mixin.util.block_entity_retrieval"),
enabled = false
enabled = false // Enabled automatically when worldedit is present
)
package net.caffeinemc.mods.lithium.fabric.mixin.compat.worldedit;

Expand Down
7 changes: 0 additions & 7 deletions lithium-fabric-mixin-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ Fluid optimizations
Fluid flow optimization

### `mixin.block.hopper`

(default: `true`)
Reduces hopper lag using caching, notification systems and BlockEntity sleeping
Requirements:
Expand Down Expand Up @@ -317,7 +316,6 @@ Skip checking whether an entity is inside powder snow for movement speed slowdow
Access entities faster when accessing a relatively small number of entity sections

### `mixin.entity.inactive_navigations`

(default: `true`)
Block updates skip notifying mobs that won't react to the block update anyways
Requirements:
Expand Down Expand Up @@ -449,7 +447,6 @@ Various VoxelShape optimizations
Use a faster collection for the full cube test cache

### `mixin.shapes.lazy_shape_context`

(default: `true`)
Entity shape contexts initialize rarely used fields only on first use

Expand Down Expand Up @@ -516,12 +513,10 @@ Requirements:
Entity sections store their position

### `mixin.util.inventory_change_listening`

(default: `true`)
Certain BlockEntity Inventories emit updates to their listeners when their stack list is changed or the inventory becomes invalid

### `mixin.util.inventory_comparator_tracking`

(default: `true`)
BlockEntity Inventories update their listeners when a comparator is placed near them
Requirements:
Expand All @@ -548,7 +543,6 @@ Various BlockEntity ticking optimizations
Allows BlockEntities to sleep, meaning they are no longer ticked until woken up, e.g. by updates to their inventory or block state

### `mixin.world.block_entity_ticking.sleeping.brewing_stand`

(default: `true`)
BlockEntity sleeping for inactive brewing stands

Expand All @@ -565,7 +559,6 @@ BlockEntity sleeping for inactive lit campfires
BlockEntity sleeping for inactive unlit campfires

### `mixin.world.block_entity_ticking.sleeping.furnace`

(default: `true`)
BlockEntity sleeping for inactive furnaces

Expand Down
7 changes: 0 additions & 7 deletions lithium-neoforge-mixin-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ Fluid optimizations
Fluid flow optimization

### `mixin.block.hopper`

(default: `true`)
Reduces hopper lag using caching, notification systems and BlockEntity sleeping
Requirements:
Expand Down Expand Up @@ -300,7 +299,6 @@ Skip checking whether an entity is inside powder snow for movement speed slowdow
Access entities faster when accessing a relatively small number of entity sections

### `mixin.entity.inactive_navigations`

(default: `true`)
Block updates skip notifying mobs that won't react to the block update anyways
Requirements:
Expand Down Expand Up @@ -426,7 +424,6 @@ Various VoxelShape optimizations
Use a faster collection for the full cube test cache

### `mixin.shapes.lazy_shape_context`

(default: `true`)
Entity shape contexts initialize rarely used fields only on first use

Expand Down Expand Up @@ -497,12 +494,10 @@ Requirements:
Entity sections store their position

### `mixin.util.inventory_change_listening`

(default: `true`)
Certain BlockEntity Inventories emit updates to their listeners when their stack list is changed or the inventory becomes invalid

### `mixin.util.inventory_comparator_tracking`

(default: `true`)
BlockEntity Inventories update their listeners when a comparator is placed near them
Requirements:
Expand All @@ -529,7 +524,6 @@ Various BlockEntity ticking optimizations
Allows BlockEntities to sleep, meaning they are no longer ticked until woken up, e.g. by updates to their inventory or block state

### `mixin.world.block_entity_ticking.sleeping.brewing_stand`

(default: `true`)
BlockEntity sleeping for inactive brewing stands

Expand All @@ -546,7 +540,6 @@ BlockEntity sleeping for inactive lit campfires
BlockEntity sleeping for inactive unlit campfires

### `mixin.world.block_entity_ticking.sleeping.furnace`

(default: `true`)
BlockEntity sleeping for inactive furnaces

Expand Down

0 comments on commit 658f5a8

Please sign in to comment.