Skip to content

Commit

Permalink
BungeeExecute update and 'as:player'
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed May 14, 2023
1 parent 5db2ae1 commit 2d65e00
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 31 deletions.
@@ -0,0 +1,25 @@
package com.denizenscript.depenizen.bukkit.bungee.packets.out;

import com.denizenscript.depenizen.bukkit.bungee.PacketOut;
import io.netty.buffer.ByteBuf;

public class ExecutePlayerCommandPacketOut extends PacketOut {

public ExecutePlayerCommandPacketOut(String player, String command) {
this.player = player;
this.command = command;
}

public String player, command;

@Override
public int getPacketId() {
return 18;
}

@Override
public void writeTo(ByteBuf buf) {
writeString(buf, player);
writeString(buf, command);
}
}
@@ -1,34 +1,38 @@
package com.denizenscript.depenizen.bukkit.commands.bungee;

import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.scripts.commands.generator.ArgDefaultNull;
import com.denizenscript.denizencore.scripts.commands.generator.ArgLinear;
import com.denizenscript.denizencore.scripts.commands.generator.ArgName;
import com.denizenscript.denizencore.scripts.commands.generator.ArgPrefixed;
import com.denizenscript.depenizen.bukkit.bungee.BungeeBridge;
import com.denizenscript.depenizen.bukkit.bungee.PacketOut;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.ExecuteCommandPacketOut;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.ExecutePlayerCommandPacketOut;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.KeepAlivePacketOut;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;

public class BungeeExecuteCommand extends AbstractCommand {

public BungeeExecuteCommand() {
setName("bungeeexecute");
setSyntax("bungeeexecute [<command>]");
setRequiredArguments(1, 1);
setSyntax("bungeeexecute [<command>] (as:<player>)");
setRequiredArguments(1, 2);
autoCompile();
}

// <--[command]
// @Name BungeeExecute
// @Syntax bungeeexecute [<command>]
// @Syntax bungeeexecute [<command>] (as:<player>)
// @Group Depenizen
// @Plugin Depenizen, DepenizenBungee, BungeeCord
// @Required 1
// @Maximum 1
// @Short Runs a command on the Bungee proxy server.
// @Maximum 2
// @Short Runs a command on the Bungee proxy server, or a player.
//
// @Description
// This command runs a command on the Bungee proxy server. Works similarly to "execute as_server".
// If you specify "as:", you can use a PlayerTag or a UUID to automatically execute as that player, similar to "execute as_player".
//
// @Tags
// None
Expand All @@ -39,32 +43,26 @@ public BungeeExecuteCommand() {
//
// -->

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("command")) {
scriptEntry.addObject("command", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("command")) {
throw new InvalidArgumentsException("Must define a COMMAND to be run.");
}
}

@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag command = scriptEntry.getElement("command");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), command);
}
public static void autoExecute(@ArgLinear @ArgName("command") String command,
@ArgPrefixed @ArgName("as") @ArgDefaultNull String asPlayer) {
if (!BungeeBridge.instance.connected) {
Debug.echoError("Cannot BungeeExecute: bungee is not connected!");
return;
}
ExecuteCommandPacketOut packet = new ExecuteCommandPacketOut(command.asString());
PacketOut packet;
if (asPlayer == null || asPlayer.length() == 0) {
packet = new ExecuteCommandPacketOut(command);
}
else {
if (asPlayer.startsWith("p@")) {
asPlayer = asPlayer.substring("p@".length());
}
if (asPlayer.length() != 36) {
Debug.echoError("Invalid UUID '" + asPlayer + "'!");
return;
}
packet = new ExecutePlayerCommandPacketOut(asPlayer, command);
}
BungeeBridge.instance.sendPacket(packet);
BungeeBridge.instance.sendPacket(new KeepAlivePacketOut());
}
Expand Down

0 comments on commit 2d65e00

Please sign in to comment.