Skip to content

Commit

Permalink
Initial tests are working!
Browse files Browse the repository at this point in the history
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...
  • Loading branch information
MoSadie committed Aug 5, 2024
1 parent 3ac00b7 commit 6803450
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
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.*;

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;
Expand Down Expand Up @@ -98,7 +98,7 @@ public boolean initServer() throws URISyntaxException {
}
} catch (FileNotFoundException | NumberFormatException e) {
e.printStackTrace();

return false;
}
} else {
try {
Expand All @@ -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;
}

Expand All @@ -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<EffectRequest>() {}.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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -565,7 +581,7 @@ public void leaveIfNeeded() {
}

@Override
public boolean openScreen(OpenScreenHandler.SCREEN screen) {
public boolean openScreen(OpenScreenEffect.SCREEN screen) {
Minecraft.getInstance().execute(() -> {
leaveIfNeeded();

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 6803450

Please sign in to comment.