Skip to content

Commit

Permalink
made stronghold generation thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregor0410 committed Sep 13, 2021
1 parent e53572e commit 42c83a2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package gregor0410.lazystronghold;

import net.minecraft.util.math.ChunkPos;

import java.util.List;

public interface ChunkGeneratorInterface {
StrongholdGen getStrongholdGen();
List<ChunkPos> getStrongholds();
}
26 changes: 12 additions & 14 deletions src/main/java/gregor0410/lazystronghold/StrongholdGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.Biomes;
import net.minecraft.world.biome.source.BiomeSource;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.chunk.StrongholdConfig;
import net.minecraft.world.gen.chunk.StructuresConfig;
import net.minecraft.world.gen.feature.StructureFeature;
import org.apache.logging.log4j.Level;

Expand All @@ -19,36 +17,36 @@

public class StrongholdGen implements Runnable {
private final StrongholdConfig config;
private ChunkGenerator generator;
private Thread thread;
private final Thread thread;
private Random random;
private int i;
private int j;
private int k;
private double d;
private int l;
private int m;
private int n;
private BiomeSource biomeSource;
private long seed;
private int n = 0;
private final BiomeSource biomeSource;
private final long seed;
private ArrayList<Biome> list;
public List<ChunkPos> strongholds;

public StrongholdGen(ChunkGenerator generator){
this.generator = generator;
this.seed = ((ChunkGeneratorAccess)generator).getField_24748();
this.biomeSource = generator.getBiomeSource();
this.biomeSource = generator.getBiomeSource().withSeed(seed); //create new biome source instance for thread safety
this.thread = new Thread(this,"Stronghold thread");
this.config = this.generator.getConfig().getStronghold();
this.config = generator.getConfig().getStronghold();
this.strongholds = ((ChunkGeneratorInterface)generator).getStrongholds();
}
public void start(){
this.thread.start();
}
@Override
public void run() {
Lazystronghold.log(Level.INFO,"Started stronghold gen thread");
while(!generateStronghold());
if(((ChunkGeneratorAccess)this.generator).getField_24749().size()!=this.config.getCount()){
Lazystronghold.log(Level.ERROR,"Only "+((ChunkGeneratorAccess)this.generator).getField_24749().size() +" strongholds generated!");
while(!this.generateStronghold());
if(this.strongholds.size()!=this.config.getCount()){
Lazystronghold.log(Level.ERROR,"Only "+this.strongholds.size() +" strongholds generated!");
}else{
Lazystronghold.log(Level.INFO,"Generated "+this.config.getCount() +" strongholds.");
}
Expand Down Expand Up @@ -79,7 +77,7 @@ private boolean generateStronghold(){
o = blockPos.getX() >> 4;
p = blockPos.getZ() >> 4;
}
((ChunkGeneratorAccess)this.generator).getField_24749().add(new ChunkPos(o,p));
this.strongholds.add(new ChunkPos(o,p));
d += Math.PI * 2 / (double)k;
++l;
if (l == k) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@

import gregor0410.lazystronghold.ChunkGeneratorInterface;
import gregor0410.lazystronghold.StrongholdGen;
import net.minecraft.world.biome.source.BiomeSource;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.chunk.StructuresConfig;
import net.minecraft.world.gen.feature.StructureFeature;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Final;
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.ModifyVariable;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Mixin(ChunkGenerator.class)
public class ChunkGeneratorMixin implements ChunkGeneratorInterface {
@Shadow @Final protected BiomeSource biomeSource;
@Shadow @Final private StructuresConfig config;
private StrongholdGen strongholdGen = null;
private final List<ChunkPos> strongholds = new CopyOnWriteArrayList<>();


@Inject(method="method_28509",at=@At("HEAD"),cancellable = true)
private void genStrongholds(CallbackInfo ci){
Expand All @@ -28,9 +35,21 @@ private void genStrongholds(CallbackInfo ci){
}
ci.cancel();
}

@ModifyVariable(at=@At("STORE"),ordinal = 0,method="locateStructure(Lnet/minecraft/server/world/ServerWorld;Lnet/minecraft/world/gen/feature/StructureFeature;Lnet/minecraft/util/math/BlockPos;IZ)Lnet/minecraft/util/math/BlockPos;")
private Iterator<ChunkPos> getStrongholdIterator(Iterator<ChunkPos> i){
return this.strongholds.iterator();
}
@Redirect(method="method_28507(Lnet/minecraft/util/math/ChunkPos;)Z",at=@At(value="FIELD",target="Lnet/minecraft/world/gen/chunk/ChunkGenerator;field_24749:Ljava/util/List;",opcode = Opcodes.GETFIELD))
private List<ChunkPos> method_28507_redirect(ChunkGenerator generator){
return this.strongholds;
}
@Override
public StrongholdGen getStrongholdGen() {
return this.strongholdGen;
}

@Override
public List<ChunkPos> getStrongholds() {
return this.strongholds;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package gregor0410.lazystronghold.mixin;

import gregor0410.lazystronghold.ChunkGeneratorInterface;
import gregor0410.lazystronghold.Lazystronghold;
import gregor0410.lazystronghold.StrongholdGen;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.SaveProperties;
import net.minecraft.world.World;
import org.apache.logging.log4j.Level;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -23,12 +20,12 @@
public class MinecraftServerMixin {
@Shadow @Final private Map<RegistryKey<World>, ServerWorld> worlds;

@Shadow @Final protected SaveProperties saveProperties;

@Inject(method="Lnet/minecraft/server/MinecraftServer;prepareStartRegion(Lnet/minecraft/server/WorldGenerationProgressListener;)V",at=@At("TAIL"))
private void prepareStartRegion(CallbackInfo ci){
this.worlds.get(World.OVERWORLD).getChunkManager().getChunkGenerator().method_28507(new ChunkPos(0,0));
StrongholdGen strongholdGen = ((ChunkGeneratorInterface)this.worlds.get(World.OVERWORLD).getChunkManager().getChunkGenerator()).getStrongholdGen();
strongholdGen.start();
if(strongholdGen!=null) {
strongholdGen.start();
}
}
}

0 comments on commit 42c83a2

Please sign in to comment.