Skip to content

Commit

Permalink
Some no-op code cleanups. (#12561)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvitkine committed May 4, 2024
1 parent db0a00b commit 3eb8bf0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class ServerGame extends AbstractGame {

private final RandomStats randomStats;
private IRandomSource randomSource = new PlainRandomSource();
private IRandomSource delegateRandomSource;
private @Nullable IRandomSource delegateRandomSource;
private final DelegateExecutionManager delegateExecutionManager = new DelegateExecutionManager();
@Nullable @Getter private final InGameLobbyWatcherWrapper inGameLobbyWatcher;
private boolean needToInitialize = true;
Expand Down Expand Up @@ -562,8 +562,7 @@ private void startStep(final boolean stepIsRestoredFromSavedGame) {
// do any initialization of game data for all players here (not based on a delegate, and should
// not be)
// we cannot do this the very first run through, because there are no history nodes yet. We
// should do after first
// node is created.
// should do after first node is created.
if (needToInitialize) {
addPlayerTypesToGameData(gamePlayers.values(), playerManager, bridge);
}
Expand Down Expand Up @@ -613,14 +612,21 @@ private void notifyGameStepChanged(final boolean loadedFromSavedGame) {
.stepChanged(stepName, delegateName, gamePlayer, round, displayName, loadedFromSavedGame);
}

private String isOrAre(String playerName) {
if (playerName.endsWith("s") || playerName.endsWith("ese") || playerName.endsWith("ish")) {
return "are";
} else {
return "is";
}
}

private void addPlayerTypesToGameData(
final Collection<Player> localPlayers,
final PlayerManager allPlayers,
final IDelegateBridge bridge) {
final GameData data = bridge.getData();
// potential bugs with adding changes to a game that has not yet started and has no history
// nodes yet. So wait for
// the first delegate to start before making changes.
// nodes yet. So wait for the first delegate to start before making changes.
if (getCurrentStep() == null || getCurrentStep().getPlayerId() == null || firstRun) {
firstRun = false;
return;
Expand All @@ -635,21 +641,16 @@ private void addPlayerTypesToGameData(
final CompositeChange change = new CompositeChange();
final Set<String> allPlayersString = allPlayers.getPlayers();
bridge.getHistoryWriter().startEvent("Game Loaded");
final var historyWriter = bridge.getHistoryWriter();
for (final Player player : localPlayers) {
allPlayersString.remove(player.getName());
final boolean isAi = player.isAi();
bridge
.getHistoryWriter()
.addChildToEvent(
player.getName()
+ ((player.getName().endsWith("s")
|| player.getName().endsWith("ese")
|| player.getName().endsWith("ish"))
? " are"
: " is")
+ " now being played by: "
+ player.getPlayerLabel());
historyWriter.addChildToEvent(
String.format(
"%s %s now being played by: %s",
player.getName(), isOrAre(player.getName()), player.getPlayerLabel()));

final GamePlayer p = data.getPlayerList().getPlayerId(player.getName());
final boolean isAi = player.isAi();
final String newWhoAmI = (isAi ? "AI" : "Human") + ":" + player.getPlayerLabel();
if (!p.getWhoAmI().equals(newWhoAmI)) {
change.add(ChangeFactory.changePlayerWhoAmIChange(p, newWhoAmI));
Expand All @@ -659,14 +660,8 @@ private void addPlayerTypesToGameData(
while (playerIter.hasNext()) {
final String player = playerIter.next();
playerIter.remove();
bridge
.getHistoryWriter()
.addChildToEvent(
player
+ ((player.endsWith("s") || player.endsWith("ese") || player.endsWith("ish"))
? " are"
: " is")
+ " now being played by: Human:Client");
historyWriter.addChildToEvent(
String.format("%s %s now being played by: Human:Client", player, isOrAre(player)));
final GamePlayer p = data.getPlayerList().getPlayerId(player);
final String newWhoAmI = "Human:Client";
if (!p.getWhoAmI().equals(newWhoAmI)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,18 @@ public class MapPanel extends ImageScrollerLargeView {
private final TerritoryHighlighter territoryHighlighter = new TerritoryHighlighter();
private final ImageScrollerSmallView smallView;
// units the mouse is currently over
private Tuple<Territory, List<Unit>> currentUnits;
private @Nullable Tuple<Territory, List<Unit>> currentUnits;
private final SmallMapImageManager smallMapImageManager;
private RouteDescription routeDescription;
private @Nullable RouteDescription routeDescription;
private final TileManager tileManager;
private BufferedImage mouseShadowImage = null;
private @Nullable BufferedImage mouseShadowImage = null;
private String movementLeftForCurrentUnits = "";
private ResourceCollection movementFuelCost;
@Getter private final UiContext uiContext;
private final ExecutorService executor =
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
@Getter private Collection<Collection<Unit>> highlightedUnits = List.of();
private Cursor hiddenCursor = null;
private @Nullable Cursor hiddenCursor = null;
private final MapRouteDrawer routeDrawer;
private Set<Territory> countriesToUpdate = new HashSet<>();
private final Object countriesToUpdateLock = new Object();
Expand Down Expand Up @@ -234,7 +234,7 @@ public void mouseReleased(final MouseEvent e) {
if (!unitSelectionListeners.isEmpty()) {
Tuple<Territory, List<Unit>> tuple = tileManager.getUnitsAtPoint(x, y, gameData);
if (tuple == null) {
tuple = Tuple.of(getTerritory(x, y), List.of());
tuple = Tuple.of(terr, List.of());
}
notifyUnitSelected(tuple.getSecond(), tuple.getFirst(), md);
}
Expand Down Expand Up @@ -457,7 +457,10 @@ public void setRoute(final Route route) {

/** Set the route, could be null. */
public void setRoute(
final Route route, final Point start, final Point end, final Image cursorImage) {
final @Nullable Route route,
final @Nullable Point start,
final @Nullable Point end,
final @Nullable Image cursorImage) {
if (route == null) {
routeDescription = null;
SwingUtilities.invokeLater(this::repaint);
Expand Down Expand Up @@ -584,7 +587,7 @@ private MouseDetails convert(final MouseEvent me) {
return new MouseDetails(me, x, y);
}

private boolean unitsChanged(final Tuple<Territory, List<Unit>> newUnits) {
private boolean unitsChanged(final @Nullable Tuple<Territory, List<Unit>> newUnits) {
return !ObjectUtils.referenceEquals(newUnits, currentUnits)
&& (newUnits == null
|| currentUnits == null
Expand Down Expand Up @@ -737,17 +740,16 @@ public void paint(final Graphics g) {
movementFuelCost,
uiContext.getResourceImageFactory());
}
final var unitImageFactory = uiContext.getUnitImageFactory();
for (final Collection<Unit> value : highlightedUnits) {
for (final UnitCategory category : UnitSeparator.categorize(value)) {
final Rectangle r = tileManager.getUnitRect(category.getUnits(), gameData);
final @Nullable Rectangle r = tileManager.getUnitRect(category.getUnits(), gameData);
if (r == null) {
continue;
}

Image image =
uiContext
.getUnitImageFactory()
.getHighlightImage(UnitImageFactory.ImageKey.of(category));
final Image image =
unitImageFactory.getHighlightImage(UnitImageFactory.ImageKey.of(category));
final AffineTransform transform =
AffineTransform.getTranslateInstance(
normalizeX(r.getX() - getXOffset()), normalizeY(r.getY() - getYOffset()));
Expand Down Expand Up @@ -869,7 +871,7 @@ public void changeSmallMapOffscreenMap() {
smallMapImageManager.updateOffscreenImage(uiContext.getMapImage().getSmallMapImage());
}

public void setMouseShadowUnits(final Collection<Unit> units) {
public void setMouseShadowUnits(final @Nullable Collection<Unit> units) {
if (units == null || units.isEmpty()) {
movementLeftForCurrentUnits = "";
mouseShadowImage = null;
Expand Down Expand Up @@ -975,12 +977,8 @@ public void clearTerritoryOverlay(final Territory territory) {

public void hideMouseCursor() {
if (hiddenCursor == null) {
hiddenCursor =
getToolkit()
.createCustomCursor(
new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR),
new Point(0, 0),
"Hidden");
final var image = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
hiddenCursor = getToolkit().createCustomCursor(image, new Point(), "Hidden");
}
setCursor(hiddenCursor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public class MovePanel extends AbstractMovePanel {
private final Map<Unit, Collection<Unit>> airTransportDependents = new HashMap<>();

// access only through getter and setter!
private Territory firstSelectedTerritory;
private Territory selectedEndpointTerritory;
@Getter private @Nullable Territory firstSelectedTerritory;
@Getter @Setter private @Nullable Territory selectedEndpointTerritory;
private Territory mouseCurrentTerritory;
private List<Territory> forced;
private @Nullable List<Territory> forced;
@Setter private boolean nonCombat;
private Point mouseSelectedPoint;
private Point mouseCurrentPoint;
Expand All @@ -99,15 +99,15 @@ public class MovePanel extends AbstractMovePanel {
// the must move with details for the currently selected territory
// note this is kept in sync because we do not modify selectedTerritory directly
// instead we only do so through the private setter
private MustMoveWithDetails mustMoveWithDetails = null;
private @Nullable MustMoveWithDetails mustMoveWithDetails = null;
// cache this so we can update it only when territory/units change
private Collection<Unit> unitsThatCanMoveOnRoute;
private @Nullable Image currentCursorImage;
private final @Nullable Image warningImage;
private final @Nullable Image errorImage;
private Route routeCached = null;
private @Nullable Route routeCached = null;
private String displayText = "Combat Move";
private MoveType moveType = MoveType.DEFAULT;
@Setter private MoveType moveType = MoveType.DEFAULT;
private final UnitScroller unitScroller;

@Getter(onMethod_ = @Override)
Expand Down Expand Up @@ -695,10 +695,6 @@ public MovePanel(final GameData data, final MapPanel map, final TripleAFrame fra
registerKeyBindings(frame);
}

public void setMoveType(final MoveType moveType) {
this.moveType = moveType;
}

private GamePlayer getUnitOwner(final Collection<Unit> units) {
return (isEditMode() && units != null && !units.isEmpty())
? CollectionUtils.getAny(units).getOwner()
Expand Down Expand Up @@ -1384,18 +1380,6 @@ final void setFirstSelectedTerritory(final @Nullable Territory firstSelectedTerr
}
}

private Territory getFirstSelectedTerritory() {
return firstSelectedTerritory;
}

final void setSelectedEndpointTerritory(final Territory selectedEndpointTerritory) {
this.selectedEndpointTerritory = selectedEndpointTerritory;
}

private Territory getSelectedEndpointTerritory() {
return selectedEndpointTerritory;
}

private List<Unit> userChooseUnits(
final Set<Unit> defaultSelections,
final Predicate<Collection<Unit>> unitsToLoadMatch,
Expand Down

0 comments on commit 3eb8bf0

Please sign in to comment.