Skip to content

Commit

Permalink
Merge pull request #55 from MoSadie/trigger-command
Browse files Browse the repository at this point in the history
Add Trigger Command
  • Loading branch information
MoSadie authored Nov 12, 2024
2 parents ea13604 + cb686f6 commit ead3e01
Show file tree
Hide file tree
Showing 16 changed files with 495 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public EffectResult execute(EffectMCCore core, Map<String, Object> args) {
}

String message = getPropAsString(args, "message");

if (message.toLowerCase().startsWith("/effectmc ")) {
return new EffectResult("Cannot trigger EffectMC commands.", EffectResult.Result.ERROR);
}

core.getExecutor().log("Sending chat message: " + message);
if (core.getExecutor().sendChatMessage(message))
return new EffectResult("Sending chat message " + message, EffectResult.Result.SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.text2speech.Narrator;
import com.mosadie.effectmc.core.EffectExecutor;
Expand All @@ -14,6 +16,7 @@
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
Expand Down Expand Up @@ -110,7 +113,16 @@ public void onInitializeClient() {
LOGGER.info("Server start result: " + result);

// Register command
ClientCommandManager.DISPATCHER.register(ClientCommandManager.literal("effectmc")
ClientCommandManager.DISPATCHER.register(clientCommand());

Header authHeader = new BasicHeader("Authorization", "Bearer " + MinecraftClient.getInstance().getSession().getAccessToken());
List<Header> headers = new ArrayList<>();
headers.add(authHeader);
authedClient = HttpClientBuilder.create().setDefaultHeaders(headers).build();
}

private LiteralArgumentBuilder<FabricClientCommandSource> clientCommand() {
return ClientCommandManager.literal("effectmc")
.then(ClientCommandManager.literal("trust").executes((context -> {
MinecraftClient.getInstance().send(core::setTrustFlag);
receiveChatMessage("[EffectMC] Now prompting to trust the next request sent.");
Expand Down Expand Up @@ -159,15 +171,46 @@ public void onInitializeClient() {
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, exporteffect, trust");
return 0;
})));
}))).then(ClientCommandManager.literal("trigger").then(ClientCommandManager.argument("json", StringArgumentType.greedyString()).executes((context -> {
String json = StringArgumentType.getString(context, "json");
EffectRequest request = core.requestFromJson(json);

Header authHeader = new BasicHeader("Authorization", "Bearer " + MinecraftClient.getInstance().getSession().getAccessToken());
List<Header> headers = new ArrayList<>();
headers.add(authHeader);
authedClient = HttpClientBuilder.create().setDefaultHeaders(headers).build();
if (request == null) {
receiveChatMessage("[EffectMC] Invalid JSON for effect request!");
return 0;
}

String worldId = getWorldState() == WorldState.SINGLEPLAYER ? getSPWorldName() : getServerIP();

Device device = new Device(worldId, getWorldState() == WorldState.SINGLEPLAYER ? DeviceType.WORLD : DeviceType.SERVER);

Effect.EffectResult result = core.triggerEffect(device, request);
switch (result.result) {
case SUCCESS:
receiveChatMessage("[EffectMC] Effect \"" + request.getEffectId() + "\" triggered successfully: " + result.message);
break;
case ERROR:
receiveChatMessage("[EffectMC] Error triggering effect: " + result.message);
break;
case UNAUTHORIZED:
receiveChatMessage("[EffectMC] World/Server not trusted. Use /effectmc trust to trust the current world/server.");
break;
case UNKNOWN:
receiveChatMessage("[EffectMC] Unknown effect.");
break;
case SKIPPED:
receiveChatMessage("[EffectMC] Effect skipped: " + result.message);
break;
case UNSUPPORTED:
receiveChatMessage("[EffectMC] Effect unsupported: " + result.message);
break;
}

return 0;
})))).executes((context -> {
receiveChatMessage("[EffectMC] Available subcommands: exportbook, exportitem, exporteffect, trigger, trust");
return 0;
}));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.text2speech.Narrator;
import com.mosadie.effectmc.core.EffectExecutor;
import com.mosadie.effectmc.core.EffectMCCore;
import com.mosadie.effectmc.core.WorldState;
import com.mosadie.effectmc.core.effect.*;
import com.mosadie.effectmc.core.effect.internal.Effect;
import com.mosadie.effectmc.core.effect.internal.EffectRequest;
import com.mosadie.effectmc.core.handler.*;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.*;
Expand Down Expand Up @@ -107,7 +111,16 @@ public void onInitializeClient() {
LOGGER.info("Server start result: " + result);

// Register command
ClientCommandManager.DISPATCHER.register(ClientCommandManager.literal("effectmc")
ClientCommandManager.DISPATCHER.register(clientCommand());

Header authHeader = new BasicHeader("Authorization", "Bearer " + MinecraftClient.getInstance().getSession().getAccessToken());
List<Header> headers = new ArrayList<>();
headers.add(authHeader);
authedClient = HttpClientBuilder.create().setDefaultHeaders(headers).build();
}

private LiteralArgumentBuilder<FabricClientCommandSource> clientCommand() {
return ClientCommandManager.literal("effectmc")
.then(ClientCommandManager.literal("trust").executes((context -> {
MinecraftClient.getInstance().send(core::setTrustFlag);
receiveChatMessage("[EffectMC] Now prompting to trust the next request sent.");
Expand Down Expand Up @@ -156,15 +169,36 @@ public void onInitializeClient() {
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, exporteffect, trust");
return 0;
})));
}))).then(ClientCommandManager.literal("trigger").then(ClientCommandManager.argument("json", StringArgumentType.greedyString()).executes((context -> {
String json = StringArgumentType.getString(context, "json");
EffectRequest request = core.requestFromJson(json);

Header authHeader = new BasicHeader("Authorization", "Bearer " + MinecraftClient.getInstance().getSession().getAccessToken());
List<Header> headers = new ArrayList<>();
headers.add(authHeader);
authedClient = HttpClientBuilder.create().setDefaultHeaders(headers).build();
if (request == null) {
receiveChatMessage("[EffectMC] Invalid JSON for effect request!");
return 0;
}

String worldId = getWorldState() == WorldState.SINGLEPLAYER ? getSPWorldName() : getServerIP();

Device device = new Device(worldId, getWorldState() == WorldState.SINGLEPLAYER ? DeviceType.WORLD : DeviceType.SERVER);

Effect.EffectResult result = core.triggerEffect(device, request);
switch (result.result) {
case SUCCESS ->
receiveChatMessage("[EffectMC] Effect \"" + request.getEffectId() + "\" triggered successfully: " + result.message);
case ERROR -> receiveChatMessage("[EffectMC] Error triggering effect: " + result.message);
case UNAUTHORIZED ->
receiveChatMessage("[EffectMC] World/Server not trusted. Use /effectmc trust to trust the current world/server.");
case UNKNOWN -> receiveChatMessage("[EffectMC] Unknown effect.");
case SKIPPED -> receiveChatMessage("[EffectMC] Effect skipped: " + result.message);
case UNSUPPORTED -> receiveChatMessage("[EffectMC] Effect unsupported: " + result.message);
}

return 0;
})))).executes((context -> {
receiveChatMessage("[EffectMC] Available subcommands: exportbook, exportitem, exporteffect, trigger, trust");
return 0;
}));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.text2speech.Narrator;
import com.mosadie.effectmc.core.EffectExecutor;
import com.mosadie.effectmc.core.EffectMCCore;
import com.mosadie.effectmc.core.WorldState;
import com.mosadie.effectmc.core.effect.*;
import com.mosadie.effectmc.core.effect.internal.Effect;
import com.mosadie.effectmc.core.effect.internal.EffectRequest;
import com.mosadie.effectmc.core.handler.*;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.*;
Expand Down Expand Up @@ -107,7 +111,16 @@ public void onInitializeClient() {
LOGGER.info("Server start result: " + result);

// Register command
ClientCommandManager.DISPATCHER.register(ClientCommandManager.literal("effectmc")
ClientCommandManager.DISPATCHER.register(clientCommand());

Header authHeader = new BasicHeader("Authorization", "Bearer " + MinecraftClient.getInstance().getSession().getAccessToken());
List<Header> headers = new ArrayList<>();
headers.add(authHeader);
authedClient = HttpClientBuilder.create().setDefaultHeaders(headers).build();
}

private LiteralArgumentBuilder<FabricClientCommandSource> clientCommand() {
return ClientCommandManager.literal("effectmc")
.then(ClientCommandManager.literal("trust").executes((context -> {
MinecraftClient.getInstance().send(core::setTrustFlag);
receiveChatMessage("[EffectMC] Now prompting to trust the next request sent.");
Expand Down Expand Up @@ -156,15 +169,36 @@ public void onInitializeClient() {
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, exporteffect, trust");
return 0;
})));
}))).then(ClientCommandManager.literal("trigger").then(ClientCommandManager.argument("json", StringArgumentType.greedyString()).executes((context -> {
String json = StringArgumentType.getString(context, "json");
EffectRequest request = core.requestFromJson(json);

Header authHeader = new BasicHeader("Authorization", "Bearer " + MinecraftClient.getInstance().getSession().getAccessToken());
List<Header> headers = new ArrayList<>();
headers.add(authHeader);
authedClient = HttpClientBuilder.create().setDefaultHeaders(headers).build();
if (request == null) {
receiveChatMessage("[EffectMC] Invalid JSON for effect request!");
return 0;
}

String worldId = getWorldState() == WorldState.SINGLEPLAYER ? getSPWorldName() : getServerIP();

Device device = new Device(worldId, getWorldState() == WorldState.SINGLEPLAYER ? DeviceType.WORLD : DeviceType.SERVER);

Effect.EffectResult result = core.triggerEffect(device, request);
switch (result.result) {
case SUCCESS ->
receiveChatMessage("[EffectMC] Effect \"" + request.getEffectId() + "\" triggered successfully: " + result.message);
case ERROR -> receiveChatMessage("[EffectMC] Error triggering effect: " + result.message);
case UNAUTHORIZED ->
receiveChatMessage("[EffectMC] World/Server not trusted. Use /effectmc trust to trust the current world/server.");
case UNKNOWN -> receiveChatMessage("[EffectMC] Unknown effect.");
case SKIPPED -> receiveChatMessage("[EffectMC] Effect skipped: " + result.message);
case UNSUPPORTED -> receiveChatMessage("[EffectMC] Effect unsupported: " + result.message);
}

return 0;
})))).executes((context -> {
receiveChatMessage("[EffectMC] Available subcommands: exportbook, exportitem, exporteffect, trigger, trust");
return 0;
}));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import java.util.ArrayList;
import java.util.List;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mosadie.effectmc.core.WorldState;
import com.mosadie.effectmc.core.effect.*;
import com.mosadie.effectmc.core.effect.internal.Effect;
import com.mosadie.effectmc.core.effect.internal.EffectRequest;
import com.mosadie.effectmc.core.handler.Device;
import com.mosadie.effectmc.core.handler.DeviceType;
Expand Down Expand Up @@ -183,8 +185,32 @@ private void registerClientCommand(CommandDispatcher<FabricClientCommandSource>
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, exporteffect trust");
}))).then(ClientCommandManager.literal("trigger").then(ClientCommandManager.argument("json", StringArgumentType.greedyString()).executes((context -> {
String json = StringArgumentType.getString(context, "json");
EffectRequest request = core.requestFromJson(json);

if (request == null) {
receiveChatMessage("[EffectMC] Invalid JSON for effect request!");
return 0;
}

String worldId = getWorldState() == WorldState.SINGLEPLAYER ? getSPWorldName() : getServerIP();

Device device = new Device(worldId, getWorldState() == WorldState.SINGLEPLAYER ? DeviceType.WORLD : DeviceType.SERVER);

Effect.EffectResult result = core.triggerEffect(device, request);
switch (result.result) {
case SUCCESS -> receiveChatMessage("[EffectMC] Effect \"" + request.getEffectId() + "\" triggered successfully: " + result.message);
case ERROR -> receiveChatMessage("[EffectMC] Error triggering effect: " + result.message);
case UNAUTHORIZED -> receiveChatMessage("[EffectMC] World/Server not trusted. Use /effectmc trust to trust the current world/server.");
case UNKNOWN -> receiveChatMessage("[EffectMC] Unknown effect.");
case SKIPPED -> receiveChatMessage("[EffectMC] Effect skipped: " + result.message);
case UNSUPPORTED -> receiveChatMessage("[EffectMC] Effect unsupported: " + result.message);
}

return 0;
})))).executes((context -> {
receiveChatMessage("[EffectMC] Available subcommands: exportbook, exportitem, exporteffect, trigger, trust");
return 0;
})));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import java.util.ArrayList;
import java.util.List;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mosadie.effectmc.core.WorldState;
import com.mosadie.effectmc.core.effect.*;
import com.mosadie.effectmc.core.effect.internal.Effect;
import com.mosadie.effectmc.core.effect.internal.EffectRequest;
import com.mosadie.effectmc.core.handler.Device;
import com.mosadie.effectmc.core.handler.DeviceType;
Expand Down Expand Up @@ -183,8 +185,32 @@ private void registerClientCommand(CommandDispatcher<FabricClientCommandSource>
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, exporteffect, trust");
}))).then(ClientCommandManager.literal("trigger").then(ClientCommandManager.argument("json", StringArgumentType.greedyString()).executes((context -> {
String json = StringArgumentType.getString(context, "json");
EffectRequest request = core.requestFromJson(json);

if (request == null) {
receiveChatMessage("[EffectMC] Invalid JSON for effect request!");
return 0;
}

String worldId = getWorldState() == WorldState.SINGLEPLAYER ? getSPWorldName() : getServerIP();

Device device = new Device(worldId, getWorldState() == WorldState.SINGLEPLAYER ? DeviceType.WORLD : DeviceType.SERVER);

Effect.EffectResult result = core.triggerEffect(device, request);
switch (result.result) {
case SUCCESS -> receiveChatMessage("[EffectMC] Effect \"" + request.getEffectId() + "\" triggered successfully: " + result.message);
case ERROR -> receiveChatMessage("[EffectMC] Error triggering effect: " + result.message);
case UNAUTHORIZED -> receiveChatMessage("[EffectMC] World/Server not trusted. Use /effectmc trust to trust the current world/server.");
case UNKNOWN -> receiveChatMessage("[EffectMC] Unknown effect.");
case SKIPPED -> receiveChatMessage("[EffectMC] Effect skipped: " + result.message);
case UNSUPPORTED -> receiveChatMessage("[EffectMC] Effect unsupported: " + result.message);
}

return 0;
})))).executes((context -> {
receiveChatMessage("[EffectMC] Available subcommands: exportbook, exportitem, exporteffect, trigger, trust");
return 0;
})));
}
Expand Down
Loading

0 comments on commit ead3e01

Please sign in to comment.