Minor changes to alternator.
This commit is contained in:
parent
60217e4672
commit
02f7de56ed
@ -1,8 +1,11 @@
|
||||
package ca.recrown.islandsurvivalcraft.world;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
|
||||
|
||||
import org.bukkit.World;
|
||||
|
||||
@ -14,19 +17,39 @@ import ca.recrown.islandsurvivalcraft.world.generation.IslandWorldGenerator;
|
||||
* Uses IslandWorldGenerator as the container for each world.
|
||||
*/
|
||||
public class IslandWorldGeneratorAlternator {
|
||||
private final ConcurrentHashMap<UUID, IslandWorldGenerator> chunkGenerators;
|
||||
private final HashMap<UUID, IslandWorldGenerator> chunkGenerators;
|
||||
private volatile IslandWorldGenerator lastGenerator;
|
||||
private volatile UUID lastUUID;
|
||||
private final IslandBiomeGenerator islandBiomeGenerator;
|
||||
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
|
||||
private final ReadLock readLock = lock.readLock();
|
||||
private final WriteLock writeLock = lock.writeLock();
|
||||
|
||||
public IslandWorldGeneratorAlternator(IslandBiomeGenerator biomeGenerator) {
|
||||
this.chunkGenerators = new ConcurrentHashMap<>();
|
||||
this.chunkGenerators = new HashMap<>();
|
||||
this.islandBiomeGenerator = biomeGenerator;
|
||||
}
|
||||
|
||||
public IslandWorldGenerator getIslandChunkGeneratorSystem(World world, Random random) {
|
||||
UUID wUuid = world.getUID();
|
||||
if (!chunkGenerators.containsKey(wUuid)) {
|
||||
chunkGenerators.put(wUuid, new IslandWorldGenerator(world, islandBiomeGenerator.getInstance(), random));
|
||||
if (lastUUID == null || (lastUUID.hashCode() != world.getUID().hashCode() && lastUUID.equals(world.getUID()))) {
|
||||
System.out.println("Alternating generator for world: " + world.getName());
|
||||
lastUUID = world.getUID();
|
||||
readLock.lock();
|
||||
try {
|
||||
lastGenerator = chunkGenerators.get(lastUUID);
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
if (lastGenerator == null) {
|
||||
lastGenerator = new IslandWorldGenerator(world, islandBiomeGenerator, random);
|
||||
writeLock.lock();
|
||||
try {
|
||||
chunkGenerators.put(lastUUID, lastGenerator);
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
return chunkGenerators.get(wUuid);
|
||||
return lastGenerator;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user