-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add update checker to check version with GitHub latest release (#64)
* add update checker to check version with GitHub latest release * better exception logger * make UpdateChecker extends Thread, cache latestVersion * show update msg when player join world * fix mod version
- Loading branch information
1 parent
afce8d1
commit fbb10a5
Showing
12 changed files
with
156 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...ava/io/github/skydynamic/quickbackupmulti/mixin/client/ClientPlayNetworkHandlerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.github.skydynamic.quickbackupmulti.mixin.client; | ||
|
||
import io.github.skydynamic.quickbackupmulti.QuickBackupMulti; | ||
import io.github.skydynamic.quickbackupmulti.utils.Messenger; | ||
import io.github.skydynamic.quickbackupmulti.utils.UpdateChecker; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.network.ClientPlayNetworkHandler; | ||
import net.minecraft.client.network.ClientPlayerEntity; | ||
import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket; | ||
import net.minecraft.text.ClickEvent; | ||
import net.minecraft.text.HoverEvent; | ||
import net.minecraft.text.MutableText; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import static io.github.skydynamic.quickbackupmulti.i18n.Translate.tr; | ||
|
||
@Mixin(ClientPlayNetworkHandler.class) | ||
public class ClientPlayNetworkHandlerMixin { | ||
@Inject( | ||
method = "onGameJoin", | ||
at = @At("TAIL") | ||
) | ||
private void showUpdateMsg(GameJoinS2CPacket packet, CallbackInfo ci) { | ||
if (MinecraftClient.getInstance().player == null) { | ||
return; | ||
} | ||
UpdateChecker checker = QuickBackupMulti.updateChecker; | ||
if (checker.needUpdate) { | ||
ClientPlayerEntity player = MinecraftClient.getInstance().player; | ||
MutableText updateText = Messenger.literal( | ||
tr("quickbackupmulti.check_update.on_player_join", checker.latestVersion) | ||
); | ||
updateText.styled(style -> style | ||
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, checker.latestVersionHtmUrl)) | ||
.withHoverEvent(new HoverEvent( | ||
HoverEvent.Action.SHOW_TEXT, Messenger.literal(tr("quickbackupmulti.check_update.click_tooltip")) | ||
)) | ||
); | ||
player.sendMessage(updateText, false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/io/github/skydynamic/quickbackupmulti/utils/UpdateChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.github.skydynamic.quickbackupmulti.utils; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import io.github.skydynamic.quickbackupmulti.QuickBackupMulti; | ||
import io.github.skydynamic.quickbackupmulti.config.Config; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.util.EntityUtils; | ||
|
||
import java.io.IOException; | ||
|
||
public class UpdateChecker extends Thread { | ||
|
||
private static final HttpClient CLIENT = HttpClients.createDefault(); | ||
private static final String RELEASE_API_URL = "https://api.github.com/repos/QuickBackupMultiMod-Dev/QuickBackupM-Fabric/releases/latest"; | ||
|
||
public String latestVersion; | ||
public String latestVersionHtmUrl; | ||
public boolean needUpdate = false; | ||
|
||
public UpdateChecker() { | ||
super("QuickBackupM-Fabric-Update-Checker"); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
if (Config.TEMP_CONFIG.modVersion == null) { | ||
QuickBackupMulti.LOGGER.warn("Current mod version is not found."); | ||
return; | ||
} | ||
HttpResponse httpResponse = CLIENT.execute(new HttpGet(RELEASE_API_URL)); | ||
String body = EntityUtils.toString(httpResponse.getEntity()); | ||
|
||
// Get Meta data | ||
JsonObject jsonObject = JsonParser.parseString(body).getAsJsonObject(); | ||
latestVersion = jsonObject.get("tag_name").getAsString(); | ||
latestVersionHtmUrl = jsonObject.get("html_url").getAsString(); | ||
|
||
String tag = latestVersion | ||
.replaceAll("\\+.*", "") | ||
.replaceFirst("^v", ""); | ||
String currentVersion = Config.TEMP_CONFIG.modVersion | ||
.replaceAll("\\+.*", "") | ||
.replaceFirst("^v", ""); | ||
|
||
if (tag.compareTo(currentVersion) > 0) { | ||
needUpdate = true; | ||
QuickBackupMulti.LOGGER.info( | ||
"{} has new version {}. You can see: {}", QuickBackupMulti.modName, latestVersion, latestVersionHtmUrl | ||
); | ||
} | ||
|
||
} catch (IOException e) { | ||
QuickBackupMulti.LOGGER.error("Check update failed", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters