Skip to content

Commit

Permalink
Merge pull request #296 from maghedo243/fix/update-and-togglablekeybinds
Browse files Browse the repository at this point in the history
This commit fixes two issues.
  • Loading branch information
Matthias1590 authored Jun 24, 2023
2 parents b343490 + 4405694 commit c5219d8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<KeyBinding> 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()
Expand All @@ -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<ServerCommandSource> dispatcher, boolean dedicated) {
var baseCommand = literal(getCommand())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@


public class MacroEditScreen extends GameOptionsScreen {

private final MacroListWidget macroListWidget;
private final Macro macro;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -63,23 +65,27 @@ 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();
} finally {
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));
}
}

This file was deleted.

0 comments on commit c5219d8

Please sign in to comment.