From 54d7d3bdc1c40672f8165445e5674e5fe3a63ea8 Mon Sep 17 00:00:00 2001 From: LunarAkai Date: Sun, 14 Jul 2024 16:02:05 +0200 Subject: [PATCH] html format works somewhat (needs a prettier solution) + correct time format --- .../blockfox/commands/TestRSSCommand.java | 8 ++++++- .../lunarakai/blockfox/utils/HTMLUtils.java | 10 ++++++--- .../lunarakai/blockfox/utils/TimeUtils.java | 22 +++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/main/java/de/lunarakai/blockfox/utils/TimeUtils.java diff --git a/src/main/java/de/lunarakai/blockfox/commands/TestRSSCommand.java b/src/main/java/de/lunarakai/blockfox/commands/TestRSSCommand.java index 54a6ba2..6ee3ec6 100644 --- a/src/main/java/de/lunarakai/blockfox/commands/TestRSSCommand.java +++ b/src/main/java/de/lunarakai/blockfox/commands/TestRSSCommand.java @@ -12,11 +12,14 @@ import de.iani.cubesideutils.bukkit.commands.exceptions.RequiresPlayerException; import de.iani.cubesideutils.commands.ArgsParser; import de.lunarakai.blockfox.BlockFoxPlugin; import de.lunarakai.blockfox.utils.HTMLUtils; +import de.lunarakai.blockfox.utils.TimeUtils; import de.lunarakai.lunarutils.StringUtils; import de.lunarakai.lunarutils.chat.MessageUtils; import java.io.IOException; import java.util.List; import java.util.Optional; + +import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -83,7 +86,10 @@ public class TestRSSCommand extends SubCommand { optionalLink.ifPresent(string -> sender.sendMessage(NamedChatColor.AQUA + string)); Optional optionalDate = items.getFirst().getPubDate(); - optionalDate.ifPresent(string -> sender.sendMessage(NamedChatColor.GRAY + string)); + if(optionalDate.isPresent()) { + String time = TimeUtils.convertStringTime(optionalDate.get()); + MessageUtils.sendSimpleSpecialMessage(player, time, NamedTextColor.GRAY); + } } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/main/java/de/lunarakai/blockfox/utils/HTMLUtils.java b/src/main/java/de/lunarakai/blockfox/utils/HTMLUtils.java index f861d46..3e0f818 100644 --- a/src/main/java/de/lunarakai/blockfox/utils/HTMLUtils.java +++ b/src/main/java/de/lunarakai/blockfox/utils/HTMLUtils.java @@ -12,10 +12,14 @@ public class HTMLUtils { // todo: with current method ul li inside another ul li are duplicated List list = new ArrayList<>(); Elements elements = document.select("ul"); + Elements li = elements.select("li"); - for (Element element : elements) { - - list.add("- " + element.html() + "\n"); + for (Element element : li) { + if(element.html().contains("ul")) { + list.add("- " + element.html().split("
    ")[0].trim() + "\n"); + continue; + } + list.add("- " + element.text() + "\n"); diff --git a/src/main/java/de/lunarakai/blockfox/utils/TimeUtils.java b/src/main/java/de/lunarakai/blockfox/utils/TimeUtils.java new file mode 100644 index 0000000..c76277e --- /dev/null +++ b/src/main/java/de/lunarakai/blockfox/utils/TimeUtils.java @@ -0,0 +1,22 @@ +package de.lunarakai.blockfox.utils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +public class TimeUtils { + public static String convertStringTime(String time) { + SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZZZ"); + try { + Date date = sdf.parse(time); + + SimpleDateFormat outputFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm"); + outputFormat.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); + + return outputFormat.format(date); + } catch (ParseException e) { + throw new RuntimeException(e); + } + } +}