Skip to content

Commit e5a830f

Browse files
author
Jasper Geurtz
committed
Simplify race/player defaults and extract hasValidId/MAX_VALID_ID
1 parent afe8c5e commit e5a830f

4 files changed

Lines changed: 28 additions & 35 deletions

File tree

‎src/main/java/bwapi/Bullet.java‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,13 @@ public boolean isVisible() {
181181
* @return true if the {@link Bullet} is visible to the specified player, false if the {@link Bullet} is not visible to the specified player.
182182
*/
183183
public boolean isVisible(Player player) {
184-
if (player == null) {
185-
player = game.self();
186-
}
184+
player = player == null ? game.self() : player;
187185
if (player != null) {
188-
final int id = player.getID();
189-
if (id >= 0 && id < 9) {
190-
return bulletData.isVisible(id);
191-
}
192-
} else {
193-
for (int i = 0; i < 9; ++i) {
194-
if (bulletData.isVisible(i)) {
195-
return true;
196-
}
186+
return player.hasValidId() && bulletData.isVisible(player.getID());
187+
}
188+
for (int i = 0; i < Player.MAX_VALID_ID; ++i) {
189+
if (bulletData.isVisible(i)) {
190+
return true;
197191
}
198192
}
199193
return false;

‎src/main/java/bwapi/Player.java‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* @see Race
2020
*/
2121
public class Player implements Comparable<Player> {
22+
static final int MAX_VALID_ID = 9;
23+
2224
private final PlayerData playerData;
2325
private final Game game;
2426
private final int id;
@@ -53,6 +55,10 @@ public int getID() {
5355
return id;
5456
}
5557

58+
boolean hasValidId() {
59+
return id >= 0 && id < MAX_VALID_ID;
60+
}
61+
5662
/**
5763
* Retrieves the name of the player.
5864
*
@@ -340,13 +346,8 @@ public int supplyTotal() {
340346
* @see #supplyUsed
341347
*/
342348
public int supplyTotal(Race race) {
343-
if (race == Race.None) {
344-
race = getRace();
345-
}
346-
if (race.id >= 0 && race.id < 3) {
347-
return playerData.getSupplyTotal(race.id);
348-
}
349-
return 0;
349+
race = race == Race.None ? getRace() : race;
350+
return race.hasValidId() ? playerData.getSupplyTotal(race.id) : 0;
350351
}
351352

352353
public int supplyUsed() {
@@ -361,10 +362,8 @@ public int supplyUsed() {
361362
* @see #supplyTotal
362363
*/
363364
public int supplyUsed(Race race) {
364-
if (race == Race.None) {
365-
race = getRace();
366-
}
367-
if (race.id < 0 || race.id >= 3) {
365+
race = race == Race.None ? getRace() : race;
366+
if (!race.hasValidId()) {
368367
return 0;
369368
}
370369
int supplyUsed = playerData.getSupplyUsed(race.id);

‎src/main/java/bwapi/Race.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,18 @@ public enum Race {
5858
Arrays.stream(Race.values()).forEach(v -> idToEnum[v.id] = v);
5959
}
6060

61+
static final int MAX_VALID_ID = 3;
62+
6163
public final int id;
6264

6365
Race(final int id) {
6466
this.id = id;
6567
}
6668

69+
boolean hasValidId() {
70+
return id >= 0 && id < MAX_VALID_ID;
71+
}
72+
6773
/**
6874
* Retrieves the default worker type for this {@link Race}.
6975
* <p>

‎src/main/java/bwapi/Unit.java‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,19 +2094,13 @@ public boolean isVisible() {
20942094
* @see #exists
20952095
*/
20962096
public boolean isVisible(Player player) {
2097-
if (player == null) {
2098-
player = game.self();
2099-
}
2097+
player = player == null ? game.self() : player;
21002098
if (player != null) {
2101-
final int id = player.getID();
2102-
if (id >= 0 && id < 9) {
2103-
return unitData.isVisible(id);
2104-
}
2105-
} else {
2106-
for (int i = 0; i < 9; ++i) {
2107-
if (unitData.isVisible(i)) {
2108-
return true;
2109-
}
2099+
return player.hasValidId() && unitData.isVisible(player.getID());
2100+
}
2101+
for (int i = 0; i < Player.MAX_VALID_ID; ++i) {
2102+
if (unitData.isVisible(i)) {
2103+
return true;
21102104
}
21112105
}
21122106
return false;

0 commit comments

Comments
 (0)