Skip to content

Commit

Permalink
Merge pull request #5682 from IllianiCBT/crewAssignmentBug
Browse files Browse the repository at this point in the history
Refactored Crew Assignment Event Handling
  • Loading branch information
Sleet01 authored Jan 9, 2025
2 parents 4d4c5e2 + 0d62b4d commit 00cc051
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 46 deletions.
41 changes: 38 additions & 3 deletions MekHQ/src/mekhq/campaign/event/PersonCrewAssignmentEvent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 The MegaMek Team. All rights reserved.
* Copyright (c) 2017-2025 The MegaMek Team. All rights reserved.
*
* This file is part of MekHQ.
*
Expand All @@ -18,22 +18,57 @@
*/
package mekhq.campaign.event;

import mekhq.campaign.Campaign;
import mekhq.campaign.force.Force;
import mekhq.campaign.personnel.Person;
import mekhq.campaign.unit.Unit;

import static mekhq.campaign.force.Force.FORCE_NONE;

/**
* Triggered when a person is assigned to or removed from a unit as pilot or crew.
* Represents an event triggered when a {@link Person} is assigned to or removed from a {@link Unit},
* either as a pilot or part of the crew.
*
* <p>This event is a subclass of {@link PersonChangedEvent} and adds the context of the
* {@link Unit} involved in the assignment or removal.</p>
*
* <p>If the {@link Unit} is associated with a force, the force's commander information will
* be updated accordingly through the {@link Force#updateCommander(Campaign)} method.</p>
*/
public class PersonCrewAssignmentEvent extends PersonChangedEvent {

private final Unit unit;

public PersonCrewAssignmentEvent(Person crew, Unit unit) {
/**
* Creates a new {@code PersonCrewAssignmentEvent}.
*
* @param campaign The {@link Campaign} to which this event belongs.
* @param crew The {@link Person} assigned or removed from the {@link Unit}.
* @param unit The {@link Unit} involved in the assignment or removal.
*
* <p>If the {@code unit} is associated with a force, the force's commander information is updated
* during the construction of this event by calling {@link Force#updateCommander(Campaign)}.</p>
*/
public PersonCrewAssignmentEvent(Campaign campaign, Person crew, Unit unit) {
super(crew);
this.unit = unit;

int forceId = unit.getForceId();

if (forceId != FORCE_NONE) {
Force force = campaign.getForce(forceId);

if (force != null) {
force.updateCommander(campaign);
}
}
}

/**
* Gets the {@link Unit} associated with this event.
*
* @return The {@link Unit} involved in the assignment or removal.
*/
public Unit getUnit() {
return unit;
}
Expand Down
86 changes: 43 additions & 43 deletions MekHQ/src/mekhq/campaign/unit/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Unit.java
*
* Copyright (c) 2009 Jay Lawson (jaylawson39 at yahoo.com). All rights reserved.
* Copyright (c) 2016-2024 The MegaMek Team. All Rights Reserved.
* Copyright (c) 2016-2025 The MegaMek Team. All Rights Reserved.
*
* This file is part of MekHQ.
*
Expand Down Expand Up @@ -4492,76 +4492,76 @@ public void addDriver(Person p) {
addDriver(p, false);
}

public void addDriver(Person p, boolean useTransfers) {
Objects.requireNonNull(p);
public void addDriver(Person person, boolean useTransfers) {
Objects.requireNonNull(person);

ensurePersonIsRegistered(p);
drivers.add(p);
p.setUnit(this);
ensurePersonIsRegistered(person);
drivers.add(person);
person.setUnit(this);
resetPilotAndEntity();
if (useTransfers) {
ServiceLogger.reassignedTo(p, getCampaign().getLocalDate(), getName());
ServiceLogger.reassignedTo(person, getCampaign().getLocalDate(), getName());
} else {
ServiceLogger.assignedTo(p, getCampaign().getLocalDate(), getName());
ServiceLogger.assignedTo(person, getCampaign().getLocalDate(), getName());
}
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(p, this));
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(campaign, person, this));
}

public void addGunner(Person p) {
addGunner(p, false);
}

public void addGunner(Person p, boolean useTransfers) {
Objects.requireNonNull(p);
public void addGunner(Person person, boolean useTransfers) {
Objects.requireNonNull(person);

ensurePersonIsRegistered(p);
gunners.add(p);
p.setUnit(this);
ensurePersonIsRegistered(person);
gunners.add(person);
person.setUnit(this);
resetPilotAndEntity();
if (useTransfers) {
ServiceLogger.reassignedTo(p, getCampaign().getLocalDate(), getName());
ServiceLogger.reassignedTo(person, getCampaign().getLocalDate(), getName());
} else {
ServiceLogger.assignedTo(p, getCampaign().getLocalDate(), getName());
ServiceLogger.assignedTo(person, getCampaign().getLocalDate(), getName());
}
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(p, this));
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(campaign, person, this));
}

public void addVesselCrew(Person p) {
addVesselCrew(p, false);
}

public void addVesselCrew(Person p, boolean useTransfers) {
Objects.requireNonNull(p);
public void addVesselCrew(Person person, boolean useTransfers) {
Objects.requireNonNull(person);

ensurePersonIsRegistered(p);
vesselCrew.add(p);
p.setUnit(this);
ensurePersonIsRegistered(person);
vesselCrew.add(person);
person.setUnit(this);
resetPilotAndEntity();
if (useTransfers) {
ServiceLogger.reassignedTo(p, getCampaign().getLocalDate(), getName());
ServiceLogger.reassignedTo(person, getCampaign().getLocalDate(), getName());
} else {
ServiceLogger.assignedTo(p, getCampaign().getLocalDate(), getName());
ServiceLogger.assignedTo(person, getCampaign().getLocalDate(), getName());
}
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(p, this));
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(campaign, person, this));
}

public void setNavigator(Person p) {
setNavigator(p, false);
}

public void setNavigator(Person p, boolean useTransfers) {
Objects.requireNonNull(p);
public void setNavigator(Person person, boolean useTransfers) {
Objects.requireNonNull(person);

ensurePersonIsRegistered(p);
navigator = p;
p.setUnit(this);
ensurePersonIsRegistered(person);
navigator = person;
person.setUnit(this);
resetPilotAndEntity();
if (useTransfers) {
ServiceLogger.reassignedTo(p, getCampaign().getLocalDate(), getName());
ServiceLogger.reassignedTo(person, getCampaign().getLocalDate(), getName());
} else {
ServiceLogger.assignedTo(p, getCampaign().getLocalDate(), getName());
ServiceLogger.assignedTo(person, getCampaign().getLocalDate(), getName());
}
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(p, this));
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(campaign, person, this));
}

public boolean isTechOfficer(@Nullable Person p) {
Expand All @@ -4572,19 +4572,19 @@ public void setTechOfficer(Person p) {
setTechOfficer(p, false);
}

public void setTechOfficer(Person p, boolean useTransfers) {
Objects.requireNonNull(p);
public void setTechOfficer(Person person, boolean useTransfers) {
Objects.requireNonNull(person);

ensurePersonIsRegistered(p);
techOfficer = p;
p.setUnit(this);
ensurePersonIsRegistered(person);
techOfficer = person;
person.setUnit(this);
resetPilotAndEntity();
if (useTransfers) {
ServiceLogger.reassignedTo(p, getCampaign().getLocalDate(), getName());
ServiceLogger.reassignedTo(person, getCampaign().getLocalDate(), getName());
} else {
ServiceLogger.assignedTo(p, getCampaign().getLocalDate(), getName());
ServiceLogger.assignedTo(person, getCampaign().getLocalDate(), getName());
}
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(p, this));
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(campaign, person, this));
}

public void setTech(Person p) {
Expand Down Expand Up @@ -4648,7 +4648,7 @@ public void addPilotOrSoldier(final Person person, final @Nullable Unit oldUnit,
ServiceLogger.addedToTOEForce(getCampaign(), person, getCampaign().getLocalDate(),
getCampaign().getForceFor(this));
}
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(person, this));
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(campaign, person, this));
}

/**
Expand Down Expand Up @@ -4681,7 +4681,7 @@ public void remove(final @Nullable Person person, final boolean log) {
engineer = null;
}
resetPilotAndEntity();
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(person, this));
MekHQ.triggerEvent(new PersonCrewAssignmentEvent(campaign, person, this));
}

if (log) {
Expand Down

0 comments on commit 00cc051

Please sign in to comment.