Skip to content

Commit

Permalink
Add ability to resolve unsafe positions by UUID where the identifier …
Browse files Browse the repository at this point in the history
…would otherwise be unsafe (#356)
  • Loading branch information
WiIIiam278 authored Apr 16, 2023
1 parent 4bd0244 commit 3c18214
Show file tree
Hide file tree
Showing 26 changed files with 188 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ public void testHomeCreation(@NotNull OnlineUser owner, @NotNull String name, @N
.anyMatch(home -> home.equals(name)));
}

// rename
@DisplayName("Test Home Renaming")
@ParameterizedTest(name = "Rename: \"{1}\" > \"{1}2\"")
@MethodSource("provideHomeData")
Expand Down Expand Up @@ -407,7 +406,6 @@ public void testHomeRenaming(@NotNull OnlineUser owner, @NotNull String name, @S
.anyMatch(home -> home.equals(name)));
}

// description
@DisplayName("Test Home Description")
@ParameterizedTest(name = "Edit Description: \"{1}\"")
@MethodSource("provideHomeData")
Expand All @@ -424,7 +422,6 @@ public void testHomeDescription(@NotNull OnlineUser owner, @NotNull String name,
Assertions.assertEquals(description, homeDescription.get());
}

// relocate
@DisplayName("Test Home Relocation")
@ParameterizedTest(name = "Relocate: \"{1}\"")
@MethodSource("provideHomeData")
Expand All @@ -443,7 +440,6 @@ public void testHomeRelocation(@NotNull OnlineUser owner, @NotNull String name,
Assertions.assertEquals(newPosition.getZ(), homePosition.get().getZ());
}

// overwrite
@DisplayName("Test Home Overwrite")
@ParameterizedTest(name = "Overwrite: \"{1}\"")
@MethodSource("provideHomeData")
Expand All @@ -462,7 +458,6 @@ public void testHomeOverwrite(@NotNull OnlineUser owner, @NotNull String name, @
Assertions.assertEquals(newPosition.getZ(), homePosition.get().getZ());
}

// make public
@DisplayName("Test Making Home Public")
@ParameterizedTest(name = "Make Public: \"{1}\"")
@MethodSource("provideHomeData")
Expand All @@ -477,7 +472,6 @@ public void testHomeMakePublic(@NotNull OnlineUser owner, @NotNull String name,
Assertions.assertTrue(plugin.getManager().homes().getPublicHomes().get(owner.getUsername()).contains(name));
}

// make private
@DisplayName("Test Making Home Private")
@ParameterizedTest(name = "Make Private: \"{1}\"")
@MethodSource("provideHomeData")
Expand All @@ -494,7 +488,6 @@ public void testHomeMakePrivate(@NotNull OnlineUser owner, @NotNull String name,
.contains(name));
}

// delete
@DisplayName("Test Home Deletion")
@ParameterizedTest(name = "Delete: \"{1}\"")
@MethodSource("provideHomeData")
Expand All @@ -509,7 +502,6 @@ public void testHomeDeletion(@NotNull OnlineUser owner, @NotNull String name, @S
plugin.getManager().homes().createHome(owner, name, position);
}

// delete all
@DisplayName("Test Deleting All Homes")
@Order(9)
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import de.themoep.minedown.adventure.MineDown;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Locales;
import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.user.CommandUser;
Expand Down Expand Up @@ -198,11 +197,11 @@ private void setHomePrivacy(@NotNull CommandUser executor, @NotNull Home home, b
final String privacy = home.isPublic() ? "public" : "private";
if (ownerEditing) {
plugin.getLocales().getLocale("edit_home_privacy_" + privacy + "_success",
home.getMeta().getName())
home.getName())
.ifPresent(executor::sendMessage);
} else {
plugin.getLocales().getLocale("edit_home_privacy_" + privacy + "_success_other",
home.getOwner().getUsername(), home.getMeta().getName())
home.getOwner().getUsername(), home.getName())
.ifPresent(executor::sendMessage);
}
});
Expand All @@ -222,10 +221,10 @@ private List<MineDown> getHomeEditorWindow(@NotNull Home home, boolean otherView
boolean showTeleportButton, boolean showPrivacyToggleButton) {
return new ArrayList<>() {{
if (!otherViewer) {
plugin.getLocales().getLocale("edit_home_menu_title", home.getMeta().getName())
plugin.getLocales().getLocale("edit_home_menu_title", home.getName())
.ifPresent(this::add);
} else {
plugin.getLocales().getLocale("edit_home_menu_title_other", home.getOwner().getUsername(), home.getMeta().getName())
plugin.getLocales().getLocale("edit_home_menu_title_other", home.getOwner().getUsername(), home.getName())
.ifPresent(this::add);
}

Expand Down Expand Up @@ -256,21 +255,17 @@ private List<MineDown> getHomeEditorWindow(@NotNull Home home, boolean otherView
String.format("%.2f", home.getYaw()), String.format("%.2f", home.getPitch()))
.ifPresent(this::add);

final String formattedName = home.getOwner().getUsername() + "." + home.getMeta().getName();
if (showTeleportButton) {
plugin.getLocales().getLocale("edit_home_menu_use_buttons",
formattedName)
plugin.getLocales().getLocale("edit_home_menu_use_buttons", home.getSafeIdentifier())
.ifPresent(this::add);
}
final String escapedName = Locales.escapeText(formattedName);
plugin.getLocales().getRawLocale("edit_home_menu_manage_buttons", escapedName,
plugin.getLocales().getRawLocale("edit_home_menu_manage_buttons", home.getSafeIdentifier(),
showPrivacyToggleButton ? plugin.getLocales()
.getRawLocale("edit_home_menu_privacy_button_"
+ (home.isPublic() ? "private" : "public"), escapedName)
+ (home.isPublic() ? "private" : "public"), home.getSafeIdentifier())
.orElse("") : "")
.map(MineDown::new).ifPresent(this::add);
plugin.getLocales().getLocale("edit_home_menu_meta_edit_buttons",
formattedName)
plugin.getLocales().getLocale("edit_home_menu_meta_edit_buttons", home.getSafeIdentifier())
.ifPresent(this::add);
}};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private void setWarpPosition(@NotNull CommandUser executor, @NotNull Warp warp)
@NotNull
private List<MineDown> getWarpEditorWindow(@NotNull Warp warp) {
return new ArrayList<>() {{
plugin.getLocales().getLocale("edit_warp_menu_title", warp.getMeta().getName())
plugin.getLocales().getLocale("edit_warp_menu_title", warp.getName())
.ifPresent(this::add);

plugin.getLocales().getLocale("edit_warp_menu_metadata",
Expand Down Expand Up @@ -169,11 +169,11 @@ private List<MineDown> getWarpEditorWindow(@NotNull Warp warp) {
String.format("%.2f", warp.getYaw()), String.format("%.2f", warp.getPitch()))
.ifPresent(this::add);

plugin.getLocales().getLocale("edit_warp_menu_use_buttons", warp.getMeta().getName())
plugin.getLocales().getLocale("edit_warp_menu_use_buttons", warp.getSafeIdentifier())
.ifPresent(this::add);
plugin.getLocales().getLocale("edit_warp_menu_manage_buttons", warp.getMeta().getName())
plugin.getLocales().getLocale("edit_warp_menu_manage_buttons", warp.getSafeIdentifier())
.ifPresent(this::add);
plugin.getLocales().getLocale("edit_warp_menu_meta_edit_buttons", warp.getMeta().getName())
plugin.getLocales().getLocale("edit_warp_menu_meta_edit_buttons", warp.getSafeIdentifier())
.ifPresent(this::add);
}};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,9 @@ private Optional<PaginatedList> generateList(@NotNull CommandUser executor, @Not
final PaginatedList homeList = PaginatedList.of(homes.stream().map(home ->
plugin.getLocales()
.getRawLocale("home_list_item",
Locales.escapeText(home.getMeta().getName()),
home.getOwner().getUsername() + "." + Locales.escapeText(home.getMeta().getName()),
Locales.escapeText(home.getName()), home.getSafeIdentifier(),
Locales.escapeText(plugin.getLocales().wrapText(home.getMeta().getDescription(), 40)))
.orElse(home.getMeta().getName())).sorted().collect(Collectors.toList()),
.orElse(home.getName())).sorted().collect(Collectors.toList()),
plugin.getLocales()
.getBaseList(plugin.getSettings().getListItemsPerPage())
.setHeaderFormat(plugin.getLocales().getRawLocale("home_list_page_title",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

public class PublicHomeListCommand extends ListCommand {


protected PublicHomeListCommand(@NotNull HuskHomes plugin) {
super("phomelist", List.of("phomes", "publichomelist"), "[page]", plugin);
}
Expand Down Expand Up @@ -70,8 +69,7 @@ private Optional<PaginatedList> generateList(@NotNull CommandUser executor, @Not
final PaginatedList homeList = PaginatedList.of(publicHomes.stream().map(home ->
plugin.getLocales()
.getRawLocale("public_home_list_item",
Locales.escapeText(home.getMeta().getName()),
home.getOwner().getUsername() + "." + Locales.escapeText(home.getMeta().getName()),
Locales.escapeText(home.getName()), home.getSafeIdentifier(),
Locales.escapeText(home.getOwner().getUsername()),
Locales.escapeText(plugin.getLocales().wrapText(home.getMeta().getDescription(), 40)))
.orElse(home.getName())).sorted().collect(Collectors.toList()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import net.william278.huskhomes.teleport.TeleportationException;
import net.william278.huskhomes.user.CommandUser;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.User;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

public abstract class SavedPositionCommand<T extends SavedPosition> extends Command implements TabProvider {

Expand Down Expand Up @@ -73,17 +75,17 @@ public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
public abstract void execute(@NotNull CommandUser executor, @NotNull T position, @NotNull String[] arguments);

private Optional<Home> resolveHome(@NotNull CommandUser executor, @NotNull String homeName) {
if (homeName.contains(".")) {
final String ownerUsername = homeName.substring(0, homeName.indexOf("."));
final String ownerHomeName = homeName.substring(homeName.indexOf(".") + 1);
if (homeName.contains(Home.IDENTIFIER_DELIMITER)) {
final String ownerUsername = homeName.substring(0, homeName.indexOf(Home.IDENTIFIER_DELIMITER));
final String ownerHomeName = homeName.substring(homeName.indexOf(Home.IDENTIFIER_DELIMITER) + 1);
if (ownerUsername.isBlank() || ownerHomeName.isBlank()) {
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
.ifPresent(executor::sendMessage);
return Optional.empty();
}

final Optional<Home> optionalHome = plugin.getDatabase().getUserDataByName(ownerUsername)
.flatMap(owner -> plugin.getDatabase().getHome(owner.getUser(), ownerHomeName));
.flatMap(owner -> resolveHomeByName(owner.getUser(), ownerHomeName));
if (optionalHome.isEmpty()) {
plugin.getLocales().getLocale(executor.hasPermission(getOtherPermission())
? "error_home_invalid_other" : "error_public_home_invalid", ownerUsername, ownerHomeName)
Expand All @@ -101,7 +103,7 @@ private Optional<Home> resolveHome(@NotNull CommandUser executor, @NotNull Strin

return optionalHome;
} else if (executor instanceof OnlineUser owner) {
final Optional<Home> optionalHome = plugin.getDatabase().getHome(owner, homeName);
final Optional<Home> optionalHome = resolveHomeByName(owner, homeName);
if (optionalHome.isEmpty()) {
plugin.getLocales().getLocale("error_home_invalid", homeName)
.ifPresent(executor::sendMessage);
Expand All @@ -115,8 +117,20 @@ private Optional<Home> resolveHome(@NotNull CommandUser executor, @NotNull Strin
}
}

private Optional<Home> resolveHomeByName(@NotNull User owner, @NotNull String homeName) {
return plugin.getDatabase()
.getHome(owner, homeName)
.or(() -> {
try {
return plugin.getDatabase().getHome(UUID.fromString(homeName));
} catch (IllegalArgumentException e) {
return Optional.empty();
}
});
}

private Optional<Warp> resolveWarp(@NotNull CommandUser executor, @NotNull String warpName) {
final Optional<Warp> warp = plugin.getDatabase().getWarp(warpName);
final Optional<Warp> warp = resolveWarpByName(warpName);
if (warp.isPresent() && executor instanceof OnlineUser user && plugin.getSettings().doPermissionRestrictWarps()
&& (!user.hasPermission(Warp.getWildcardPermission()) && !user.hasPermission(Warp.getPermission(warpName)))) {
plugin.getLocales().getLocale("error_warp_invalid", warpName)
Expand All @@ -126,6 +140,17 @@ private Optional<Warp> resolveWarp(@NotNull CommandUser executor, @NotNull Strin
return warp;
}

private Optional<Warp> resolveWarpByName(@NotNull String warpName) {
return plugin.getDatabase().getWarp(warpName)
.or(() -> {
try {
return plugin.getDatabase().getWarp(UUID.fromString(warpName));
} catch (IllegalArgumentException e) {
return Optional.empty();
}
});
}

protected void teleport(@NotNull CommandUser executor, @NotNull Teleportable teleporter, @NotNull T position) {
if (!teleporter.equals(executor) && !executor.hasPermission(getPermission("other"))) {
plugin.getLocales().getLocale("error_no_permission")
Expand Down Expand Up @@ -153,7 +178,7 @@ public List<String> suggest(@NotNull CommandUser executor, @NotNull String[] arg
if (positionType == Home.class) {
return switch (args.length) {
case 0, 1 -> {
if (args.length == 1 && args[0].contains(".")) {
if (args.length == 1 && args[0].contains(Home.IDENTIFIER_DELIMITER)) {
if (executor.hasPermission(getOtherPermission())) {
yield filter(plugin.getManager().homes().getUserHomeNames(), args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private Optional<PaginatedList> generateList(@NotNull CommandUser executor, @Not
final PaginatedList warpList = PaginatedList.of(warps.stream().map(warp ->
plugin.getLocales()
.getRawLocale("warp_list_item",
Locales.escapeText(warp.getName()),
Locales.escapeText(warp.getName()), warp.getSafeIdentifier(),
Locales.escapeText(plugin.getLocales().wrapText(warp.getMeta().getDescription(), 40)))
.orElse(warp.getName())).sorted().collect(Collectors.toList()),
plugin.getLocales()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected int setSavedPosition(@NotNull SavedPosition position, @NotNull Connect
Statement.RETURN_GENERATED_KEYS)) {

statement.setInt(1, setPosition(position, connection));
statement.setString(2, position.getMeta().getName());
statement.setString(2, position.getName());
statement.setString(3, position.getMeta().getDescription());
statement.setString(4, position.getMeta().getSerializedTags());
statement.setTimestamp(5, Timestamp.from(position.getMeta().getCreationTime()));
Expand Down Expand Up @@ -205,7 +205,7 @@ protected void updateSavedPosition(int savedPositionId, @NotNull SavedPosition p
`description`=?,
`tags`=?
WHERE `id`=?;"""))) {
updateStatement.setString(1, position.getMeta().getName());
updateStatement.setString(1, position.getName());
updateStatement.setString(2, position.getMeta().getDescription());
updateStatement.setString(3, position.getMeta().getSerializedTags());
updateStatement.setInt(4, savedPositionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ protected int setSavedPosition(@NotNull SavedPosition position, @NotNull Connect
Statement.RETURN_GENERATED_KEYS)) {

statement.setInt(1, setPosition(position, connection));
statement.setString(2, position.getMeta().getName());
statement.setString(2, position.getName());
statement.setString(3, position.getMeta().getDescription());
statement.setString(4, position.getMeta().getSerializedTags());
statement.setTimestamp(5, Timestamp.from(position.getMeta().getCreationTime()));
Expand Down Expand Up @@ -216,7 +216,7 @@ protected void updateSavedPosition(int savedPositionId, @NotNull SavedPosition p
`description`=?,
`tags`=?
WHERE `id`=?;"""))) {
updateStatement.setString(1, position.getMeta().getName());
updateStatement.setString(1, position.getName());
updateStatement.setString(2, position.getMeta().getDescription());
updateStatement.setString(3, position.getMeta().getSerializedTags());
updateStatement.setInt(4, savedPositionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void updateHome(@NotNull Home home) {
final String markerId = home.getOwner().getUuid() + ":" + home.getUuid();
markerSet.remove(markerId);
markerSet.put(markerId, POIMarker.builder()
.label("/phome " + home.getOwner().getUsername() + "." + home.getName())
.label("/phome " + home.getIdentifier())
.position(home.getX(), home.getY(), home.getZ())
.maxDistance(5000)
.icon(getIcon(PUBLIC_HOME_MARKER_IMAGE_NAME), 25, 25)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void updateHome(@NotNull Home home) {
.thumbnail(PUBLIC_HOME_MARKER_IMAGE_NAME)
.field("Owner", home.getOwner().getUsername())
.field("Description", plugin.getLocales().wrapText(home.getMeta().getDescription(), 60))
.field("Command", "/phome " + home.getOwner().getUsername() + "." + home.getName())
.field("Command", "/phome " + home.getIdentifier())
.toHtml());
});
});
Expand Down
Loading

0 comments on commit 3c18214

Please sign in to comment.