From fb7a320d02b12c69da57d72c2852110ebfa88989 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Sun, 2 Apr 2023 00:40:39 +0200 Subject: [PATCH] PlexToolUi start Signed-off-by: PlexSheep --- .../java/de/cscherr/plextool/PlexMenu.java | 6 -- .../java/de/cscherr/plextool/PlexTool.java | 6 -- .../de/cscherr/plextool/PlexToolUiScreen.java | 43 ++++++++ .../cscherr/plextool/mixin/PlexToolMixin.java | 37 ------- .../plextool/mixin/PlexToolPackets.java | 97 +++++++++++++++++++ .../plextool/mixin/PlexToolUIMixin.java | 33 +++++++ src/main/resources/plextool.mixins.json | 3 +- 7 files changed, 175 insertions(+), 50 deletions(-) delete mode 100644 src/main/java/de/cscherr/plextool/PlexMenu.java create mode 100644 src/main/java/de/cscherr/plextool/PlexToolUiScreen.java delete mode 100644 src/main/java/de/cscherr/plextool/mixin/PlexToolMixin.java create mode 100644 src/main/java/de/cscherr/plextool/mixin/PlexToolPackets.java create mode 100644 src/main/java/de/cscherr/plextool/mixin/PlexToolUIMixin.java diff --git a/src/main/java/de/cscherr/plextool/PlexMenu.java b/src/main/java/de/cscherr/plextool/PlexMenu.java deleted file mode 100644 index fc3d4e0..0000000 --- a/src/main/java/de/cscherr/plextool/PlexMenu.java +++ /dev/null @@ -1,6 +0,0 @@ -package de.cscherr.plextool; -public class PlexMenu { - - public PlexMenu() { - } -} \ No newline at end of file diff --git a/src/main/java/de/cscherr/plextool/PlexTool.java b/src/main/java/de/cscherr/plextool/PlexTool.java index fb8a5cb..46b44f3 100644 --- a/src/main/java/de/cscherr/plextool/PlexTool.java +++ b/src/main/java/de/cscherr/plextool/PlexTool.java @@ -4,9 +4,6 @@ import net.fabricmc.api.ModInitializer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import de.cscherr.plextool.PlexMenu; -import de.cscherr.plextool.mixin.PlexToolMixin; - public class PlexTool implements ModInitializer { // This logger is used to write text to the console and the log file. // It is considered best practice to use your mod id as the logger's name. @@ -15,8 +12,6 @@ public class PlexTool implements ModInitializer { public static final String VERSION_NUMBER = "0.1.0"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); - public PlexMenu menu; - @Override public void onInitialize() { // This code runs as soon as Minecraft is in a mod-load-ready state. @@ -24,6 +19,5 @@ public class PlexTool implements ModInitializer { // Proceed with mild caution. LOGGER.info("Initializing PlexTool"); - menu = new PlexMenu(); } } \ No newline at end of file diff --git a/src/main/java/de/cscherr/plextool/PlexToolUiScreen.java b/src/main/java/de/cscherr/plextool/PlexToolUiScreen.java new file mode 100644 index 0000000..311eef6 --- /dev/null +++ b/src/main/java/de/cscherr/plextool/PlexToolUiScreen.java @@ -0,0 +1,43 @@ +package de.cscherr.plextool; + +import de.cscherr.plextool.mixin.PlexToolUIMixin; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.util.Monitor; +import net.minecraft.client.util.Window; +import net.minecraft.screen.ScreenTexts; +import net.minecraft.text.Text; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PlexToolUiScreen extends Screen { + + private static final Logger LOGGER = LoggerFactory.getLogger(PlexToolUiScreen.class.getSimpleName()); + private static final Text INFO_TEXT = Text.literal("Hello world"); + private final Screen parent; + + public PlexToolUiScreen(String name, Screen parent) { + super(Text.literal(name)); + this.parent = parent; + LOGGER.info("Constructed the UI"); + } + @Override + protected void init() { + //assert this.client != null; + //Window window = this.client.getWindow(); + //Monitor monitor = window.getMonitor(); + + this.addDrawable(ButtonWidget.builder(INFO_TEXT, button -> { + LOGGER.info("Pressed dummy button"); + }).dimensions(this.width / 2 - 100, this.height - 10, 200, 20).build()); + //this.addDrawableChild(ButtonWidget.builder(ScreenTexts.DONE, button -> { + // this.client.options.write(); + // this.apply(); + // this.client.setScreen(this.parent); + //}).dimensions(this.width / 2 - 100, this.height - 27, 200, 20).build()); + } + + private void apply() { + + } +} diff --git a/src/main/java/de/cscherr/plextool/mixin/PlexToolMixin.java b/src/main/java/de/cscherr/plextool/mixin/PlexToolMixin.java deleted file mode 100644 index 002f0e9..0000000 --- a/src/main/java/de/cscherr/plextool/mixin/PlexToolMixin.java +++ /dev/null @@ -1,37 +0,0 @@ -package de.cscherr.plextool.mixin; - -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -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.slf4j.Logger; - -import java.time.Duration; -import de.cscherr.plextool.PlexTool; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayNetworkHandler; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.listener.ServerPlayPacketListener; -import net.minecraft.network.packet.Packet; -import java.util.function.BooleanSupplier; -import net.minecraft.util.Identifier; - -@Mixin(ClientPlayNetworkHandler.class) -public class PlexToolMixin { - // 'V' is appended because the return type is void - // see https://fabricmc.net/wiki/tutorial:mixin_injects - public final Logger PLEX_LOGGER = PlexTool.LOGGER; - public boolean log_packets = true; - @Inject(at = @At("HEAD"), method = "sendPacket()V") - // Method signature is diffrent, but thats what the mixin library wants according to its errors. - // I could not find this in the documentation. - private void sendPacket(Packet packet, CallbackInfo callInfo) { - // this works! - // I am now logging every packet i send! Yay me! - if log_packets { - this.PLEX_LOGGER.info("Sending Packet: " + packet.toString()); - } - } - -} \ No newline at end of file diff --git a/src/main/java/de/cscherr/plextool/mixin/PlexToolPackets.java b/src/main/java/de/cscherr/plextool/mixin/PlexToolPackets.java new file mode 100644 index 0000000..ad8eb49 --- /dev/null +++ b/src/main/java/de/cscherr/plextool/mixin/PlexToolPackets.java @@ -0,0 +1,97 @@ +package de.cscherr.plextool.mixin; + +import net.minecraft.network.ClientConnection; +import net.minecraft.network.PacketCallbacks; +import net.minecraft.network.listener.PacketListener; +import net.minecraft.network.packet.Packet; +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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; + +/* +@Mixin(ClientPlayNetworkHandler.class) +public class PlexToolMixin { + // 'V' is appended because the return type is void + // see https://fabricmc.net/wiki/tutorial:mixin_injects + public final Logger PLEX_LOGGER = PlexTool.LOGGER; + public boolean log_packets = true; + @Inject(at = @At("HEAD"), method = "sendPacket()V") + // Method signature is diffrent, but thats what the mixin library wants according to its errors. + // I could not find this in the documentation. + private void sendPacket(Packet packet, CallbackInfo callInfo) { + // this works! + // I am now logging every packet i send! Yay me! + if (log_packets) { + this.PLEX_LOGGER.info( + "Sending Packet: " + ); + this.PLEX_LOGGER.debug("Callbackinfo for package dump: {}", callInfo); + } + } + +} +*/ +@Mixin(ClientConnection.class) +public class PlexToolPackets { + + private static final Logger LOGGER = LoggerFactory.getLogger("PlexToolMixin"); + private static boolean logSent = true; + private static boolean logReceived = false; + + public void setLogSent(boolean logSent) { + this.logSent = logSent; + } + + public void setLogReceived(boolean logReceived) { + this.logReceived = logReceived; + } + + public boolean isLogSent() { + return logSent; + } + + public boolean isLogReceived() { + return logReceived; + } + + /** + * Inject to the method that sends packets. Must not be static for some reason + * + * @param packet the Network packet to be sent + * @param callbacks magical fabric mixin stuff + * @param ci magical fabric mixin stuff + */ + @Inject(method = "sendImmediately", at = @At("HEAD")) + private void logSentPacket(Packet packet, @Nullable PacketCallbacks callbacks, CallbackInfo ci) { + if (logSent) { + LOGGER.info(String.format("Sent Package: %s", + packet.getClass().getSimpleName() + )); + } + } + + /** + * Inject to the method that receives and handles packets. + * Must be static for some reason + * + * @param packet the Network packet to be sent + * @param listener i dont know what this does + * @param ci magical fabric mixin stuff + */ + @Inject(method = "handlePacket", at = @At("HEAD")) + private static void logReceivedPacket(Packet packet, PacketListener listener, CallbackInfo ci) { + if (logReceived) { + LOGGER.info(String.format("Received Package: %s", + packet.getClass().getSimpleName() + )); + } + } +} \ No newline at end of file diff --git a/src/main/java/de/cscherr/plextool/mixin/PlexToolUIMixin.java b/src/main/java/de/cscherr/plextool/mixin/PlexToolUIMixin.java new file mode 100644 index 0000000..b07db8c --- /dev/null +++ b/src/main/java/de/cscherr/plextool/mixin/PlexToolUIMixin.java @@ -0,0 +1,33 @@ +package de.cscherr.plextool.mixin; + +import de.cscherr.plextool.PlexToolUiScreen; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.screen.option.OptionsScreen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.util.Monitor; +import net.minecraft.client.util.Window; +import net.minecraft.text.Text; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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; + +@Mixin(OptionsScreen.class) +public abstract class PlexToolUIMixin extends Screen { + private static final Text PLEXTOOL_TEXT = Text.literal("PlexTool"); + private static final Logger LOGGER = LoggerFactory.getLogger(PlexToolUIMixin.class.getSimpleName()); + + protected PlexToolUIMixin(Text title) { + super(title); + } + + @Inject(method = "init", at = @At("RETURN")) + private void init(CallbackInfo callback) { + addDrawable(ButtonWidget.builder(Text.literal("PT"), button -> { + assert client != null; + client.setScreen(new PlexToolUiScreen(PLEXTOOL_TEXT.getString(), (OptionsScreen)(Object)this)); + }).position(this.width / 2 - 180, this.height / 6 + 120 - 6).size(20, 20).build()); + } +} diff --git a/src/main/resources/plextool.mixins.json b/src/main/resources/plextool.mixins.json index 7463761..5c2139c 100644 --- a/src/main/resources/plextool.mixins.json +++ b/src/main/resources/plextool.mixins.json @@ -6,7 +6,8 @@ "mixins": [ ], "client": [ - "PlexToolMixin" + "PlexToolPackets", + "PlexToolUIMixin" ], "injectors": { "defaultRequire": 1