Allow freezing/unfreezing offline players.
package io.github.nucleuspowered.nucleus.modules.freezeplayer.commands;
import com.google.inject.Inject;
+import io.github.nucleuspowered.nucleus.argumentparsers.NicknameArgument;
+import io.github.nucleuspowered.nucleus.argumentparsers.SelectorWrapperArgument;
import io.github.nucleuspowered.nucleus.dataservices.loaders.UserDataManager;
import io.github.nucleuspowered.nucleus.internal.annotations.Permissions;
import io.github.nucleuspowered.nucleus.internal.annotations.RegisterCommand;
@@ -12,13 +14,12 @@ import io.github.nucleuspowered.nucleus.internal.command.AbstractCommand;
import io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation;
import io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel;
import io.github.nucleuspowered.nucleus.modules.freezeplayer.datamodules.FreezePlayerUserDataModule;
-import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.args.CommandElement;
import org.spongepowered.api.command.args.GenericArguments;
-import org.spongepowered.api.entity.living.player.Player;
+import org.spongepowered.api.entity.living.player.User;
import org.spongepowered.api.text.Text;
import java.util.HashMap;
@@ -31,6 +32,7 @@ public class FreezePlayerCommand extends AbstractCommand<CommandSource> {
@Inject private UserDataManager userConfigLoader;
private final String player = "subject";
+ private final String truefalsekey = "true|false";
@Override
public Map<String, PermissionInformation> permissionSuffixesToRegister() {
@@ -42,28 +44,20 @@ public class FreezePlayerCommand extends AbstractCommand<CommandSource> {
@Override
public CommandElement[] getArguments() {
return new CommandElement[] {
- GenericArguments.optional(GenericArguments.requiringPermission(
- GenericArguments.onlyOne(GenericArguments.player(Text.of(player))), permissions.getPermissionWithSuffix("others")))
+ GenericArguments.optionalWeak(GenericArguments.requiringPermission(
+ GenericArguments.onlyOne(SelectorWrapperArgument.nicknameSelector(
+ Text.of(player),
+ NicknameArgument.UnderlyingType.USER
+ )), permissions.getPermissionWithSuffix("others"))),
+ GenericArguments.optional(GenericArguments.bool(Text.of(truefalsekey)))
};
}
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
- Player pl = this.getUserFromArgs(Player.class, src, player, args);
- FreezePlayerUserDataModule nu;
- try {
- nu = userConfigLoader.getUser(pl).get().get(FreezePlayerUserDataModule.class);
- } catch (Exception e) {
- e.printStackTrace();
- throw new CommandException(plugin.getMessageProvider().getTextMessageWithFormat("command.file.load"), e);
- }
-
- if (nu.isFrozen()) {
- nu.setFrozen(false);
- } else {
- nu.setFrozen(true);
- }
-
+ User pl = this.getUserFromArgs(User.class, src, player, args);
+ FreezePlayerUserDataModule nu = userConfigLoader.getUnchecked(pl).get(FreezePlayerUserDataModule.class);
+ nu.setFrozen(args.<Boolean>getOne(truefalsekey).orElseGet(() -> !nu.isFrozen()));
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat(
nu.isFrozen() ? "command.freezeplayer.success.frozen" : "command.freezeplayer.success.unfrozen", plugin.getNameUtil().getSerialisedName(pl)));
return CommandResult.success();
