From ea42f47c7b5e358e800b6ffbf6a0df19e9a45a6d Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 20 Sep 2018 19:00:42 +0100 Subject: [PATCH] Properly initialise console permissions using the PermissionsSetupEvent --- .../com/velocitypowered/proxy/Velocity.java | 3 +- .../velocitypowered/proxy/VelocityServer.java | 22 ++++-------- .../proxy/console/VelocityConsole.java | 35 ++++++++++++++++--- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/Velocity.java b/proxy/src/main/java/com/velocitypowered/proxy/Velocity.java index c628775c4..f2f05f49f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/Velocity.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/Velocity.java @@ -1,6 +1,5 @@ package com.velocitypowered.proxy; -import com.velocitypowered.proxy.console.VelocityConsole; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -28,6 +27,6 @@ public class Velocity { double bootTime = (System.currentTimeMillis() - startTime) / 1000d; logger.info("Done ({}s)!", new DecimalFormat("#.##").format(bootTime)); - new VelocityConsole(server).start(); + server.getConsoleCommandSource().start(); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index 2cd0df2d9..4fa54cdfe 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -4,7 +4,6 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.event.EventManager; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; @@ -22,6 +21,7 @@ import com.velocitypowered.proxy.command.VelocityCommandManager; import com.velocitypowered.proxy.config.AnnotatedConfig; import com.velocitypowered.proxy.config.VelocityConfiguration; import com.velocitypowered.proxy.connection.client.ConnectedPlayer; +import com.velocitypowered.proxy.console.VelocityConsole; import com.velocitypowered.proxy.messages.VelocityChannelRegistrar; import com.velocitypowered.proxy.network.ConnectionManager; import com.velocitypowered.proxy.network.http.NettyHttpClient; @@ -37,7 +37,6 @@ import com.velocitypowered.proxy.util.Ratelimiter; import io.netty.bootstrap.Bootstrap; import net.kyori.text.Component; import net.kyori.text.TextComponent; -import net.kyori.text.serializer.ComponentSerializers; import net.kyori.text.serializer.GsonComponentSerializer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -73,17 +72,7 @@ public class VelocityServer implements ProxyServer { private final Map connectionsByUuid = new ConcurrentHashMap<>(); private final Map connectionsByName = new ConcurrentHashMap<>(); - private final CommandSource consoleCommandSource = new CommandSource() { - @Override - public void sendMessage(Component component) { - logger.info(ComponentSerializers.LEGACY.serialize(component)); - } - - @Override - public @NonNull Tristate getPermissionValue(@NonNull String permission) { - return Tristate.TRUE; - } - }; + private final VelocityConsole console = new VelocityConsole(this); private Ratelimiter ipAttemptLimiter; private VelocityEventManager eventManager; private VelocityScheduler scheduler; @@ -148,6 +137,9 @@ public class VelocityServer implements ProxyServer { // issues) and there is almost no chance ExecutionException will be thrown. } + // init console permissions after plugins are loaded + console.setupPermissions(); + this.cm.bind(configuration.getBind()); if (configuration.isQueryEnabled()) { @@ -297,8 +289,8 @@ public class VelocityServer implements ProxyServer { } @Override - public CommandSource getConsoleCommandSource() { - return consoleCommandSource; + public VelocityConsole getConsoleCommandSource() { + return console; } @Override diff --git a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java index 4e233ddce..c654a2388 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java @@ -1,9 +1,18 @@ package com.velocitypowered.proxy.console; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.event.permission.PermissionsSetupEvent; +import com.velocitypowered.api.permission.PermissionFunction; +import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.proxy.VelocityServer; +import net.kyori.text.Component; import net.kyori.text.TextComponent; import net.kyori.text.format.TextColor; +import net.kyori.text.serializer.ComponentSerializers; import net.minecrell.terminalconsole.SimpleTerminalConsole; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.checkerframework.checker.nullness.qual.NonNull; import org.jline.reader.Candidate; import org.jline.reader.LineReader; import org.jline.reader.LineReaderBuilder; @@ -11,20 +20,38 @@ import org.jline.reader.LineReaderBuilder; import java.util.List; import java.util.Optional; -public final class VelocityConsole extends SimpleTerminalConsole { +public final class VelocityConsole extends SimpleTerminalConsole implements CommandSource { + private static final Logger logger = LogManager.getLogger(VelocityConsole.class); private final VelocityServer server; + private PermissionFunction permissionFunction = PermissionFunction.ALWAYS_TRUE; public VelocityConsole(VelocityServer server) { this.server = server; } + @Override + public void sendMessage(Component component) { + logger.info(ComponentSerializers.LEGACY.serialize(component)); + } + + @Override + public @NonNull Tristate getPermissionValue(@NonNull String permission) { + return this.permissionFunction.getPermissionValue(permission); + } + + public void setupPermissions() { + PermissionsSetupEvent event = new PermissionsSetupEvent(this, s -> PermissionFunction.ALWAYS_TRUE); + this.server.getEventManager().fire(event).join(); // this is called on startup, we can safely #join + this.permissionFunction = event.createFunction(this); + } + @Override protected LineReader buildReader(LineReaderBuilder builder) { return super.buildReader(builder .appName("Velocity") .completer((reader, parsedLine, list) -> { - Optional> o = server.getCommandManager().offerSuggestions(server.getConsoleCommandSource(), parsedLine.line()); + Optional> o = this.server.getCommandManager().offerSuggestions(this, parsedLine.line()); o.ifPresent(offers -> { for (String offer : offers) { list.add(new Candidate(offer)); @@ -41,8 +68,8 @@ public final class VelocityConsole extends SimpleTerminalConsole { @Override protected void runCommand(String command) { - if (!this.server.getCommandManager().execute(this.server.getConsoleCommandSource(), command)) { - server.getConsoleCommandSource().sendMessage(TextComponent.of("Command not found.", TextColor.RED)); + if (!this.server.getCommandManager().execute(this, command)) { + sendMessage(TextComponent.of("Command not found.", TextColor.RED)); } }