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