mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 14:38:53 +00:00
New API methods, refactor position object to use static creators
This commit is contained in:
1 parent
42adc13425
commit
6b8c515b61
42 files changed
+587
-232
No files matched your search
+20
-14
@@ -13,19 +13,19 @@ import java.util.concurrent.CompletableFuture;
|
||||
/**
|
||||
* A random teleport engine that uses a Gaussian normal distribution to generate random positions.
|
||||
*/
|
||||
public class NormalDistributionEngine extends RandomTeleportEngine {
|
||||
public final class NormalDistributionEngine extends RandomTeleportEngine {
|
||||
|
||||
protected final int radius;
|
||||
protected final int spawnRadius;
|
||||
private final int radius;
|
||||
private final int spawnRadius;
|
||||
private final float mean;
|
||||
private final float standardDeviation;
|
||||
|
||||
public NormalDistributionEngine(@NotNull HuskHomes implementor) {
|
||||
super(implementor, "Normal Distribution");
|
||||
this.radius = implementor.getSettings().getRtpRadius();
|
||||
this.spawnRadius = implementor.getSettings().getRtpSpawnRadius();
|
||||
this.mean = implementor.getSettings().getRtpDistributionMean();
|
||||
this.standardDeviation = implementor.getSettings().getRtpDistributionStandardDeviation();
|
||||
public NormalDistributionEngine(@NotNull HuskHomes plugin) {
|
||||
super(plugin, "Normal Distribution");
|
||||
this.radius = plugin.getSettings().getRtpRadius();
|
||||
this.spawnRadius = plugin.getSettings().getRtpSpawnRadius();
|
||||
this.mean = plugin.getSettings().getRtpDistributionMean();
|
||||
this.standardDeviation = plugin.getSettings().getRtpDistributionStandardDeviation();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,7 +36,7 @@ public class NormalDistributionEngine extends RandomTeleportEngine {
|
||||
* @return A generated location
|
||||
*/
|
||||
@NotNull
|
||||
protected static Location generateLocation(@NotNull Location origin, float mean, float standardDeviation,
|
||||
public static Location generateLocation(@NotNull Location origin, float mean, float standardDeviation,
|
||||
float spawnRadius, float maxRadius) {
|
||||
// Generate random values
|
||||
final float radius = getDistributedRadius(mean, standardDeviation, spawnRadius, maxRadius);
|
||||
@@ -46,7 +46,12 @@ public class NormalDistributionEngine extends RandomTeleportEngine {
|
||||
final float z = (float) (radius * Math.cos(angle));
|
||||
final float x = (float) (radius * Math.sin(angle));
|
||||
|
||||
return new Location(Math.round(origin.getX()) + x, 128d, Math.round(origin.getZ()) + z, origin.getWorld());
|
||||
return Location.at(
|
||||
Math.round(origin.getX()) + x,
|
||||
128d,
|
||||
Math.round(origin.getZ()) + z,
|
||||
origin.getWorld()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,7 +62,7 @@ public class NormalDistributionEngine extends RandomTeleportEngine {
|
||||
*/
|
||||
private CompletableFuture<Optional<Location>> generateSafeLocation(@NotNull World world) {
|
||||
return plugin.resolveSafeGroundLocation(generateLocation(
|
||||
getOrigin(world), mean, standardDeviation, spawnRadius, radius));
|
||||
getCenterPoint(world), mean, standardDeviation, spawnRadius, radius));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,10 +95,11 @@ public class NormalDistributionEngine extends RandomTeleportEngine {
|
||||
int attempts = 0;
|
||||
while (location.isEmpty()) {
|
||||
location = generateSafeLocation(world).join();
|
||||
if (attempts > randomTimeout) {
|
||||
if (attempts >= maxAttempts) {
|
||||
return Optional.empty();
|
||||
}
|
||||
attempts++;
|
||||
}
|
||||
return location.map(resolved -> new Position(resolved, plugin.getServerName()));
|
||||
return location.map(resolved -> Position.at(resolved, plugin.getServerName()));
|
||||
}
|
||||
}
|
||||
@@ -12,19 +12,9 @@ import java.util.Optional;
|
||||
*/
|
||||
public abstract class RandomTeleportEngine {
|
||||
|
||||
@NotNull
|
||||
protected final HuskHomes plugin;
|
||||
|
||||
/**
|
||||
* The name of the random teleport engine
|
||||
*/
|
||||
@NotNull
|
||||
public final String name;
|
||||
|
||||
/**
|
||||
* How many attempts to allow {@link #getRandomPosition} lookups before timing out
|
||||
*/
|
||||
public long randomTimeout = 8;
|
||||
public long maxAttempts = 12;
|
||||
|
||||
/**
|
||||
* Constructor for a random teleport engine
|
||||
@@ -37,15 +27,21 @@ public abstract class RandomTeleportEngine {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public final String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the origin position (spawn) of this server
|
||||
*
|
||||
* @return The origin position
|
||||
*/
|
||||
protected final Position getOrigin(@NotNull World world) {
|
||||
@NotNull
|
||||
protected Position getCenterPoint(@NotNull World world) {
|
||||
return plugin.getLocalCachedSpawn()
|
||||
.map(s -> s.getPosition(plugin.getServerName()))
|
||||
.orElse(new Position(0d, 128d, 0d, 0f, 0f,
|
||||
.orElse(Position.at(0d, 128d, 0d, 0f, 0f,
|
||||
world, plugin.getServerName()));
|
||||
}
|
||||
|
||||
@@ -56,7 +52,7 @@ public abstract class RandomTeleportEngine {
|
||||
* @param world The world to find a random position in
|
||||
* @param args The arguments to pass to the random teleport engine
|
||||
* @return The position, optionally, which will be empty if the random teleport engine timed out after a
|
||||
* {@link #randomTimeout configured number of attempts}
|
||||
* {@link #maxAttempts configured number of attempts}
|
||||
*/
|
||||
public abstract Optional<Position> getRandomPosition(@NotNull World world, @NotNull String[] args);
|
||||
|
||||
|
||||
Reference in new issue
Block a user