feat: Make /rtp in other worlds use world-specific command (#626)

* Update RtpCommand.java

* Fix bug about suggestion filtering

* Fix bug about suggestion filtering

* Fix bug about suggestion filtering

* Fix bug about suggestion filtering

* Filter restricted world
This commit is contained in:
alazeprt authored and GitHub committed 2024-05-11 13:13:39 +01:00
1 parent 4d86f580ae
commit 2b436a23fa
1 file changed
+12 -11
@@ -29,18 +29,18 @@ import net.william278.huskhomes.util.TransactionResolver;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List; import java.util.*;
import java.util.Map;
import java.util.Optional;
public class RtpCommand extends Command implements UserListTabProvider { public class RtpCommand extends Command implements UserListTabProvider {
protected RtpCommand(@NotNull HuskHomes plugin) { protected RtpCommand(@NotNull HuskHomes plugin) {
super("rtp", List.of(), "[player] [world]", plugin); super("rtp", List.of(), "[player] [world]", plugin);
addAdditionalPermissions(Map.of( Map<String, Boolean> map = new HashMap<>();
"other", true, map.put("other", true);
"world", true plugin.getWorlds().stream()
)); .filter(world -> !plugin.getSettings().getRtp().isWorldRtpRestricted(world))
.map(World::getName).toList().forEach(world -> map.put(world, true));
addAdditionalPermissions(map);
} }
@Override @Override
@@ -69,11 +69,12 @@ public class RtpCommand extends Command implements UserListTabProvider {
@Override @Override
public List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) { public List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) {
return switch (args.length) { return switch (args.length) {
case 0, 1 -> user.hasPermission("other") ? UserListTabProvider.super.suggestLocal(args) case 0, 1 -> user.hasPermission(getPermission("other")) ? UserListTabProvider.super.suggestLocal(args)
: user instanceof OnlineUser online ? List.of(online.getUsername()) : List.of(); : user instanceof OnlineUser online ? List.of(online.getUsername()) : List.of();
case 2 -> user.hasPermission("world") ? plugin.getWorlds().stream() case 2 -> plugin.getWorlds().stream()
.filter(world -> !plugin.getSettings().getRtp().isWorldRtpRestricted(world)) .filter(world -> !plugin.getSettings().getRtp().isWorldRtpRestricted(world))
.map(World::getName).toList() : List.of(); .map(World::getName)
.filter(world -> user.hasPermission(getPermission(world))).toList();
default -> null; default -> null;
}; };
} }
@@ -112,7 +113,7 @@ public class RtpCommand extends Command implements UserListTabProvider {
// Ensure the user has permission to randomly teleport in the world // Ensure the user has permission to randomly teleport in the world
final World world = optionalWorld.get(); final World world = optionalWorld.get();
if (!world.equals(teleporterWorld) && !executor.hasPermission(getPermission("world"))) { if (!world.equals(teleporterWorld) && !executor.hasPermission(getPermission(world.getName()))) {
plugin.getLocales().getLocale("error_no_permission") plugin.getLocales().getLocale("error_no_permission")
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
return Optional.empty(); return Optional.empty();