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 ddbc6c30..2ace5531 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,14 @@ 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; +import tools.redstone.redstonetools.features.feedback.FeedbackSender; +import tools.redstone.redstonetools.utils.ReflectionUtils; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; @@ -24,11 +33,53 @@ import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import java.util.ArrayList; +import java.util.List; + +import static tools.redstone.redstonetools.RedstoneToolsClient.INJECTOR; + import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; public abstract class ToggleableFeature extends AbstractFeature { + private static final List keyBindings = new ArrayList<>(); + + @Override + public void register() { + super.register(); + + // load user settings + // and register save hook + loadConfig(); + ClientLifecycleEvents.CLIENT_STOPPING.register(client -> { + saveConfig(); + }); + + 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()); + } + }); + } + private static final Executor IO_EXECUTOR = Executors.newSingleThreadExecutor(); private static final Gson GSON = new GsonBuilder() @@ -42,19 +93,8 @@ public abstract class ToggleableFeature extends AbstractFeature { private final Path configFile = RedstoneToolsClient.CONFIG_DIR .resolve("features").resolve(getID() + ".json"); - @Override - public void register() { - super.register(); - - // load user settings - // and register save hook - loadConfig(); - ClientLifecycleEvents.CLIENT_STOPPING.register(client -> { - saveConfig(); - }); - } - @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override protected void registerCommands(CommandDispatcher dispatcher, boolean dedicated) { var baseCommand = literal(getCommand()) 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")); - } -}