Skip to content

Commit

Permalink
Re-add minimal mode (-m)
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Mar 27, 2022
1 parent 2865b34 commit dce554e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 32 deletions.
81 changes: 58 additions & 23 deletions src/net/ME1312/CBS/Command.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
package net.ME1312.CBS;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.Directional;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.util.Vector;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.PrimitiveIterator;
import java.util.UUID;
import java.util.*;

import static org.bukkit.ChatColor.*;

final class Command implements CommandExecutor {
final class Command extends org.bukkit.command.Command {
private final RuntimeException FAILURE;
private final MethodHandle COMMANDS;
private final Class<?> CBS;
private final boolean FLAT;
private final EmulationManager plugin;
Command(EmulationManager plugin, Class<?> extension, RuntimeException reference) {
Command(EmulationManager plugin, Class<?> extension, RuntimeException reference) throws Throwable {
super("cbs", "CommandBlock Support", "/cbs [-flags] [command] [args...]", Collections.emptyList());
this.plugin = plugin;
boolean flat;
try { // noinspection ConstantConditions
flat = Block.class.getMethod("getBlockData") != null;
} catch (NoSuchMethodException | NoSuchMethodError e) {
flat = false;
}
Server server = Bukkit.getServer();
Field commands = server.getClass().getDeclaredField("commandMap");
commands.setAccessible(true);
((CommandMap) (COMMANDS = MethodHandles.publicLookup()
.unreflectGetter(commands)
.asType(MethodType.methodType(CommandMap.class, Server.class))
).invokeExact(server)).register("cbs", this);

FLAT = flat;
CBS = extension;
FAILURE = reference;
Expand All @@ -44,7 +54,7 @@ private String prefix(ChatColor color, ChatColor accent) {
}

@SuppressWarnings("NullableProblems")
public boolean onCommand(CommandSender sender, org.bukkit.command.Command context, String label, String[] args) {
public boolean execute(CommandSender sender, String label, String[] args) {
if (args.length == 0) {
PluginDescriptionFile desc = plugin.plugin.getDescription();
sender.sendMessage("");
Expand All @@ -53,6 +63,19 @@ public boolean onCommand(CommandSender sender, org.bukkit.command.Command contex
sender.sendMessage("");
sender.sendMessage(GRAY + ITALIC.toString() + UNDERLINE + desc.getWebsite() + "/wiki/Flags");
sender.sendMessage("");
} else if (args[0].matches("-+m")) {
if (args.length > 1) { // Minimal mode (-m) has the sender run the command as themselves. No further permission checks required.
boolean success = run(sender, sender, args, 1);
if (!success) {
if (sender instanceof BlockCommandSender) {
throw FAILURE;
} else {
return sender.getClass() != CBS;
}
}
} else {
sender.sendMessage(prefix(RED, DARK_RED) + "No command to execute");
}
} else {
boolean sub = false, debug = false;
double x = 0, y = 0, z = 0;
Expand All @@ -64,7 +87,7 @@ public boolean onCommand(CommandSender sender, org.bukkit.command.Command contex
if (sender instanceof Player) {
if (sender.getClass() == CBS) {
sender.sendMessage(prefix(RED, DARK_RED) + "This command cannot be nested");
throw FAILURE;
return false;
} else if (!sender.isOp()) {
sender.sendMessage(prefix(RED, DARK_RED) + "This command may only be tested by server operators");
return true;
Expand Down Expand Up @@ -179,6 +202,9 @@ public boolean onCommand(CommandSender sender, org.bukkit.command.Command contex
throw new CommandException("Invalid decimal number: " + DARK_RED + n);
}
}
case 'm': {
throw new CommandException("The " + DARK_RED + "-m" + RED + " flag cannot be combined with any other flags");
}
case '-': if (starting) {
continue;
}
Expand Down Expand Up @@ -208,7 +234,7 @@ public boolean onCommand(CommandSender sender, org.bukkit.command.Command contex
boolean execute = i < args.length;
if (execute || sub) player.subs.add(sender);
if (execute) {
if (!Bukkit.getServer().dispatchCommand(player.getPlayer(), join(args, i)) && sender instanceof BlockCommandSender) {
if (!run(sender, player.getPlayer(), args, i) && sender instanceof BlockCommandSender) {
throw FAILURE;
}
} else {
Expand All @@ -222,6 +248,23 @@ public boolean onCommand(CommandSender sender, org.bukkit.command.Command contex
return true;
}

private boolean run(CommandSender sender, CommandSender target, String[] args, int i) {
org.bukkit.command.Command command;
String label = args[i];
try {
command = ((CommandMap) COMMANDS.invoke(Bukkit.getServer())).getCommand(label);
} catch (Throwable e) {
throw new RuntimeException(e);
}

if (command == null) {
sender.sendMessage(prefix(RED, DARK_RED) + "Unknown command: " + RED + label);
return false;
} else {
return command.execute(target, label, Arrays.copyOfRange(args, ++i, args.length));
}
}

private static void flag(int length, int args, String usage) {
if (length <= args) throw new CommandException("Missing argument" + ((length == args)?"":"s") + " for flag: " + DARK_RED + usage);
}
Expand All @@ -242,16 +285,8 @@ private static double relative(String input, double current) {
}
}

private static String join(String[] args, int i) {
String command = args[i++];
if (i < args.length) {
StringBuilder builder = new StringBuilder(command);
do {
builder.append(' ');
builder.append(args[i]);
} while (++i < args.length);
command = builder.toString();
}
return command;
@Override
public List<String> tabComplete(CommandSender sender, String label, String[] args) throws IllegalArgumentException {
return super.tabComplete(sender, label, args); // TODO
}
}
8 changes: 4 additions & 4 deletions src/net/ME1312/CBS/EmulatedPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ public final class EmulatedPlayer /* extends Player */ {
}
msg.append("}\nreturns: ")
.append(returns.getCanonicalName())
.append("\ncallers:");
.append("\ncallers:\n ");
int i = 2;
do {
e = stack[i];
if (CBS.equals(e.getClassName())) {
msg.replace(msg.lastIndexOf("\n") + 5, msg.length(), "... ")
.append(stack.length + 1 - i)
msg.append("... ")
.append(stack.length - i)
.append(" more");
break;
}
msg.append("\n ").append(e);
msg.append(e).append("\n ");
} while (++i < stack.length);
msg.append('\n');
Bukkit.getLogger().log((translated)? Level.INFO : Level.WARNING, msg.toString());
Expand Down
2 changes: 1 addition & 1 deletion src/net/ME1312/CBS/EmulationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public EmulationManager(JavaPlugin plugin, RuntimeException reference) throws Th
.findConstructor(extension, MethodType.methodType(void.class, EmulatedPlayer.class))
.asType(MethodType.methodType(void.class, EmulatedPlayer.class));

plugin.getCommand("cbs").setExecutor(new Command(this, extension, reference));
new Command(this, extension, reference);
new BStats(plugin, 14759).addCustomChart(new BStats.SingleLineChart("emulators", players::size));
}

Expand Down
8 changes: 4 additions & 4 deletions src/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ main: net.ME1312.CBS.CBS
version: "1.18.2a"
authors: ["ME1312"]
website: "https://github.com/ME1312/CommandBlock-Support"
commands:
cbs:
description: "CommandBlock Support"
usage: "/cbs [-flags] [command] [args...]"
#commands:
# cbs:
# description: "CommandBlock Support"
# usage: "/cbs [-flags] [command] [args...]"

0 comments on commit dce554e

Please sign in to comment.