From 0d528e59d7a27941af7a3a49c3bc94602ce94550 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Mon, 21 Oct 2024 19:09:41 -0700 Subject: [PATCH] Work on compatibility with porting lib on Fabric --- .../moonrise/fabric/FabricHooks.java | 32 +++++++++++++++++-- .../mixin/chunk_system/FabricLevelMixin.java | 27 ++++++++++++++++ .../resources/moonrise-fabric.mixins.json | 1 + .../chunk_system/NeoForgeLevelMixin.java | 31 ++++++++++++++++++ .../resources/moonrise-neoforge.mixins.json | 1 + .../mixin/chunk_system/LevelMixin.java | 23 ------------- 6 files changed, 89 insertions(+), 26 deletions(-) create mode 100644 fabric/src/main/java/ca/spottedleaf/moonrise/fabric/mixin/chunk_system/FabricLevelMixin.java create mode 100644 neoforge/src/main/java/ca/spottedleaf/moonrise/neoforge/mixin/chunk_system/NeoForgeLevelMixin.java diff --git a/fabric/src/main/java/ca/spottedleaf/moonrise/fabric/FabricHooks.java b/fabric/src/main/java/ca/spottedleaf/moonrise/fabric/FabricHooks.java index c70877c8..e87843f0 100644 --- a/fabric/src/main/java/ca/spottedleaf/moonrise/fabric/FabricHooks.java +++ b/fabric/src/main/java/ca/spottedleaf/moonrise/fabric/FabricHooks.java @@ -29,7 +29,7 @@ public final class FabricHooks implements PlatformHooks { public interface OnExplosionDetonate { - void onExplosion(final Level world, final Explosion explosion, final List possiblyAffecting, final double diameter); + public void onExplosion(final Level world, final Explosion explosion, final List possiblyAffecting, final double diameter); } public static final Event ON_EXPLOSION_DETONATE = EventFactory.createArrayBacked( @@ -41,6 +41,32 @@ public interface OnExplosionDetonate { } ); + public interface OnChunkWatch { + public void onChunkWatch(final ServerLevel world, final LevelChunk chunk, final ServerPlayer player); + } + + public static final Event ON_CHUNK_WATCH = EventFactory.createArrayBacked( + OnChunkWatch.class, + listeners -> (final ServerLevel world, final LevelChunk chunk, final ServerPlayer player) -> { + for (int i = 0; i < listeners.length; i++) { + listeners[i].onChunkWatch(world, chunk, player); + } + } + ); + + public interface OnChunkUnwatch { + public void onChunkUnwatch(final ServerLevel world, final ChunkPos chunk, final ServerPlayer player); + } + + public static final Event ON_CHUNK_UNWATCH = EventFactory.createArrayBacked( + OnChunkUnwatch.class, + listeners -> (final ServerLevel world, final ChunkPos chunk, final ServerPlayer player) -> { + for (int i = 0; i < listeners.length; i++) { + listeners[i].onChunkUnwatch(world, chunk, player); + } + } + ); + @Override public String getBrand() { return "Moonrise"; @@ -110,12 +136,12 @@ public void chunkSyncSave(final ServerLevel world, final ChunkAccess chunk, fina @Override public void onChunkWatch(final ServerLevel world, final LevelChunk chunk, final ServerPlayer player) { - + ON_CHUNK_WATCH.invoker().onChunkWatch(world, chunk, player); } @Override public void onChunkUnWatch(final ServerLevel world, final ChunkPos chunk, final ServerPlayer player) { - + ON_CHUNK_UNWATCH.invoker().onChunkUnwatch(world, chunk, player); } @Override diff --git a/fabric/src/main/java/ca/spottedleaf/moonrise/fabric/mixin/chunk_system/FabricLevelMixin.java b/fabric/src/main/java/ca/spottedleaf/moonrise/fabric/mixin/chunk_system/FabricLevelMixin.java new file mode 100644 index 00000000..3c9acfd7 --- /dev/null +++ b/fabric/src/main/java/ca/spottedleaf/moonrise/fabric/mixin/chunk_system/FabricLevelMixin.java @@ -0,0 +1,27 @@ +package ca.spottedleaf.moonrise.fabric.mixin.chunk_system; + +import net.minecraft.server.level.FullChunkStatus; +import net.minecraft.world.level.Level; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(Level.class) +abstract class FabricLevelMixin { + /** + * @reason Allow block updates in non-ticking chunks, as new chunk system sends non-ticking chunks to clients + * @author Spottedleaf + */ + @Redirect( + method = "setBlock(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;II)Z", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/server/level/FullChunkStatus;isOrAfter(Lnet/minecraft/server/level/FullChunkStatus;)Z" + ) + ) + private boolean sendUpdatesForFullChunks(final FullChunkStatus instance, + final FullChunkStatus fullChunkStatus) { + + return instance.isOrAfter(FullChunkStatus.FULL); + } +} diff --git a/fabric/src/main/resources/moonrise-fabric.mixins.json b/fabric/src/main/resources/moonrise-fabric.mixins.json index 268ddbf1..ba46f0fc 100644 --- a/fabric/src/main/resources/moonrise-fabric.mixins.json +++ b/fabric/src/main/resources/moonrise-fabric.mixins.json @@ -3,6 +3,7 @@ "package": "ca.spottedleaf.moonrise.fabric.mixin", "mixins": [ "chunk_system.FabricDistanceManagerMixin", + "chunk_system.FabricLevelMixin", "chunk_system.FabricMinecraftServerMixin", "chunk_system.FabricServerLevelMixin", "collisions.EntityMixin" diff --git a/neoforge/src/main/java/ca/spottedleaf/moonrise/neoforge/mixin/chunk_system/NeoForgeLevelMixin.java b/neoforge/src/main/java/ca/spottedleaf/moonrise/neoforge/mixin/chunk_system/NeoForgeLevelMixin.java new file mode 100644 index 00000000..9b0cd9e3 --- /dev/null +++ b/neoforge/src/main/java/ca/spottedleaf/moonrise/neoforge/mixin/chunk_system/NeoForgeLevelMixin.java @@ -0,0 +1,31 @@ +package ca.spottedleaf.moonrise.neoforge.mixin.chunk_system; + +import net.minecraft.server.level.FullChunkStatus; +import net.minecraft.world.level.Level; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(Level.class) +abstract class NeoForgeLevelMixin { + /** + * @reason Allow block updates in non-ticking chunks, as new chunk system sends non-ticking chunks to clients + * @author Spottedleaf + */ + @Redirect( + method = { + // "setBlock(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;II)Z", + // NeoForge splits logic from the original method into this one + "markAndNotifyBlock(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/chunk/LevelChunk;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/block/state/BlockState;II)V" + }, + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/server/level/FullChunkStatus;isOrAfter(Lnet/minecraft/server/level/FullChunkStatus;)Z" + ) + ) + private boolean sendUpdatesForFullChunks(final FullChunkStatus instance, + final FullChunkStatus fullChunkStatus) { + + return instance.isOrAfter(FullChunkStatus.FULL); + } +} diff --git a/neoforge/src/main/resources/moonrise-neoforge.mixins.json b/neoforge/src/main/resources/moonrise-neoforge.mixins.json index 01a85dcd..dc0d96de 100644 --- a/neoforge/src/main/resources/moonrise-neoforge.mixins.json +++ b/neoforge/src/main/resources/moonrise-neoforge.mixins.json @@ -3,6 +3,7 @@ "package": "ca.spottedleaf.moonrise.neoforge.mixin", "mixins": [ "chunk_system.NeoForgeDistanceManagerMixin", + "chunk_system.NeoForgeLevelMixin", "chunk_system.NeoForgeMinecraftServerMixin", "chunk_system.NeoForgeServerLevelMixin", "collisions.EntityMixin" diff --git a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/LevelMixin.java b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/LevelMixin.java index ee00682e..fafaa22d 100644 --- a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/LevelMixin.java +++ b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/LevelMixin.java @@ -9,7 +9,6 @@ import ca.spottedleaf.moonrise.patches.chunk_system.level.entity.dfl.DefaultEntityLookup; import ca.spottedleaf.moonrise.patches.chunk_system.world.ChunkSystemEntityGetter; import net.minecraft.core.BlockPos; -import net.minecraft.server.level.FullChunkStatus; import net.minecraft.util.profiling.ProfilerFiller; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; @@ -28,7 +27,6 @@ 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.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import java.util.ArrayList; import java.util.List; @@ -303,27 +301,6 @@ public BlockPos getHeightmapPos(Heightmap.Types types, BlockPos blockPos) { return new BlockPos(blockPos.getX(), this.getHeight(types, blockPos.getX(), blockPos.getZ()), blockPos.getZ()); } - /** - * @reason Allow block updates in non-ticking chunks, as new chunk system sends non-ticking chunks to clients - * @author Spottedleaf - */ - @Redirect( - method = { - "setBlock(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;II)Z", - // NeoForge splits logic from the original method into this one - "markAndNotifyBlock(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/chunk/LevelChunk;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/block/state/BlockState;II)V" - }, - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/server/level/FullChunkStatus;isOrAfter(Lnet/minecraft/server/level/FullChunkStatus;)Z" - ) - ) - private boolean sendUpdatesForFullChunks(final FullChunkStatus instance, - final FullChunkStatus fullChunkStatus) { - - return instance.isOrAfter(FullChunkStatus.FULL); - } - // TODO: Thread.currentThread() != this.thread to TickThread?