Skip to content

Commit 1396fab

Browse files
committed
Fix: Use new news.json endpoint
The javaNews.json resource doesn't seem to exist anymore. Therefore, we now use the news.json resource, which also provides us with more but slightly changed data.
1 parent 5828bf2 commit 1396fab

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

lib/src/minecraft/minecraft_news.dart

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
11
/// A Minecraft News item.
22
class MinecraftNews {
33
/// The title of this news item.
4-
String title;
4+
final String title;
5+
6+
final String tag;
7+
8+
/// The category of this news article, e.g. "Minecraft: Java Edition"
9+
final String category;
10+
11+
/// The date on which this news article was published.
12+
final String _date;
513

614
/// The description of this news. Formatted in HTML.
7-
String description;
15+
final String text;
816

917
/// The url to this news article on https://minecraft.net.
10-
String url;
18+
final String url;
1119

1220
/// The id of this news item.
13-
String id;
21+
final String id;
22+
23+
final bool important;
1424

1525
/// The shortened url to the patch's image.
1626
/// Can be properly obtained from [getImageUrl].
1727
final String _imageUrl;
1828

19-
/// If this news item is only for demo users who do not own the game.
20-
bool forDemoUsersOnly;
21-
2229
MinecraftNews.fromJson(Map<String, dynamic> data)
2330
: title = data['title'],
24-
description = data['description'],
25-
url = data['url'],
31+
tag = data.containsKey('tag') ? data['tag'] : '',
32+
category = data['category'],
33+
_date = data['date'],
34+
text = data['text'],
35+
url = data['readMoreLink'],
2636
id = data['id'],
27-
_imageUrl = data['image']['url'],
28-
forDemoUsersOnly = data['forDemoUsersOnly'] ?? false;
37+
_imageUrl = data['newsPageImage']['url'],
38+
important = data.containsKey('cardBorder') ? data['cardBorder'] : false;
2939

3040
/// Get's the url to the image of this news item. News images are
3141
/// usually 1200x512 pixels in size.
3242
String get imageUrl => 'https://launchercontent.mojang.com/$_imageUrl';
43+
44+
/// Get's the date at which this article was published.
45+
/// Note that this is always at midnight, as the original
46+
/// timestamp does not provide any hours/minutes.
47+
DateTime get date => DateTime.parse(_date);
3348
}

lib/src/minecraft_api.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ const String _resourcesApi = 'http://resources.download.minecraft.net/';
1818
/// This data can be found on the "News" page in the official
1919
/// Minecraft Launcher.
2020
Future<List<MinecraftNews>> getNews() async {
21-
final response =
22-
await request(http.get, _launcherContentApi, 'javaNews.json');
21+
final response = await request(http.get, _launcherContentApi, 'news.json');
2322
final data = parseResponseMap(response);
2423
final news = <MinecraftNews>[];
2524
for (Map<String, dynamic> e in data['entries']) {

test/minecraft_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ void main() async {
88
final versionManifest = await getVersions();
99
expect(versionManifest, isA<VersionManifest>());
1010
expect(versionManifest.versions, isNotEmpty);
11+
expect(versionManifest.versions.where((v) {
12+
return v.id == '1.18.1';
13+
}), isNotEmpty);
1114
});
1215

1316
test('Test if the patch notes are empty or not.', () async {
@@ -20,4 +23,9 @@ void main() async {
2023
expect(patchNotes.first.version,
2124
(String item) => item.isNotEmpty && item.length >= 4);
2225
});
26+
27+
test('Test if Minecraft news exists', () async {
28+
final news = await getNews();
29+
expect(news, isNotEmpty);
30+
});
2331
}

0 commit comments

Comments
 (0)