Update command handling

This commit is contained in:
Camotoy 2021-01-21 14:57:06 -05:00
parent 24da65dcb5
commit c3569093b6
3 changed files with 27 additions and 24 deletions

View file

@ -149,11 +149,12 @@ public class GeyserFabricMod implements ModInitializer, GeyserBootstrap {
// Start command building
// Set just "geyser" as the help command
LiteralArgumentBuilder<ServerCommandSource> builder = net.minecraft.server.command.CommandManager.literal("geyser")
.executes(new GeyserFabricCommandExecutor(connector, "help", !playerCommands.contains("help")));
.executes(new GeyserFabricCommandExecutor(connector, connector.getCommandManager().getCommands().get("help"),
!playerCommands.contains("help")));
for (Map.Entry<String, GeyserCommand> command : connector.getCommandManager().getCommands().entrySet()) {
// Register all subcommands as valid
builder.then(net.minecraft.server.command.CommandManager.literal(
command.getKey()).executes(new GeyserFabricCommandExecutor(connector, command.getKey(),
command.getKey()).executes(new GeyserFabricCommandExecutor(connector, command.getValue(),
!playerCommands.contains(command.getKey()))));
}
server.getCommandManager().getDispatcher().register(builder);

View file

@ -25,8 +25,8 @@
package org.geysermc.platform.fabric.command;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.command.CommandSender;
@ -47,20 +47,15 @@ public class FabricCommandSender implements CommandSender {
@Override
public void sendMessage(String message) {
try {
source.getPlayer().sendMessage(new LiteralText(message), false);
} catch (CommandSyntaxException e) { // why
if (source.getEntity() instanceof ServerPlayerEntity) {
((ServerPlayerEntity) source.getEntity()).sendMessage(new LiteralText(message), false);
} else {
GeyserConnector.getInstance().getLogger().info(ChatColor.toANSI(message + ChatColor.RESET));
}
}
@Override
public boolean isConsole() {
try {
source.getPlayer();
return false;
} catch (CommandSyntaxException e) {
return true;
}
return !(source.getEntity() instanceof ServerPlayerEntity);
}
}

View file

@ -29,22 +29,24 @@ import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.ServerCommandSource;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.command.CommandExecutor;
import org.geysermc.connector.command.GeyserCommand;
import org.geysermc.connector.common.ChatColor;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.utils.LanguageUtils;
import org.geysermc.platform.fabric.GeyserFabricMod;
public class GeyserFabricCommandExecutor implements Command<ServerCommandSource> {
public class GeyserFabricCommandExecutor extends CommandExecutor implements Command<ServerCommandSource> {
private final String commandName;
private final GeyserConnector connector;
private final GeyserCommand command;
/**
* Whether the command requires an OP permission level of 2 or greater
*/
private final boolean requiresPermission;
public GeyserFabricCommandExecutor(GeyserConnector connector, String commandName, boolean requiresPermission) {
this.commandName = commandName;
this.connector = connector;
public GeyserFabricCommandExecutor(GeyserConnector connector, GeyserCommand command, boolean requiresPermission) {
super(connector);
this.command = command;
this.requiresPermission = requiresPermission;
}
@ -56,14 +58,19 @@ public class GeyserFabricCommandExecutor implements Command<ServerCommandSource>
sender.sendMessage(LanguageUtils.getLocaleStringLog("geyser.bootstrap.command.permission_fail"));
return 0;
}
if (this.commandName.equals("reload")) {
if (this.command.getName().equals("reload")) {
GeyserFabricMod.getInstance().setReloading(true);
}
getCommand(commandName).execute(sender, new String[0]);
GeyserSession session = null;
if (command.isBedrockOnly()) {
session = getGeyserSession(sender);
if (session == null) {
sender.sendMessage(ChatColor.RED + LanguageUtils.getPlayerLocaleString("geyser.bootstrap.command.bedrock_only", sender.getLocale()));
return 0;
}
private GeyserCommand getCommand(String label) {
return connector.getCommandManager().getCommands().get(label);
}
command.execute(session, sender, new String[0]);
return 0;
}
}