From fa77573c96f6b7a117d2c77d2ae6386af90da6e9 Mon Sep 17 00:00:00 2001 From: Quie_ Date: Thu, 15 Jun 2023 13:20:52 -0400 Subject: [PATCH] This commit fixes two issues. 1: The Update Screen has been removed and now just shows the version and update if available in the corner. 2: All toggleable features now have a keybind option. --- .../features/toggleable/BigDustFeature.java | 1 + .../toggleable/ToggleableFeature.java | 37 +++++++++++++++++++ .../macros/gui/screen/MacroEditScreen.java | 1 - .../mixin/update/CheckUpdateMixin.java | 36 ++++++++++-------- .../redstonetools/update/UpdateScreen.java | 21 ----------- 5 files changed, 59 insertions(+), 37 deletions(-) delete mode 100644 src/main/java/tools/redstone/redstonetools/update/UpdateScreen.java diff --git a/src/main/java/tools/redstone/redstonetools/features/toggleable/BigDustFeature.java b/src/main/java/tools/redstone/redstonetools/features/toggleable/BigDustFeature.java index 5508f655..f900ba2b 100644 --- a/src/main/java/tools/redstone/redstonetools/features/toggleable/BigDustFeature.java +++ b/src/main/java/tools/redstone/redstonetools/features/toggleable/BigDustFeature.java @@ -7,4 +7,5 @@ @AutoService(AbstractFeature.class) @Feature(name = "Big Dust", description = "Change the size of redstone's hitbox.", command = "bigdust") public class BigDustFeature extends ToggleableFeature { + } diff --git a/src/main/java/tools/redstone/redstonetools/features/toggleable/ToggleableFeature.java b/src/main/java/tools/redstone/redstonetools/features/toggleable/ToggleableFeature.java index cfdacb5a..5beb6260 100644 --- a/src/main/java/tools/redstone/redstonetools/features/toggleable/ToggleableFeature.java +++ b/src/main/java/tools/redstone/redstonetools/features/toggleable/ToggleableFeature.java @@ -1,5 +1,9 @@ package tools.redstone.redstonetools.features.toggleable; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; import tools.redstone.redstonetools.features.AbstractFeature; import tools.redstone.redstonetools.features.Feature; import tools.redstone.redstonetools.features.feedback.Feedback; @@ -11,6 +15,9 @@ import net.minecraft.server.command.ServerCommandSource; +import java.util.ArrayList; +import java.util.List; + import static tools.redstone.redstonetools.RedstoneToolsClient.INJECTOR; import static net.minecraft.server.command.CommandManager.literal; @@ -18,6 +25,36 @@ public abstract class ToggleableFeature extends AbstractFeature { private boolean enabled; private Feature info; + private static final List keyBindings = new ArrayList<>(); + + @Override + public void register() { + super.register(); + + var containsRequiredArguments = ReflectionUtils.getArguments(getClass()).stream() + .anyMatch(a -> !a.isOptional()); + if (containsRequiredArguments) { + return; + } + + var info = ReflectionUtils.getFeatureInfo(getClass()); + var keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding( + info.name(), + InputUtil.Type.KEYSYM, + -1, + "Redstone Tools" + )); + + keyBindings.add(keyBinding); + + ClientTickEvents.END_CLIENT_TICK.register(client -> { + while (keyBinding.wasPressed()) { + assert client.player != null; + client.player.sendChatMessage("/" + info.command()); + } + }); + } + @Override protected void registerCommands(CommandDispatcher dispatcher, boolean dedicated) { info = ReflectionUtils.getFeatureInfo(getClass()); diff --git a/src/main/java/tools/redstone/redstonetools/macros/gui/screen/MacroEditScreen.java b/src/main/java/tools/redstone/redstonetools/macros/gui/screen/MacroEditScreen.java index f3895aac..8ad466c9 100644 --- a/src/main/java/tools/redstone/redstonetools/macros/gui/screen/MacroEditScreen.java +++ b/src/main/java/tools/redstone/redstonetools/macros/gui/screen/MacroEditScreen.java @@ -32,7 +32,6 @@ public class MacroEditScreen extends GameOptionsScreen { - private final MacroListWidget macroListWidget; private final Macro macro; diff --git a/src/main/java/tools/redstone/redstonetools/mixin/update/CheckUpdateMixin.java b/src/main/java/tools/redstone/redstonetools/mixin/update/CheckUpdateMixin.java index c304c268..d2642ae4 100644 --- a/src/main/java/tools/redstone/redstonetools/mixin/update/CheckUpdateMixin.java +++ b/src/main/java/tools/redstone/redstonetools/mixin/update/CheckUpdateMixin.java @@ -2,16 +2,16 @@ import com.google.gson.Gson; import com.google.gson.JsonObject; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.TitleScreen; -import net.minecraft.text.Text; +import net.minecraft.client.gui.widget.PressableTextWidget; +import net.minecraft.text.*; +import net.minecraft.util.Util; 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 tools.redstone.redstonetools.RedstoneToolsClient; -import tools.redstone.redstonetools.update.UpdateScreen; import java.net.URI; import java.net.http.HttpClient; @@ -25,12 +25,14 @@ public class CheckUpdateMixin extends Screen { private static boolean updateChecked = false; - public CheckUpdateMixin(Text title) { - super(title); + private static MutableText updateStatus = (MutableText) Text.of("Redstone Tools Version: " + MOD_VERSION + "(Bug found, report on Github)"); + private static URI uri; + public CheckUpdateMixin() { + super(Text.of("UpdateText(Bug found, report on Github)")); } @Inject(method = "init", at = @At("TAIL")) - public void init(CallbackInfo ci) { + public void checkUpdate(CallbackInfo ci) { if (updateChecked) return; @@ -54,7 +56,7 @@ public void init(CallbackInfo ci) { Gson gson = new Gson(); JsonObject release = gson.fromJson(responseBody, JsonObject.class); - URI uri = new URI(release.get("html_url").getAsString()); + uri = new URI(release.get("html_url").getAsString()); String newVersion = release.get("tag_name").getAsString(); LOGGER.info("Found latest version: " + newVersion); @@ -63,18 +65,17 @@ public void init(CallbackInfo ci) { return; } - //TODO REMEMBER TO REMOVE THE EXCLAMATION + Style underline = Style.EMPTY; if (RedstoneToolsClient.MOD_VERSION.equals(newVersion)) { LOGGER.info("Already up to date, current version: " + MOD_VERSION); - return; + updateStatus = (MutableText) Text.of("Redstone Tools " + MOD_VERSION); + } else { + LOGGER.info("Found newer version, current version: " + RedstoneToolsClient.MOD_VERSION + ", new version: " + newVersion); + updateStatus = (MutableText) Text.of("Redstone Tools " + MOD_VERSION + " ("); + updateStatus.append(Text.of("Click to Update").getWithStyle(underline.withUnderline(true)).get(0)); + updateStatus.append(")"); } - LOGGER.info("Found newer version, current version: " + RedstoneToolsClient.MOD_VERSION + ", new version: " + newVersion); - - var parentScreen = MinecraftClient.getInstance().currentScreen; - var popup = new UpdateScreen(parentScreen,uri,"Update Available","An update is available for redstone tools! You are on version " + MOD_VERSION + " but version " + newVersion + " is available."); - - MinecraftClient.getInstance().setScreen(popup); } catch (Exception e) { LOGGER.warn("Failed to check for RedstoneTools updates"); e.printStackTrace(); @@ -82,4 +83,9 @@ public void init(CallbackInfo ci) { updateChecked = true; } } + + @Inject(method="init", at = @At("HEAD")) + public void updateTextInjection(CallbackInfo ci){ + this.addDrawableChild(new PressableTextWidget(4,4, textRenderer.getWidth(updateStatus), textRenderer.fontHeight,updateStatus,button -> {Util.getOperatingSystem().open(uri);},textRenderer)); + } } diff --git a/src/main/java/tools/redstone/redstonetools/update/UpdateScreen.java b/src/main/java/tools/redstone/redstonetools/update/UpdateScreen.java deleted file mode 100644 index b90c6bf2..00000000 --- a/src/main/java/tools/redstone/redstonetools/update/UpdateScreen.java +++ /dev/null @@ -1,21 +0,0 @@ -package tools.redstone.redstonetools.update; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.screen.ConfirmScreen; -import net.minecraft.client.gui.screen.Screen; -import net.minecraft.text.Text; -import net.minecraft.util.Util; - -import java.net.URI; - -public class UpdateScreen extends ConfirmScreen { - public UpdateScreen(Screen parentScreen, URI updateSource, String title, String message){ - super(confirmed -> { - MinecraftClient.getInstance().setScreen(parentScreen); - - if (confirmed) { - Util.getOperatingSystem().open(updateSource); - } - }, Text.of(title),Text.of(message),Text.of("Go to release"), Text.of("Ignore")); - } -}