Fix edithome permission logic check & warn against bad rtp spawn radius (#531)

* Fixup edithome logic

* Add warning against bad spawn radius
This commit is contained in:
William authored and GitHub committed 2023-12-05 16:30:26 +00:00
1 parent 8a69e4a075
commit 859993b37e
3 files changed
+28 -10

No files matched your search

@@ -184,15 +184,19 @@ public class EditHomeCommand extends SavedPositionCommand<Home> {
// Set the home privacy // Set the home privacy
home.setPublic(parseStringArg(args, 1) home.setPublic(parseStringArg(args, 1)
.map(String::toLowerCase).map("public"::equals) .map("public"::equalsIgnoreCase)
.orElse(!home.isPublic())); .orElse(!home.isPublic()));
// Fire the event
plugin.fireEvent(plugin.getHomeEditEvent(home, executor), (event) -> { plugin.fireEvent(plugin.getHomeEditEvent(home, executor), (event) -> {
try { try {
plugin.getManager().homes().setHomePrivacy(event.getHome(), home.isPublic()); plugin.getManager().homes().setHomePrivacy(
home.getOwner().equals(executor) ? (OnlineUser) executor : home.getOwner(),
home,
home.isPublic()
);
} catch (ValidationException e) { } catch (ValidationException e) {
int maxHomes = plugin.getManager().homes() int maxHomes = plugin.getManager().homes().getMaxPublicHomes(executor instanceof OnlineUser user ? user : null);
.getMaxPublicHomes(executor instanceof OnlineUser user ? user : null);
e.dispatchHomeError(executor, false, plugin, Integer.toString(maxHomes)); e.dispatchHomeError(executor, false, plugin, Integer.toString(maxHomes));
return; return;
} }
@@ -336,14 +336,16 @@ public class HomesManager {
throw new ValidationException(ValidationException.Type.NOT_FOUND); throw new ValidationException(ValidationException.Type.NOT_FOUND);
} }
this.setHomePrivacy(optionalHome.get(), isPublic); this.setHomePrivacy(owner, optionalHome.get(), isPublic);
} }
public void setHomePrivacy(@NotNull Home home, boolean isPublic) { public void setHomePrivacy(@NotNull Home home, boolean isPublic) {
if (isPublic && home.getOwner() instanceof OnlineUser online) { this.setHomePrivacy(home.getOwner(), home, isPublic);
final int publicHomes = plugin.getDatabase().getHomes(home.getOwner()).stream() }
.filter(Home::isPublic)
.toList().size(); public void setHomePrivacy(@NotNull User owner, @NotNull Home home, boolean isPublic) {
if (isPublic && owner instanceof OnlineUser online) {
int publicHomes = plugin.getDatabase().getHomes(owner).stream().filter(Home::isPublic).toList().size();
if (publicHomes >= getMaxPublicHomes(online)) { if (publicHomes >= getMaxPublicHomes(online)) {
throw new ValidationException(ValidationException.Type.REACHED_MAX_PUBLIC_HOMES); throw new ValidationException(ValidationException.Type.REACHED_MAX_PUBLIC_HOMES);
} }
@@ -28,6 +28,7 @@ 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.logging.Level;
/** /**
* 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.
@@ -42,11 +43,22 @@ public final class NormalDistributionEngine extends RandomTeleportEngine {
public NormalDistributionEngine(@NotNull HuskHomes plugin) { public NormalDistributionEngine(@NotNull HuskHomes plugin) {
super(plugin, "Normal Distribution"); super(plugin, "Normal Distribution");
this.radius = plugin.getSettings().getRtpRadius(); this.radius = plugin.getSettings().getRtpRadius();
this.spawnRadius = plugin.getSettings().getRtpSpawnRadius(); this.spawnRadius = determineSpawnRadius(radius, plugin.getSettings().getRtpSpawnRadius(), plugin);
this.mean = plugin.getSettings().getRtpDistributionMean(); this.mean = plugin.getSettings().getRtpDistributionMean();
this.standardDeviation = plugin.getSettings().getRtpDistributionStandardDeviation(); this.standardDeviation = plugin.getSettings().getRtpDistributionStandardDeviation();
} }
// Utility for determining a valid spawn radius
private static int determineSpawnRadius(int radius, int spawnRadius, @NotNull HuskHomes plugin) {
if (spawnRadius >= radius) {
plugin.log(Level.WARNING, "The RTP spawn radius is greater than or equal to the RTP radius. " +
"This will result in the RTP engine being unable to find a suitable location to teleport to. " +
"Please set the RTP spawn radius to a value less than the RTP radius.");
return radius - 1;
}
return spawnRadius;
}
/** /**
* Generate a {@link Location} through a randomized normally-distributed radius and random angle using the mean and * Generate a {@link Location} through a randomized normally-distributed radius and random angle using the mean and
* standard deviation, about the origin position. * standard deviation, about the origin position.