From 68034506b22bc62d6c5884f02618965bc5e3c06b Mon Sep 17 00:00:00 2001 From: MoSadie Date: Sun, 4 Aug 2024 17:59:51 -0700 Subject: [PATCH] Initial tests are working! Still need to fully test, but was able to export an effect as a tellraw command and have it trigger in-game! Now to update all the other Minecraft versions... --- .../mosadie/effectmc/core/EffectMCCore.java | 21 ++++++- .../effectmc/core/effect/SetSkinEffect.java | 2 +- .../effectmc/core/handler/EffectHandler.java | 16 ++++- .../handler/http/EffectRequestHandler.java | 2 + .../java/com/mosadie/effectmc/EffectMC.java | 58 ++++++++++++------- 5 files changed, 73 insertions(+), 26 deletions(-) diff --git a/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/EffectMCCore.java b/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/EffectMCCore.java index 1aaeca3..c414c01 100644 --- a/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/EffectMCCore.java +++ b/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/EffectMCCore.java @@ -12,11 +12,9 @@ import com.mosadie.effectmc.core.handler.EffectHandler; import com.mosadie.effectmc.core.handler.TrustHandler; import com.mosadie.effectmc.core.handler.http.*; -import com.sun.net.httpserver.HttpServer; import it.unimi.dsi.fastutil.booleans.BooleanConsumer; import java.io.*; -import java.net.InetSocketAddress; import java.net.URISyntaxException; import java.util.*; @@ -24,6 +22,8 @@ public class EffectMCCore { private final static int DEFAULT_PORT = 3000; + public static final String TRANSLATION_TRIGGER_KEY = "com.mosadie.effectmc.trigger"; + private final File configFile; private final File trustFile; private final EffectExecutor executor; @@ -98,7 +98,7 @@ public boolean initServer() throws URISyntaxException { } } catch (FileNotFoundException | NumberFormatException e) { e.printStackTrace(); - + return false; } } else { try { @@ -110,10 +110,12 @@ public boolean initServer() throws URISyntaxException { writer.close(); } catch (IOException e) { e.printStackTrace(); + return false; } } if (!trustHandler.readTrustFile()) { + getExecutor().log("Failed to read trust file."); return false; } @@ -122,12 +124,25 @@ public boolean initServer() throws URISyntaxException { httpServer = new EffectHttpServer(port, this); if (!httpServer.initServer()) { + getExecutor().log("Failed to start http server."); return false; } return true; } + public EffectRequest requestFromJson(String json) { + try { + return gson.fromJson(json, new TypeToken() {}.getType()); + } catch (JsonSyntaxException e) { + executor.log("Invalid Request JSON! " + e.getMessage()); + return null; + } catch (Exception e) { + executor.log("Exception parsin Request JSON: " + e.getMessage()); + return null; + } + } + public JsonObject fromJson(String json) { try { return gson.fromJson(json, JsonObject.class); diff --git a/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/effect/SetSkinEffect.java b/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/effect/SetSkinEffect.java index 5d118e7..efdfd72 100644 --- a/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/effect/SetSkinEffect.java +++ b/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/effect/SetSkinEffect.java @@ -14,7 +14,7 @@ public class SetSkinEffect extends Effect { public SetSkinEffect() { super(); getPropertyManager().addStringProperty("url", "", true, "Skin URL", ""); - getPropertyManager().addSelectionProperty("skinType", SKIN_TYPE.CLASSIC.getValue(), true, "Skin Type", SKIN_TYPE.toStringArray()); + getPropertyManager().addSelectionProperty("skinType", SKIN_TYPE.CLASSIC.name(), true, "Skin Type", SKIN_TYPE.toStringArray()); getPropertyManager().addCommentProperty("NOTE: This does not refresh your skin in-game, rejoin the server to refresh your skin."); getPropertyManager().lock(); } diff --git a/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/handler/EffectHandler.java b/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/handler/EffectHandler.java index d0fcf24..ec19a85 100644 --- a/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/handler/EffectHandler.java +++ b/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/handler/EffectHandler.java @@ -63,10 +63,24 @@ public Effect.EffectResult handleRequest(Device device, EffectRequest request) { // If there is an export flag set, export the effect request as a json string in the log if (exportFlag) { exportFlag = false; - core.getExecutor().log("Exported Effect Request: " + core.toJson(request)); + String json = core.toJson(request); + core.getExecutor().log("Exported Effect Request JSON: " + json); + core.getExecutor().log("/TellRaw Command for Exported Effect: /tellraw @p " + core.toJson(new EffectTranslatableComponent(json))); } // Execute the effect return effect.execute(core, request.getArgs()); } + + public static class EffectTranslatableComponent { + public final String type = "translatable"; + public final String translate = EffectMCCore.TRANSLATION_TRIGGER_KEY; + public final String fallback = ""; + + public final String[] with; + + public EffectTranslatableComponent(String requestJson) { + with = new String[] { requestJson }; + } + } } diff --git a/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/handler/http/EffectRequestHandler.java b/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/handler/http/EffectRequestHandler.java index 55a4481..c383c55 100644 --- a/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/handler/http/EffectRequestHandler.java +++ b/MinecraftMod/core/src/main/java/com/mosadie/effectmc/core/handler/http/EffectRequestHandler.java @@ -84,6 +84,8 @@ public void handle(HttpExchange exchange) throws IOException { Device device = new Device(isBody ? bodyParameters.get("device").toString() : parameters.get("device").toString(), DeviceType.OTHER); + request.getArgs().remove("device"); + // Execute effect Effect.EffectResult response = core.triggerEffect(device, request); diff --git a/MinecraftMod/forge/1.20.4/src/main/java/com/mosadie/effectmc/EffectMC.java b/MinecraftMod/forge/1.20.4/src/main/java/com/mosadie/effectmc/EffectMC.java index 654a4b9..cb4e420 100644 --- a/MinecraftMod/forge/1.20.4/src/main/java/com/mosadie/effectmc/EffectMC.java +++ b/MinecraftMod/forge/1.20.4/src/main/java/com/mosadie/effectmc/EffectMC.java @@ -4,11 +4,13 @@ import com.google.gson.JsonPrimitive; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.text2speech.Narrator; -import com.mosadie.effectmc.core.DeviceType; import com.mosadie.effectmc.core.EffectExecutor; import com.mosadie.effectmc.core.EffectMCCore; import com.mosadie.effectmc.core.WorldState; -import com.mosadie.effectmc.core.handler.*; +import com.mosadie.effectmc.core.effect.*; +import com.mosadie.effectmc.core.effect.internal.EffectRequest; +import com.mosadie.effectmc.core.handler.Device; +import com.mosadie.effectmc.core.handler.DeviceType; import net.minecraft.client.CameraType; import net.minecraft.client.Minecraft; import net.minecraft.client.Options; @@ -117,7 +119,7 @@ public void registerClientCommand(RegisterClientCommandsEvent event) { LOGGER.info("Registering effectmc command."); event.getDispatcher().register(Commands.literal("effectmc") .then(Commands.literal("trust").executes((context -> { - Minecraft.getInstance().execute(core::setTrustNextRequest); + Minecraft.getInstance().execute(core::setTrustFlag); receiveChatMessage("[EffectMC] Now prompting to trust the next request sent."); return 0; }))) @@ -160,30 +162,44 @@ public void registerClientCommand(RegisterClientCommandsEvent event) { showItemToast(NbtUtils.prettyPrint(tag), "Exported", Minecraft.getInstance().player.getMainHandItem().getDisplayName().getString()); receiveChatMessage("[EffectMC] Exported held item data to log file!"); return 0; - }))).executes((context -> { + }))).then(Commands.literal("exporteffect").executes(context -> { + core.setExportFlag(); + receiveChatMessage("[EffectMC] Will export the next triggered effect as JSON to the current log file."); + return 0; + })).executes((context -> { receiveChatMessage("[EffectMC] Available subcommands: exportbook, exportitem, trust"); return 0; }))); LOGGER.info("Registered effectmc command."); } - private static final String translationPrefix = "com.mosadie.effectmc.trigger."; + private void listenForTranslation(ClientChatReceivedEvent event) { Component component = event.getMessage(); ComponentContents contents = component.getContents(); - if (contents instanceof TranslatableContents translatableContents && translatableContents.getKey().startsWith(translationPrefix)) { + if (contents instanceof TranslatableContents translatableContents && translatableContents.getKey().equals(EffectMCCore.TRANSLATION_TRIGGER_KEY)) { event.setCanceled(true); - String slug = translatableContents.getKey().substring(translationPrefix.length()); - String worldId = getWorldState() == WorldState.SINGLEPLAYER ? getSPWorldName() : getServerIP(); - List args = new ArrayList<>(); - for (Object arg : translatableContents.getArgs()) { - args.add(arg.toString()); + if (translatableContents.getArgs().length != 1) { + log("Invalid length of args for translation trigger!"); + return; } - core.executeFromChatMessage(slug, worldId, args); + String data = String.valueOf(translatableContents.getArgs()[0]); + + EffectRequest request = core.requestFromJson(data); + + if (request == null) { + log("Invalid request json for translation trigger!"); + return; + } + + String worldId = getWorldState() == WorldState.SINGLEPLAYER ? getSPWorldName() : getServerIP(); + + Device device = new Device(worldId, getWorldState() == WorldState.SINGLEPLAYER ? DeviceType.WORLD : DeviceType.SERVER); + core.triggerEffect(device, request); } } @@ -220,7 +236,7 @@ public boolean joinServer(String serverIp) { } @Override - public boolean setSkinLayer(SkinLayerHandler.SKIN_SECTION section, boolean visibility) { + public boolean setSkinLayer(SkinLayerEffect.SKIN_SECTION section, boolean visibility) { Options options = Minecraft.getInstance().options; switch (section) { @@ -263,7 +279,7 @@ public boolean setSkinLayer(SkinLayerHandler.SKIN_SECTION section, boolean visib } @Override - public boolean toggleSkinLayer(SkinLayerHandler.SKIN_SECTION section) { + public boolean toggleSkinLayer(SkinLayerEffect.SKIN_SECTION section) { Options options = Minecraft.getInstance().options; switch (section) { @@ -352,7 +368,7 @@ public boolean showActionMessage(String message) { } @Override - public boolean triggerDisconnect(DisconnectHandler.NEXT_SCREEN nextScreenType, String title, String message) { + public boolean triggerDisconnect(DisconnectEffect.NEXT_SCREEN nextScreenType, String title, String message) { Minecraft.getInstance().execute(() -> { leaveIfNeeded(); @@ -416,9 +432,9 @@ public boolean playSound(String soundID, String categoryName, float volume, floa } @Override - public void showTrustPrompt(String device, DeviceType type) { + public void showTrustPrompt(Device device) { Minecraft.getInstance().execute(() -> { - ConfirmScreen screen = new ConfirmScreen(new EffectMCCore.TrustBooleanConsumer(device, type, core), Component.literal("EffectMC - Trust Prompt"), Component.literal("Do you want to trust this device?\n(Type: " + type + (type == DeviceType.OTHER ? " Device Id:" + device : "") + ")")); + ConfirmScreen screen = new ConfirmScreen(new EffectMCCore.TrustBooleanConsumer(device.getId(), device.getType(), core), Component.literal("EffectMC - Trust Prompt"), Component.literal("Do you want to trust this device?\n(Type: " + device.getType() + (device.getType() == DeviceType.OTHER ? " Device Id:" + device.getId() : "") + ")")); Minecraft.getInstance().setScreen(screen); }); } @@ -519,7 +535,7 @@ public boolean loadWorld(String worldName) { } @Override - public boolean setSkin(URL skinUrl, SetSkinHandler.SKIN_TYPE skinType) { + public boolean setSkin(URL skinUrl, SetSkinEffect.SKIN_TYPE skinType) { if (skinUrl == null) { LOGGER.warn("Skin URL is null!"); return false; @@ -565,7 +581,7 @@ public void leaveIfNeeded() { } @Override - public boolean openScreen(OpenScreenHandler.SCREEN screen) { + public boolean openScreen(OpenScreenEffect.SCREEN screen) { Minecraft.getInstance().execute(() -> { leaveIfNeeded(); @@ -599,7 +615,7 @@ public boolean setFOV(int fov) { } @Override - public boolean setPOV(SetPovHandler.POV pov) { + public boolean setPOV(SetPovEffect.POV pov) { CameraType mcPov; switch (pov) { @@ -645,7 +661,7 @@ public boolean setGamma(double gamma) { } @Override - public boolean setChatVisibility(ChatVisibilityHandler.VISIBILITY visibility) { + public boolean setChatVisibility(ChatVisibilityEffect.VISIBILITY visibility) { ChatVisiblity result; switch (visibility) { case SHOW: