Skip to content

Commit

Permalink
Added Who and What commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityfox committed Dec 25, 2016
1 parent b563c94 commit 69a65b4
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import net.foxdenstudio.sponge.foxcore.common.network.server.packet.ServerPrintStringPacket;
import net.foxdenstudio.sponge.foxcore.plugin.command.*;
import net.foxdenstudio.sponge.foxcore.plugin.command.misc.CommandPWD;
import net.foxdenstudio.sponge.foxcore.plugin.command.misc.CommandWhat;
import net.foxdenstudio.sponge.foxcore.plugin.command.misc.CommandWho;
import net.foxdenstudio.sponge.foxcore.plugin.listener.WandBlockListener;
import net.foxdenstudio.sponge.foxcore.plugin.listener.WandEntityListener;
import net.foxdenstudio.sponge.foxcore.plugin.state.FCStateManager;
Expand Down Expand Up @@ -180,7 +182,9 @@ private void configureCommands() {
fcDispatcher.register(new CommandAbout(builder.build()), "about", "info");

FCCommandDispatcher miscDispatcher = new FCCommandDispatcher("/foxcore misc", "Misc commands that may be helpful.");
miscDispatcher.register(new CommandPWD(), "pwd", "directory");
miscDispatcher.register(new CommandPWD(), "pwd", "directory", "dir");
miscDispatcher.register(new CommandWho(), "who", "plugin");
miscDispatcher.register(new CommandWhat(), "what", "command");

fcDispatcher.register(miscDispatcher, "misc", "miscellaneous", "util");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import java.util.Optional;

/**
* Created by Fox on 11/5/2016.
*/
Expand All @@ -22,4 +24,20 @@ public CommandResult process(CommandSource source, String arguments) throws Comm
public boolean testPermission(CommandSource source) {
return source.hasPermission("foxcore.command.misc.pwd");
}

@Override
public Optional<Text> getShortDescription(CommandSource source) {
return Optional.of(Text.of("Prints the working directory of the game."));
}

@Override
public Optional<Text> getHelp(CommandSource source) {
return Optional.of(Text.of("This command prints the directory from which the game was started.\n" +
"On servers, this corresponds to the folder the server was started from, usually the folder with the server jar."));
}

@Override
public Text getUsage(CommandSource source) {
return Text.of("/foxcore misc pwd");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package net.foxdenstudio.sponge.foxcore.plugin.command.misc;

import com.google.common.collect.ImmutableList;
import net.foxdenstudio.sponge.foxcore.plugin.command.FCCommandBase;
import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParser;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.*;
import org.spongepowered.api.plugin.PluginContainer;
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.format.TextStyles;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import javax.annotation.Nullable;
import java.util.*;

/**
* Created by Fox on 12/22/2016.
*/
public class CommandWhat extends FCCommandBase {

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
AdvCmdParser.ParseResult parse = AdvCmdParser.builder()
.arguments(arguments)
.parse();
CommandManager manager = Sponge.getCommandManager();

if (parse.args.length == 0) {
source.sendMessage(Text.of(TextColors.GREEN, "Usage: ", TextColors.RESET, "/foxcore misc what <command>"));
} else {
String commandName = parse.args[0];
Set<? extends CommandMapping> mappings = manager.getAll(commandName);
if (mappings.size() > 0) {
Text.Builder builder = Text.builder();
builder.append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n"));
if (mappings.size() == 1) {
CommandMapping mapping = mappings.iterator().next();

generateText(mapping, builder, source, manager);

source.sendMessage(builder.build());
} else {
Optional<? extends CommandMapping> primaryMappingOpt = manager.get(commandName);
Set<? extends CommandMapping> secondaryMappings = new HashSet<>(mappings);
if (primaryMappingOpt.isPresent()) {
CommandMapping primaryMapping = primaryMappingOpt.get();
secondaryMappings.remove(primaryMapping);

builder.append(Text.of(TextColors.GREEN, "------- Primary -------\n"));

generateText(primaryMapping, builder, source, manager);

builder.append(Text.of(TextColors.GREEN, "\n------- Secondary -------\n"));

Iterator<? extends CommandMapping> mappingIterator = secondaryMappings.iterator();

while (mappingIterator.hasNext()) {
CommandMapping mapping = mappingIterator.next();
generateText(mapping, builder, source, manager);
if (mappingIterator.hasNext()) {
builder.append(Text.of("\n\n"));
}
}
} else {
source.sendMessage(Text.of(TextColors.RED, "Something very strange happened. What the heck did you do?"));
}
}
} else {
source.sendMessage(Text.of(TextColors.RED, "No command with this name: ", TextColors.RESET, commandName));
}
}
return CommandResult.empty();
}

@Override
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException {
AdvCmdParser.ParseResult parse = AdvCmdParser.builder()
.arguments(arguments)
.autoCloseQuotes(true)
.excludeCurrent(true)
.parse();
CommandManager manager = Sponge.getCommandManager();

if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.ARGUMENT) {
if (parse.current.index == 0) {
return manager.getSuggestions(source, parse.current.token, targetPosition);
}
} else if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.COMPLETE) {
return ImmutableList.of(parse.current.prefix + " ");
}
return ImmutableList.of();
}

@Override
public boolean testPermission(CommandSource source) {
return source.hasPermission("foxcore.command.misc.what");
}

@Override
public Optional<Text> getShortDescription(CommandSource source) {
return Optional.of(Text.of("Tells you information about any command."));
}

@Override
public Text getUsage(CommandSource source) {
return Text.of("<command>");
}

private String colTS(Collection col) {
String str = "";
Iterator it = col.iterator();
while (it.hasNext()) {
str += it.next().toString();
if (it.hasNext()) str += ", ";
}
return str;
}

private void generateText(CommandMapping mapping, Text.Builder builder, CommandSource source, CommandManager manager) {
String primaryAlias = mapping.getPrimaryAlias();
builder.append(Text.of(TextColors.GOLD, "Primary Alias: ", TextColors.RESET, primaryAlias, "\n"));

Set<String> secondaryAliases = new HashSet<>(mapping.getAllAliases());
secondaryAliases.remove(primaryAlias);

if (secondaryAliases.size() > 1) {
builder.append(Text.of(TextColors.GREEN, "Secondary Aliases: "));
} else {
builder.append(Text.of(TextColors.GREEN, "Secondary Alias: "));
}
builder.append(Text.of(TextColors.RESET, colTS(secondaryAliases), "\n"));

CommandCallable callable = mapping.getCallable();

builder.append(Text.of(TextColors.AQUA, "Usage: "));
builder.append(callable.getUsage(source));
builder.append(Text.NEW_LINE);

Optional<Text> descriptionOpt = callable.getShortDescription(source);
if (descriptionOpt.isPresent()) {
Text description = descriptionOpt.get();
builder.append(Text.of(TextColors.AQUA, "Description: "));
builder.append(description);
builder.append(Text.NEW_LINE);
}

Optional<PluginContainer> containerOpt = manager.getOwner(mapping);

if (containerOpt.isPresent()) {
PluginContainer container = containerOpt.get();
String id = container.getId();
builder.append(Text.of(
TextActions.showText(Text.of("Click to show plugin details")),
TextActions.runCommand("/foxcore misc who " + id),
TextColors.LIGHT_PURPLE, "Plugin: ", TextColors.RESET, id)
);
} else {
builder.append(Text.of(TextColors.LIGHT_PURPLE, "Plugin: ", TextColors.GRAY, TextStyles.ITALIC, "unknown"));
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package net.foxdenstudio.sponge.foxcore.plugin.command.misc;

import com.google.common.collect.ImmutableList;
import net.foxdenstudio.sponge.foxcore.plugin.command.FCCommandBase;
import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParser;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.plugin.PluginManager;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.util.GuavaCollectors;
import org.spongepowered.api.util.StartsWithPredicate;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
* Created by Fox on 12/24/2016.
*/
public class CommandWho extends FCCommandBase {

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
AdvCmdParser.ParseResult parse = AdvCmdParser.builder()
.arguments(arguments)
.parse();

PluginManager manager = Sponge.getPluginManager();

if (parse.args.length == 0) {
source.sendMessage(Text.of(TextColors.GREEN, "Usage: ", TextColors.RESET, "/foxcore misc who <plugin>"));
} else {
String pluginId = parse.args[0];

Optional<PluginContainer> containerOpt = manager.getPlugin(pluginId);

if (containerOpt.isPresent()) {
PluginContainer container = containerOpt.get();
Text.Builder builder = Text.builder();
builder.append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n"));
builder.append(Text.of(TextColors.GREEN, "Id: ", TextColors.RESET, container.getId(), "\n"));
builder.append(Text.of(TextColors.GREEN, "Name: ", TextColors.RESET, container.getName()));

container.getVersion().ifPresent(s -> builder.append(Text.of(TextColors.GREEN, "\nVersion: ", TextColors.RESET, s)));

List<String> authors = container.getAuthors();
if (!authors.isEmpty()) {
if (authors.size() > 1) {
builder.append(Text.of(TextColors.AQUA, "\nAuthors: "));
} else {
builder.append(Text.of(TextColors.AQUA, "\nAuthor: "));
}
builder.append(Text.of(TextColors.RESET, colTS(authors)));
}

container.getDescription().ifPresent(s -> builder.append(Text.of(TextColors.AQUA, "\nDescription: ", TextColors.RESET, s)));
container.getUrl().ifPresent(s -> builder.append(Text.of(TextColors.AQUA, "\nURL: ", TextColors.RESET, s)));
container.getSource().ifPresent(p -> builder.append(Text.of(TextColors.LIGHT_PURPLE, "\nFile: ", TextColors.RESET, p.getFileName())));
container.getInstance().ifPresent(i -> builder.append(Text.of(TextColors.LIGHT_PURPLE, "\nMain Class: ", TextColors.RESET, i.getClass().getName())));

source.sendMessage(builder.build());
} else {
source.sendMessage(Text.of(TextColors.RED, "No plugin with this id: ", TextColors.RESET, pluginId));
}

}
return CommandResult.empty();
}

@Override
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException {
AdvCmdParser.ParseResult parse = AdvCmdParser.builder()
.arguments(arguments)
.autoCloseQuotes(true)
.excludeCurrent(true)
.parse();
PluginManager manager = Sponge.getPluginManager();

if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.ARGUMENT) {
if (parse.current.index == 0) {
return manager.getPlugins().stream()
.map(PluginContainer::getId)
.filter(new StartsWithPredicate(parse.current.token))
.map(args -> parse.current.prefix + args)
.collect(GuavaCollectors.toImmutableList());
}
} else if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.COMPLETE) {
return ImmutableList.of(parse.current.prefix + " ");
}
return ImmutableList.of();
}

@Override
public boolean testPermission(CommandSource source) {
return source.hasPermission("foxcore.command.misc.who");
}

@Override
public Optional<Text> getShortDescription(CommandSource source) {
return Optional.of(Text.of("Tells you information about any plugin."));
}

@Override
public Text getUsage(CommandSource source) {
return Text.of("<plugin>");
}

private String colTS(Collection col) {
String str = "";
Iterator it = col.iterator();
while (it.hasNext()) {
str += it.next().toString();
if (it.hasNext()) str += ", ";
}
return str;
}

}

0 comments on commit 69a65b4

Please sign in to comment.