mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 22:16:08 +00:00
feat: allow /phome <name> on uniquely named homes
If multiple public homes with the same name exist, typing /phome (name) will bring up a filtered public home list containing the list of public homes with that name for you to click on. The username-prefixed name will also be the only one to show up in TAB. This commit also fixes a bug with home/warp lists showing a blank description on hover instead of `(none)`.
This commit is contained in:
32 files changed
+353
-81
No files matched your search
@@ -31,7 +31,7 @@ import java.util.List;
|
||||
public class DelHomeCommand extends SavedPositionCommand<Home> {
|
||||
|
||||
public DelHomeCommand(@NotNull HuskHomes plugin) {
|
||||
super("delhome", List.of(), Home.class, List.of(), plugin);
|
||||
super("delhome", List.of(), PositionCommandType.HOME, List.of(), plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -30,7 +30,7 @@ import java.util.List;
|
||||
public class DelWarpCommand extends SavedPositionCommand<Warp> {
|
||||
|
||||
public DelWarpCommand(@NotNull HuskHomes plugin) {
|
||||
super("delwarp", List.of(), Warp.class, List.of(), plugin);
|
||||
super("delwarp", List.of(), PositionCommandType.WARP, List.of(), plugin);
|
||||
setOperatorCommand(true);
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,13 @@ import java.util.regex.Pattern;
|
||||
public class EditHomeCommand extends SavedPositionCommand<Home> {
|
||||
|
||||
public EditHomeCommand(@NotNull HuskHomes plugin) {
|
||||
super("edithome", List.of(), Home.class, List.of("rename", "description", "relocate", "privacy"), plugin);
|
||||
super(
|
||||
"edithome",
|
||||
List.of(),
|
||||
PositionCommandType.HOME,
|
||||
List.of("rename", "description", "relocate", "privacy"),
|
||||
plugin
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -37,7 +37,13 @@ import java.util.regex.Pattern;
|
||||
public class EditWarpCommand extends SavedPositionCommand<Warp> {
|
||||
|
||||
public EditWarpCommand(@NotNull HuskHomes plugin) {
|
||||
super("editwarp", List.of(), Warp.class, List.of("rename", "description", "relocate"), plugin);
|
||||
super(
|
||||
"editwarp",
|
||||
List.of(),
|
||||
PositionCommandType.WARP,
|
||||
List.of("rename", "description", "relocate"),
|
||||
plugin
|
||||
);
|
||||
setOperatorCommand(true);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,8 +32,9 @@ import java.util.Optional;
|
||||
|
||||
public abstract class HomeCommand extends SavedPositionCommand<Home> {
|
||||
|
||||
protected HomeCommand(@NotNull String name, @NotNull List<String> aliases, @NotNull HuskHomes plugin) {
|
||||
super(name, aliases, Home.class, List.of("player"), plugin);
|
||||
protected HomeCommand(@NotNull String name, @NotNull List<String> aliases, @NotNull PositionCommandType type,
|
||||
@NotNull HuskHomes plugin) {
|
||||
super(name, aliases, type, List.of("player"), plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -109,13 +109,17 @@ public class HomeListCommand extends ListCommand {
|
||||
final PaginatedList homeList = PaginatedList.of(homes.stream().map(home ->
|
||||
plugin.getLocales()
|
||||
.getRawLocale("home_list_item",
|
||||
Locales.escapeText(home.getName()), home.getSafeIdentifier(),
|
||||
Locales.escapeText(home.getName()),
|
||||
home.getSafeIdentifier(),
|
||||
home.isPublic()
|
||||
? plugin.getLocales().getRawLocale("home_is_public")
|
||||
.orElse("Public")
|
||||
: plugin.getLocales().getRawLocale("home_is_private")
|
||||
.orElse("Private"),
|
||||
Locales.escapeText(home.getMeta().getDescription()))
|
||||
home.getMeta().getDescription().isBlank()
|
||||
? plugin.getLocales().getNone()
|
||||
: Locales.escapeText(home.getMeta().getDescription())
|
||||
)
|
||||
.orElse(home.getName())).sorted().collect(Collectors.toList()),
|
||||
plugin.getLocales()
|
||||
.getBaseList(plugin.getSettings().getGeneral().getListItemsPerPage())
|
||||
|
||||
@@ -30,7 +30,7 @@ import java.util.List;
|
||||
public class PrivateHomeCommand extends HomeCommand {
|
||||
|
||||
protected PrivateHomeCommand(@NotNull HuskHomes plugin) {
|
||||
super("home", List.of(), plugin);
|
||||
super("home", List.of(), PositionCommandType.HOME, plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.position.Home;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -28,7 +29,7 @@ import java.util.List;
|
||||
public class PublicHomeCommand extends HomeCommand implements TabProvider {
|
||||
|
||||
protected PublicHomeCommand(@NotNull HuskHomes plugin) {
|
||||
super("phome", List.of("publichome"), plugin);
|
||||
super("phome", List.of("publichome"), PositionCommandType.PUBLIC_HOME, plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -36,7 +37,7 @@ public class PublicHomeCommand extends HomeCommand implements TabProvider {
|
||||
// Display the public home list if no arguments are provided
|
||||
if (args.length == 0) {
|
||||
plugin.getCommand(PublicHomeListCommand.class)
|
||||
.ifPresent(command -> command.showPublicHomeList(executor, 1));
|
||||
.ifPresent(command -> command.showPublicHomeList(executor, null, 1));
|
||||
return;
|
||||
}
|
||||
super.execute(executor, args);
|
||||
@@ -46,7 +47,10 @@ public class PublicHomeCommand extends HomeCommand implements TabProvider {
|
||||
@NotNull
|
||||
public List<String> suggest(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||
if (args.length <= 2) {
|
||||
return filter(plugin.getManager().homes().getPublicHomeIdentifiers(), args);
|
||||
if (args.length >= 1 && args[0].contains(Home.IDENTIFIER_DELIMITER)) {
|
||||
return plugin.getManager().homes().getPublicHomeIdentifierNames();
|
||||
}
|
||||
return plugin.getManager().homes().getPublicHomeNames();
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import net.william278.huskhomes.user.CommandUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.paginedown.PaginatedList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -40,19 +41,20 @@ public class PublicHomeListCommand extends ListCommand {
|
||||
@Override
|
||||
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||
final int pageNumber = parseIntArg(args, 0).orElse(1);
|
||||
this.showPublicHomeList(executor, pageNumber);
|
||||
this.showPublicHomeList(executor, null, pageNumber);
|
||||
}
|
||||
|
||||
protected void showPublicHomeList(@NotNull CommandUser executor, int pageNumber) {
|
||||
if (executor instanceof OnlineUser user && cachedLists.containsKey(user.getUuid())) {
|
||||
public void showPublicHomeList(@NotNull CommandUser executor, @Nullable String nameFilter, int pageNumber) {
|
||||
if (nameFilter == null && executor instanceof OnlineUser user && cachedLists.containsKey(user.getUuid())) {
|
||||
executor.sendMessage(cachedLists.get(user.getUuid()).getNearestValidPage(pageNumber));
|
||||
return;
|
||||
}
|
||||
|
||||
final List<Home> homes = plugin.getDatabase().getPublicHomes();
|
||||
final List<Home> homes = nameFilter == null ? plugin.getDatabase().getPublicHomes()
|
||||
: plugin.getDatabase().getPublicHomes(nameFilter);
|
||||
plugin.fireEvent(plugin.getViewHomeListEvent(homes, executor, true),
|
||||
(event) -> this.generateList(executor, event.getHomes()).ifPresent(homeList -> {
|
||||
if (executor instanceof OnlineUser onlineUser) {
|
||||
if (nameFilter == null && executor instanceof OnlineUser onlineUser) {
|
||||
cachedLists.put(onlineUser.getUuid(), homeList);
|
||||
}
|
||||
executor.sendMessage(homeList.getNearestValidPage(pageNumber));
|
||||
@@ -69,9 +71,15 @@ public class PublicHomeListCommand extends ListCommand {
|
||||
final PaginatedList homeList = PaginatedList.of(publicHomes.stream().map(home ->
|
||||
plugin.getLocales()
|
||||
.getRawLocale("public_home_list_item",
|
||||
Locales.escapeText(home.getName()), home.getSafeIdentifier(),
|
||||
publicHomes.stream()
|
||||
.filter(h -> h.getName().equals(home.getName())).count() > 1
|
||||
? home.getSafeIdentifier() : Locales.escapeText(home.getName()),
|
||||
home.getSafeIdentifier(),
|
||||
Locales.escapeText(home.getOwner().getUsername()),
|
||||
Locales.escapeText(home.getMeta().getDescription()))
|
||||
home.getMeta().getDescription().isBlank()
|
||||
? plugin.getLocales().getNone()
|
||||
: Locales.escapeText(home.getMeta().getDescription())
|
||||
)
|
||||
.orElse(home.getName())).sorted().collect(Collectors.toList()),
|
||||
plugin.getLocales()
|
||||
.getBaseList(plugin.getSettings().getGeneral().getListItemsPerPage())
|
||||
|
||||
@@ -35,10 +35,11 @@ import java.util.*;
|
||||
|
||||
public abstract class SavedPositionCommand<T extends SavedPosition> extends Command implements TabProvider {
|
||||
|
||||
private final Class<T> positionType;
|
||||
protected final List<String> arguments;
|
||||
protected final PositionCommandType positionType;
|
||||
|
||||
protected SavedPositionCommand(@NotNull String name, @NotNull List<String> aliases, @NotNull Class<T> positionType,
|
||||
protected SavedPositionCommand(@NotNull String name, @NotNull List<String> aliases,
|
||||
@NotNull PositionCommandType positionType,
|
||||
@NotNull List<String> arguments, @NotNull HuskHomes plugin) {
|
||||
super(name, aliases, "<name>" + formatUsage(arguments), plugin);
|
||||
this.positionType = positionType;
|
||||
@@ -50,7 +51,10 @@ public abstract class SavedPositionCommand<T extends SavedPosition> extends Comm
|
||||
|
||||
@NotNull
|
||||
public String getOtherPermission() {
|
||||
return (positionType == Home.class ? super.getPermission("other") : super.getPermission());
|
||||
if (positionType != PositionCommandType.WARP) {
|
||||
return super.getPermission("other");
|
||||
}
|
||||
return super.getPermission("warp");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -63,9 +67,10 @@ public abstract class SavedPositionCommand<T extends SavedPosition> extends Comm
|
||||
return;
|
||||
}
|
||||
|
||||
final Optional<?> position = (positionType == Home.class
|
||||
? resolveHome(executor, name.get())
|
||||
: resolveWarp(executor, name.get()));
|
||||
// Resolve and execute
|
||||
final Optional<?> position = (positionType == PositionCommandType.WARP
|
||||
? resolveWarp(executor, name.get()) // Resolve warps
|
||||
: resolveHome(executor, name.get())); // Resolve homes & public homes
|
||||
position.ifPresent(p -> execute(executor, (T) p, removeFirstArg(args)));
|
||||
}
|
||||
|
||||
@@ -73,45 +78,68 @@ public abstract class SavedPositionCommand<T extends SavedPosition> extends Comm
|
||||
|
||||
private Optional<Home> resolveHome(@NotNull CommandUser executor, @NotNull String homeName) {
|
||||
if (homeName.contains(Home.IDENTIFIER_DELIMITER)) {
|
||||
final String ownerUsername = homeName.substring(0, homeName.indexOf(Home.IDENTIFIER_DELIMITER));
|
||||
final String ownerHome = homeName.substring(homeName.indexOf(Home.IDENTIFIER_DELIMITER) + 1);
|
||||
if (ownerUsername.isBlank() || ownerHome.isBlank()) {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final Optional<Home> optionalHome = plugin.getDatabase().getUserDataByName(ownerUsername)
|
||||
.flatMap(owner -> resolveHomeByName(owner.getUser(), ownerHome));
|
||||
if (optionalHome.isEmpty()) {
|
||||
plugin.getLocales().getLocale(executor.hasPermission(getOtherPermission())
|
||||
? "error_home_invalid_other" : "error_public_home_invalid", ownerUsername, ownerHome)
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final Home home = optionalHome.get();
|
||||
if (executor instanceof OnlineUser user && !home.isPublic() && !user.equals(home.getOwner())
|
||||
&& !user.hasPermission(getOtherPermission())) {
|
||||
plugin.getLocales().getLocale("error_public_home_invalid", ownerUsername, ownerHome)
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return optionalHome;
|
||||
return resolveDelimitedHome(executor, homeName);
|
||||
} else if (positionType == PositionCommandType.PUBLIC_HOME) {
|
||||
return resolvePublicHome(executor, homeName);
|
||||
} else if (executor instanceof OnlineUser owner) {
|
||||
final Optional<Home> optionalHome = resolveHomeByName(owner, homeName);
|
||||
if (optionalHome.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_home_invalid", homeName)
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
return optionalHome;
|
||||
} else {
|
||||
return resolveOwnerHome(owner, homeName);
|
||||
}
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private Optional<Home> resolveDelimitedHome(@NotNull CommandUser executor, @NotNull String homeName) {
|
||||
final String ownerUsername = homeName.substring(0, homeName.indexOf(Home.IDENTIFIER_DELIMITER));
|
||||
final String ownerHome = homeName.substring(homeName.indexOf(Home.IDENTIFIER_DELIMITER) + 1);
|
||||
if (ownerUsername.isBlank() || ownerHome.isBlank()) {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final Optional<Home> optionalHome = plugin.getDatabase().getUserDataByName(ownerUsername)
|
||||
.flatMap(owner -> resolveHomeByName(owner.getUser(), ownerHome));
|
||||
if (optionalHome.isEmpty()) {
|
||||
plugin.getLocales().getLocale(executor.hasPermission(getOtherPermission())
|
||||
? "error_home_invalid_other" : "error_public_home_invalid", ownerUsername, ownerHome)
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final Home home = optionalHome.get();
|
||||
if (executor instanceof OnlineUser user && !home.isPublic() && !user.equals(home.getOwner())
|
||||
&& !user.hasPermission(getOtherPermission())) {
|
||||
plugin.getLocales().getLocale("error_public_home_invalid", ownerUsername, ownerHome)
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
return optionalHome;
|
||||
}
|
||||
|
||||
private Optional<Home> resolvePublicHome(@NotNull CommandUser executor, @NotNull String homeName) {
|
||||
final List<Home> publicHomes = plugin.getDatabase().getPublicHomes(homeName);
|
||||
if (publicHomes.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_unknown_public_home", homeName)
|
||||
.ifPresent(executor::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
if (publicHomes.size() > 1) {
|
||||
plugin.getCommand(PublicHomeListCommand.class)
|
||||
.ifPresent(command -> command.showPublicHomeList(executor, homeName, 1));
|
||||
return Optional.empty();
|
||||
}
|
||||
return publicHomes.stream().findFirst();
|
||||
}
|
||||
|
||||
private Optional<Home> resolveOwnerHome(@NotNull OnlineUser owner, @NotNull String homeName) {
|
||||
final Optional<Home> optionalHome = resolveHomeByName(owner, homeName);
|
||||
if (optionalHome.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_home_invalid", homeName)
|
||||
.ifPresent(owner::sendMessage);
|
||||
return Optional.empty();
|
||||
}
|
||||
return optionalHome;
|
||||
}
|
||||
|
||||
private Optional<Home> resolveHomeByName(@NotNull User owner, @NotNull String homeName) {
|
||||
@@ -191,14 +219,23 @@ public abstract class SavedPositionCommand<T extends SavedPosition> extends Comm
|
||||
@Override
|
||||
@NotNull
|
||||
public List<String> suggest(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||
if (positionType != Home.class) {
|
||||
return switch (args.length) {
|
||||
case 0, 1 -> plugin.getManager().warps().getUsableWarps(executor);
|
||||
case 2 -> arguments.stream().filter(a -> executor.hasPermission(getPermission(a))).toList();
|
||||
default -> List.of();
|
||||
};
|
||||
}
|
||||
return switch (positionType) {
|
||||
case HOME, PUBLIC_HOME -> suggestHome(executor, args);
|
||||
case WARP -> suggestWarp(executor, args);
|
||||
};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<String> suggestWarp(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||
return switch (args.length) {
|
||||
case 0, 1 -> plugin.getManager().warps().getUsableWarps(executor);
|
||||
case 2 -> arguments.stream().filter(a -> executor.hasPermission(getPermission(a))).toList();
|
||||
default -> List.of();
|
||||
};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<String> suggestHome(@NotNull CommandUser executor, @NotNull String[] args) {
|
||||
return switch (args.length) {
|
||||
case 0, 1 -> {
|
||||
if (args.length == 1 && args[0].contains(Home.IDENTIFIER_DELIMITER)) {
|
||||
@@ -217,4 +254,10 @@ public abstract class SavedPositionCommand<T extends SavedPosition> extends Comm
|
||||
};
|
||||
}
|
||||
|
||||
protected enum PositionCommandType {
|
||||
HOME,
|
||||
PUBLIC_HOME,
|
||||
WARP,
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,7 +32,7 @@ import java.util.Optional;
|
||||
public class WarpCommand extends SavedPositionCommand<Warp> {
|
||||
|
||||
protected WarpCommand(@NotNull HuskHomes plugin) {
|
||||
super("warp", List.of(), Warp.class, List.of("player"), plugin);
|
||||
super("warp", List.of(), PositionCommandType.WARP, List.of("player"), plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -69,8 +69,11 @@ public class WarpListCommand extends ListCommand {
|
||||
final PaginatedList warpList = PaginatedList.of(warps.stream().map(warp ->
|
||||
plugin.getLocales()
|
||||
.getRawLocale("warp_list_item",
|
||||
Locales.escapeText(warp.getName()), warp.getSafeIdentifier(),
|
||||
Locales.escapeText(warp.getMeta().getDescription())
|
||||
Locales.escapeText(warp.getName()),
|
||||
warp.getSafeIdentifier(),
|
||||
warp.getMeta().getDescription().isBlank()
|
||||
? plugin.getLocales().getNone()
|
||||
: Locales.escapeText(warp.getMeta().getDescription())
|
||||
)
|
||||
.orElse(warp.getName())).sorted().collect(Collectors.toList()),
|
||||
plugin.getLocales()
|
||||
|
||||
@@ -243,6 +243,27 @@ public abstract class Database {
|
||||
*/
|
||||
public abstract List<Home> getPublicHomes();
|
||||
|
||||
/**
|
||||
* Get a list of publicly-set {@link Home}s with a specific name.
|
||||
*
|
||||
* @param name The name of the home
|
||||
* @param caseInsensitive Whether the name lookup query should be case-insensitive
|
||||
* @return A list containing all publicly-set {@link Home}s with the specified name
|
||||
*/
|
||||
public abstract List<Home> getPublicHomes(@NotNull String name, boolean caseInsensitive);
|
||||
|
||||
/**
|
||||
* Get a list of publicly-set {@link Home}s with a specific name.
|
||||
*
|
||||
* @param name The name of the home
|
||||
* @return A list containing all publicly-set {@link Home}s with the specified name
|
||||
* @apiNote Whether lookup is case-sensitive is determined by the {@code general.case_insensitive_names} setting
|
||||
*/
|
||||
public List<Home> getPublicHomes(@NotNull String name) {
|
||||
return getPublicHomes(name, plugin.getSettings().getGeneral().getNames().isCaseInsensitive());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a list of publicly-set {@link Home}s on <i>this {@link Server server}</i>.
|
||||
*
|
||||
|
||||
@@ -296,19 +296,19 @@ public class H2Database extends Database {
|
||||
try (Connection connection = getConnection()) {
|
||||
// Delete Position
|
||||
PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
||||
DELETE FROM `%positions_table%`
|
||||
WHERE `id`
|
||||
IN ((SELECT `last_position` FROM `%players_table%` WHERE `uuid` = ?),
|
||||
(SELECT `offline_position` FROM `%players_table%` WHERE `uuid` = ?),
|
||||
(SELECT `respawn_position` FROM `%players_table%` WHERE `uuid` = ?));"""));
|
||||
DELETE FROM `%positions_table%`
|
||||
WHERE `id`
|
||||
IN ((SELECT `last_position` FROM `%players_table%` WHERE `uuid` = ?),
|
||||
(SELECT `offline_position` FROM `%players_table%` WHERE `uuid` = ?),
|
||||
(SELECT `respawn_position` FROM `%players_table%` WHERE `uuid` = ?));"""));
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, uuid.toString());
|
||||
statement.setString(3, uuid.toString());
|
||||
statement.executeUpdate();
|
||||
|
||||
statement = connection.prepareStatement(formatStatementTables("""
|
||||
DELETE FROM `%players_table%`
|
||||
WHERE `uuid`=?;"""));
|
||||
DELETE FROM `%players_table%`
|
||||
WHERE `uuid`=?;"""));
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.executeUpdate();
|
||||
|
||||
@@ -498,6 +498,54 @@ public class H2Database extends Database {
|
||||
return userHomes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Home> getPublicHomes(@NotNull String name, boolean caseInsensitive) {
|
||||
final List<Home> userHomes = new ArrayList<>();
|
||||
try (Connection connection = getConnection()) {
|
||||
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
||||
SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`,
|
||||
`description`, `tags`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`,
|
||||
`server_name`, `public`
|
||||
FROM `%homes_table%`
|
||||
INNER JOIN `%saved_positions_table%`
|
||||
ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id`
|
||||
INNER JOIN `%positions_table`
|
||||
ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id`
|
||||
INNER JOIN `%players_table`
|
||||
ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid`
|
||||
WHERE `public`=true
|
||||
AND ((? AND UPPER(`name`) LIKE UPPER(?)) OR (`name`=?))
|
||||
ORDER BY `name`;"""))) {
|
||||
statement.setBoolean(1, caseInsensitive);
|
||||
statement.setString(2, name);
|
||||
statement.setString(3, name);
|
||||
|
||||
final ResultSet resultSet = statement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
userHomes.add(Home.from(resultSet.getDouble("x"),
|
||||
resultSet.getDouble("y"),
|
||||
resultSet.getDouble("z"),
|
||||
resultSet.getFloat("yaw"),
|
||||
resultSet.getFloat("pitch"),
|
||||
World.from(resultSet.getString("world_name"),
|
||||
UUID.fromString(resultSet.getString("world_uuid"))),
|
||||
resultSet.getString("server_name"),
|
||||
PositionMeta.from(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant(),
|
||||
resultSet.getString("tags")),
|
||||
UUID.fromString(resultSet.getString("home_uuid")),
|
||||
User.of(UUID.fromString(resultSet.getString("owner_uuid")),
|
||||
resultSet.getString("owner_username")),
|
||||
resultSet.getBoolean("public")));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
plugin.log(Level.SEVERE, "Failed to query the database for public home data");
|
||||
}
|
||||
return userHomes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Home> getHome(@NotNull User user, @NotNull String homeName, boolean caseInsensitive) {
|
||||
try (Connection connection = getConnection()) {
|
||||
|
||||
@@ -552,6 +552,54 @@ public class MySqlDatabase extends Database {
|
||||
return userHomes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Home> getPublicHomes(@NotNull String name, boolean caseInsensitive) {
|
||||
final List<Home> userHomes = new ArrayList<>();
|
||||
try (Connection connection = getConnection()) {
|
||||
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
||||
SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`,
|
||||
`description`, `tags`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`,
|
||||
`server_name`, `public`
|
||||
FROM `%homes_table%`
|
||||
INNER JOIN `%saved_positions_table%`
|
||||
ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id`
|
||||
INNER JOIN `%positions_table%`
|
||||
ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id`
|
||||
INNER JOIN `%players_table%`
|
||||
ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid`
|
||||
WHERE `public`=true
|
||||
AND ((? AND UPPER(`name`) LIKE UPPER(?)) OR (`name`=?))
|
||||
ORDER BY `name`;"""))) {
|
||||
statement.setBoolean(1, caseInsensitive);
|
||||
statement.setString(2, name);
|
||||
statement.setString(3, name);
|
||||
|
||||
final ResultSet resultSet = statement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
userHomes.add(Home.from(resultSet.getDouble("x"),
|
||||
resultSet.getDouble("y"),
|
||||
resultSet.getDouble("z"),
|
||||
resultSet.getFloat("yaw"),
|
||||
resultSet.getFloat("pitch"),
|
||||
World.from(resultSet.getString("world_name"),
|
||||
UUID.fromString(resultSet.getString("world_uuid"))),
|
||||
resultSet.getString("server_name"),
|
||||
PositionMeta.from(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant(),
|
||||
resultSet.getString("tags")),
|
||||
UUID.fromString(resultSet.getString("home_uuid")),
|
||||
User.of(UUID.fromString(resultSet.getString("owner_uuid")),
|
||||
resultSet.getString("owner_username")),
|
||||
resultSet.getBoolean("public")));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
plugin.log(Level.SEVERE, "Failed to query the database for public home data");
|
||||
}
|
||||
return userHomes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Home> getHome(@NotNull User user, @NotNull String homeName, boolean caseInsensitive) {
|
||||
try (Connection connection = getConnection()) {
|
||||
|
||||
@@ -514,6 +514,52 @@ public class SqLiteDatabase extends Database {
|
||||
return userHomes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Home> getPublicHomes(@NotNull String name, boolean caseInsensitive) {
|
||||
final List<Home> userHomes = new ArrayList<>();
|
||||
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
|
||||
SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`,
|
||||
`name`, `description`, `tags`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`,
|
||||
`world_uuid`, `server_name`, `public`
|
||||
FROM `%homes_table%`
|
||||
INNER JOIN `%saved_positions_table%`
|
||||
ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id`
|
||||
INNER JOIN `%positions_table%`
|
||||
ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id`
|
||||
INNER JOIN `%players_table%`
|
||||
ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid`
|
||||
WHERE `public`=true
|
||||
AND ((? AND UPPER(`name`) LIKE UPPER(?)) OR (`name`=?))
|
||||
ORDER BY `name`;"""))) {
|
||||
statement.setBoolean(1, caseInsensitive);
|
||||
statement.setString(2, name);
|
||||
statement.setString(3, name);
|
||||
|
||||
final ResultSet resultSet = statement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
userHomes.add(Home.from(resultSet.getDouble("x"),
|
||||
resultSet.getDouble("y"),
|
||||
resultSet.getDouble("z"),
|
||||
resultSet.getFloat("yaw"),
|
||||
resultSet.getFloat("pitch"),
|
||||
World.from(resultSet.getString("world_name"),
|
||||
UUID.fromString(resultSet.getString("world_uuid"))),
|
||||
resultSet.getString("server_name"),
|
||||
PositionMeta.from(resultSet.getString("name"),
|
||||
resultSet.getString("description"),
|
||||
resultSet.getTimestamp("timestamp").toInstant(),
|
||||
resultSet.getString("tags")),
|
||||
UUID.fromString(resultSet.getString("home_uuid")),
|
||||
User.of(UUID.fromString(resultSet.getString("owner_uuid")),
|
||||
resultSet.getString("owner_username")),
|
||||
resultSet.getBoolean("public")));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
plugin.log(Level.SEVERE, "Failed to query the database for public home data");
|
||||
}
|
||||
return userHomes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Home> getHome(@NotNull User user, @NotNull String homeName, boolean caseInsensitive) {
|
||||
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
|
||||
|
||||
@@ -81,9 +81,11 @@ public class HomesManager {
|
||||
*/
|
||||
@NotNull
|
||||
public Map<String, List<String>> getPublicHomes() {
|
||||
return publicHomes.stream()
|
||||
.collect(HashMap::new, (m, e) -> m.put(e.getOwner().getUsername(), List.of(e.getName())),
|
||||
HashMap::putAll);
|
||||
return publicHomes.stream().collect(
|
||||
HashMap::new,
|
||||
(m, e) -> m.put(e.getOwner().getUsername(), List.of(e.getName())),
|
||||
HashMap::putAll
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,9 +94,26 @@ public class HomesManager {
|
||||
* @return a list of all cached public home identifiers
|
||||
*/
|
||||
@NotNull
|
||||
public List<String> getPublicHomeIdentifiers() {
|
||||
public List<String> getPublicHomeIdentifierNames() {
|
||||
return publicHomes.stream().map(Home::getIdentifier).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all cached public homes.
|
||||
* <p>
|
||||
* This will return the full home identifier if there are multiple homes with the same name.
|
||||
*
|
||||
* @return a list of all cached public home identifiers
|
||||
*/
|
||||
@NotNull
|
||||
public List<String> getPublicHomeNames() {
|
||||
return publicHomes.stream()
|
||||
.map(Home::getIdentifier)
|
||||
.map(home -> {
|
||||
if (publicHomes.stream().filter(h -> h.getName().equals(home.getName())).count() > 1) {
|
||||
return home.getIdentifier();
|
||||
}
|
||||
return home.getName();
|
||||
})
|
||||
.toList();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user