Fixup plan expansion and RTP engine

This commit is contained in:
William278 committed 2023-03-20 15:10:21 +00:00
1 parent 8b809b6b5b
commit 547870e6c1
7 files changed
+142 -194

No files matched your search

@@ -3,7 +3,6 @@ package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.position.Warp;
import net.william278.huskhomes.user.CommandUser;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.util.ValidationException;
import org.jetbrains.annotations.NotNull;
@@ -25,9 +24,10 @@ public class DelWarpCommand extends SavedPositionCommand<Warp> {
@Override
public void execute(@NotNull CommandUser executor, @NotNull Warp warp, @NotNull String[] args) {
if (executor instanceof OnlineUser user && !warp.hasPermission(plugin.getSettings().isPermissionRestrictWarps(), user)) {
if (plugin.getSettings().isPermissionRestrictWarps() && !executor.hasPermission(warp.getPermission())
&& !executor.hasPermission(Warp.getWildcardPermission())) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(user::sendMessage);
.ifPresent(executor::sendMessage);
return;
}
@@ -1,132 +0,0 @@
package net.william278.huskhomes.hook;
import com.djrapitops.plan.extension.CallEvents;
import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.annotation.*;
import com.djrapitops.plan.extension.icon.Color;
import com.djrapitops.plan.extension.icon.Family;
import net.william278.huskhomes.database.Database;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.user.SavedUser;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
@PluginInfo(
name = "HuskHomes",
iconName = "home",
iconFamily = Family.SOLID,
color = Color.LIGHT_BLUE
)
@SuppressWarnings("unused")
public class PlanDataExtension implements DataExtension {
private Database database;
private boolean crossServer;
private static final String UNKNOWN_STRING = "N/A";
protected PlanDataExtension(@NotNull Database database, boolean crossServer) {
this.database = database;
}
protected PlanDataExtension() {
}
@Override
public CallEvents[] callExtensionMethodsOn() {
return new CallEvents[]{
CallEvents.PLAYER_JOIN,
CallEvents.PLAYER_LEAVE
};
}
@BooleanProvider(
text = "Has Data",
description = "Whether this user has HuskHomes data.",
iconName = "bell",
iconFamily = Family.SOLID,
conditionName = "hasData",
hidden = true
)
public boolean getHasUserData(@NotNull UUID uuid) {
return database.getUserData(uuid).isPresent();
}
@NumberProvider(
text = "Home Count",
description = "The number of homes this user has set.",
iconName = "home",
iconFamily = Family.SOLID,
priority = 5
)
@Conditional("hasData")
public long getHomeCount(@NotNull UUID uuid) {
return database.getUserData(uuid)
.map(userData -> (long) database.getHomes(userData.user()).size())
.orElse(0L);
}
@NumberProvider(
text = "Public Homes",
description = "The number of homes this user has made public.",
iconName = "sun",
iconFamily = Family.SOLID,
priority = 4
)
@Conditional("hasData")
public long getPublicHomeCount(@NotNull UUID uuid) {
return database.getUserData(uuid)
.map(userData -> database.getHomes(userData.user()).stream()
.filter(Home::isPublic).count())
.orElse(0L);
}
@NumberProvider(
text = "Home Slots",
description = "The number of extra home slots this user has purchased.",
iconName = "money-check-alt",
iconFamily = Family.SOLID,
priority = 3
)
@Conditional("hasData")
public long getPurchasedHomeSlots(@NotNull UUID uuid) {
return database.getUserData(uuid)
.map(userData -> (long) userData.homeSlots())
.orElse(0L);
}
@BooleanProvider(
text = "Ignoring /tpa Requests",
description = "Whether this player is ignoring /tpa requests.",
iconName = "phone-slash",
iconFamily = Family.SOLID,
priority = 2
)
@Conditional("hasData")
public boolean isIgnoringTeleportRequests(@NotNull UUID uuid) {
return database.getUserData(uuid)
.map(SavedUser::ignoringTeleports)
.orElse(false);
}
@StringProvider(
text = "Offline Position",
description = "The location where this user logged out.",
iconName = "door-open",
iconFamily = Family.SOLID,
priority = 1
)
@Conditional("hasData")
public String getOfflinePosition(@NotNull UUID uuid) {
return database.getUserData(uuid)
.map(userData -> database.getOfflinePosition(userData.user())
.map(position -> "x: " + (int) position.getX() + ", y: " + (int) position.getY() + ", z: " + (int) position.getZ()
+ " (" + position.getWorld().getName() +
((crossServer) ? "/" + position.getServer() + ")" : ")"))
.orElse(UNKNOWN_STRING))
.orElse(UNKNOWN_STRING);
}
}
@@ -1,11 +1,20 @@
package net.william278.huskhomes.hook;
import com.djrapitops.plan.capability.CapabilityService;
import com.djrapitops.plan.extension.CallEvents;
import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.ExtensionService;
import com.djrapitops.plan.extension.annotation.*;
import com.djrapitops.plan.extension.icon.Color;
import com.djrapitops.plan.extension.icon.Family;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.util.Logger;
import net.william278.huskhomes.database.Database;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.user.SavedUser;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.logging.Level;
/**
@@ -56,4 +65,121 @@ public class PlanHook extends PluginHook {
}
});
}
@PluginInfo(
name = "HuskHomes",
iconName = "home",
iconFamily = Family.SOLID,
color = Color.LIGHT_BLUE
)
@SuppressWarnings("unused")
protected static class PlanDataExtension implements DataExtension {
private Database database;
private boolean crossServer;
private static final String UNKNOWN_STRING = "N/A";
protected PlanDataExtension(@NotNull Database database, boolean crossServer) {
this.database = database;
}
protected PlanDataExtension() {
}
@Override
public CallEvents[] callExtensionMethodsOn() {
return new CallEvents[]{
CallEvents.PLAYER_JOIN,
CallEvents.PLAYER_LEAVE
};
}
@BooleanProvider(
text = "Has Data",
description = "Whether this user has HuskHomes data.",
iconName = "bell",
iconFamily = Family.SOLID,
conditionName = "hasData",
hidden = true
)
public boolean getHasUserData(@NotNull UUID uuid) {
return database.getUserData(uuid).isPresent();
}
@NumberProvider(
text = "Home Count",
description = "The number of homes this user has set.",
iconName = "home",
iconFamily = Family.SOLID,
priority = 5
)
@Conditional("hasData")
public long getHomeCount(@NotNull UUID uuid) {
return database.getUserData(uuid)
.map(userData -> (long) database.getHomes(userData.getUser()).size())
.orElse(0L);
}
@NumberProvider(
text = "Public Homes",
description = "The number of homes this user has made public.",
iconName = "sun",
iconFamily = Family.SOLID,
priority = 4
)
@Conditional("hasData")
public long getPublicHomeCount(@NotNull UUID uuid) {
return database.getUserData(uuid)
.map(userData -> database.getHomes(userData.getUser()).stream()
.filter(Home::isPublic).count())
.orElse(0L);
}
@NumberProvider(
text = "Home Slots",
description = "The number of extra home slots this user has purchased.",
iconName = "money-check-alt",
iconFamily = Family.SOLID,
priority = 3
)
@Conditional("hasData")
public long getPurchasedHomeSlots(@NotNull UUID uuid) {
return database.getUserData(uuid)
.map(userData -> (long) userData.getHomeSlots())
.orElse(0L);
}
@BooleanProvider(
text = "Ignoring /tpa Requests",
description = "Whether this player is ignoring /tpa requests.",
iconName = "phone-slash",
iconFamily = Family.SOLID,
priority = 2
)
@Conditional("hasData")
public boolean isIgnoringTeleportRequests(@NotNull UUID uuid) {
return database.getUserData(uuid)
.map(SavedUser::isIgnoringTeleports)
.orElse(false);
}
@StringProvider(
text = "Offline Position",
description = "The location where this user logged out.",
iconName = "door-open",
iconFamily = Family.SOLID,
priority = 1
)
@Conditional("hasData")
public String getOfflinePosition(@NotNull UUID uuid) {
return database.getUserData(uuid)
.map(userData -> database.getOfflinePosition(userData.getUser())
.map(Position::toString)
.orElse(UNKNOWN_STRING))
.orElse(UNKNOWN_STRING);
}
}
}
@@ -3,9 +3,6 @@ package net.william278.huskhomes.position;
import net.william278.huskhomes.teleport.Target;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.UUID;
/**
* Represents a position - a {@link Location} somewhere on the proxy network or server
*/
@@ -34,53 +31,6 @@ public class Position extends Location implements Target {
this.setServer(newPosition.getServer());
}
/**
* Parses the position from a set of values
*
* @param args The values to parse
* @param relativeTo The {@link Position} to use as a reference for relative coordinates
* @return The position if it could be parsed, otherwise an empty optional
*/
public static Optional<Position> parse(@NotNull String[] args, @NotNull Position relativeTo) {
// Validate argument length
if (args.length < 3 || args.length > 5) {
return Optional.empty();
}
// Parse, handle relatives, and return, catching NumberFormatExceptions and returning an empty optional
try {
final double x = parseCoordinate(args[0], relativeTo.getX());
final double y = parseCoordinate(args[1], relativeTo.getY());
final double z = parseCoordinate(args[2], relativeTo.getZ());
final World world = args.length > 3 ? new World(args[3], UUID.randomUUID()) : relativeTo.getWorld();
final String server = args.length > 4 ? args[4] : relativeTo.getServer();
return Optional.of(new Position(x, y, z, relativeTo.getYaw(), relativeTo.getPitch(), world, server));
} catch (NumberFormatException ignored) {
return Optional.empty();
}
}
/**
* Parses a coordinate double value from a string
*
* @param value The string to parse
* @param currentValue The current value of the coordinate, for handling relatives
* @return The parsed coordinate
* @throws NumberFormatException If the string could not be parsed
*/
private static double parseCoordinate(@NotNull String value, double currentValue) throws NumberFormatException {
// Future: Consider supporting ^ ^ ^ (position-relative-to-facing)
if (value.startsWith("~")) {
if (value.length() == 1) {
return currentValue;
} else {
return currentValue + Double.parseDouble(value.substring(1));
}
} else {
return Double.parseDouble(value);
}
}
/**
* The name of the server the position is on
*/
@@ -92,4 +42,13 @@ public class Position extends Location implements Target {
public void setServer(@NotNull String server) {
this.server = server;
}
@Override
public String toString() {
return "x: " + (int) getX() + ", " +
"y: " + (int) getY() + ", " +
"z: " + (int) getZ() + " " +
"(" + getWorld().getName() + " / " + getServer() + ")";
}
}
@@ -44,12 +44,7 @@ public abstract class RandomTeleportEngine {
*/
protected final Position getOrigin(@NotNull World world) {
return plugin.getLocalCachedSpawn()
.flatMap(spawn -> {
if (!spawn.worldUuid.equals(world.getUuid().toString())) {
return Optional.empty();
}
return spawn.getPosition(plugin.getServerName());
})
.map(s -> s.getPosition(plugin.getServerName()))
.orElse(new Position(0d, 128d, 0d, 0f, 0f,
world, plugin.getServerName()));
}