diff options
Diffstat (limited to 'spark-sponge/src/main/java')
-rw-r--r-- | spark-sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java | 36 | ||||
-rw-r--r-- | spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeCommandSender.java | 62 |
2 files changed, 72 insertions, 26 deletions
diff --git a/spark-sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java b/spark-sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java index c0727ac..537bfea 100644 --- a/spark-sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java +++ b/spark-sponge/src/main/java/me/lucko/spark/sponge/SparkSpongePlugin.java @@ -21,12 +21,11 @@ package me.lucko.spark.sponge; import com.google.inject.Inject; +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.adapter.spongeapi.TextAdapter; import org.spongepowered.api.Game; import org.spongepowered.api.command.CommandCallable; import org.spongepowered.api.command.CommandResult; @@ -40,21 +39,16 @@ import org.spongepowered.api.plugin.Plugin; import org.spongepowered.api.scheduler.AsynchronousExecutor; import org.spongepowered.api.scheduler.SpongeExecutorService; import org.spongepowered.api.text.Text; -import org.spongepowered.api.text.action.TextActions; -import org.spongepowered.api.text.format.TextColors; -import org.spongepowered.api.text.serializer.TextSerializers; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; import javax.annotation.Nullable; -import java.net.MalformedURLException; -import java.net.URL; import java.nio.file.Path; -import java.util.Collections; -import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; @Plugin( id = "spark", @@ -67,13 +61,13 @@ import java.util.Set; @Dependency(id = "spongeapi") } ) -public class SparkSpongePlugin implements SparkPlugin<CommandSource> { +public class SparkSpongePlugin implements SparkPlugin { private final Game game; private final Path configDirectory; private final SpongeExecutorService asyncExecutor; - private final SparkPlatform<CommandSource> platform = new SparkPlatform<>(this); + private final SparkPlatform platform = new SparkPlatform(this); @Inject public SparkSpongePlugin(Game game, @ConfigDir(sharedRoot = false) Path configDirectory, @AsynchronousExecutor SpongeExecutorService asyncExecutor) { @@ -109,16 +103,11 @@ public class SparkSpongePlugin implements SparkPlugin<CommandSource> { } @Override - public Set<CommandSource> getSendersWithPermission(String permission) { - Set<CommandSource> senders = new HashSet<>(this.game.getServer().getOnlinePlayers()); + public Set<CommandSender> getSendersWithPermission(String permission) { + List<CommandSource> senders = new LinkedList<>(this.game.getServer().getOnlinePlayers()); senders.removeIf(sender -> !sender.hasPermission(permission)); senders.add(this.game.getServer().getConsole()); - return senders; - } - - @Override - public void sendMessage(CommandSource sender, Component message) { - TextAdapter.sendComponent(sender, message); + return senders.stream().map(SpongeCommandSender::new).collect(Collectors.toSet()); } @Override @@ -145,18 +134,13 @@ public class SparkSpongePlugin implements SparkPlugin<CommandSource> { @Override public CommandResult process(CommandSource source, String arguments) { - if (!testPermission(source)) { - source.sendMessage(Text.builder("You do not have permission to use this command.").color(TextColors.RED).build()); - return CommandResult.empty(); - } - - this.plugin.platform.executeCommand(source, arguments.split(" ")); + this.plugin.platform.executeCommand(new SpongeCommandSender(source), arguments.split(" ")); return CommandResult.empty(); } @Override public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) { - return Collections.emptyList(); + return this.plugin.platform.tabCompleteCommand(new SpongeCommandSender(source), arguments.split(" ")); } @Override diff --git a/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeCommandSender.java b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeCommandSender.java new file mode 100644 index 0000000..5b434fc --- /dev/null +++ b/spark-sponge/src/main/java/me/lucko/spark/sponge/SpongeCommandSender.java @@ -0,0 +1,62 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * 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 <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.sponge; + +import me.lucko.spark.common.CommandSender; +import net.kyori.text.Component; +import net.kyori.text.adapter.spongeapi.TextAdapter; +import org.spongepowered.api.command.CommandSource; + +public class SpongeCommandSender implements CommandSender { + private final CommandSource source; + + public SpongeCommandSender(CommandSource source) { + this.source = source; + } + + @Override + public String getName() { + return this.source.getName(); + } + + @Override + public void sendMessage(Component message) { + TextAdapter.sendComponent(this.source, 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; + SpongeCommandSender that = (SpongeCommandSender) o; + return this.source.equals(that.source); + } + + @Override + public int hashCode() { + return this.source.hashCode(); + } +} |