Command feedback

This commit is contained in:
IxPrumxI 2025-01-17 10:14:15 +03:00
parent 90d89ade8a
commit 58d81a7c73
3 changed files with 86 additions and 3 deletions

View File

@ -25,6 +25,12 @@ import com.discordsrv.common.core.logging.backend.LoggingBackend;
import com.discordsrv.common.core.logging.backend.impl.Log4JLoggerImpl;
import com.discordsrv.common.feature.console.Console;
import com.discordsrv.fabric.console.executor.FabricCommandExecutor;
import com.discordsrv.fabric.console.executor.FabricCommandFeedbackExecutor;
import net.kyori.adventure.text.Component;
import net.minecraft.server.command.ServerCommandSource;
import java.util.function.Consumer;
import java.util.function.Function;
public class FabricConsole extends FabricCommandSender implements Console {
@ -34,7 +40,9 @@ public class FabricConsole extends FabricCommandSender implements Console {
public FabricConsole(FabricDiscordSRV discordSRV) {
super(discordSRV, discordSRV.getServer().getCommandSource());
this.loggingBackend = Log4JLoggerImpl.getRoot();
this.executorProvider = consumer -> new FabricCommandExecutor(discordSRV);
Function<Consumer<Component>, ServerCommandSource> commandSenderProvider = consumer -> new FabricCommandFeedbackExecutor(discordSRV.getServer(), consumer).getCommandSource();
this.executorProvider = consumer -> new FabricCommandExecutor(discordSRV, commandSenderProvider.apply(consumer));
}
@Override

View File

@ -27,9 +27,9 @@ public class FabricCommandExecutor implements CommandExecutor {
private final FabricDiscordSRV discordSRV;
private final ServerCommandSource source;
public FabricCommandExecutor(FabricDiscordSRV discordSRV) {
public FabricCommandExecutor(FabricDiscordSRV discordSRV, ServerCommandSource source) {
this.discordSRV = discordSRV;
this.source = discordSRV.getServer().getCommandSource();
this.source = source;
}
@Override

View File

@ -0,0 +1,75 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2025 Austin "Scarsz" Shapiro, Henri "Vankka" Schubin and DiscordSRV contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.discordsrv.fabric.console.executor;
import net.kyori.adventure.platform.modcommon.MinecraftServerAudiences;
import net.kyori.adventure.text.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandOutput;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
import java.util.function.Consumer;
public class FabricCommandFeedbackExecutor implements CommandOutput, Consumer<Component> {
private final MinecraftServer server;
private final Consumer<Component> componentConsumer;
public FabricCommandFeedbackExecutor(MinecraftServer server, Consumer<Component> componentConsumer) {
this.server = server;
this.componentConsumer = componentConsumer;
}
public ServerCommandSource getCommandSource() {
ServerWorld serverWorld = server.getOverworld();
return new ServerCommandSource(
this, serverWorld == null ? Vec3d.ZERO : Vec3d.of(serverWorld.getSpawnPos()), Vec2f.ZERO, serverWorld, 4, "DiscordSRV", Text.literal("DiscordSRV"), server, null
);
}
@Override
public void sendMessage(Text message) {
accept(Component.text(Formatting.strip(message.getString())));
}
@Override
public void accept(Component component) {
componentConsumer.accept(component);
}
@Override
public boolean shouldReceiveFeedback() {
return true;
}
@Override
public boolean shouldTrackOutput() {
return true;
}
@Override
public boolean shouldBroadcastConsoleToOps() {
return true;
}
}