Skip to content

Commit

Permalink
feat: Make /rtp in other worlds use world-specific command (#626)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alazeprt authored May 11, 2024
1 parent 4d86f58 commit 2b436a2
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

public class RtpCommand extends Command implements UserListTabProvider {

protected RtpCommand(@NotNull HuskHomes plugin) {
super("rtp", List.of(), "[player] [world]", plugin);
addAdditionalPermissions(Map.of(
"other", true,
"world", true
));
Map<String, Boolean> map = new HashMap<>();
map.put("other", true);
plugin.getWorlds().stream()
.filter(world -> !plugin.getSettings().getRtp().isWorldRtpRestricted(world))
.map(World::getName).toList().forEach(world -> map.put(world, true));
addAdditionalPermissions(map);
}

@Override
Expand Down Expand Up @@ -69,11 +69,12 @@ public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
@Override
public List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) {
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();
case 2 -> user.hasPermission("world") ? plugin.getWorlds().stream()
case 2 -> plugin.getWorlds().stream()
.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;
};
}
Expand Down Expand Up @@ -112,7 +113,7 @@ private Optional<World> validateRtp(@NotNull OnlineUser teleporter, @NotNull Com

// Ensure the user has permission to randomly teleport in the world
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")
.ifPresent(executor::sendMessage);
return Optional.empty();
Expand Down

0 comments on commit 2b436a2

Please sign in to comment.