aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/shedaniel/rei/update/UpdateAnnouncer.java
blob: fce623da765ff5f046f1fca32a75fe475e2b0e55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package me.shedaniel.rei.update;

import com.google.common.collect.Lists;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;

import java.util.List;

import static me.shedaniel.rei.update.UpdateChecker.*;

public class UpdateAnnouncer {
    
    private static World lastWorld = null;
    
    public static void clientTick(Minecraft client) {
        if (client.world != lastWorld) {
            lastWorld = client.world;
            if (lastWorld != null) {
                if (checkUpdates() && isOutdated()) {
                    String currentVersionString = getCurrentVersion() == null ? "null" : getCurrentVersion().toString();
                    List<Version> versions = getVersionsHigherThan(getCurrentVersion());
                    String t[] = I18n.format("text.rei.update_outdated", currentVersionString, getLatestForGame(), getUpdatePriority(versions).name().toUpperCase()).split("\n");
                    for(String s : t)
                        client.player.sendStatusMessage(new TextComponentString(s), false);
                    getChangelog(getCurrentVersion()).forEach(s -> client.player.sendStatusMessage(new TextComponentString(s), false));
                }
            }
        }
    }
    
    public static List<String> getChangelog(Version currentVersion) {
        List<String> changelogs = Lists.newLinkedList();
        JsonArray array = getElement().getChangelogs().getRift();
        array.forEach(jsonElement -> {
            JsonObject jsonObject = jsonElement.getAsJsonObject();
            Version jsonVersion = new Version(jsonObject.get("version").getAsString());
            if (jsonVersion.compareTo(currentVersion) > 0 && jsonVersion.compareTo(getLatestForGame()) <= 0)
                changelogs.add(I18n.format("text.rei.update_changelog_line", jsonObject.get("text").getAsString()));
        });
        return changelogs;
    }
    
}