Skip to content

Commit 9982401

Browse files
MpcsNicholasBatesNZ
authored andcommitted
Commands can now return Message. Removed newLine field from Message, Message static functions for quick creation.
1 parent 441bc1d commit 9982401

File tree

9 files changed

+49
-77
lines changed

9 files changed

+49
-77
lines changed

engine/src/main/java/org/destinationsol/game/console/AbstractCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private Object[] processParametersMethod(List<String> rawParameters) throws Comm
213213
}
214214

215215
@Override
216-
public final String execute(List<String> rawParameters) throws CommandExecutionException {
216+
public final Object execute(List<String> rawParameters) throws CommandExecutionException {
217217
Object[] processedParameters;
218218

219219
try {
@@ -226,13 +226,13 @@ public final String execute(List<String> rawParameters) throws CommandExecutionE
226226
warning += ": " + message;
227227
}
228228

229-
return warning;
229+
return Message.WARN(warning);
230230
}
231231

232232
try {
233233
Object result = executionMethod.getAccessibleObject().invoke(executionMethod.getTarget(), processedParameters);
234234

235-
return result != null ? String.valueOf(result) : null;
235+
return result != null ? result : null;
236236
} catch (InvocationTargetException t) {
237237
if (t.getCause() != null) {
238238
throw new CommandExecutionException(t.getCause()); //Skip InvocationTargetException

engine/src/main/java/org/destinationsol/game/console/Console.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,6 @@ public interface Console {
5252
*/
5353
void addMessage(Message message);
5454

55-
/**
56-
* Adds a message to the console (as a CoreMessageType.CONSOLE message)
57-
*
58-
* @param message The message to be added, as a string.
59-
* @param newLine A boolean: True causes a newline character to be appended at the end of the message. False doesn't.
60-
*/
61-
void addMessage(String message, boolean newLine);
62-
63-
/**
64-
* Adds a message to the console (as a CoreMessageType.CONSOLE message)
65-
*
66-
* @param message The message to be added, as a string.
67-
* @param type The type of the message
68-
* @param newLine A boolean: True causes a newline character to be appended at the end of the message. False doesn't.
69-
*/
70-
void addMessage(String message, MessageType type, boolean newLine);
71-
7255
/**
7356
* @return An iterator over all messages in the console
7457
*/

engine/src/main/java/org/destinationsol/game/console/ConsoleCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public int compare(ConsoleCommand o1, ConsoleCommand o2) {
9595
* @param parameters Parameters in an Object[] array as defined in {@link AbstractCommand#getCommandParameters()}.
9696
* @return A reply to the sender. TODO
9797
*/
98-
String execute(List<String> parameters) throws CommandExecutionException;
98+
Object execute(List<String> parameters) throws CommandExecutionException;
9999

100100
/**
101101
* Suggests valid parameters.

engine/src/main/java/org/destinationsol/game/console/ConsoleImpl.java

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.collect.Maps;
2323
import com.google.common.collect.Sets;
2424
import org.destinationsol.game.SolGame;
25+
import org.destinationsol.game.console.annotations.Command;
2526
import org.destinationsol.game.console.annotations.RegisterCommands;
2627
import org.destinationsol.game.console.exceptions.CommandExecutionException;
2728
import org.destinationsol.game.context.Context;
@@ -155,24 +156,7 @@ private void addErrorMessage(String message) {
155156
* Adds a message to the console
156157
*
157158
* @param message The message to be added, as a string.
158-
* @param newLine A boolean: True causes a newline character to be appended at the end of the message. False doesn't.
159159
*/
160-
@Override
161-
public void addMessage(String message, boolean newLine) {
162-
addMessage(new Message(message, newLine));
163-
}
164-
165-
/**
166-
* Adds a message to the console
167-
*
168-
* @param message The message to be added, as a string.
169-
* @param type The type of the message
170-
* @param newLine A boolean: True causes a newline character to be appended at the end of the message. False doesn't.
171-
*/
172-
@Override
173-
public void addMessage(String message, MessageType type, boolean newLine) {
174-
addMessage(new Message(message, type, newLine));
175-
}
176160

177161
/**
178162
* Adds a message to the console
@@ -189,7 +173,7 @@ public void addMessage(Message message) {
189173
//check for newlines and split into submessages
190174
if (message.getMessage().indexOf(NEW_LINE) > 0) {
191175
for (String line : message.getMessage().split(NEW_LINE)) {
192-
newlinedMessages.add(new Message(line, message.getType(), false));
176+
newlinedMessages.add(new Message(line, message.getType()));
193177
}
194178
} else {
195179
newlinedMessages.add(message);
@@ -206,13 +190,13 @@ public void addMessage(Message message) {
206190
messageWidth += charWidth;
207191
fittingMessageText.append(c);
208192
if (messageWidth > MAX_WIDTH_OF_LINE) {
209-
Message newMessage = new Message(fittingMessageText.toString(), message.getType(), message.hasNewLine());
193+
Message newMessage = new Message(fittingMessageText.toString(), message.getType());
210194
messageHistory.add(newMessage);
211195
fittingMessageText = new StringBuilder();
212196
messageWidth = 0;
213197
}
214198
}
215-
Message newMessage = new Message(fittingMessageText.toString(), message.getType(), message.hasNewLine());
199+
Message newMessage = new Message(fittingMessageText.toString(), message.getType());
216200
messageHistory.add(newMessage);
217201
}
218202

@@ -313,8 +297,16 @@ public boolean execute(String commandName, List<String> params) {
313297
}
314298

315299
try {
316-
String result = cmd.execute(params);
317-
this.addMessage(result);
300+
Object result = cmd.execute(params);
301+
if (result == null) {
302+
return true;
303+
}
304+
if (result instanceof Message) {
305+
Message message = (Message)result;
306+
this.addMessage(message.getMessage(), message.getType());
307+
} else if (result instanceof String) {
308+
this.addMessage(result.toString());
309+
}
318310
} catch (CommandExecutionException e) {
319311
e.printStackTrace();
320312
}

engine/src/main/java/org/destinationsol/game/console/Message.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,15 @@ public class Message {
1919

2020
private final MessageType type;
2121
private final String message;
22-
private final boolean newLine;
2322

2423
public Message(String message) {
2524
this.message = message;
2625
this.type = CoreMessageType.INFO;
27-
this.newLine = true;
2826
}
2927

3028
public Message(String message, MessageType type) {
3129
this.message = message;
3230
this.type = type;
33-
this.newLine = true;
34-
}
35-
36-
public Message(String message, MessageType type, boolean newLine) {
37-
this.message = message;
38-
this.type = type;
39-
this.newLine = newLine;
40-
}
41-
42-
public Message(String message, boolean newLine) {
43-
this.message = message;
44-
this.type = CoreMessageType.INFO;
45-
this.newLine = newLine;
4631
}
4732

4833
public String getMessage() {
@@ -53,12 +38,20 @@ public MessageType getType() {
5338
return type;
5439
}
5540

56-
public boolean hasNewLine() {
57-
return newLine;
58-
}
59-
6041
@Override
6142
public String toString() {
6243
return String.format("[%s] '%s'", type, message);
6344
}
45+
46+
public static Message SUCCESS(String status) {
47+
return new Message(status, CoreMessageType.INFO);
48+
}
49+
50+
public static Message FAILURE(String status) {
51+
return new Message(status, CoreMessageType.ERROR);
52+
}
53+
54+
public static Message WARN(String status) {
55+
return new Message(status, CoreMessageType.WARN);
56+
}
6457
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.destinationsol.game.Hero;
1919
import org.destinationsol.game.SolGame;
20+
import org.destinationsol.game.console.Message;
2021
import org.destinationsol.game.console.annotations.Command;
2122
import org.destinationsol.game.console.annotations.CommandParam;
2223
import org.destinationsol.game.console.annotations.Game;
@@ -34,10 +35,10 @@
3435
public class ChangeShipCommandHandler {
3536

3637
@Command(shortDescription = "changes hero ship")
37-
public String changeShip(@Game SolGame game, @CommandParam(value = "newShip", suggester = HullConfigSuggester.class) HullConfig hullConfig) {
38+
public Message changeShip(@Game SolGame game, @CommandParam(value = "newShip", suggester = HullConfigSuggester.class) HullConfig hullConfig) {
3839

3940
if (hullConfig == null) {
40-
return "Could not find such ship";
41+
return Message.FAILURE("Could not find such ship");
4142
}
4243

4344
Hero hero = game.getHero();
@@ -47,7 +48,7 @@ public String changeShip(@Game SolGame game, @CommandParam(value = "newShip", su
4748
game.getObjectManager().removeObjDelayed(hero.getShip());
4849
game.getObjectManager().addObjDelayed(newShip);
4950
hero.setSolShip(newShip, game);
50-
return "Ship changed";
51+
return Message.SUCCESS("Ship changed");
5152
}
5253

5354

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.destinationsol.game.DmgType;
1919
import org.destinationsol.game.Hero;
2020
import org.destinationsol.game.SolGame;
21+
import org.destinationsol.game.console.Message;
2122
import org.destinationsol.game.console.annotations.Command;
2223
import org.destinationsol.game.console.annotations.Game;
2324
import org.destinationsol.game.console.annotations.RegisterCommands;
@@ -29,15 +30,15 @@
2930
public class DieCommandHandler {
3031

3132
@Command(shortDescription = "Kills the hero")
32-
public String die(@Game SolGame game) {
33+
public Message die(@Game SolGame game) {
3334
Hero hero = game.getHero();
3435
if (hero.isTranscendent()) {
35-
return "Cannot kill hero when transcendent!";
36+
return Message.FAILURE("Cannot kill hero when transcendent!");
3637
}
3738
if (!hero.isAlive()) {
38-
return "Hero is already dead!";
39+
return Message.FAILURE("Hero is already dead!");
3940
}
4041
hero.getShip().receivePiercingDmg(hero.getHull().getHullConfig().getMaxLife() + 1f, game, hero.getPosition(), DmgType.CRASH);
41-
return "Hero killed!";
42+
return Message.SUCCESS("Hero killed!");
4243
}
4344
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.destinationsol.game.Hero;
1919
import org.destinationsol.game.SolGame;
20+
import org.destinationsol.game.console.Message;
2021
import org.destinationsol.game.console.annotations.Command;
2122
import org.destinationsol.game.console.annotations.Game;
2223
import org.destinationsol.game.console.annotations.RegisterCommands;
@@ -28,20 +29,20 @@
2829
public class InvincibleCommandHandler {
2930

3031
@Command(shortDescription = "Makes the hero invincible")
31-
public String godMode(@Game SolGame game) {
32+
public Message godMode(@Game SolGame game) {
3233
Hero hero = game.getHero();
3334
if (hero.isDead()) {
34-
return "Cannot make invincible when dead";
35+
return Message.FAILURE("Cannot make invincible when dead");
3536
}
3637
if (hero.isTranscendent()) {
37-
return "Cannot make invincible when transdencent";
38+
return Message.FAILURE("Cannot make invincible when transdencent");
3839
}
3940
if (!hero.isInvincible()) {
4041
hero.setInvincible(true);
41-
return "Set player as invincible";
42+
return Message.SUCCESS("Set player as invincible");
4243
} else {
4344
hero.setInvincible(false);
44-
return "Set player as non invincible";
45+
return Message.SUCCESS("Set player as non invincible");
4546
}
4647
}
4748
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.destinationsol.game.Hero;
1919
import org.destinationsol.game.SolGame;
20+
import org.destinationsol.game.console.Message;
2021
import org.destinationsol.game.console.annotations.Command;
2122
import org.destinationsol.game.console.annotations.Game;
2223
import org.destinationsol.game.console.annotations.RegisterCommands;
@@ -28,12 +29,12 @@
2829
public class RespawnCommandHandler {
2930

3031
@Command(shortDescription = "Respawns player if dead")
31-
public String respawn(@Game SolGame game) {
32+
public Message respawn(@Game SolGame game) {
3233
Hero hero = game.getHero();
3334
if (hero.isAlive()) {
34-
return "Cannot respawn hero when not dead!";
35+
return Message.FAILURE("Cannot respawn hero when not dead!");
3536
}
3637
game.respawn();
37-
return "Hero respawned!";
38+
return Message.SUCCESS("Hero respawned!");
3839
}
3940
}

0 commit comments

Comments
 (0)