Skip to content

Commit

Permalink
Split some mixins to their platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Aug 5, 2024
1 parent faca605 commit aa114b2
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -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 <T> void addRegionTicket(final TicketType<T> 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 <T> void removeRegionTicket(final TicketType<T> type, final ChunkPos pos, final int radius, final T identifier) {
this.moonrise$getChunkHolderManager().removeTicketAtLevel(type, pos, ChunkLevel.byStatus(FullChunkStatus.FULL) - radius, identifier);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
2 changes: 2 additions & 0 deletions fabric/src/main/resources/moonrise-fabric.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"parent": "moonrise.mixins.json",
"package": "ca.spottedleaf.moonrise.fabric.mixin",
"mixins": [
"chunk_system.FabricDistanceManagerMixin",
"chunk_system.FabricMinecraftServerMixin"
],
"client": [
]
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <T> void addRegionTicket(final TicketType<T> 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 <T> void removeRegionTicket(final TicketType<T> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
2 changes: 2 additions & 0 deletions neoforge/src/main/resources/moonrise-neoforge.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"parent": "moonrise.mixins.json",
"package": "ca.spottedleaf.moonrise.neoforge.mixin",
"mixins": [
"chunk_system.NeoForgeDistanceManagerMixin",
"chunk_system.NeoForgeMinecraftServerMixin"
],
"client": [
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -111,7 +108,7 @@ private ChunkHolderManager getChunkHolderManager() {
*/
@Overwrite
public void purgeStaleTickets() {
this.getChunkHolderManager().tick();
this.moonrise$getChunkHolderManager().tick();
}

/**
Expand All @@ -120,7 +117,7 @@ public void purgeStaleTickets() {
*/
@Overwrite
public boolean runAllUpdates(final ChunkMap chunkStorage) {
return this.getChunkHolderManager().processTicketUpdates();
return this.moonrise$getChunkHolderManager().processTicketUpdates();
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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 <T> void addRegionTicket(final TicketType<T> 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 <T> void removeRegionTicket(final TicketType<T> 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);
}

/**
Expand All @@ -175,9 +154,9 @@ public SortedArraySet<Ticket<?>> 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);
}
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -186,39 +184,6 @@ private static <S> void initHook(Function<Thread, S> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ private void redirectAutoSave(final PersistentEntitySectionManager<Entity> 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"
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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();

}

0 comments on commit aa114b2

Please sign in to comment.