Skip to content

Commit 441bc1d

Browse files
MpcsNicholasBatesNZ
authored andcommitted
Add help command, remove some debug lines
1 parent 1d3ed2c commit 441bc1d

File tree

4 files changed

+87
-8
lines changed

4 files changed

+87
-8
lines changed

engine/src/main/java/org/destinationsol/game/console/commands/ChangeShipCommandHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
@RegisterCommands
3434
public class ChangeShipCommandHandler {
3535

36-
@Command
36+
@Command(shortDescription = "changes hero ship")
3737
public String changeShip(@Game SolGame game, @CommandParam(value = "newShip", suggester = HullConfigSuggester.class) HullConfig hullConfig) {
3838

3939
if (hullConfig == null) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2019 MovingBlocks
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.destinationsol.game.console.commands;
17+
18+
import com.google.common.collect.Ordering;
19+
import org.destinationsol.game.SolGame;
20+
import org.destinationsol.game.console.Console;
21+
import org.destinationsol.game.console.ConsoleColors;
22+
import org.destinationsol.game.console.ConsoleCommand;
23+
import org.destinationsol.game.console.annotations.Command;
24+
import org.destinationsol.game.console.annotations.CommandParam;
25+
import org.destinationsol.game.console.annotations.Game;
26+
import org.destinationsol.game.console.annotations.RegisterCommands;
27+
import org.destinationsol.game.console.suggesters.CommandNameSuggester;
28+
29+
import java.util.List;
30+
31+
@RegisterCommands
32+
public class HelpCommandHandler {
33+
@Command(shortDescription = "Prints out short descriptions for all available commands, or a longer help text if a command is provided.")
34+
public String help(@Game SolGame game, @CommandParam(value = "command", required = false, suggester = CommandNameSuggester.class) String commandName) {
35+
Console console = game.getScreens().consoleScreen.getConsole();
36+
if (commandName == null) {
37+
StringBuilder msg = new StringBuilder();
38+
// Get all commands, with appropriate sorting
39+
List<ConsoleCommand> commands = Ordering.natural().immutableSortedCopy(console.getCommands());
40+
41+
for (ConsoleCommand cmd : commands) {
42+
if (!msg.toString().isEmpty()) {
43+
msg.append(Console.NEW_LINE);
44+
}
45+
46+
msg.append(cmd.getUsage());
47+
msg.append(" - ");
48+
msg.append(cmd.getDescription());
49+
}
50+
51+
return msg.toString();
52+
} else {
53+
ConsoleCommand cmd = console.getCommand(commandName);
54+
if (cmd == null) {
55+
return "No help available for command '" + commandName + "'. Unknown command.";
56+
} else {
57+
StringBuilder msg = new StringBuilder();
58+
59+
msg.append("=====================================================================");
60+
msg.append(Console.NEW_LINE);
61+
msg.append(cmd.getUsage());
62+
msg.append(Console.NEW_LINE);
63+
msg.append("=====================================================================");
64+
msg.append(Console.NEW_LINE);
65+
if (!cmd.getHelpText().isEmpty()) {
66+
msg.append(cmd.getHelpText());
67+
msg.append(Console.NEW_LINE);
68+
msg.append("=====================================================================");
69+
msg.append(Console.NEW_LINE);
70+
} else if (!cmd.getDescription().isEmpty()) {
71+
msg.append(cmd.getDescription());
72+
msg.append(Console.NEW_LINE);
73+
msg.append("=====================================================================");
74+
msg.append(Console.NEW_LINE);
75+
}
76+
77+
return msg.toString();
78+
}
79+
}
80+
}
81+
}

engine/src/main/java/org/destinationsol/game/console/suggesters/CommandNameSuggester.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,10 @@
2828
*
2929
*/
3030
public final class CommandNameSuggester implements CommandParameterSuggester<String> {
31-
private final Console console;
32-
33-
public CommandNameSuggester(Console console) {
34-
this.console = console;
35-
}
3631

3732
@Override
3833
public Set<String> suggest(SolGame game, Object... resolvedParameters) {
39-
Collection<ConsoleCommand> commands = console.getCommands();
34+
Collection<ConsoleCommand> commands = game.getScreens().consoleScreen.getConsole().getCommands();
4035
Set<String> suggestions = Sets.newHashSetWithExpectedSize(commands.size());
4136

4237
for (ConsoleCommand command : commands) {

engine/src/main/java/org/destinationsol/game/screens/ConsoleScreen.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ public void onNewConsoleMessage(Message message) {
257257

258258
public void onCharEntered (char character) {
259259
if (isActive) {
260-
logger.info(character + " CHAR");
261260
if (character == '\t' && this.inputLine.length() > 0) {
262261
this.inputLine = new StringBuilder(this.completionEngine.complete(inputLine.toString()));
263262
} else if (character != '\t') {
@@ -283,4 +282,8 @@ public void onCharEntered (char character) {
283282
private float getLineY(float line) {
284283
return TOP_LEFT.y + 2 * FRAME_WIDTH + line * UiDrawer.FONT_SIZE * 0.5f * 1.8f;
285284
}
285+
286+
public Console getConsole() {
287+
return console;
288+
}
286289
}

0 commit comments

Comments
 (0)