Skip to content

Commit

Permalink
add check for new config option
Browse files Browse the repository at this point in the history
  • Loading branch information
Z0rdak committed Feb 15, 2024
1 parent 6d5c253 commit 8b350a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/main/java/de/z0rdak/yawp/commands/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import de.z0rdak.yawp.YetAnotherWorldProtector;
import de.z0rdak.yawp.config.server.CommandPermissionConfig;
import de.z0rdak.yawp.commands.arguments.ArgumentUtil;
import de.z0rdak.yawp.config.server.CommandPermissionConfig;
import de.z0rdak.yawp.handler.CommandInterceptor;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.util.text.IFormattableTextComponent;
Expand Down Expand Up @@ -44,6 +45,7 @@ public static void register(String modRootCmd) {

private static LiteralArgumentBuilder<CommandSource> buildCommands(String baseCmd) {
return Commands.literal(baseCmd)
.requires(CommandInterceptor::isAllowedForNonOp)
.executes(ctx -> promptHelp(ctx.getSource()))
.then(ArgumentUtil.literal(CommandConstants.HELP)
.executes(ctx -> promptHelp(ctx.getSource())))
Expand Down
28 changes: 27 additions & 1 deletion src/main/java/de/z0rdak/yawp/handler/CommandInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.z0rdak.yawp.commands.CommandConstants;
import de.z0rdak.yawp.commands.CommandSourceType;
import de.z0rdak.yawp.commands.CommandUtil;
import de.z0rdak.yawp.config.server.CommandPermissionConfig;
import de.z0rdak.yawp.core.region.GlobalRegion;
import de.z0rdak.yawp.core.region.IMarkableRegion;
import de.z0rdak.yawp.core.region.IProtectedRegion;
Expand Down Expand Up @@ -391,9 +392,13 @@ private static boolean hasCmdPermission(CommandContextBuilder<CommandSource> ctx
}

private static boolean hasCmdPermission(CommandContextBuilder<CommandSource> ctx, CommandSourceType cmdSrcType) throws CommandSyntaxException {
return hasCmdPermission(ctx.getSource(), cmdSrcType);
}

private static boolean hasCmdPermission(CommandSource src, CommandSourceType cmdSrcType) throws CommandSyntaxException {
switch (cmdSrcType) {
case PLAYER: {
ServerPlayerEntity player = ctx.getSource().getPlayerOrException();
ServerPlayerEntity player = src.getPlayerOrException();
return hasPlayerPermission(player);
}
case SERVER:
Expand All @@ -404,4 +409,25 @@ private static boolean hasCmdPermission(CommandContextBuilder<CommandSource> ctx
return false;
}
}

public static boolean hasCmdPermission(CommandSource src) {
CommandSourceType cmdSrcType = CommandSourceType.of(src);
try {
return hasCmdPermission(src, cmdSrcType);
} catch (CommandSyntaxException e) {
return false;
}
}

public static boolean isAllowedForNonOp(CommandSource src) {
CommandSourceType cmdSrcType = CommandSourceType.of(src);
try {
if (CommandPermissionConfig.isCmdDisabledForNonOp()) {
return hasCmdPermission(src, cmdSrcType);
}
return false;
} catch (CommandSyntaxException e) {
return false;
}
}
}

0 comments on commit 8b350a1

Please sign in to comment.