-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from ariesninjadev/master
feat: Added remote "SendCommand" command
- Loading branch information
Showing
4 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/com/jelly/farmhelperv2/remote/command/commands/impl/SendCommandCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/jelly/farmhelperv2/remote/command/discordCommands/impl/SendCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |