Skip to content

Commit

Permalink
1.20.6 (stable, still working on next version)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBreakNFix committed May 28, 2024
1 parent d798893 commit 03e68af
Show file tree
Hide file tree
Showing 18 changed files with 1,337 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/uiutils/GithubRelease.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.uiutils;

import com.google.gson.annotations.SerializedName;

public class GithubRelease {
@SerializedName("tag_name")
private String tagName;

public String getTagName() {
return tagName;
}
}
537 changes: 537 additions & 0 deletions src/main/java/org/uiutils/MainClient.java

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions src/main/java/org/uiutils/SharedVariables.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.uiutils;

import net.minecraft.client.gui.screen.Screen;
import net.minecraft.network.packet.Packet;
import net.minecraft.screen.ScreenHandler;

import java.util.ArrayList;

public class SharedVariables {
public static boolean sendUIPackets = true;
public static boolean delayUIPackets = false;
public static boolean shouldEditSign = true;

public static ArrayList<Packet<?>> delayedUIPackets = new ArrayList<>();

public static Screen storedScreen = null;
public static ScreenHandler storedScreenHandler = null;

public static boolean enabled = true;
public static boolean isMac = false;
public static boolean bypassResourcePack = false;
public static boolean resourcePackForceDeny = false;
}
86 changes: 86 additions & 0 deletions src/main/java/org/uiutils/UpdateUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.uiutils;

import com.google.gson.Gson;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;
import org.uiutils.gui.UpdateScreen;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;

import static org.uiutils.MainClient.getModVersion;

public class UpdateUtils {

public static boolean isOutdated;
public static String version;
public static boolean messageShown;
public static final String currentVersion = getModVersion("uiutils");

public static void checkForUpdates() {
ExecutorService executorService = Executors.newSingleThreadExecutor();
Callable<String> task = () -> {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.github.com/repos/Coderx-Gamer/ui-utils/releases/latest"))
.header("Accept", "application/vnd.github.v3+json")
.build();

try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
Gson gson = new Gson();
GithubRelease release = gson.fromJson(response.body(), GithubRelease.class);
return release.getTagName();
} else {
MainClient.LOGGER.error("Failed to fetch the latest version. Status code: " + response.statusCode());
return null;
}
} catch (IOException | InterruptedException e) {
MainClient.LOGGER.error("Failed to fetch the latest version: " + e);
return null;
}
};

Future<String> future = executorService.submit(task);
try {
String latestVersion = future.get();
MainClient.LOGGER.info("Latest version: " + latestVersion + " Current version: " + currentVersion);
version = latestVersion;
if (latestVersion != null && !latestVersion.equals(currentVersion)) {
isOutdated = true;
}

} catch (Exception e) {
MainClient.LOGGER.error("Failed to check for updates: " + e);
} finally {
executorService.shutdown();
}
}

public static void downloadUpdate() {
MainClient.LOGGER.info("Opening download link...");
try {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(new URI("https://ui-utils.com?ref=ingame"));
} else {
Runtime runtime = Runtime.getRuntime();
runtime.exec(new String[]{"xdg-open", "https://ui-utils.com?ref=ingame"});
}
} catch (IOException | URISyntaxException e) {
MainClient.LOGGER.info(e.getLocalizedMessage(), Level.SEVERE);
}
MinecraftClient.getInstance().setScreen(new UpdateScreen(Text.empty()));
}
}

36 changes: 36 additions & 0 deletions src/main/java/org/uiutils/gui/UpdateScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.uiutils.gui;

import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextWidget;
import net.minecraft.text.Text;

public class UpdateScreen extends Screen {

public UpdateScreen(Text title) {
super(title);
}

@Override
protected void init() {
super.init();
Text message1 = Text.of("In order to update UI-Utils, first quit the game then");
Text message2 = Text.of("delete the old UI-Utils jar file, and replace it with the new one you got on the website.");
int centerX = this.width / 2;

this.addDrawableChild(new TextWidget(centerX - textRenderer.getWidth(message1) / 2, 80, textRenderer.getWidth(message1), 20, message1, this.textRenderer));
this.addDrawableChild(new TextWidget(centerX - textRenderer.getWidth(message2) / 2, 95, textRenderer.getWidth(message2), 20, Text.of(message2), this.textRenderer));

int quitX = centerX - 85;
int backX = centerX + 5;

this.addDrawableChild(ButtonWidget.builder(Text.of("Quit"), (button) -> {
this.client.stop();
}).width(80).position(quitX, 145).build());

this.addDrawableChild(ButtonWidget.builder(Text.of("Back"), (button) -> {
this.client.setScreen(null);
}).width(80).position(backX, 145).build());
}

}
67 changes: 67 additions & 0 deletions src/main/java/org/uiutils/mixin/BookEditScreenMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.uiutils.mixin;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.BookEditScreen;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.uiutils.MainClient;
import org.uiutils.SharedVariables;

import java.util.regex.Pattern;

@Mixin(BookEditScreen.class)
public class BookEditScreenMixin extends Screen {
protected BookEditScreenMixin(Text title) {
super(title);
}
@Unique
private static final MinecraftClient mc = MinecraftClient.getInstance();

private TextFieldWidget addressField;
@Inject(at = @At("TAIL"), method = "init")
public void init(CallbackInfo ci) {
if (SharedVariables.enabled) {
MainClient.createWidgets(mc, this);

// create chat box
this.addressField = new TextFieldWidget(textRenderer, 5, 245, 160, 20, Text.of("Chat ...")) {
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (keyCode == GLFW.GLFW_KEY_ENTER) {
if (this.getText().equals("^toggleuiutils")) {
SharedVariables.enabled = !SharedVariables.enabled;
if (mc.player != null) {
mc.player.sendMessage(Text.of("UI-Utils is now " + (SharedVariables.enabled ? "enabled" : "disabled") + "."));
}
return false;
}

if (mc.getNetworkHandler() != null) {
if (this.getText().startsWith("/")) {
mc.getNetworkHandler().sendChatCommand(this.getText().replaceFirst(Pattern.quote("/"), ""));
} else {
mc.getNetworkHandler().sendChatMessage(this.getText());
}
} else {
MainClient.LOGGER.warn("Minecraft network handler (mc.getNetworkHandler()) was null while trying to send chat message from UI Utils.");
}

this.setText("");
}
return super.keyPressed(keyCode, scanCode, modifiers);
}
};
this.addressField.setText("");
this.addressField.setMaxLength(255);

this.addDrawableChild(this.addressField);
}
}
}
67 changes: 67 additions & 0 deletions src/main/java/org/uiutils/mixin/BookScreenMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.uiutils.mixin;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.BookScreen;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.uiutils.MainClient;
import org.uiutils.SharedVariables;

import java.util.regex.Pattern;

@Mixin(BookScreen.class)
public class BookScreenMixin extends Screen {
protected BookScreenMixin(Text title) {
super(title);
}
@Unique
private static final MinecraftClient mc = MinecraftClient.getInstance();

private TextFieldWidget addressField;
@Inject(at = @At("TAIL"), method = "init")
public void init(CallbackInfo ci) {
if (SharedVariables.enabled) {
MainClient.createWidgets(mc, this);

// create chat box
this.addressField = new TextFieldWidget(textRenderer, 5, 245, 160, 20, Text.of("Chat ...")) {
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (keyCode == GLFW.GLFW_KEY_ENTER) {
if (this.getText().equals("^toggleuiutils")) {
SharedVariables.enabled = !SharedVariables.enabled;
if (mc.player != null) {
mc.player.sendMessage(Text.of("UI-Utils is now " + (SharedVariables.enabled ? "enabled" : "disabled") + "."));
}
return false;
}

if (mc.getNetworkHandler() != null) {
if (this.getText().startsWith("/")) {
mc.getNetworkHandler().sendChatCommand(this.getText().replaceFirst(Pattern.quote("/"), ""));
} else {
mc.getNetworkHandler().sendChatMessage(this.getText());
}
} else {
MainClient.LOGGER.warn("Minecraft network handler (mc.getNetworkHandler()) was null while trying to send chat message from UI Utils.");
}

this.setText("");
}
return super.keyPressed(keyCode, scanCode, modifiers);
}
};
this.addressField.setText("");
this.addressField.setMaxLength(255);

this.addDrawableChild(this.addressField);
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/org/uiutils/mixin/ChatScreenMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.uiutils.mixin;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.text.Text;
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 org.uiutils.MainClient;
import org.uiutils.SharedVariables;

@Mixin(ChatScreen.class)
public class ChatScreenMixin {
@Inject(at = @At("HEAD"), method = "sendMessage", cancellable = true)
public void sendMessage(String chatText, boolean addToHistory, CallbackInfo ci) {
if (chatText.equals("^toggleuiutils")) {
SharedVariables.enabled = !SharedVariables.enabled;
if (MinecraftClient.getInstance().player != null) {
MinecraftClient.getInstance().player.sendMessage(Text.of("UI-Utils is now " + (SharedVariables.enabled ? "enabled" : "disabled") + "."));
} else {
MainClient.LOGGER.warn("Minecraft player was nulling while enabling / disabling UI Utils.");
}
MinecraftClient.getInstance().inGameHud.getChatHud().addToMessageHistory(chatText);
MinecraftClient.getInstance().setScreen(null);
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.uiutils.mixin;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientCommonNetworkHandler;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.common.ResourcePackStatusC2SPacket;
import net.minecraft.network.packet.s2c.common.ResourcePackSendS2CPacket;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.uiutils.MainClient;
import org.uiutils.SharedVariables;

@Mixin(ClientCommonNetworkHandler.class)
public abstract class ClientCommonNetworkHandlerMixin {
@Shadow
@Final
protected MinecraftClient client;

@Shadow
public abstract void sendPacket(Packet<?> packet);

@Inject(at = @At("HEAD"), method = "onResourcePackSend", cancellable = true)
public void onResourcePackSend(ResourcePackSendS2CPacket packet, CallbackInfo ci) {
if (SharedVariables.bypassResourcePack && (packet.required() || SharedVariables.resourcePackForceDeny)) {
this.sendPacket(new ResourcePackStatusC2SPacket(MinecraftClient.getInstance().getSession().getUuidOrNull(), ResourcePackStatusC2SPacket.Status.ACCEPTED));
this.sendPacket(new ResourcePackStatusC2SPacket(MinecraftClient.getInstance().getSession().getUuidOrNull(), ResourcePackStatusC2SPacket.Status.SUCCESSFULLY_LOADED));
MainClient.LOGGER.info(
"[UI Utils]: Required Resource Pack Bypassed, Message: " +
(packet.prompt().isEmpty() ? "<no message>" : packet.prompt().toString()) +
", URL: " + (packet.url() == null ? "<no url>" : packet.url())
);
ci.cancel();
}
}
}
Loading

0 comments on commit 03e68af

Please sign in to comment.