<commit_before>/*
 *
 * Copyright (c) 2000-2003 by <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */
package VASSAL.command;

import VASSAL.build.GameModule;
import VASSAL.build.module.GameState;
import VASSAL.build.module.GlobalOptions;
import VASSAL.build.module.map.HighlightLastMoved;
import VASSAL.counters.GamePiece;
import VASSAL.counters.Properties;

/**
 * This Command adds a {@link GamePiece} to a game.  Its undo
 * Command is {@link RemovePiece}.
 */
public class AddPiece extends Command {
  private Command undo;
  private final GamePiece target;
  private final String state;

  public AddPiece(GamePiece p) {
    this(p, p.getState());
  }

  public AddPiece(GamePiece p, String state) {
    target = p;
    this.state = state;
  }

  /**
   * Adds a piece by invoking {@link GameState#addPiece}, followed by
   * {@link GamePiece#setState}
   */
  @Override
  protected void executeCommand() {
    if (target != null) {
      GameModule.getGameModule().getGameState().addPiece(target);
      target.setState(state);
      if (target.getMap() != null) {
        HighlightLastMoved.setLastMoved(target);
        if (GlobalOptions.getInstance().centerOnOpponentsMove()
            && !Boolean.TRUE.equals(target.getProperty(Properties.INVISIBLE_TO_ME))) {
          target.getMap().ensureVisible(target.getMap().selectionBoundsOf(target));
          target.getMap().repaint();
        }
      }
    }
  }

  @Override
  protected Command myUndoCommand() {
    if (undo == null) {
      undo = new RemovePiece(target);
    }
    return undo;
  }

  public GamePiece getTarget() {
    return target;
  }

  public String getState() {
    return state;
  }
}
<commit_msg>10387 Do not center on opponents moves if moving units are invisible<commit_after>/*
 *
 * Copyright (c) 2000-2003 by <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */
package VASSAL.command;

import VASSAL.build.GameModule;
import VASSAL.build.module.GameState;
import VASSAL.build.module.GlobalOptions;
import VASSAL.build.module.map.HighlightLastMoved;
import VASSAL.counters.GamePiece;
import VASSAL.counters.Properties;
import VASSAL.counters.Stack;

/**
 * This Command adds a {@link GamePiece} to a game.  Its undo
 * Command is {@link RemovePiece}.
 */
public class AddPiece extends Command {
  private Command undo;
  private final GamePiece target;
  private final String state;

  public AddPiece(GamePiece p) {
    this(p, p.getState());
  }

  public AddPiece(GamePiece p, String state) {
    target = p;
    this.state = state;
  }

  /**
   * Adds a piece by invoking {@link GameState#addPiece}, followed by
   * {@link GamePiece#setState}
   */
  @Override
  protected void executeCommand() {
    if (target != null) {
      GameModule.getGameModule().getGameState().addPiece(target);
      target.setState(state);
      if (target.getMap() != null) {
        HighlightLastMoved.setLastMoved(target);
        if (GlobalOptions.getInstance().centerOnOpponentsMove()
            && !Boolean.TRUE.equals(target.getProperty(Properties.INVISIBLE_TO_ME))) {
          if (target instanceof Stack) {
            // Do not center on an empty Stack
            if (((Stack) target).getPieceCount() == 0) return;
            // Do not center on a Stack of invisible pieces
            int visibleCount = 0;
            for (final GamePiece gp : ((Stack) target).asList()) {
              if (!Boolean.TRUE.equals(gp.getProperty(Properties.INVISIBLE_TO_ME))) {
                visibleCount++;
                break;
              }
            }
            if (visibleCount == 0) return;
          }
          target.getMap().ensureVisible(target.getMap().selectionBoundsOf(target));
          target.getMap().repaint();
        }
      }
    }
  }

  @Override
  protected Command myUndoCommand() {
    if (undo == null) {
      undo = new RemovePiece(target);
    }
    return undo;
  }

  public GamePiece getTarget() {
    return target;
  }

  public String getState() {
    return state;
  }
}

{'fraction_non_alphanumeric': 0.10067895247332687, 'fraction_numerical': 0.004655674102812803, 'mean_word_length': 3.0344287949921753, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5391486', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1472, 'n_words': 473}