Skip to content

Commit 171cd79

Browse files
MpcsNicholasBatesNZ
authored andcommitted
Command now can return only String or void. Commands can now throw CommandExecutionErrors, displayed in red in console
1 parent 021b7c2 commit 171cd79

File tree

8 files changed

+45
-36
lines changed

8 files changed

+45
-36
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 Object execute(List<String> rawParameters) throws CommandExecutionException {
216+
public final String execute(List<String> rawParameters) throws CommandExecutionException {
217217
Object[] processedParameters;
218218

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

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

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

235-
return result != null ? result : null;
235+
return result != null ? String.valueOf(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/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-
Object execute(List<String> parameters) throws CommandExecutionException;
98+
String execute(List<String> parameters) throws CommandExecutionException;
9999

100100
/**
101101
* Suggests valid parameters.

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.destinationsol.game.console;
1717

1818
import com.badlogic.gdx.graphics.g2d.BitmapFont;
19+
import com.google.common.base.Strings;
1920
import com.google.common.collect.Collections2;
2021
import com.google.common.collect.ImmutableList;
2122
import com.google.common.collect.Lists;
@@ -297,21 +298,30 @@ public boolean execute(String commandName, List<String> params) {
297298
}
298299

299300
try {
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());
301+
String result = cmd.execute(params);
302+
if (result != null) {
303+
this.addMessage(result);
309304
}
305+
return true;
310306
} catch (CommandExecutionException e) {
311-
e.printStackTrace();
312-
}
307+
Throwable cause = e.getCause();
308+
String causeMessage;
309+
if (cause != null) {
310+
causeMessage = cause.getLocalizedMessage();
311+
if (Strings.isNullOrEmpty(causeMessage)) {
312+
causeMessage = cause.toString();
313+
}
314+
} else {
315+
causeMessage = e.getLocalizedMessage();
316+
}
313317

314-
return true;
318+
logger.error("An error occurred while executing a command '" + cmd.getName() + "' : " + e.getMessage(), CoreMessageType.ERROR);
319+
320+
if (!Strings.isNullOrEmpty(causeMessage)) {
321+
this.addMessage(new Message("Error: " + causeMessage));
322+
}
323+
return false;
324+
}
315325
}
316326

317327
@Override

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

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

1818
import org.destinationsol.game.Hero;
1919
import org.destinationsol.game.SolGame;
20-
import org.destinationsol.game.console.Message;
2120
import org.destinationsol.game.console.annotations.Command;
2221
import org.destinationsol.game.console.annotations.CommandParam;
2322
import org.destinationsol.game.console.annotations.Game;
2423
import org.destinationsol.game.console.annotations.RegisterCommands;
24+
import org.destinationsol.game.console.exceptions.CommandExecutionException;
2525
import org.destinationsol.game.console.suggesters.HullConfigSuggester;
2626
import org.destinationsol.game.ship.ShipRepairer;
2727
import org.destinationsol.game.ship.SolShip;
@@ -35,10 +35,10 @@
3535
public class ChangeShipCommandHandler {
3636

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

4040
if (hullConfig == null) {
41-
return Message.FAILURE("Could not find such ship");
41+
throw new CommandExecutionException("Could not find such ship");
4242
}
4343

4444
Hero hero = game.getHero();
@@ -48,7 +48,7 @@ public Message changeShip(@Game SolGame game, @CommandParam(value = "newShip", s
4848
game.getObjectManager().removeObjDelayed(hero.getShip());
4949
game.getObjectManager().addObjDelayed(newShip);
5050
hero.setSolShip(newShip, game);
51-
return Message.SUCCESS("Ship changed");
51+
return "Ship changed";
5252
}
5353

5454

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
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;
2221
import org.destinationsol.game.console.annotations.Command;
2322
import org.destinationsol.game.console.annotations.Game;
2423
import org.destinationsol.game.console.annotations.RegisterCommands;
24+
import org.destinationsol.game.console.exceptions.CommandExecutionException;
2525

2626
/**
2727
* A command used to instantly destroy the hero's ship, mostly for debugging purposes.
@@ -30,15 +30,15 @@
3030
public class DieCommandHandler {
3131

3232
@Command(shortDescription = "Kills the hero")
33-
public Message die(@Game SolGame game) {
33+
public String die(@Game SolGame game) throws CommandExecutionException {
3434
Hero hero = game.getHero();
3535
if (hero.isTranscendent()) {
36-
return Message.FAILURE("Cannot kill hero when transcendent!");
36+
throw new CommandExecutionException("Cannot kill hero when transcendent!");
3737
}
3838
if (!hero.isAlive()) {
39-
return Message.FAILURE("Hero is already dead!");
39+
throw new CommandExecutionException("Hero is already dead!");
4040
}
4141
hero.getShip().receivePiercingDmg(hero.getHull().getHullConfig().getMaxLife() + 1f, game, hero.getPosition(), DmgType.CRASH);
42-
return Message.SUCCESS("Hero killed!");
42+
return "Hero killed!";
4343
}
4444
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.google.common.collect.Ordering;
1919
import org.destinationsol.game.SolGame;
2020
import org.destinationsol.game.console.Console;
21-
import org.destinationsol.game.console.ConsoleColors;
2221
import org.destinationsol.game.console.ConsoleCommand;
2322
import org.destinationsol.game.console.annotations.Command;
2423
import org.destinationsol.game.console.annotations.CommandParam;

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

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

1818
import org.destinationsol.game.Hero;
1919
import org.destinationsol.game.SolGame;
20-
import org.destinationsol.game.console.Message;
2120
import org.destinationsol.game.console.annotations.Command;
2221
import org.destinationsol.game.console.annotations.Game;
2322
import org.destinationsol.game.console.annotations.RegisterCommands;
23+
import org.destinationsol.game.console.exceptions.CommandExecutionException;
2424

2525
/**
2626
* A command used to make player invincible
@@ -29,20 +29,20 @@
2929
public class InvincibleCommandHandler {
3030

3131
@Command(shortDescription = "Makes the hero invincible")
32-
public Message godMode(@Game SolGame game) {
32+
public String godMode(@Game SolGame game) throws CommandExecutionException {
3333
Hero hero = game.getHero();
3434
if (hero.isDead()) {
35-
return Message.FAILURE("Cannot make invincible when dead");
35+
throw new IllegalArgumentException("Cannot make invincible when dead");
3636
}
3737
if (hero.isTranscendent()) {
38-
return Message.FAILURE("Cannot make invincible when transdencent");
38+
throw new IllegalArgumentException("Cannot make invincible when transdencent");
3939
}
4040
if (!hero.isInvincible()) {
4141
hero.setInvincible(true);
42-
return Message.SUCCESS("Set player as invincible");
42+
return "Set player as invincible";
4343
} else {
4444
hero.setInvincible(false);
45-
return Message.SUCCESS("Set player as non invincible");
45+
return "Set player as non invincible";
4646
}
4747
}
4848
}

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

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

1818
import org.destinationsol.game.Hero;
1919
import org.destinationsol.game.SolGame;
20-
import org.destinationsol.game.console.Message;
2120
import org.destinationsol.game.console.annotations.Command;
2221
import org.destinationsol.game.console.annotations.Game;
2322
import org.destinationsol.game.console.annotations.RegisterCommands;
23+
import org.destinationsol.game.console.exceptions.CommandExecutionException;
2424

2525
/**
2626
* A command used to respawn player's ship
@@ -29,12 +29,12 @@
2929
public class RespawnCommandHandler {
3030

3131
@Command(shortDescription = "Respawns player if dead")
32-
public Message respawn(@Game SolGame game) {
32+
public String respawn(@Game SolGame game) throws CommandExecutionException {
3333
Hero hero = game.getHero();
3434
if (hero.isAlive()) {
35-
return Message.FAILURE("Cannot respawn hero when not dead!");
35+
throw new CommandExecutionException("Cannot respawn hero when not dead!");
3636
}
3737
game.respawn();
38-
return Message.SUCCESS("Hero respawned!");
38+
return "Hero respawned!";
3939
}
4040
}

0 commit comments

Comments
 (0)