Skip to content

Commit

Permalink
Adjust command output texts for urls
Browse files Browse the repository at this point in the history
  • Loading branch information
ustc-zzzz committed Apr 2, 2023
1 parent 52e776a commit 946e3f4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 33 deletions.
78 changes: 51 additions & 27 deletions src/main/java/org/teacon/slides/admin/SlideCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.datafixers.util.Either;
import net.minecraft.ChatFormatting;
import net.minecraft.FieldsAreNonnullByDefault;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.apache.commons.lang3.StringUtils;
import org.teacon.slides.SlideShow;
import org.teacon.slides.network.ProjectorURLPrefetchPacket;
import org.teacon.slides.url.ProjectorURL;
import org.teacon.slides.url.ProjectorURLArgument;
import org.teacon.slides.url.ProjectorURLSavedData;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -54,57 +59,76 @@ private static LiteralArgumentBuilder<CommandSourceStack> command(String name) {
}

private static int prefetchProjectorUrl(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
// noinspection DuplicatedCode
var level = ctx.getSource().getLevel();
var data = ProjectorURLSavedData.get(level);
var arg = ProjectorURLArgument.getUrl(ctx, "url");
var data = ProjectorURLSavedData.get(ctx.getSource().getLevel());
var urlOptional = arg.map(data::getUrlById, Optional::of);
if (urlOptional.isPresent()) {
var url = urlOptional.get();
var uuid = data.getOrCreateIdByCommand(url, ctx.getSource());
new ProjectorURLPrefetchPacket(Set.of(uuid), data).sendToAll();
var msg = Component.translatable("command.slide_show.prefetch_projector_url.success", url, uuid);
var msg = Component.translatable("command.slide_show.prefetch_projector_url.success", toText(uuid, url));
ctx.getSource().sendSuccess(msg.withStyle(ChatFormatting.GREEN), true);
return Command.SINGLE_SUCCESS;
}
throw URL_NOT_EXIST.create(arg.map(UUID::toString, u -> u.toUrl().toString()));
throw URL_NOT_EXIST.create(arg.map(SlideCommand::toText, SlideCommand::toText));
}

private static int blockByProjectorUrl(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
// noinspection DuplicatedCode
var level = ctx.getSource().getLevel();
var data = ProjectorURLSavedData.get(level);
var data = ProjectorURLSavedData.get(ctx.getSource().getLevel());
var arg = ProjectorURLArgument.getUrl(ctx, "url");
var urlOptional = arg.map(data::getUrlById, Optional::of);
var uuidOptional = arg.map(Optional::of, data::getIdByUrl);
if (urlOptional.isPresent() && uuidOptional.isPresent()) {
var url = urlOptional.get();
var uuid = uuidOptional.get();
if (data.setBlockedStatusByCommand(uuid, url, ctx.getSource(), true)) {
var msg = Component.translatable("command.slide_show.block_projector_url.success", url, uuid);
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(UUID::toString, u -> u.toUrl().toString()));
throw URL_NOT_EXIST.create(arg.map(SlideCommand::toText, SlideCommand::toText));
}

private static int unblockByProjectorUrl(CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
// noinspection DuplicatedCode
var level = ctx.getSource().getLevel();
var data = ProjectorURLSavedData.get(level);
var data = ProjectorURLSavedData.get(ctx.getSource().getLevel());
var arg = ProjectorURLArgument.getUrl(ctx, "url");
var urlOptional = arg.map(data::getUrlById, Optional::of);
var uuidOptional = arg.map(Optional::of, data::getIdByUrl);
if (urlOptional.isPresent() && uuidOptional.isPresent()) {
var url = urlOptional.get();
var uuid = uuidOptional.get();
if (data.setBlockedStatusByCommand(uuid, url, ctx.getSource(), false)) {
var msg = Component.translatable("command.slide_show.unblock_projector_url.success", url, uuid);
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(UUID::toString, u -> u.toUrl().toString()));
throw URL_NOT_EXIST.create(arg.map(SlideCommand::toText, SlideCommand::toText));
}

private static Optional<Map.Entry<UUID, ProjectorURL>> toPairOpt(ProjectorURLSavedData data,
Either<UUID, ProjectorURL> arg) {
return arg.map(
id -> data.getUrlById(id).map(url -> Map.entry(id, url)),
url -> data.getIdByUrl(url).map(id -> Map.entry(id, url)));
}

private static Component toText(UUID id, ProjectorURL url) {
var click = new ClickEvent(ClickEvent.Action.OPEN_URL, url.toUrl().toString());
var text = StringUtils.abbreviate(StringUtils.substringAfter(url.toString(), "://"), 15);
var hover = new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal("UUID:\n" + id + "\n\nURL:\n" + url.toUrl()));
return Component.literal(text).withStyle(s -> s.withColor(ChatFormatting.AQUA).withHoverEvent(hover).withClickEvent(click));
}

private static Component toText(ProjectorURL url) {
var click = new ClickEvent(ClickEvent.Action.OPEN_URL, url.toUrl().toString());
var text = StringUtils.abbreviate(StringUtils.substringAfter(url.toString(), "://"), 15);
var hover = new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal("URL:\n" + url.toUrl()));
return Component.literal(text).withStyle(s -> s.withColor(ChatFormatting.AQUA).withHoverEvent(hover).withClickEvent(click));
}

private static Component toText(UUID id) {
var text = StringUtils.abbreviate(id.toString(), 15);
var hover = new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.literal("UUID:\n" + id));
return Component.literal(text).withStyle(s -> s.withColor(ChatFormatting.AQUA).withHoverEvent(hover));
}
}
6 changes: 3 additions & 3 deletions src/main/resources/assets/slide_show/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

"block.slide_show.projector": "Slide Show Projector",

"command.slide_show.prefetch_projector_url.success": "Successfully prefetched url %s (%s)",
"command.slide_show.block_projector_url.success": "The projector url %s (%s) is successfully blocked",
"command.slide_show.unblock_projector_url.success": "The projector url %s (%s) is successfully unblocked",
"command.slide_show.prefetch_projector_url.success": "Successfully prefetched the requested url (%s)",
"command.slide_show.block_projector_url.success": "The projector url (%s) is successfully blocked",
"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",

Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/assets/slide_show/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

"block.slide_show.projector": "幻灯片投影仪",

"command.slide_show.prefetch_projector_url.success": "已成功预加载 URL %s(%s)",
"command.slide_show.block_projector_url.success": "已成功屏蔽幻灯片 URL %s(%s)",
"command.slide_show.unblock_projector_url.success": "已成功为幻灯片 URL %s(%s)解除屏蔽",
"command.slide_show.prefetch_projector_url.success": "已成功预加载请求的 URL(%s)",
"command.slide_show.block_projector_url.success": "已成功屏蔽幻灯片 URL(%s)",
"command.slide_show.unblock_projector_url.success": "已成功为幻灯片 URL(%s)解除屏蔽",

"command.slide_show.failed.url_not_exist": "输入 URL 或 UUID(%s)无法用于进一步操作",

Expand Down

0 comments on commit 946e3f4

Please sign in to comment.