8000 Fix: Fixed Ammo Not Stacking in the Warehouse by IllianiBird · Pull Request #6816 · MegaMek/mekhq · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix: Fixed Ammo Not Stacking in the Warehouse #6816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions MekHQ/src/mekhq/campaign/Quartermaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
*
* Catalyst Game Labs and the Catalyst Game Labs logo are trademarks of
* InMediaRes Productions, LLC.
*
* MechWarrior Copyright Microsoft Corporation. MekHQ was created under
* Microsoft's "Game Content Usage Rules"
* <https://www.xbox.com/en-US/developers/rules> and it is not endorsed by or
* affiliated with Microsoft.
*/
package mekhq.campaign;

Expand Down
54 changes: 51 additions & 3 deletions MekHQ/src/mekhq/campaign/Warehouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
*
* Catalyst Game Labs and the Catalyst Game Labs logo are trademarks of
* InMediaRes Productions, LLC.
*
* MechWarrior Copyright Microsoft Corporation. MekHQ was created under
* Microsoft's "Game Content Usage Rules"
* <https://www.xbox.com/en-US/developers/rules> and it is not endorsed by or
* affiliated with Microsoft.
*/
package mekhq.campaign;

Expand All @@ -39,6 +44,7 @@
import java.util.stream.Stream;

import megamek.common.annotations.Nullable;
import megamek.logging.MMLogger;
import mekhq.MekHQ;
import mekhq.campaign.event.PartChangedEvent;
import mekhq.campaign.event.PartNewEvent;
Expand All @@ -52,6 +58,8 @@
* Stores parts for a Campaign.
*/
public class Warehouse {
private static final MMLogger logger = MMLogger.create(Warehouse.class);

private final TreeMap<Integer, Part> parts = new TreeMap<>();

/**
Expand All @@ -78,7 +86,7 @@ public Part addPart(Part part, boolean mergeWithExisting) {
Part mergedPart = mergePartWithExisting(part);

// CAW: intentional reference equality
if (mergedPart != part) {
if (!mergedPart.equals(part)) {
// We've merged parts, so let interested parties know we've
// updated the merged part.
MekHQ.triggerEvent(new PartChangedEvent(mergedPart));
Expand Down Expand Up @@ -235,7 +243,7 @@ private Part mergePartWithExisting(Part part) {

// Check if the part has no unit, no parent part, and is not reserved for replacement
if ((null == part.getUnit()) && !part.hasParentPart() && !part.isReservedForReplacement()) {
Part spare = checkForExistingSparePart(part);
Part spare = checkForExistingSparePart(part, true);

// Ensure a matching spare exists and both parts share the same isBrandNew state
if (spare != null && part.isBrandNew() == spare.isBrandNew()) {
Expand Down Expand Up @@ -263,13 +271,53 @@ private Part mergePartWithExisting(Part part) {
* @return The matching spare part or null if none were found.
*/
public @Nullable Part checkForExistingSparePart(Part part) {
Objects.requireNonNull(part);
if (part == null) {
logger.error(new NullPointerException("Part is null"), "checkForExistingSparePart(Part): Part is null");
return null;
}

return findSparePart(spare ->
(spare.getId() != part.getId())
&& part.isSamePartTypeAndStatus(spare));
}

/**
* Checks for an existing spare part in inventory that matches the given {@code part}.
*
* <p>In addition to comparing type and status, this method can optionally consider whether both parts are
* equally brand new based on the {@code includeNewnessCheck} parameter.</p>
*
* <ul>
* <li>If {@code includeNewnessCheck} is {@code true}, this method defers to
* {@link #checkForExistingSparePart(Part)} for a match based on type and status.</li>
* <li>If {@code part} is {@code null}, an error is logged and {@code null} is returned.</li>
* <li>Otherwise, returns a matching spare part or {@code null} if none is found.</li>
* </ul>
*
* @param part the part to find a match for; may not be {@code null}
* @param includeNewnessCheck whether to require matching "brand new" status as part of the comparison
*
* @return an existing spare part matching the given part and criteria, or {@code null} if no match is found
*
* @author Illiani
* @since 0.50.06
*/
public @Nullable Part checkForExistingSparePart(Part part, boolean includeNewnessCheck) {
if (part == null) {
logger.error(new NullPointerException("Part is null"),
"checkForExistingSparePart(Part, boolean): Part is null");
return null;
}

if (!includeNewnessCheck) {
return checkForExistingSparePart(part);
}

return findSparePart(spare -> (spare.getId() != part.getId()) &&
part.isSamePartTypeAndStatus(spare) &&
(part.isBrandNew() == spare.isBrandNew()));
}

/**
* Gets a list of spare parts in the warehouse.
* @return A list of spare parts in the warehouse.
Expand Down
Loading
0