Skip to content

Commit 63180d0

Browse files
committed
Blockbench update notifier
1 parent e7f1a2a commit 63180d0

File tree

6 files changed

+174
-2
lines changed

6 files changed

+174
-2
lines changed

src/commander/java/com/mcmoddev/mmdbot/commander/config/Configuration.java

+9
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ public List<SnowflakeValue> parchment() {
178178
public List<SnowflakeValue> minecraft() {
179179
return minecraft;
180180
}
181+
182+
@Required
183+
@Setting("blockbench")
184+
@Comment("A list of Snowflake IDs of channels in which to send Blockbench update notifiers.")
185+
private List<SnowflakeValue> blockbench = new ArrayList<>();
186+
187+
public List<SnowflakeValue> blockbench() {
188+
return blockbench;
189+
}
181190
}
182191
}
183192

src/commander/java/com/mcmoddev/mmdbot/commander/updatenotifiers/UpdateNotifiers.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
package com.mcmoddev.mmdbot.commander.updatenotifiers;
2222

23+
import com.mcmoddev.mmdbot.commander.updatenotifiers.blockbench.BlockbenchUpdateNotifier;
2324
import com.mcmoddev.mmdbot.commander.updatenotifiers.fabric.FabricApiUpdateNotifier;
2425
import com.mcmoddev.mmdbot.commander.updatenotifiers.forge.ForgeUpdateNotifier;
2526
import com.mcmoddev.mmdbot.commander.updatenotifiers.minecraft.MinecraftUpdateNotifier;
@@ -55,15 +56,16 @@ public static void register() {
5556
}
5657
wasRegistered = true;
5758
Events.MISC_BUS.addListener((TaskScheduler.CollectTasksEvent event) -> {
58-
final var checkingPeriod = 15;
59+
final long checkingPeriod = 15;
5960
LOGGER.info("Checking for Minecraft, Forge, Quilt and Fabric updates every {} minutes.", checkingPeriod);
6061
event.addTask(new MinecraftUpdateNotifier(), 0, checkingPeriod, TimeUnit.MINUTES);
6162
event.addTask(new ForgeUpdateNotifier(), 0, checkingPeriod, TimeUnit.MINUTES);
6263
event.addTask(new QuiltUpdateNotifier(), 0, checkingPeriod, TimeUnit.MINUTES);
6364
event.addTask(new FabricApiUpdateNotifier(), 0, checkingPeriod, TimeUnit.MINUTES);
6465

65-
LOGGER.info("Checking for Parchment updates every hour.");
66+
LOGGER.info("Checking for Parchment and Blockbench updates every hour.");
6667
event.addTask(new ParchmentUpdateNotifier(), 0, 1, TimeUnit.HOURS);
68+
event.addTask(new BlockbenchUpdateNotifier(), 0, 1, TimeUnit.HOURS);
6769
});
6870
}
6971
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2023 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation;
8+
* Specifically version 2.1 of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
21+
package com.mcmoddev.mmdbot.commander.updatenotifiers.blockbench;
22+
23+
import com.mcmoddev.mmdbot.commander.config.Configuration;
24+
import com.mcmoddev.mmdbot.commander.updatenotifiers.UpdateNotifier;
25+
import com.mcmoddev.mmdbot.commander.util.StringSerializer;
26+
import com.mcmoddev.mmdbot.core.util.Utils;
27+
import net.dv8tion.jda.api.EmbedBuilder;
28+
import net.dv8tion.jda.api.entities.MessageEmbed;
29+
import org.jetbrains.annotations.NotNull;
30+
31+
import javax.annotation.Nullable;
32+
import java.io.IOException;
33+
import java.time.Instant;
34+
import java.util.Comparator;
35+
import java.util.stream.Collectors;
36+
import java.util.stream.Stream;
37+
38+
public class BlockbenchUpdateNotifier extends UpdateNotifier<GithubRelease> {
39+
public BlockbenchUpdateNotifier() {
40+
super(NotifierConfiguration.<GithubRelease>builder()
41+
.name("blockbench")
42+
.channelGetter(Configuration.Channels.UpdateNotifiers::blockbench)
43+
.versionComparator(Comparator.comparing(release -> Instant.parse(release.published_at())))
44+
.serializer(StringSerializer.json(StringSerializer.RECORD_GSON, GithubRelease.class))
45+
.webhookInfo(new WebhookInfo("Blockbench Updates", "https://www.blockbench.net/favicon.png"))
46+
.build());
47+
}
48+
49+
@Nullable
50+
@Override
51+
protected GithubRelease queryLatest() throws IOException {
52+
return BlockbenchVersionHelper.getLatest(loggingMarker);
53+
}
54+
55+
@NotNull
56+
@Override
57+
protected EmbedBuilder getEmbed(@Nullable final GithubRelease oldVersion, final @NotNull GithubRelease newVersion) {
58+
return new EmbedBuilder()
59+
.setTitle("New Blockbench %s: %s".formatted(newVersion.prerelease() ? "pre-release" : "release", newVersion.name()), newVersion.html_url())
60+
.setColor(newVersion.prerelease() ? 0x29CFD8 : 0x1E93D9)
61+
.setDescription(Utils.truncate(Stream.of(newVersion.body().split("\n"))
62+
.map(str -> str.trim().startsWith("#") ? "**" + str.replace("#", "") + "**" : str)
63+
.collect(Collectors.joining("\n")), MessageEmbed.DESCRIPTION_MAX_LENGTH / 2)); // 4k char embed is big...
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2023 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation;
8+
* Specifically version 2.1 of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
21+
package com.mcmoddev.mmdbot.commander.updatenotifiers.blockbench;
22+
23+
import com.google.gson.reflect.TypeToken;
24+
import com.mcmoddev.mmdbot.commander.updatenotifiers.UpdateNotifiers;
25+
import com.mcmoddev.mmdbot.commander.util.StringSerializer;
26+
import com.mcmoddev.mmdbot.core.util.Constants;
27+
import com.mcmoddev.mmdbot.core.util.Utils;
28+
import lombok.SneakyThrows;
29+
import org.jetbrains.annotations.Nullable;
30+
import org.slf4j.Marker;
31+
32+
import java.io.IOException;
33+
import java.net.URI;
34+
import java.net.http.HttpRequest;
35+
import java.net.http.HttpResponse;
36+
import java.util.List;
37+
38+
public class BlockbenchVersionHelper {
39+
/**
40+
* {@return the latest Blockbench version}
41+
*/
42+
@Nullable
43+
@SneakyThrows(InterruptedException.class)
44+
public static GithubRelease getLatest(final Marker loggingMarker) throws IOException {
45+
final HttpResponse<List<GithubRelease>> response = Constants.HTTP_CLIENT.send(HttpRequest.newBuilder()
46+
.uri(URI.create("https://api.github.com/repos/JannisX11/blockbench/releases"))
47+
.header("Accept", "application/json")
48+
.build(), Utils.ofGson(StringSerializer.RECORD_GSON, new TypeToken<>() {}));
49+
50+
if (response.statusCode() != 200) {
51+
UpdateNotifiers.LOGGER.error(loggingMarker, "Server replied with non-200 status code {}.", response.statusCode());
52+
return null;
53+
}
54+
55+
final List<GithubRelease> releases = response.body();
56+
return releases.isEmpty() ? null : releases.get(0); // First is the latest
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2023 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation;
8+
* Specifically version 2.1 of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
21+
package com.mcmoddev.mmdbot.commander.updatenotifiers.blockbench;
22+
23+
record GithubRelease(
24+
String html_url, String name, boolean prerelease, String tag_name, String body, String published_at
25+
) {}

src/core/java/com/mcmoddev/mmdbot/core/util/Utils.java

+13
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222

2323
import club.minnced.discord.webhook.send.WebhookEmbedBuilder;
2424
import club.minnced.discord.webhook.send.WebhookMessageBuilder;
25+
import com.google.gson.Gson;
26+
import com.google.gson.reflect.TypeToken;
2527
import net.dv8tion.jda.api.entities.Message;
2628
import net.dv8tion.jda.api.entities.MessageEmbed;
2729

2830
import java.io.BufferedReader;
2931
import java.io.InputStreamReader;
3032
import java.net.URL;
33+
import java.net.http.HttpResponse;
3134
import java.time.Instant;
3235
import java.time.OffsetDateTime;
3336
import java.time.ZoneOffset;
@@ -121,4 +124,14 @@ public static String rgbToString(int rgb) {
121124
// toHexString returns an ARGB color
122125
return "#" + Integer.toHexString(rgb).substring(2);
123126
}
127+
128+
public static <T> HttpResponse.BodyHandler<T> ofGson(Gson gson, TypeToken<T> token) {
129+
return responseInfo -> HttpResponse.BodySubscribers.mapping(
130+
HttpResponse.BodySubscribers.ofInputStream(),
131+
io.github.matyrobbrt.curseforgeapi.util.Utils.rethrowFunction(stream -> {
132+
final InputStreamReader reader = new InputStreamReader(stream);
133+
return gson.fromJson(reader, token);
134+
})
135+
);
136+
}
124137
}

0 commit comments

Comments
 (0)