From c3ae37e88f967a21522af7d0cb79a571326cd7e9 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 28 Apr 2019 15:37:32 +0100 Subject: Start implementing activity log feature --- .../lucko/spark/velocity/SparkVelocityPlugin.java | 36 +++++------ .../spark/velocity/VelocityCommandSender.java | 69 ++++++++++++++++++++++ 2 files changed, 83 insertions(+), 22 deletions(-) create mode 100644 spark-velocity/src/main/java/me/lucko/spark/velocity/VelocityCommandSender.java (limited to 'spark-velocity/src/main/java/me/lucko/spark') diff --git a/spark-velocity/src/main/java/me/lucko/spark/velocity/SparkVelocityPlugin.java b/spark-velocity/src/main/java/me/lucko/spark/velocity/SparkVelocityPlugin.java index c1c2f2d..4a5f500 100644 --- a/spark-velocity/src/main/java/me/lucko/spark/velocity/SparkVelocityPlugin.java +++ b/spark-velocity/src/main/java/me/lucko/spark/velocity/SparkVelocityPlugin.java @@ -30,21 +30,19 @@ import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.ProxyServer; +import me.lucko.spark.common.CommandSender; import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.SparkPlugin; import me.lucko.spark.common.sampler.ThreadDumper; import me.lucko.spark.common.sampler.TickCounter; -import net.kyori.text.Component; -import net.kyori.text.TextComponent; -import net.kyori.text.event.ClickEvent; -import net.kyori.text.format.TextColor; -import net.kyori.text.serializer.ComponentSerializers; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.optional.qual.MaybePresent; import java.nio.file.Path; -import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; import java.util.Set; +import java.util.stream.Collectors; @Plugin( id = "spark", @@ -53,9 +51,9 @@ import java.util.Set; description = "@desc@", authors = {"Luck", "sk89q"} ) -public class SparkVelocityPlugin implements SparkPlugin, Command { +public class SparkVelocityPlugin implements SparkPlugin, Command { - private final SparkPlatform platform = new SparkPlatform<>(this); + private final SparkPlatform platform = new SparkPlatform(this); private final ProxyServer proxy; private final Path configDirectory; @@ -79,13 +77,12 @@ public class SparkVelocityPlugin implements SparkPlugin, Command @Override public void execute(@MaybePresent CommandSource sender, @NonNull @MaybePresent String[] args) { - if (!sender.hasPermission("spark")) { - TextComponent msg = TextComponent.builder("You do not have permission to use this command.").color(TextColor.RED).build(); - sender.sendMessage(msg); - return; - } + this.platform.executeCommand(new VelocityCommandSender(sender), args); + } - this.platform.executeCommand(sender, args); + @Override + public @MaybePresent List suggest(@MaybePresent CommandSource sender, @NonNull @MaybePresent String[] currentArgs) { + return this.platform.tabCompleteCommand(new VelocityCommandSender(sender), currentArgs); } @Override @@ -104,16 +101,11 @@ public class SparkVelocityPlugin implements SparkPlugin, Command } @Override - public Set getSendersWithPermission(String permission) { - Set senders = new HashSet<>(this.proxy.getAllPlayers()); + public Set getSendersWithPermission(String permission) { + List senders = new LinkedList<>(this.proxy.getAllPlayers()); senders.removeIf(sender -> !sender.hasPermission(permission)); senders.add(this.proxy.getConsoleCommandSource()); - return senders; - } - - @Override - public void sendMessage(CommandSource sender, Component message) { - sender.sendMessage(message); + return senders.stream().map(VelocityCommandSender::new).collect(Collectors.toSet()); } @Override diff --git a/spark-velocity/src/main/java/me/lucko/spark/velocity/VelocityCommandSender.java b/spark-velocity/src/main/java/me/lucko/spark/velocity/VelocityCommandSender.java new file mode 100644 index 0000000..1cad493 --- /dev/null +++ b/spark-velocity/src/main/java/me/lucko/spark/velocity/VelocityCommandSender.java @@ -0,0 +1,69 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) 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 . + */ + +package me.lucko.spark.velocity; + +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.proxy.ConsoleCommandSource; +import com.velocitypowered.api.proxy.Player; +import me.lucko.spark.common.CommandSender; +import net.kyori.text.Component; + +public class VelocityCommandSender implements CommandSender { + private final CommandSource source; + + public VelocityCommandSender(CommandSource source) { + this.source = source; + } + + @Override + public String getName() { + if (this.source instanceof Player) { + return ((Player) this.source).getUsername(); + } else if (this.source instanceof ConsoleCommandSource) { + return "Console"; + } else { + return "unknown:" + this.source.getClass().getSimpleName(); + } + } + + @Override + public void sendMessage(Component message) { + this.source.sendMessage(message); + } + + @Override + public boolean hasPermission(String permission) { + return this.source.hasPermission(permission); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + VelocityCommandSender that = (VelocityCommandSender) o; + return this.source.equals(that.source); + } + + @Override + public int hashCode() { + return this.source.hashCode(); + } +} -- cgit