Skip to content

Commit 69a65b4

Browse files
committed
Added Who and What commands.
1 parent b563c94 commit 69a65b4

File tree

4 files changed

+315
-1
lines changed

4 files changed

+315
-1
lines changed

src/main/java/net/foxdenstudio/sponge/foxcore/plugin/FoxCoreMain.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import net.foxdenstudio.sponge.foxcore.common.network.server.packet.ServerPrintStringPacket;
3131
import net.foxdenstudio.sponge.foxcore.plugin.command.*;
3232
import net.foxdenstudio.sponge.foxcore.plugin.command.misc.CommandPWD;
33+
import net.foxdenstudio.sponge.foxcore.plugin.command.misc.CommandWhat;
34+
import net.foxdenstudio.sponge.foxcore.plugin.command.misc.CommandWho;
3335
import net.foxdenstudio.sponge.foxcore.plugin.listener.WandBlockListener;
3436
import net.foxdenstudio.sponge.foxcore.plugin.listener.WandEntityListener;
3537
import net.foxdenstudio.sponge.foxcore.plugin.state.FCStateManager;
@@ -180,7 +182,9 @@ private void configureCommands() {
180182
fcDispatcher.register(new CommandAbout(builder.build()), "about", "info");
181183

182184
FCCommandDispatcher miscDispatcher = new FCCommandDispatcher("/foxcore misc", "Misc commands that may be helpful.");
183-
miscDispatcher.register(new CommandPWD(), "pwd", "directory");
185+
miscDispatcher.register(new CommandPWD(), "pwd", "directory", "dir");
186+
miscDispatcher.register(new CommandWho(), "who", "plugin");
187+
miscDispatcher.register(new CommandWhat(), "what", "command");
184188

185189
fcDispatcher.register(miscDispatcher, "misc", "miscellaneous", "util");
186190
}

src/main/java/net/foxdenstudio/sponge/foxcore/plugin/command/misc/CommandPWD.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.spongepowered.api.text.Text;
88
import org.spongepowered.api.text.format.TextColors;
99

10+
import java.util.Optional;
11+
1012
/**
1113
* Created by Fox on 11/5/2016.
1214
*/
@@ -22,4 +24,20 @@ public CommandResult process(CommandSource source, String arguments) throws Comm
2224
public boolean testPermission(CommandSource source) {
2325
return source.hasPermission("foxcore.command.misc.pwd");
2426
}
27+
28+
@Override
29+
public Optional<Text> getShortDescription(CommandSource source) {
30+
return Optional.of(Text.of("Prints the working directory of the game."));
31+
}
32+
33+
@Override
34+
public Optional<Text> getHelp(CommandSource source) {
35+
return Optional.of(Text.of("This command prints the directory from which the game was started.\n" +
36+
"On servers, this corresponds to the folder the server was started from, usually the folder with the server jar."));
37+
}
38+
39+
@Override
40+
public Text getUsage(CommandSource source) {
41+
return Text.of("/foxcore misc pwd");
42+
}
2543
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package net.foxdenstudio.sponge.foxcore.plugin.command.misc;
2+
3+
import com.google.common.collect.ImmutableList;
4+
import net.foxdenstudio.sponge.foxcore.plugin.command.FCCommandBase;
5+
import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParser;
6+
import org.spongepowered.api.Sponge;
7+
import org.spongepowered.api.command.*;
8+
import org.spongepowered.api.plugin.PluginContainer;
9+
import org.spongepowered.api.text.Text;
10+
import org.spongepowered.api.text.action.TextActions;
11+
import org.spongepowered.api.text.format.TextColors;
12+
import org.spongepowered.api.text.format.TextStyles;
13+
import org.spongepowered.api.world.Location;
14+
import org.spongepowered.api.world.World;
15+
16+
import javax.annotation.Nullable;
17+
import java.util.*;
18+
19+
/**
20+
* Created by Fox on 12/22/2016.
21+
*/
22+
public class CommandWhat extends FCCommandBase {
23+
24+
@Override
25+
public CommandResult process(CommandSource source, String arguments) throws CommandException {
26+
AdvCmdParser.ParseResult parse = AdvCmdParser.builder()
27+
.arguments(arguments)
28+
.parse();
29+
CommandManager manager = Sponge.getCommandManager();
30+
31+
if (parse.args.length == 0) {
32+
source.sendMessage(Text.of(TextColors.GREEN, "Usage: ", TextColors.RESET, "/foxcore misc what <command>"));
33+
} else {
34+
String commandName = parse.args[0];
35+
Set<? extends CommandMapping> mappings = manager.getAll(commandName);
36+
if (mappings.size() > 0) {
37+
Text.Builder builder = Text.builder();
38+
builder.append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n"));
39+
if (mappings.size() == 1) {
40+
CommandMapping mapping = mappings.iterator().next();
41+
42+
generateText(mapping, builder, source, manager);
43+
44+
source.sendMessage(builder.build());
45+
} else {
46+
Optional<? extends CommandMapping> primaryMappingOpt = manager.get(commandName);
47+
Set<? extends CommandMapping> secondaryMappings = new HashSet<>(mappings);
48+
if (primaryMappingOpt.isPresent()) {
49+
CommandMapping primaryMapping = primaryMappingOpt.get();
50+
secondaryMappings.remove(primaryMapping);
51+
52+
builder.append(Text.of(TextColors.GREEN, "------- Primary -------\n"));
53+
54+
generateText(primaryMapping, builder, source, manager);
55+
56+
builder.append(Text.of(TextColors.GREEN, "\n------- Secondary -------\n"));
57+
58+
Iterator<? extends CommandMapping> mappingIterator = secondaryMappings.iterator();
59+
60+
while (mappingIterator.hasNext()) {
61+
CommandMapping mapping = mappingIterator.next();
62+
generateText(mapping, builder, source, manager);
63+
if (mappingIterator.hasNext()) {
64+
builder.append(Text.of("\n\n"));
65+
}
66+
}
67+
} else {
68+
source.sendMessage(Text.of(TextColors.RED, "Something very strange happened. What the heck did you do?"));
69+
}
70+
}
71+
} else {
72+
source.sendMessage(Text.of(TextColors.RED, "No command with this name: ", TextColors.RESET, commandName));
73+
}
74+
}
75+
return CommandResult.empty();
76+
}
77+
78+
@Override
79+
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException {
80+
AdvCmdParser.ParseResult parse = AdvCmdParser.builder()
81+
.arguments(arguments)
82+
.autoCloseQuotes(true)
83+
.excludeCurrent(true)
84+
.parse();
85+
CommandManager manager = Sponge.getCommandManager();
86+
87+
if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.ARGUMENT) {
88+
if (parse.current.index == 0) {
89+
return manager.getSuggestions(source, parse.current.token, targetPosition);
90+
}
91+
} else if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.COMPLETE) {
92+
return ImmutableList.of(parse.current.prefix + " ");
93+
}
94+
return ImmutableList.of();
95+
}
96+
97+
@Override
98+
public boolean testPermission(CommandSource source) {
99+
return source.hasPermission("foxcore.command.misc.what");
100+
}
101+
102+
@Override
103+
public Optional<Text> getShortDescription(CommandSource source) {
104+
return Optional.of(Text.of("Tells you information about any command."));
105+
}
106+
107+
@Override
108+
public Text getUsage(CommandSource source) {
109+
return Text.of("<command>");
110+
}
111+
112+
private String colTS(Collection col) {
113+
String str = "";
114+
Iterator it = col.iterator();
115+
while (it.hasNext()) {
116+
str += it.next().toString();
117+
if (it.hasNext()) str += ", ";
118+
}
119+
return str;
120+
}
121+
122+
private void generateText(CommandMapping mapping, Text.Builder builder, CommandSource source, CommandManager manager) {
123+
String primaryAlias = mapping.getPrimaryAlias();
124+
builder.append(Text.of(TextColors.GOLD, "Primary Alias: ", TextColors.RESET, primaryAlias, "\n"));
125+
126+
Set<String> secondaryAliases = new HashSet<>(mapping.getAllAliases());
127+
secondaryAliases.remove(primaryAlias);
128+
129+
if (secondaryAliases.size() > 1) {
130+
builder.append(Text.of(TextColors.GREEN, "Secondary Aliases: "));
131+
} else {
132+
builder.append(Text.of(TextColors.GREEN, "Secondary Alias: "));
133+
}
134+
builder.append(Text.of(TextColors.RESET, colTS(secondaryAliases), "\n"));
135+
136+
CommandCallable callable = mapping.getCallable();
137+
138+
builder.append(Text.of(TextColors.AQUA, "Usage: "));
139+
builder.append(callable.getUsage(source));
140+
builder.append(Text.NEW_LINE);
141+
142+
Optional<Text> descriptionOpt = callable.getShortDescription(source);
143+
if (descriptionOpt.isPresent()) {
144+
Text description = descriptionOpt.get();
145+
builder.append(Text.of(TextColors.AQUA, "Description: "));
146+
builder.append(description);
147+
builder.append(Text.NEW_LINE);
148+
}
149+
150+
Optional<PluginContainer> containerOpt = manager.getOwner(mapping);
151+
152+
if (containerOpt.isPresent()) {
153+
PluginContainer container = containerOpt.get();
154+
String id = container.getId();
155+
builder.append(Text.of(
156+
TextActions.showText(Text.of("Click to show plugin details")),
157+
TextActions.runCommand("/foxcore misc who " + id),
158+
TextColors.LIGHT_PURPLE, "Plugin: ", TextColors.RESET, id)
159+
);
160+
} else {
161+
builder.append(Text.of(TextColors.LIGHT_PURPLE, "Plugin: ", TextColors.GRAY, TextStyles.ITALIC, "unknown"));
162+
}
163+
}
164+
165+
166+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package net.foxdenstudio.sponge.foxcore.plugin.command.misc;
2+
3+
import com.google.common.collect.ImmutableList;
4+
import net.foxdenstudio.sponge.foxcore.plugin.command.FCCommandBase;
5+
import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParser;
6+
import org.spongepowered.api.Sponge;
7+
import org.spongepowered.api.command.CommandException;
8+
import org.spongepowered.api.command.CommandResult;
9+
import org.spongepowered.api.command.CommandSource;
10+
import org.spongepowered.api.plugin.PluginContainer;
11+
import org.spongepowered.api.plugin.PluginManager;
12+
import org.spongepowered.api.text.Text;
13+
import org.spongepowered.api.text.format.TextColors;
14+
import org.spongepowered.api.util.GuavaCollectors;
15+
import org.spongepowered.api.util.StartsWithPredicate;
16+
import org.spongepowered.api.world.Location;
17+
import org.spongepowered.api.world.World;
18+
19+
import javax.annotation.Nullable;
20+
import java.util.Collection;
21+
import java.util.Iterator;
22+
import java.util.List;
23+
import java.util.Optional;
24+
25+
/**
26+
* Created by Fox on 12/24/2016.
27+
*/
28+
public class CommandWho extends FCCommandBase {
29+
30+
@Override
31+
public CommandResult process(CommandSource source, String arguments) throws CommandException {
32+
AdvCmdParser.ParseResult parse = AdvCmdParser.builder()
33+
.arguments(arguments)
34+
.parse();
35+
36+
PluginManager manager = Sponge.getPluginManager();
37+
38+
if (parse.args.length == 0) {
39+
source.sendMessage(Text.of(TextColors.GREEN, "Usage: ", TextColors.RESET, "/foxcore misc who <plugin>"));
40+
} else {
41+
String pluginId = parse.args[0];
42+
43+
Optional<PluginContainer> containerOpt = manager.getPlugin(pluginId);
44+
45+
if (containerOpt.isPresent()) {
46+
PluginContainer container = containerOpt.get();
47+
Text.Builder builder = Text.builder();
48+
builder.append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n"));
49+
builder.append(Text.of(TextColors.GREEN, "Id: ", TextColors.RESET, container.getId(), "\n"));
50+
builder.append(Text.of(TextColors.GREEN, "Name: ", TextColors.RESET, container.getName()));
51+
52+
container.getVersion().ifPresent(s -> builder.append(Text.of(TextColors.GREEN, "\nVersion: ", TextColors.RESET, s)));
53+
54+
List<String> authors = container.getAuthors();
55+
if (!authors.isEmpty()) {
56+
if (authors.size() > 1) {
57+
builder.append(Text.of(TextColors.AQUA, "\nAuthors: "));
58+
} else {
59+
builder.append(Text.of(TextColors.AQUA, "\nAuthor: "));
60+
}
61+
builder.append(Text.of(TextColors.RESET, colTS(authors)));
62+
}
63+
64+
container.getDescription().ifPresent(s -> builder.append(Text.of(TextColors.AQUA, "\nDescription: ", TextColors.RESET, s)));
65+
container.getUrl().ifPresent(s -> builder.append(Text.of(TextColors.AQUA, "\nURL: ", TextColors.RESET, s)));
66+
container.getSource().ifPresent(p -> builder.append(Text.of(TextColors.LIGHT_PURPLE, "\nFile: ", TextColors.RESET, p.getFileName())));
67+
container.getInstance().ifPresent(i -> builder.append(Text.of(TextColors.LIGHT_PURPLE, "\nMain Class: ", TextColors.RESET, i.getClass().getName())));
68+
69+
source.sendMessage(builder.build());
70+
} else {
71+
source.sendMessage(Text.of(TextColors.RED, "No plugin with this id: ", TextColors.RESET, pluginId));
72+
}
73+
74+
}
75+
return CommandResult.empty();
76+
}
77+
78+
@Override
79+
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException {
80+
AdvCmdParser.ParseResult parse = AdvCmdParser.builder()
81+
.arguments(arguments)
82+
.autoCloseQuotes(true)
83+
.excludeCurrent(true)
84+
.parse();
85+
PluginManager manager = Sponge.getPluginManager();
86+
87+
if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.ARGUMENT) {
88+
if (parse.current.index == 0) {
89+
return manager.getPlugins().stream()
90+
.map(PluginContainer::getId)
91+
.filter(new StartsWithPredicate(parse.current.token))
92+
.map(args -> parse.current.prefix + args)
93+
.collect(GuavaCollectors.toImmutableList());
94+
}
95+
} else if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.COMPLETE) {
96+
return ImmutableList.of(parse.current.prefix + " ");
97+
}
98+
return ImmutableList.of();
99+
}
100+
101+
@Override
102+
public boolean testPermission(CommandSource source) {
103+
return source.hasPermission("foxcore.command.misc.who");
104+
}
105+
106+
@Override
107+
public Optional<Text> getShortDescription(CommandSource source) {
108+
return Optional.of(Text.of("Tells you information about any plugin."));
109+
}
110+
111+
@Override
112+
public Text getUsage(CommandSource source) {
113+
return Text.of("<plugin>");
114+
}
115+
116+
private String colTS(Collection col) {
117+
String str = "";
118+
Iterator it = col.iterator();
119+
while (it.hasNext()) {
120+
str += it.next().toString();
121+
if (it.hasNext()) str += ", ";
122+
}
123+
return str;
124+
}
125+
126+
}

0 commit comments

Comments
 (0)