Skip to content

Commit 20da175

Browse files
committed
Improve harvester block entity compatibility
Closes #187
1 parent 88d8ba1 commit 20da175

5 files changed

Lines changed: 40 additions & 83 deletions

File tree

neoforge/src/main/java/dev/ryanhcode/sable/neoforge/mixin/compatibility/create/behaviour_compatibility/harvester_block_entity/HarvesterBlockEntityMixin.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@
77
import dev.ryanhcode.sable.api.block.BlockEntitySubLevelActor;
88
import dev.ryanhcode.sable.companion.math.JOMLConversion;
99
import dev.ryanhcode.sable.neoforge.mixinhelper.compatibility.create.harvester.HarvesterLerpedSpeed;
10-
import dev.ryanhcode.sable.neoforge.mixinhelper.compatibility.create.harvester.HarvesterMovementBehaviourExtension;
10+
import dev.ryanhcode.sable.neoforge.mixinhelper.compatibility.create.harvester.HarvesterTicker;
1111
import dev.ryanhcode.sable.sublevel.ServerSubLevel;
1212
import net.createmod.catnip.animation.LerpedFloat;
1313
import net.minecraft.core.BlockPos;
14+
import net.minecraft.core.Position;
1415
import net.minecraft.world.level.block.entity.BlockEntityType;
1516
import net.minecraft.world.level.block.state.BlockState;
16-
import net.minecraft.world.phys.Vec3;
1717
import org.spongepowered.asm.mixin.Mixin;
1818
import org.spongepowered.asm.mixin.Unique;
1919

20-
import static dev.ryanhcode.sable.neoforge.mixinhelper.compatibility.create.harvester.HarvesterTicker.blockEntityBehaviour;
21-
2220
@Mixin(HarvesterBlockEntity.class)
2321
public abstract class HarvesterBlockEntityMixin extends CachedRenderBBBlockEntity implements HarvesterLerpedSpeed, BlockEntitySubLevelActor {
2422

@@ -46,30 +44,23 @@ public HarvesterBlockEntityMixin(final BlockEntityType<?> type, final BlockPos p
4644
@Override
4745
public void sable$tick(final ServerSubLevel subLevel) {
4846
final ActiveSableCompanion helper = Sable.HELPER;
49-
final Vec3 center = this.getBlockPos().getCenter();
50-
BlockPos gatheredPos = helper.runIncludingSubLevels(this.getLevel(), center, false, helper.getContaining(this), (sublevel, pos) -> {
51-
if (blockEntityBehaviour.isValidCrop(this.getLevel(), pos, this.getLevel().getBlockState(pos))) {
47+
final Position center = this.getBlockPos().getCenter();
48+
BlockPos gatheredPos = helper.runIncludingSubLevels(this.level, center, false, helper.getContaining(this), (sublevel, pos) -> {
49+
if (HarvesterTicker.blockEntityBehaviour.isValidCrop(this.level, pos, this.level.getBlockState(pos))) {
5250
return pos;
53-
} else {
54-
return null;
5551
}
52+
53+
return null;
5654
});
5755

5856
if (gatheredPos == null) {
59-
gatheredPos = BlockPos.containing(helper.projectOutOfSubLevel(this.getLevel(), center));
57+
gatheredPos = BlockPos.containing(helper.projectOutOfSubLevel(this.level, center));
6058
}
6159

6260
if (!this.sable$previousPos.equals(gatheredPos)) {
6361
this.sable$previousPos = gatheredPos;
64-
65-
final HarvesterMovementBehaviourExtension duck = (HarvesterMovementBehaviourExtension) blockEntityBehaviour;
66-
duck.sable$setManualLevel(this.getLevel());
67-
duck.sable$setSelfPos(this.getBlockPos());
68-
69-
blockEntityBehaviour.visitNewPosition(null, this.sable$previousPos);
70-
71-
duck.sable$setManualLevel(null);
72-
duck.sable$setSelfPos(null);
62+
HarvesterTicker.dummyMovementContext.update(this.level, this.getBlockPos(), this.getBlockState(), null);
63+
HarvesterTicker.blockEntityBehaviour.visitNewPosition(HarvesterTicker.dummyMovementContext, this.sable$previousPos);
7364
}
7465
}
7566

neoforge/src/main/java/dev/ryanhcode/sable/neoforge/mixin/compatibility/create/behaviour_compatibility/harvester_block_entity/HarvesterBlockEntityUsageMixin.java

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,19 @@
44
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
55
import com.simibubi.create.content.contraptions.actors.harvester.HarvesterMovementBehaviour;
66
import com.simibubi.create.content.contraptions.behaviour.MovementContext;
7-
import dev.ryanhcode.sable.neoforge.mixinhelper.compatibility.create.harvester.HarvesterMovementBehaviourExtension;
7+
import dev.ryanhcode.sable.neoforge.mixinhelper.compatibility.create.behavior_compatibility.harvester_block_entity.DummyMovementContext;
88
import dev.ryanhcode.sable.neoforge.mixinhelper.compatibility.create.harvester.HarvesterTicker;
9-
import net.minecraft.core.BlockPos;
109
import net.minecraft.world.item.ItemStack;
11-
import net.minecraft.world.level.Level;
12-
import org.objectweb.asm.Opcodes;
1310
import org.spongepowered.asm.mixin.Mixin;
14-
import org.spongepowered.asm.mixin.Unique;
1511
import org.spongepowered.asm.mixin.injection.At;
16-
import org.spongepowered.asm.mixin.injection.Redirect;
17-
18-
import java.lang.ref.Reference;
19-
import java.lang.ref.WeakReference;
2012

2113
@Mixin(HarvesterMovementBehaviour.class)
22-
public class HarvesterBlockEntityUsageMixin implements HarvesterMovementBehaviourExtension {
23-
24-
@Unique
25-
private Reference<Level> sable$manualLevel;
26-
27-
@Unique
28-
private BlockPos sable$selfPos = null;
29-
30-
@Override
31-
public BlockPos sable$getSelfPos() {
32-
return this.sable$selfPos;
33-
}
34-
35-
@Override
36-
public void sable$setSelfPos(final BlockPos sable$selfPos) {
37-
this.sable$selfPos = sable$selfPos;
38-
}
39-
40-
@Override
41-
public Level sable$getManualLevel() {
42-
return this.sable$manualLevel.get();
43-
}
44-
45-
@Override
46-
public void sable$setManualLevel(final Level level) {
47-
this.sable$manualLevel = new WeakReference<>(level);
48-
}
49-
50-
@Redirect(method = "visitNewPosition", at = @At(value = "FIELD", target = "Lcom/simibubi/create/content/contraptions/behaviour/MovementContext;world:Lnet/minecraft/world/level/Level;", opcode = Opcodes.GETFIELD))
51-
public Level sable$replaceWorld(final MovementContext instance) {
52-
if (instance == null) { // we're only going to be passing in null from our mixin, so this is valid
53-
return this.sable$getManualLevel();
54-
} else {
55-
return instance.world;
56-
}
57-
}
14+
public class HarvesterBlockEntityUsageMixin {
5815

5916
@WrapOperation(method = "lambda$visitNewPosition$0", at = @At(value = "INVOKE", target = "Lcom/simibubi/create/content/contraptions/actors/harvester/HarvesterMovementBehaviour;collectOrDropItem(Lcom/simibubi/create/content/contraptions/behaviour/MovementContext;Lnet/minecraft/world/item/ItemStack;)V"))
6017
public void sable$replaceDropItem(final HarvesterMovementBehaviour instance, final MovementContext movementContext, final ItemStack itemStack, final Operation<Void> original) {
61-
if (movementContext == null) {
62-
if (this.sable$getManualLevel() != null && this.sable$getSelfPos() != null) {
63-
HarvesterTicker.dropItem(this.sable$getManualLevel(), itemStack, this.sable$getSelfPos());
64-
}
18+
if (movementContext instanceof DummyMovementContext) {
19+
HarvesterTicker.dropItem(movementContext.world, itemStack, movementContext.localPos);
6520
} else {
6621
original.call(instance, movementContext, itemStack);
6722
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package dev.ryanhcode.sable.neoforge.mixinhelper.compatibility.create.behavior_compatibility.harvester_block_entity;
2+
3+
import com.simibubi.create.content.contraptions.behaviour.MovementContext;
4+
import net.minecraft.core.BlockPos;
5+
import net.minecraft.nbt.CompoundTag;
6+
import net.minecraft.world.level.Level;
7+
import net.minecraft.world.level.block.Blocks;
8+
import net.minecraft.world.level.block.state.BlockState;
9+
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
public class DummyMovementContext extends MovementContext {
13+
14+
public DummyMovementContext() {
15+
super(null, new StructureTemplate.StructureBlockInfo(BlockPos.ZERO, Blocks.AIR.defaultBlockState(), null), null);
16+
}
17+
18+
public void update(final Level level, final BlockPos pos, final BlockState state, @Nullable final CompoundTag blockEntityData) {
19+
this.world = level;
20+
this.state = state;
21+
this.localPos = pos;
22+
this.blockEntityData = blockEntityData;
23+
}
24+
}

neoforge/src/main/java/dev/ryanhcode/sable/neoforge/mixinhelper/compatibility/create/harvester/HarvesterMovementBehaviourExtension.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

neoforge/src/main/java/dev/ryanhcode/sable/neoforge/mixinhelper/compatibility/create/harvester/HarvesterTicker.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.ryanhcode.sable.neoforge.mixinhelper.compatibility.create.harvester;
22

33
import com.simibubi.create.content.contraptions.actors.harvester.HarvesterMovementBehaviour;
4+
import dev.ryanhcode.sable.neoforge.mixinhelper.compatibility.create.behavior_compatibility.harvester_block_entity.DummyMovementContext;
45
import net.minecraft.core.BlockPos;
56
import net.minecraft.world.entity.item.ItemEntity;
67
import net.minecraft.world.item.ItemStack;
@@ -13,6 +14,7 @@
1314
public class HarvesterTicker<T extends BlockEntity & HarvesterLerpedSpeed> implements BlockEntityTicker<T> {
1415

1516
public static final HarvesterMovementBehaviour blockEntityBehaviour = new HarvesterMovementBehaviour();
17+
public static final DummyMovementContext dummyMovementContext = new DummyMovementContext();
1618

1719
@Override
1820
public void tick(final Level level, final BlockPos arg2, final BlockState arg3, final T be) {

0 commit comments

Comments
 (0)