Skip to content

Commit

Permalink
Merge pull request #275 from ariesninjadev/master
Browse files Browse the repository at this point in the history
feat: Added remote "SendCommand" command
  • Loading branch information
onixiya1337 authored Jan 23, 2025
2 parents 5db1874 + 8ca8d5a commit c684980
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public DiscordBotHandler() {
new Disconnect(),
new Screenshot(),
new SetSpeed(),
new SendCommand(),
new Info(),
new AutoSell()));
LogUtils.sendDebug("Registered " + commands.size() + " commands");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,19 @@ public class WebsocketHandler {
private int reconnectAttempts = 0;
public WebsocketHandler() {
SetSpeedCommand speedCommand = new SetSpeedCommand();
SendCommandCommand sendCommandCommand = new SendCommandCommand();
commands.addAll(Arrays.asList(
new InfoCommand(),
new ReconnectCommand(),
new ScreenshotCommand(),
speedCommand,
sendCommandCommand,
new ToggleCommand(),
new DisconnectCommand(),
new AutoSellCommand()
));
MinecraftForge.EVENT_BUS.register(speedCommand);
MinecraftForge.EVENT_BUS.register(sendCommandCommand);
LogUtils.sendDebug("[Remote Control] Registered " + commands.size() + " commands.");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.jelly.farmhelperv2.remote.command.commands.impl;

import com.google.gson.JsonObject;
import com.jelly.farmhelperv2.handler.MacroHandler;
import com.jelly.farmhelperv2.remote.command.commands.ClientCommand;
import com.jelly.farmhelperv2.remote.command.commands.Command;
import com.jelly.farmhelperv2.remote.struct.RemoteMessage;
import com.jelly.farmhelperv2.util.InventoryUtils;
import com.jelly.farmhelperv2.util.LogUtils;
import com.jelly.farmhelperv2.util.PlayerUtils;
import com.jelly.farmhelperv2.util.helper.Clock;
import com.jelly.farmhelperv2.util.helper.SignUtils;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;

@Command(label = "sendcommand")
public class SendCommandCommand extends ClientCommand {
private static final Clock clock = new Clock();
private static String cmd = "";
private static JsonObject data;
private static boolean enabled = false;
private static State currentState = State.NONE;
boolean wasMacroing = false;

@Override
public void execute(RemoteMessage message) {
JsonObject args = message.args;
cmd = args.get("command").getAsString();
data = new JsonObject();
data.addProperty("username", mc.getSession().getUsername());
data.addProperty("uuid", mc.getSession().getPlayerID());
data.addProperty("command", cmd);

try {
enabled = true;
currentState = State.START;
return;
} catch (Exception e) {
e.printStackTrace();
}


RemoteMessage response = new RemoteMessage(label, data);
send(response);
}

@SubscribeEvent
public void onTick(TickEvent.ClientTickEvent event) {
if (!enabled) return;
if (ClientCommand.mc.thePlayer == null || ClientCommand.mc.theWorld == null) return;
if (clock.isScheduled() && !clock.passed()) return;

switch (currentState) {
case NONE:
clock.reset();
enabled = false;
break;
case START:
wasMacroing = MacroHandler.getInstance().isMacroToggled();
if (wasMacroing) {
MacroHandler.getInstance().pauseMacro();
}
currentState = State.TYPE_COMMAND;
clock.schedule(200);
break;
case TYPE_COMMAND:
mc.thePlayer.sendChatMessage("/" + cmd);
currentState = State.END;
clock.schedule(100);
break;
case END:
if (wasMacroing) {
MacroHandler.getInstance().resumeMacro();
}
LogUtils.sendSuccess("Command sent: " + cmd);
currentState = State.NONE;
clock.reset();
enabled = false;
RemoteMessage response = new RemoteMessage(label, data);
send(response);
break;
}
}

private void disableWithError(String message) {
currentState = State.NONE;
clock.reset();
enabled = false;
LogUtils.sendError(message);
data.addProperty("error", message);
RemoteMessage response = new RemoteMessage(label, data);
send(response);
}

@SubscribeEvent
public void onWorldChange(WorldEvent.Unload event) {
if (enabled) {
disableWithError("World change detected! Disabling...");
}
}

public enum State {
NONE,
START,
TYPE_COMMAND,
END
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.jelly.farmhelperv2.remote.command.discordCommands.impl;

import com.google.gson.JsonObject;
import com.jelly.farmhelperv2.remote.WebsocketHandler;
import com.jelly.farmhelperv2.remote.command.discordCommands.DiscordCommand;
import com.jelly.farmhelperv2.remote.command.discordCommands.Option;
import com.jelly.farmhelperv2.remote.waiter.Waiter;
import com.jelly.farmhelperv2.remote.waiter.WaiterHandler;
import com.jelly.farmhelperv2.util.AvatarUtils;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;

import java.util.Objects;

public class SendCommand extends DiscordCommand {
public static final String name = "sendcommand";
public static final String description = "Run a minecraft command";

public SendCommand() {
super(name, description);
addOptions(new Option(OptionType.STRING, "command", "The command WITHOUT THE SLASH", true, false),
new Option(OptionType.STRING, "ign", "The IGN of the instance", false, true)
);
}

@Override
public void execute(SlashCommandInteractionEvent event) {
event.deferReply().queue();
WaiterHandler.register(new Waiter(
15000,
name,
action -> {
String username = action.args.get("username").getAsString();
String cmd = action.args.get("command").getAsString();
String uuid = action.args.get("uuid").getAsString();
boolean error = action.args.has("error");

EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.addField("Username", username, false);
if (error) {
embedBuilder.addField("Error", action.args.get("error").getAsString(), false);
} else {
embedBuilder.addField("Ran command:", cmd, false);
}
int random = (int) (Math.random() * 0xFFFFFF);
embedBuilder.setColor(random);
embedBuilder.setFooter("-> FarmHelper Remote Control", "https://cdn.discordapp.com/attachments/861700235890130986/1144673641951395982/icon.png");
String avatar = AvatarUtils.getAvatarUrl(uuid);
embedBuilder.setAuthor("Instance name -> " + username, avatar, avatar);

try {
event.getHook().sendMessageEmbeds(embedBuilder.build()).queue();
} catch (Exception e) {
event.getChannel().sendMessageEmbeds(embedBuilder.build()).queue();
}
},
timeoutAction -> event.getHook().sendMessage("Can't run command").queue(),
event
));

int cmd = Objects.requireNonNull(event.getOption("command")).getAsInt();
JsonObject args = new JsonObject();
args.addProperty("command", cmd);
if (event.getOption("ign") != null) {
String ign = Objects.requireNonNull(event.getOption("ign")).getAsString();
if (WebsocketHandler.getInstance().getWebsocketServer().minecraftInstances.containsValue(ign)) {
WebsocketHandler.getInstance().getWebsocketServer().minecraftInstances.forEach((webSocket, s) -> {
if (s.equals(ign)) {
sendMessage(webSocket, args);
}
});
} else {
event.getHook().sendMessage("There isn't any instances connected with that IGN").queue();
}
} else {
WebsocketHandler.getInstance().getWebsocketServer().minecraftInstances.forEach((webSocket, s) -> sendMessage(webSocket, args));
}
}
}

0 comments on commit c684980

Please sign in to comment.