Skip to content

Commit

Permalink
feat: ignore button
Browse files Browse the repository at this point in the history
  • Loading branch information
Commander07 committed May 10, 2024
1 parent b2b433d commit b7b00ca
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ org.gradle.parallel=true
loader_version=0.14.21

# Mod Properties
mod_version = 1.0.0-1.20
mod_version = 1.1.0-1.20
maven_group = io.github.wynncraft_overhaul.notifier
archives_base_name = notifier

Expand Down
32 changes: 32 additions & 0 deletions src/main/java/io/github/wynncraft_overhaul/notifier/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.github.wynncraft_overhaul.notifier;

import com.google.gson.Gson;
import net.fabricmc.loader.api.FabricLoader;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;

public class Config {
private static final Gson gson = new Gson();
private static final Path PATH = FabricLoader.getInstance().getConfigDir().resolve("notifier.json");
public ArrayList<String> ignoredVersions = new ArrayList<>();

public static Config load() throws IOException {
Config config;
if (Files.isRegularFile(PATH)) {
config = gson.fromJson(new String(Files.readAllBytes(PATH)), Config.class);
} else {
config = new Config();
Files.createDirectories(PATH.getParent());
Files.createFile(PATH);
Files.write(PATH, gson.toJson(config).getBytes());
}
return config;
}

public void save() throws IOException {
Files.write(PATH, gson.toJson(this).getBytes());
}
}
13 changes: 11 additions & 2 deletions src/main/java/io/github/wynncraft_overhaul/notifier/Notifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ public class Notifier implements ModInitializer {
public static Path INSTALLER_PATH;
public static String UPSTREAM_VERSION;
public static String LOCAL_VERSION;
public static Config config;

@Override
static {
try {
config = Config.load();
} catch (IOException e) {
LOGGER.error("Failed to load config!", e);
}
}

@Override
public void onInitialize() {
Gson gson = new Gson();
try {
Expand All @@ -43,7 +52,7 @@ public void onInitialize() {
return;
}
UPSTREAM_VERSION = upstream_manifest.get("modpack_version").getAsString();
if (!UPSTREAM_VERSION.equals(LOCAL_VERSION)) {
if (!UPSTREAM_VERSION.equals(LOCAL_VERSION) && !config.ignoredVersions.contains(UPSTREAM_VERSION)) {
UPDATE_AVAILABLE = true;
Path installer_path = Path.of(local_manifest.get("installer_path").getAsString());
if (installer_path.toFile().isFile()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@
import net.minecraft.client.gui.screen.TitleScreen;
import net.minecraft.client.gui.screen.WarningScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.io.IOException;

public class UpdateAvailableScreen extends WarningScreen {
public UpdateAvailableScreen(Text header, Text message, Text narratedText) {
super(header, message, narratedText);
public UpdateAvailableScreen() {
super(
Text.literal("Update Available!").setStyle(Style.EMPTY.withColor(Formatting.YELLOW).withBold(true).withItalic(true)),
Text.literal("You are on version: " + Notifier.LOCAL_VERSION + ", when " + Notifier.UPSTREAM_VERSION + " is the latest version.\nPlease open the installer and update the modpack!"),
Text.literal("Update available!\nYou are on version: " + Notifier.LOCAL_VERSION + ", when " + Notifier.UPSTREAM_VERSION + " is the latest version.\nPlease open the installer and update the modpack!")
);
}

@Override
protected void initButtons(int yOffset) {
int l = this.height / 4 + 48;
int width = 110;
int margin = (this.width / 3 - width) / 2;

if (Notifier.INSTALLER_PATH != null) {
this.addDrawableChild(ButtonWidget.builder(Text.literal("Open Installer"), (button) -> {
Expand All @@ -25,16 +33,27 @@ protected void initButtons(int yOffset) {
e.printStackTrace();
}
this.client.scheduleStop();
}).dimensions(this.width / 2 - 100, l + 72 + 12, 98, 20).build());
}).dimensions(this.width / 3 - width, l + 72 + 12, width, 20).build());
} else {
this.addDrawableChild(ButtonWidget.builder(Text.literal("Quit"), (button) -> {
this.client.scheduleStop();
}).dimensions(this.width / 2 - 100, l + 72 + 12, 98, 20).build());
}).dimensions(this.width / 3 - width, l + 72 + 12, width, 20).build());
}

this.addDrawableChild(ButtonWidget.builder(Text.literal("Don't Update"), (button) -> {
Notifier.UPDATE_AVAILABLE = false;
this.client.setScreen(new TitleScreen());
}).dimensions(this.width / 2 + 2, l + 72 + 12, 98, 20).build());
}).dimensions(this.width / 3 + margin, l + 72 + 12, width, 20).build());

this.addDrawableChild(ButtonWidget.builder(Text.literal("Ignore This Version"), (button) -> {
Notifier.UPDATE_AVAILABLE = false;
Notifier.config.ignoredVersions.add(Notifier.UPSTREAM_VERSION);
try {
Notifier.config.save();
} catch (IOException e) {
Notifier.LOGGER.error("Failed to save config!", e);
}
this.client.setScreen(new TitleScreen());
}).dimensions(this.width / 3 + width + margin * 2, l + 72 + 12, width, 20).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import io.github.wynncraft_overhaul.notifier.gui.UpdateAvailableScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.TitleScreen;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -22,7 +20,7 @@ protected TitleScreenMixin(Text title) {
@Inject(method = "init", at = @At("HEAD"))
public void init(CallbackInfo ci) {
if (Notifier.UPDATE_AVAILABLE) {
this.client.setScreen(new UpdateAvailableScreen(Text.literal("Update Available!").setStyle(Style.EMPTY.withColor(Formatting.YELLOW).withBold(true).withItalic(true)), Text.literal("You are on version: " + Notifier.LOCAL_VERSION + ", when " + Notifier.UPSTREAM_VERSION + " is the latest version!\nPlease open the installer and update the modpack!"), Text.empty()));
this.client.setScreen(new UpdateAvailableScreen());
}
}
}

0 comments on commit b7b00ca

Please sign in to comment.