Skip to content

Commit

Permalink
Fix chunk loading passenger mount event fix
Browse files Browse the repository at this point in the history
  • Loading branch information
2No2Name committed Jan 15, 2025
1 parent 8fbd063 commit e67bf23
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package no2.bugfixerupper.common.threadlocals;

public class MountEventSilencer {
private static final ThreadLocal<Boolean> SILENCE_MOUNT_EVENT = ThreadLocal.withInitial(() -> false);

public static void silenceMountEvent() {
SILENCE_MOUNT_EVENT.set(true);
}

public static void unsilenceMountEvent() {
SILENCE_MOUNT_EVENT.set(false);
}

public static boolean isMountEventSilenced() {
return SILENCE_MOUNT_EVENT.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.entity.EntityInLevelCallback;
import net.minecraft.world.level.gameevent.GameEvent;
import no2.bugfixerupper.common.threadlocals.MountEventSilencer;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -19,9 +20,7 @@ public class EntityMixin {
method = "addPassenger(Lnet/minecraft/world/entity/Entity;)V",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;gameEvent(Lnet/minecraft/core/Holder;Lnet/minecraft/world/entity/Entity;)V")
)
private boolean isEntityPartOfWorld(Entity thisInstance, Holder<GameEvent> holder, @Nullable Entity passenger) {
return this.levelCallback != null; // True if entity is part of the world
//If the vehicle is part of the world, mounting it should emit a game event. Otherwise, we are
// probably loading the chunk with entities, or something similar, and we should not emit the event.
private boolean shouldEmitMountEvent(Entity thisInstance, Holder<GameEvent> holder, @Nullable Entity passenger) {
return !MountEventSilencer.isMountEventSilenced();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package no2.bugfixerupper.mixin.chunkload.passenger_mount_event;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import no2.bugfixerupper.common.threadlocals.MountEventSilencer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(EntityType.class)
public class EntityTypeMixin {


@Redirect(
method = "method_17843",
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;startRiding(Lnet/minecraft/world/entity/Entity;Z)Z")
)
private static boolean startRidingSilently(Entity passenger, Entity vehicle, boolean bl) {
MountEventSilencer.silenceMountEvent();
boolean ret = passenger.startRiding(vehicle, bl);
MountEventSilencer.unsilenceMountEvent();
return ret;
}
}
3 changes: 2 additions & 1 deletion src/main/resources/bug-fixer-upper.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"compatibilityLevel": "JAVA_21",
"mixins": [
"chunkload.item_frame_sound.ItemFrameMixin",
"chunkload.passenger_mount_event.EntityMixin"
"chunkload.passenger_mount_event.EntityMixin",
"chunkload.passenger_mount_event.EntityTypeMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit e67bf23

Please sign in to comment.