Skip to content

Commit

Permalink
Add interact url permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
ustc-zzzz committed Apr 8, 2023
1 parent 7785834 commit 9c6130f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 23 deletions.
53 changes: 31 additions & 22 deletions src/main/java/org/teacon/slides/admin/SlideCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.datafixers.util.Either;
import net.minecraft.ChatFormatting;
import net.minecraft.FieldsAreNonnullByDefault;
Expand Down Expand Up @@ -39,6 +40,8 @@
public class SlideCommand {
private static final DynamicCommandExceptionType URL_NOT_EXIST = new DynamicCommandExceptionType(v -> Component.translatable("command.slide_show.failed.url_not_exist", v));

private static final SimpleCommandExceptionType PERM_NOT_EXIST = new SimpleCommandExceptionType(Component.translatable("command.slide_show.failed.perm_not_exist").withStyle(ChatFormatting.RED));

@SubscribeEvent
public static void onCommandsRegister(RegisterCommandsEvent event) {
var node = event.getDispatcher().register(command(SlideShow.ID.replace('_', '-')));
Expand Down Expand Up @@ -74,35 +77,41 @@ private static int prefetchProjectorUrl(CommandContext<CommandSourceStack> ctx)
}

private static int blockByProjectorUrl(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
var data = ProjectorURLSavedData.get(ctx.getSource().getLevel());
var arg = ProjectorURLArgument.getUrl(ctx, "url");
var pairOptional = toPairOpt(data, arg);
if (pairOptional.isPresent()) {
var pair = pairOptional.get();
var text = toText(pair.getKey(), pair.getValue());
if (data.setBlockedStatusByCommand(pair.getKey(), pair.getValue(), ctx.getSource(), true)) {
var msg = Component.translatable("command.slide_show.block_projector_url.success", text);
ctx.getSource().sendSuccess(msg.withStyle(ChatFormatting.GREEN), true);
return Command.SINGLE_SUCCESS;
if (SlidePermission.canBlockUrl(ctx.getSource().source)) {
var data = ProjectorURLSavedData.get(ctx.getSource().getLevel());
var arg = ProjectorURLArgument.getUrl(ctx, "url");
var pairOptional = toPairOpt(data, arg);
if (pairOptional.isPresent()) {
var pair = pairOptional.get();
var text = toText(pair.getKey(), pair.getValue());
if (data.setBlockedStatusByCommand(pair.getKey(), pair.getValue(), ctx.getSource(), true)) {
var msg = Component.translatable("command.slide_show.block_projector_url.success", text);
ctx.getSource().sendSuccess(msg.withStyle(ChatFormatting.GREEN), true);
return Command.SINGLE_SUCCESS;
}
}
throw URL_NOT_EXIST.create(arg.map(SlideCommand::toText, SlideCommand::toText));
}
throw URL_NOT_EXIST.create(arg.map(SlideCommand::toText, SlideCommand::toText));
throw PERM_NOT_EXIST.create();
}

private static int unblockByProjectorUrl(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
var data = ProjectorURLSavedData.get(ctx.getSource().getLevel());
var arg = ProjectorURLArgument.getUrl(ctx, "url");
var pairOptional = toPairOpt(data, arg);
if (pairOptional.isPresent()) {
var pair = pairOptional.get();
var text = toText(pair.getKey(), pair.getValue());
if (data.setBlockedStatusByCommand(pair.getKey(), pair.getValue(), ctx.getSource(), false)) {
var msg = Component.translatable("command.slide_show.unblock_projector_url.success", text);
ctx.getSource().sendSuccess(msg.withStyle(ChatFormatting.GREEN), true);
return Command.SINGLE_SUCCESS;
if (SlidePermission.canUnblockUrl(ctx.getSource().source)) {
var data = ProjectorURLSavedData.get(ctx.getSource().getLevel());
var arg = ProjectorURLArgument.getUrl(ctx, "url");
var pairOptional = toPairOpt(data, arg);
if (pairOptional.isPresent()) {
var pair = pairOptional.get();
var text = toText(pair.getKey(), pair.getValue());
if (data.setBlockedStatusByCommand(pair.getKey(), pair.getValue(), ctx.getSource(), false)) {
var msg = Component.translatable("command.slide_show.unblock_projector_url.success", text);
ctx.getSource().sendSuccess(msg.withStyle(ChatFormatting.GREEN), true);
return Command.SINGLE_SUCCESS;
}
}
throw URL_NOT_EXIST.create(arg.map(SlideCommand::toText, SlideCommand::toText));
}
throw URL_NOT_EXIST.create(arg.map(SlideCommand::toText, SlideCommand::toText));
throw PERM_NOT_EXIST.create();
}

private static Optional<Map.Entry<UUID, ProjectorURL>> toPairOpt(ProjectorURLSavedData data,
Expand Down
41 changes: 40 additions & 1 deletion src/main/java/org/teacon/slides/admin/SlidePermission.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,74 @@

import net.minecraft.FieldsAreNonnullByDefault;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.commands.CommandSource;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.rcon.RconConsoleSource;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.server.permission.PermissionAPI;
import net.minecraftforge.server.permission.events.PermissionGatherEvent;
import net.minecraftforge.server.permission.nodes.PermissionDynamicContext;
import net.minecraftforge.server.permission.nodes.PermissionNode;
import net.minecraftforge.server.permission.nodes.PermissionTypes;
import org.teacon.slides.SlideShow;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Objects;
import java.util.UUID;

@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)
public final class SlidePermission {
private static @Nullable PermissionNode<Boolean> INTERACT_PERM;
private static @Nullable PermissionNode<Boolean> BLOCK_PERM;
private static @Nullable PermissionNode<Boolean> UNBLOCK_PERM;

@SubscribeEvent
public static void gatherPermNodes(PermissionGatherEvent.Nodes event) {
// FIXME: permission resolving
event.addNodes(INTERACT_PERM = new PermissionNode<>(SlideShow.ID,
"interact.projector", PermissionTypes.BOOLEAN, (player, playerUUID, context) -> true));
"interact.projector", PermissionTypes.BOOLEAN, SlidePermission::everyone));
event.addNodes(BLOCK_PERM = new PermissionNode<>(SlideShow.ID,
"interact_url.block", PermissionTypes.BOOLEAN, SlidePermission::operator));
event.addNodes(UNBLOCK_PERM = new PermissionNode<>(SlideShow.ID,
"interact_url.unblock", PermissionTypes.BOOLEAN, SlidePermission::operator));
}

public static boolean canInteract(Player p) {
return p instanceof ServerPlayer sp && PermissionAPI.getPermission(sp, Objects.requireNonNull(INTERACT_PERM));
}

public static boolean canBlockUrl(CommandSource p) {
if (p instanceof ServerPlayer sp) {
return PermissionAPI.getPermission(sp, Objects.requireNonNull(BLOCK_PERM));
}
if (p instanceof MinecraftServer || p instanceof RconConsoleSource) {
return true;
}
return false;
}

public static boolean canUnblockUrl(CommandSource p) {
if (p instanceof ServerPlayer sp) {
return PermissionAPI.getPermission(sp, Objects.requireNonNull(UNBLOCK_PERM));
}
if (p instanceof MinecraftServer || p instanceof RconConsoleSource) {
return true;
}
return false;
}

private static boolean everyone(@Nullable ServerPlayer player, UUID uuid, PermissionDynamicContext<?>... context) {
return true;
}

private static Boolean operator(@Nullable ServerPlayer player, UUID uuid, PermissionDynamicContext<?>... context) {
return player != null && player.hasPermissions(2);
}
}
1 change: 1 addition & 0 deletions src/main/resources/assets/slide_show/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"command.slide_show.unblock_projector_url.success": "The projector url (%s) is successfully unblocked",

"command.slide_show.failed.url_not_exist": "The input url or uuid (%s) cannot be used for further operations",
"command.slide_show.failed.perm_not_exist": "You do not have related permission for executing the command",

"gui.slide_show.url": "Image Link",
"gui.slide_show.color": "Slide Color",
Expand Down

0 comments on commit 9c6130f

Please sign in to comment.