generated from quentin452/Catzmod1.7.10
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
956d802
commit 116050b
Showing
4 changed files
with
99 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/fr/iamacat/multithreading/mixins/common/core/MixinChunkProviderServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package fr.iamacat.multithreading.mixins.common.core; | ||
|
||
import fr.iamacat.multithreading.config.MultithreadingandtweaksMultithreadingConfig; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.world.ChunkCoordIntPair; | ||
import net.minecraft.world.World; | ||
import net.minecraft.world.WorldServer; | ||
import net.minecraft.world.chunk.Chunk; | ||
import net.minecraft.world.gen.ChunkProviderServer; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
@Mixin(ChunkProviderServer.class) | ||
public class MixinChunkProviderServer { | ||
@Shadow | ||
public Chunk loadChunk(int x, int z) { | ||
return null; | ||
} | ||
|
||
@Inject(method = "tick", at = @At("HEAD"), cancellable = true) | ||
private void tick(CallbackInfo ci) { | ||
if (MultithreadingandtweaksMultithreadingConfig.enableMixinChunkProviderServer) { | ||
try { | ||
// Number of chunks to process per tick | ||
int batchSize = MultithreadingandtweaksMultithreadingConfig.batchsize; | ||
// Number of threads to use | ||
int numThreads = MultithreadingandtweaksMultithreadingConfig.numberofcpus; | ||
|
||
// Create a list of chunks to process | ||
List<Chunk> chunksToProcess = new ArrayList<>(); | ||
WorldServer world = MinecraftServer.getServer().worldServers[0]; | ||
ChunkProviderServer chunkProvider = (ChunkProviderServer) world.getChunkProvider(); | ||
|
||
// Use reflection to make the currentChunkSet field accessible | ||
Field currentChunkSetField = ChunkProviderServer.class.getDeclaredField("currentChunkSet"); | ||
currentChunkSetField.setAccessible(true); | ||
ChunkCoordIntPair currentChunkSet = (ChunkCoordIntPair) currentChunkSetField.get(chunkProvider); | ||
|
||
for (int chunkX = currentChunkSet.chunkXPos - batchSize; chunkX <= currentChunkSet.chunkXPos + batchSize; chunkX++) { | ||
for (int chunkZ = currentChunkSet.chunkZPos - batchSize; chunkZ <= currentChunkSet.chunkZPos + batchSize; chunkZ++) { | ||
Chunk chunk = chunkProvider.loadChunk(chunkX, chunkZ); | ||
if (chunk != null) { | ||
chunksToProcess.add(chunk); | ||
} | ||
} | ||
} | ||
|
||
// Process the chunks using a thread pool | ||
ExecutorService executor = Executors.newFixedThreadPool(numThreads); | ||
for (Chunk chunk : chunksToProcess) { | ||
executor.execute(() -> { | ||
ChunkCoordIntPair chunkCoords = new ChunkCoordIntPair(chunk.xPosition, chunk.zPosition); | ||
if (chunk.isModified) { | ||
chunk.isModified = false; | ||
chunkProvider.saveChunks(true, null); | ||
} | ||
}); | ||
} | ||
executor.shutdown(); | ||
|
||
ci.cancel(); | ||
} catch (NoSuchFieldException | IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
|