Further improvements to the random teleport engine caching system

This commit is contained in:
William278 committed 2022-09-08 18:34:52 +03:00
1 parent e5bca1dad0
commit ab0455c29a
2 files changed
+24 -34

No files matched your search

@@ -7,7 +7,6 @@ import org.jetbrains.annotations.NotNull;
import java.util.Optional; import java.util.Optional;
import java.util.Random; import java.util.Random;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
/** /**
* A random teleport engine that uses a Gaussian normal distribution to generate random positions. * A random teleport engine that uses a Gaussian normal distribution to generate random positions.
@@ -32,16 +31,7 @@ public class NormalDistributionEngine extends RandomTeleportEngine {
@Override @Override
protected @NotNull CompletableFuture<Optional<Location>> generateRandomLocation(@NotNull Location origin, protected @NotNull CompletableFuture<Optional<Location>> generateRandomLocation(@NotNull Location origin,
@NotNull String... args) { @NotNull String... args) {
return CompletableFuture.supplyAsync(() -> { return plugin.getSafeGroundLocation(generateLocation(origin, mean, standardDeviation, spawnRadius, radius));
Optional<Location> generated = plugin.getSafeGroundLocation(generateLocation(origin, mean,
standardDeviation, spawnRadius, radius)).join();
while (generated.isEmpty()) {
generated = plugin.getSafeGroundLocation(generateLocation(origin, mean, standardDeviation,
spawnRadius, radius)).join();
}
return generated;
}).orTimeout(5, TimeUnit.SECONDS)
.exceptionally(throwable -> Optional.empty());
} }
/** /**
@@ -36,10 +36,10 @@ public abstract class RandomTeleportEngine {
@NotNull @NotNull
private final List<World> worlds; private final List<World> worlds;
/** /**
* The cache of random positions for each world * A cache of random {@link Location}s for each random teleport world
*/ */
@NotNull @NotNull
private final Map<World, Queue<Location>> cachedRandomLocations; private final Map<World, Queue<Location>> cachedPositions;
/** /**
* <b>(Internal use only)</b> Create a new random teleport engine, using HuskHomes config settings * <b>(Internal use only)</b> Create a new random teleport engine, using HuskHomes config settings
@@ -54,7 +54,7 @@ public abstract class RandomTeleportEngine {
.filter(world -> world.environment != World.Environment.NETHER) .filter(world -> world.environment != World.Environment.NETHER)
.filter(world -> !implementor.getSettings().rtpRestrictedWorlds.contains(world.name)) .filter(world -> !implementor.getSettings().rtpRestrictedWorlds.contains(world.name))
.collect(Collectors.toList()); .collect(Collectors.toList());
this.cachedRandomLocations = new HashMap<>(); this.cachedPositions = new HashMap<>();
} }
/** /**
@@ -71,7 +71,7 @@ public abstract class RandomTeleportEngine {
this.name = name; this.name = name;
this.cacheSize = 10; this.cacheSize = 10;
this.worlds = worlds; this.worlds = worlds;
this.cachedRandomLocations = new HashMap<>(); this.cachedPositions = new HashMap<>();
} }
/** /**
@@ -83,7 +83,7 @@ public abstract class RandomTeleportEngine {
public void initialize() { public void initialize() {
CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
for (final World world : worlds) { for (final World world : worlds) {
cachedRandomLocations.put(world, new LinkedList<>()); cachedPositions.put(world, new LinkedList<>());
populateCache(world, cacheSize); populateCache(world, cacheSize);
} }
}); });
@@ -98,12 +98,17 @@ public abstract class RandomTeleportEngine {
* @since 3.0 * @since 3.0
*/ */
private void populateCache(@NotNull World world, final int cacheSize) { private void populateCache(@NotNull World world, final int cacheSize) {
final int amountToPopulate = Math.max(1, cacheSize) - cachedRandomLocations.get(world).size(); CompletableFuture.runAsync(() -> {
final int amountToPopulate = Math.max(1, cacheSize) - cachedPositions.get(world).size();
final Location originLocation = new Location(0, 0, 0, world); final Location originLocation = new Location(0, 0, 0, world);
for (int i = 0; i < amountToPopulate; i++) { for (int i = 0; i < amountToPopulate; i++) {
generateRandomLocation(originLocation).thenAccept(location -> location generateRandomLocation(originLocation)
.ifPresent(generated -> cachedRandomLocations.get(world).add(generated))); .thenAcceptAsync(location -> location
.ifPresent(generated -> cachedPositions.get(world).add(generated)))
.orTimeout(10, TimeUnit.SECONDS)
.exceptionally(throwable -> null);
} }
});
} }
/** /**
@@ -149,14 +154,8 @@ public abstract class RandomTeleportEngine {
*/ */
public CompletableFuture<Optional<Position>> getRandomPosition(@NotNull OnlineUser onlineUser, public CompletableFuture<Optional<Position>> getRandomPosition(@NotNull OnlineUser onlineUser,
@NotNull String... args) { @NotNull String... args) {
final Position userPosition = onlineUser.getPosition(); return getRandomLocation(onlineUser.getPosition().world, args).thenApply(location ->
return getRandomLocation(userPosition.world, args).thenApply(location -> { location.map(position -> new Position(position, onlineUser.getPosition().server)));
if (location.isEmpty()) {
return Optional.empty();
}
return Optional.of(new Position(location.get().x, location.get().y, location.get().z, location.get().yaw,
location.get().pitch, location.get().world, userPosition.server));
});
} }
/** /**
@@ -170,20 +169,21 @@ public abstract class RandomTeleportEngine {
* @since 3.0 * @since 3.0
*/ */
private CompletableFuture<Optional<Location>> getRandomLocation(@NotNull World world, @NotNull String... args) { private CompletableFuture<Optional<Location>> getRandomLocation(@NotNull World world, @NotNull String... args) {
return CompletableFuture.supplyAsync(() -> { final Optional<World> keyedWorld = cachedPositions.keySet().stream()
final Optional<World> keyedWorld = cachedRandomLocations.keySet().stream() .filter(cachedWorld -> cachedWorld.uuid.equals(world.uuid))
.filter(cachedWorld -> cachedWorld.uuid.equals(world.uuid)).findFirst(); .findFirst();
if (keyedWorld.isPresent()) { if (keyedWorld.isPresent()) {
final Queue<Location> cachedWorldLocations = cachedRandomLocations.get(keyedWorld.get()); final Queue<Location> cachedWorldLocations = cachedPositions.get(keyedWorld.get());
if (!cachedWorldLocations.isEmpty()) { if (!cachedWorldLocations.isEmpty()) {
populateCache(keyedWorld.get(), cacheSize); populateCache(keyedWorld.get(), cacheSize);
return Optional.of(cachedWorldLocations.remove()); return CompletableFuture.completedFuture(Optional.of(cachedWorldLocations.remove()));
} else { } else {
populateCache(keyedWorld.get(), cacheSize); populateCache(keyedWorld.get(), cacheSize);
} }
} }
return generateRandomLocation(new Location(0, 0, 0, 0f, 0f, world), args).join(); return generateRandomLocation(new Location(0, 0, 0, world), args)
}).orTimeout(5, TimeUnit.SECONDS).exceptionally(throwable -> Optional.empty()); .orTimeout(5, TimeUnit.SECONDS)
.exceptionally(throwable -> Optional.empty());
} }
} }