From aa114b2015885a3a0d506f25bdd84926925422b9 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Sun, 4 Aug 2024 20:17:15 -0700 Subject: [PATCH] Split some mixins to their platforms --- .../FabricDistanceManagerMixin.java | 33 ++++++++++++++ .../FabricMinecraftServerMixin.java | 27 ++++++++++++ .../resources/moonrise-fabric.mixins.json | 2 + .../NeoForgeDistanceManagerMixin.java | 33 ++++++++++++++ .../NeoForgeMinecraftServerMixin.java | 27 ++++++++++++ .../resources/moonrise-neoforge.mixins.json | 2 + .../moonrise/common/util/TickThread.java | 8 ++-- .../chunk_system/DistanceManagerMixin.java | 43 +++++-------------- .../chunk_system/MinecraftServerMixin.java | 35 --------------- .../mixin/chunk_system/ServerLevelMixin.java | 1 + .../chunk/ChunkSystemDistanceManager.java | 3 ++ 11 files changed, 143 insertions(+), 71 deletions(-) create mode 100644 fabric/src/main/java/ca/spottedleaf/moonrise/fabric/mixin/chunk_system/FabricDistanceManagerMixin.java create mode 100644 fabric/src/main/java/ca/spottedleaf/moonrise/fabric/mixin/chunk_system/FabricMinecraftServerMixin.java create mode 100644 neoforge/src/main/java/ca/spottedleaf/moonrise/neoforge/mixin/chunk_system/NeoForgeDistanceManagerMixin.java create mode 100644 neoforge/src/main/java/ca/spottedleaf/moonrise/neoforge/mixin/chunk_system/NeoForgeMinecraftServerMixin.java diff --git a/fabric/src/main/java/ca/spottedleaf/moonrise/fabric/mixin/chunk_system/FabricDistanceManagerMixin.java b/fabric/src/main/java/ca/spottedleaf/moonrise/fabric/mixin/chunk_system/FabricDistanceManagerMixin.java new file mode 100644 index 00000000..826673d4 --- /dev/null +++ b/fabric/src/main/java/ca/spottedleaf/moonrise/fabric/mixin/chunk_system/FabricDistanceManagerMixin.java @@ -0,0 +1,33 @@ +package ca.spottedleaf.moonrise.fabric.mixin.chunk_system; + +import ca.spottedleaf.moonrise.patches.chunk_system.level.chunk.ChunkSystemDistanceManager; +import net.minecraft.server.level.ChunkLevel; +import net.minecraft.server.level.DistanceManager; +import net.minecraft.server.level.FullChunkStatus; +import net.minecraft.server.level.TicketType; +import net.minecraft.world.level.ChunkPos; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; + +@Mixin(DistanceManager.class) +abstract class FabricDistanceManagerMixin implements ChunkSystemDistanceManager { + + /** + * @reason Route to new chunk system + * @author Spottedleaf + */ + @Overwrite + public void addRegionTicket(final TicketType type, final ChunkPos pos, final int radius, final T identifier) { + this.moonrise$getChunkHolderManager().addTicketAtLevel(type, pos, ChunkLevel.byStatus(FullChunkStatus.FULL) - radius, identifier); + } + + /** + * @reason Route to new chunk system + * @author Spottedleaf + */ + @Overwrite + public void removeRegionTicket(final TicketType type, final ChunkPos pos, final int radius, final T identifier) { + this.moonrise$getChunkHolderManager().removeTicketAtLevel(type, pos, ChunkLevel.byStatus(FullChunkStatus.FULL) - radius, identifier); + } + +} diff --git a/fabric/src/main/java/ca/spottedleaf/moonrise/fabric/mixin/chunk_system/FabricMinecraftServerMixin.java b/fabric/src/main/java/ca/spottedleaf/moonrise/fabric/mixin/chunk_system/FabricMinecraftServerMixin.java new file mode 100644 index 00000000..fcb8ae25 --- /dev/null +++ b/fabric/src/main/java/ca/spottedleaf/moonrise/fabric/mixin/chunk_system/FabricMinecraftServerMixin.java @@ -0,0 +1,27 @@ +package ca.spottedleaf.moonrise.fabric.mixin.chunk_system; + +import ca.spottedleaf.moonrise.common.util.TickThread; +import net.minecraft.server.MinecraftServer; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(MinecraftServer.class) +abstract class FabricMinecraftServerMixin { + + /** + * @reason Make server thread an instance of TickThread for thread checks + * @author Spottedleaf + */ + @Redirect( + method = "spin", + at = @At( + value = "NEW", + target = "(Ljava/lang/Runnable;Ljava/lang/String;)Ljava/lang/Thread;" + ) + ) + private static Thread createTickThread(final Runnable target, final String name) { + return new TickThread(target, name); + } + +} diff --git a/fabric/src/main/resources/moonrise-fabric.mixins.json b/fabric/src/main/resources/moonrise-fabric.mixins.json index 48fcedc4..c624d4b8 100644 --- a/fabric/src/main/resources/moonrise-fabric.mixins.json +++ b/fabric/src/main/resources/moonrise-fabric.mixins.json @@ -2,6 +2,8 @@ "parent": "moonrise.mixins.json", "package": "ca.spottedleaf.moonrise.fabric.mixin", "mixins": [ + "chunk_system.FabricDistanceManagerMixin", + "chunk_system.FabricMinecraftServerMixin" ], "client": [ ] diff --git a/neoforge/src/main/java/ca/spottedleaf/moonrise/neoforge/mixin/chunk_system/NeoForgeDistanceManagerMixin.java b/neoforge/src/main/java/ca/spottedleaf/moonrise/neoforge/mixin/chunk_system/NeoForgeDistanceManagerMixin.java new file mode 100644 index 00000000..7f59513a --- /dev/null +++ b/neoforge/src/main/java/ca/spottedleaf/moonrise/neoforge/mixin/chunk_system/NeoForgeDistanceManagerMixin.java @@ -0,0 +1,33 @@ +package ca.spottedleaf.moonrise.neoforge.mixin.chunk_system; + +import ca.spottedleaf.moonrise.patches.chunk_system.level.chunk.ChunkSystemDistanceManager; +import net.minecraft.server.level.ChunkLevel; +import net.minecraft.server.level.DistanceManager; +import net.minecraft.server.level.FullChunkStatus; +import net.minecraft.server.level.TicketType; +import net.minecraft.world.level.ChunkPos; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; + +@Mixin(DistanceManager.class) +abstract class NeoForgeDistanceManagerMixin implements ChunkSystemDistanceManager { + + /** + * @reason Route to new chunk system + * @author Spottedleaf + */ + @Overwrite + public void addRegionTicket(final TicketType type, final ChunkPos pos, final int radius, final T identifier, final boolean forceTicks) { + this.moonrise$getChunkHolderManager().addTicketAtLevel(type, pos, ChunkLevel.byStatus(FullChunkStatus.FULL) - radius, identifier); + } + + /** + * @reason Route to new chunk system + * @author Spottedleaf + */ + @Overwrite + public void removeRegionTicket(final TicketType type, final ChunkPos pos, final int radius, final T identifier, final boolean forceTicks) { + this.moonrise$getChunkHolderManager().removeTicketAtLevel(type, pos, ChunkLevel.byStatus(FullChunkStatus.FULL) - radius, identifier); + } + +} diff --git a/neoforge/src/main/java/ca/spottedleaf/moonrise/neoforge/mixin/chunk_system/NeoForgeMinecraftServerMixin.java b/neoforge/src/main/java/ca/spottedleaf/moonrise/neoforge/mixin/chunk_system/NeoForgeMinecraftServerMixin.java new file mode 100644 index 00000000..d5e96f06 --- /dev/null +++ b/neoforge/src/main/java/ca/spottedleaf/moonrise/neoforge/mixin/chunk_system/NeoForgeMinecraftServerMixin.java @@ -0,0 +1,27 @@ +package ca.spottedleaf.moonrise.neoforge.mixin.chunk_system; + +import ca.spottedleaf.moonrise.common.util.TickThread; +import net.minecraft.server.MinecraftServer; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(MinecraftServer.class) +abstract class NeoForgeMinecraftServerMixin { + + /** + * @reason Make server thread an instance of TickThread for thread checks + * @author Spottedleaf + */ + @Redirect( + method = "spin", + at = @At( + value = "NEW", + target = "(Ljava/lang/ThreadGroup;Ljava/lang/Runnable;Ljava/lang/String;)Ljava/lang/Thread;" + ) + ) + private static Thread createTickThread(final ThreadGroup group, final Runnable task, final String name) { + return new TickThread(group, task, name); + } + +} diff --git a/neoforge/src/main/resources/moonrise-neoforge.mixins.json b/neoforge/src/main/resources/moonrise-neoforge.mixins.json index eac96c11..7cc05dd9 100644 --- a/neoforge/src/main/resources/moonrise-neoforge.mixins.json +++ b/neoforge/src/main/resources/moonrise-neoforge.mixins.json @@ -2,6 +2,8 @@ "parent": "moonrise.mixins.json", "package": "ca.spottedleaf.moonrise.neoforge.mixin", "mixins": [ + "chunk_system.NeoForgeDistanceManagerMixin", + "chunk_system.NeoForgeMinecraftServerMixin" ], "client": [ ] diff --git a/src/main/java/ca/spottedleaf/moonrise/common/util/TickThread.java b/src/main/java/ca/spottedleaf/moonrise/common/util/TickThread.java index b0960203..217d1f90 100644 --- a/src/main/java/ca/spottedleaf/moonrise/common/util/TickThread.java +++ b/src/main/java/ca/spottedleaf/moonrise/common/util/TickThread.java @@ -77,14 +77,14 @@ public TickThread(final String name) { } public TickThread(final Runnable run, final String name) { - this(run, name, null); + this(null, run, name); } - public TickThread(final Runnable run, final String name, final ThreadGroup group) { - this(run, name, group, ID_GENERATOR.incrementAndGet()); + public TickThread(final ThreadGroup group, final Runnable run, final String name) { + this(group, run, name, ID_GENERATOR.incrementAndGet()); } - private TickThread(final Runnable run, final String name, final ThreadGroup group, final int id) { + private TickThread(final ThreadGroup group, final Runnable run, final String name, final int id) { super(group, run, name); this.id = id; } diff --git a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/DistanceManagerMixin.java b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/DistanceManagerMixin.java index 1905a5cc..a9253e6c 100644 --- a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/DistanceManagerMixin.java +++ b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/DistanceManagerMixin.java @@ -7,11 +7,9 @@ import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import it.unimi.dsi.fastutil.longs.LongSet; import net.minecraft.server.level.ChunkHolder; -import net.minecraft.server.level.ChunkLevel; import net.minecraft.server.level.ChunkMap; import net.minecraft.server.level.ChunkTaskPriorityQueueSorter; import net.minecraft.server.level.DistanceManager; -import net.minecraft.server.level.FullChunkStatus; import net.minecraft.server.level.Ticket; import net.minecraft.server.level.TicketType; import net.minecraft.server.level.TickingTracker; @@ -22,7 +20,6 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; -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; @@ -100,8 +97,8 @@ private void destroyFields(final CallbackInfo ci) { this.simulationDistance = -1; } - @Unique - private ChunkHolderManager getChunkHolderManager() { + @Override + public ChunkHolderManager moonrise$getChunkHolderManager() { return ((ChunkSystemServerLevel)this.moonrise$getChunkMap().level).moonrise$getChunkTaskScheduler().chunkHolderManager; } @@ -111,7 +108,7 @@ private ChunkHolderManager getChunkHolderManager() { */ @Overwrite public void purgeStaleTickets() { - this.getChunkHolderManager().tick(); + this.moonrise$getChunkHolderManager().tick(); } /** @@ -120,7 +117,7 @@ public void purgeStaleTickets() { */ @Overwrite public boolean runAllUpdates(final ChunkMap chunkStorage) { - return this.getChunkHolderManager().processTicketUpdates(); + return this.moonrise$getChunkHolderManager().processTicketUpdates(); } /** @@ -129,7 +126,7 @@ public boolean runAllUpdates(final ChunkMap chunkStorage) { */ @Overwrite public void addTicket(final long pos, final Ticket ticket) { - this.getChunkHolderManager().addTicketAtLevel((TicketType)ticket.getType(), pos, ticket.getTicketLevel(), ticket.key); + this.moonrise$getChunkHolderManager().addTicketAtLevel((TicketType)ticket.getType(), pos, ticket.getTicketLevel(), ticket.key); } /** @@ -138,25 +135,7 @@ public void addTicket(final long pos, final Ticket ticket) { */ @Overwrite public void removeTicket(final long pos, final Ticket ticket) { - this.getChunkHolderManager().removeTicketAtLevel((TicketType)ticket.getType(), pos, ticket.getTicketLevel(), ticket.key); - } - - /** - * @reason Route to new chunk system - * @author Spottedleaf - */ - @Overwrite - public void addRegionTicket(final TicketType type, final ChunkPos pos, final int radius, final T identifier, boolean forceTicks) { // TODO: Neo added param - this.getChunkHolderManager().addTicketAtLevel(type, pos, ChunkLevel.byStatus(FullChunkStatus.FULL) - radius, identifier); - } - - /** - * @reason Route to new chunk system - * @author Spottedleaf - */ - @Overwrite - public void removeRegionTicket(final TicketType type, final ChunkPos pos, final int radius, final T identifier, boolean forceTicks) { // TODO: Neo added param - this.getChunkHolderManager().removeTicketAtLevel(type, pos, ChunkLevel.byStatus(FullChunkStatus.FULL) - radius, identifier); + this.moonrise$getChunkHolderManager().removeTicketAtLevel((TicketType)ticket.getType(), pos, ticket.getTicketLevel(), ticket.key); } /** @@ -175,9 +154,9 @@ public SortedArraySet> getTickets(final long pos) { @Overwrite public void updateChunkForced(final ChunkPos pos, final boolean forced) { if (forced) { - this.getChunkHolderManager().addTicketAtLevel(TicketType.FORCED, pos, ChunkMap.FORCED_TICKET_LEVEL, pos); + this.moonrise$getChunkHolderManager().addTicketAtLevel(TicketType.FORCED, pos, ChunkMap.FORCED_TICKET_LEVEL, pos); } else { - this.getChunkHolderManager().removeTicketAtLevel(TicketType.FORCED, pos, ChunkMap.FORCED_TICKET_LEVEL, pos); + this.moonrise$getChunkHolderManager().removeTicketAtLevel(TicketType.FORCED, pos, ChunkMap.FORCED_TICKET_LEVEL, pos); } } @@ -282,7 +261,7 @@ public int getPlayerTicketLevel() { */ @Overwrite public boolean inEntityTickingRange(final long pos) { - final NewChunkHolder chunkHolder = this.getChunkHolderManager().getChunkHolder(pos); + final NewChunkHolder chunkHolder = this.moonrise$getChunkHolderManager().getChunkHolder(pos); return chunkHolder != null && chunkHolder.isEntityTickingReady(); } @@ -292,7 +271,7 @@ public boolean inEntityTickingRange(final long pos) { */ @Overwrite public boolean inBlockTickingRange(final long pos) { - final NewChunkHolder chunkHolder = this.getChunkHolderManager().getChunkHolder(pos); + final NewChunkHolder chunkHolder = this.moonrise$getChunkHolderManager().getChunkHolder(pos); return chunkHolder != null && chunkHolder.isTickingReady(); } @@ -302,7 +281,7 @@ public boolean inBlockTickingRange(final long pos) { */ @Overwrite public String getTicketDebugString(final long pos) { - return this.getChunkHolderManager().getTicketDebugString(pos); + return this.moonrise$getChunkHolderManager().getTicketDebugString(pos); } /** diff --git a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/MinecraftServerMixin.java b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/MinecraftServerMixin.java index d198c676..54ffeb7c 100644 --- a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/MinecraftServerMixin.java +++ b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/MinecraftServerMixin.java @@ -1,6 +1,5 @@ package ca.spottedleaf.moonrise.mixin.chunk_system; -import ca.spottedleaf.moonrise.common.util.TickThread; import ca.spottedleaf.moonrise.patches.chunk_system.io.RegionFileIOThread; import ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemServerLevel; import ca.spottedleaf.moonrise.patches.chunk_system.scheduling.ChunkTaskScheduler; @@ -22,7 +21,6 @@ import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import java.util.Iterator; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Stream; @@ -186,39 +184,6 @@ private static void initHook(Function function, CallbackInfoRetur ChunkTaskScheduler.init(); } - /** - * @reason Make server thread an instance of TickThread for thread checks - * @author Spottedleaf - */ - /* TODO NeoForge adds ThreadGroup - @Redirect( - method = "spin", - at = @At( - value = "NEW", - target = "(Ljava/lang/Runnable;Ljava/lang/String;)Ljava/lang/Thread;" - ) - ) - private static Thread createTickThread(final Runnable target, final String name) { - return new TickThread(target, name); - } - */ - - /** - * @reason Make server thread an instance of TickThread for thread checks - * @author Spottedleaf - */ - @Redirect( - method = "spin", - at = @At( - value = "NEW", - target = "(Ljava/lang/ThreadGroup;Ljava/lang/Runnable;Ljava/lang/String;)Ljava/lang/Thread;" - ) - ) - private static Thread createTickThreadNeo(final ThreadGroup group, final Runnable task, final String name) { - return new TickThread(task, name, group); - } - - /** * @reason Close logic is re-written so that we do not wait for tasks to complete and unload everything * but rather we halt all task processing and then save. diff --git a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/ServerLevelMixin.java b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/ServerLevelMixin.java index db92dbf9..22e0887b 100644 --- a/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/ServerLevelMixin.java +++ b/src/main/java/ca/spottedleaf/moonrise/mixin/chunk_system/ServerLevelMixin.java @@ -452,6 +452,7 @@ private void redirectAutoSave(final PersistentEntitySectionManager insta method = "addPlayer", at = @At( value = "INVOKE", + // TODO - this is currently covering addEntity & addNewEntityWithoutEvent on NeoForge - split into Fabric & Neo mixins? target = "Lnet/minecraft/world/level/entity/PersistentEntitySectionManager;*(Lnet/minecraft/world/level/entity/EntityAccess;)Z" ) ) diff --git a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/chunk/ChunkSystemDistanceManager.java b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/chunk/ChunkSystemDistanceManager.java index 883fe640..aacd543f 100644 --- a/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/chunk/ChunkSystemDistanceManager.java +++ b/src/main/java/ca/spottedleaf/moonrise/patches/chunk_system/level/chunk/ChunkSystemDistanceManager.java @@ -1,9 +1,12 @@ package ca.spottedleaf.moonrise.patches.chunk_system.level.chunk; +import ca.spottedleaf.moonrise.patches.chunk_system.scheduling.ChunkHolderManager; import net.minecraft.server.level.ChunkMap; public interface ChunkSystemDistanceManager { public ChunkMap moonrise$getChunkMap(); + public ChunkHolderManager moonrise$getChunkHolderManager(); + }