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

Conversation

IllianiBird
Copy link
Collaborator
@IllianiBird IllianiBird commented Apr 29, 2025

Dev Notes

This issue was caused by the code that checks whether an item already exists in the warehouse, prior to attempting to stack the parts. What was happening is we checked the warehouse and if the first eligible item we found didn't have the same newness as the part we were comparing MekHQ would mistakenly think there were no eligible items. This resulted in the part (usually ammo) not stacking.

We also had a couple of lines of code that were trying to specially handle ammo. However, we were already handling ammo later in the process. So I removed it.

### Dev Notes
We had a couple of lines of code that were trying to specially handle ammo. However, we were already handling ammo later in the process. The two handlers were incompatible, which resulted in ammo refusing to stack.
@IllianiBird IllianiBird added Bug Severity: Medium Issues described as medium severity as per the new issue form labels Apr 29, 2025
@IllianiBird IllianiBird self-assigned this Apr 29, 2025
Copy link
codecov bot commented Apr 29, 2025

Codecov Report

Attention: Patch coverage is 35.71429% with 9 lines in your changes missing coverage. Please review.

Project coverage is 11.44%. Comparing base (5edad64) to head (9cc1134).
Report is 62 commits behind head on master.

Files with missing lines Patch % Lines
MekHQ/src/mekhq/campaign/Warehouse.java 35.71% 5 Missing and 4 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6816      +/-   ##
============================================
- Coverage     11.46%   11.44%   -0.03%     
+ Complexity     6508     6507       -1     
============================================
  Files          1098     1099       +1     
  Lines        140812   140909      +97     
  Branches      21831    21841      +10     
============================================
- Hits          16150    16132      -18     
- Misses       122995   123104     +109     
- Partials       1667     1673       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@IllianiBird IllianiBird changed the title Fixed Ammo Not Stacking in the Warehouse Fix: Fixed Ammo Not Stacking in the Warehouse Apr 30, 2025
@@ -263,13 +271,52 @@ 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("checkForExistingSparePart(Part): Part is null");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an error, it's a debug, Maybe an info, but it will show up in the log as an error and it will mean nothing, it won't have any kind of information, wont show an ID, nothing, so its an error log that will exist but no one will be able to do anything to act upon this error being logged.

Comment on lines 310 to 313
if (part == null) {
logger.error("checkForExistingSparePart(Part, boolean): Part is null");
return null;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for null first unless checkForExistingSparePart accepts @nullable.

Also, this log is another instance of an error state that isn't really an error, since there is nothing that can be done about it other than say "something happened". Either you raise that as a special exception so somewhere "upstream" you can manually handle the error, or you just accept that if you receive a null you will return a null. In either case, you can remove this error log.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely we should be logging unexpected null? Otherwise we end up in situations where erroneous nulls are being swallowed silently with nothing in the logs to show it happened. As occurred in 50.05's dev cycle which nearly led us to shipping 50.05 with completely broken OpFor generation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you log null you dont get any meaningful information other than that a null arrived there, there is no ID that generated the null, no entity, nothing, meaning it is useless from a bug-fixing standpoint. If you want to log that something went wrong, sure, add a warning with something like expected part ID {} but received null, which won't give you any information unless you had an info log prior to that which gave something like logger.info("Looking at unit {}", unit);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I ended up doing has logging a handled NPE, so that we'd get the stack dump which will tell us where the error was coming from.

Comment on lines 150 to 152
if ((part instanceof AmmoBin) && (null == part.getUnit())) {
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we 100% certain that this piece of code is unnecessary? I won't judge, just want a second confirmation from you @IllianiBird that this is 100% as intended.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be necessary, but you know what? Let's add it back. If it's not necessary it won't do any harm, and if it is somehow necessary (despite being handled elsewhere in the pipeline) it's sitting pretty.

@IllianiBird IllianiBird requested a review from Scoppio May 1, 2025 04:09
@HammerGS HammerGS requested a review from Copilot May 2, 2025 03:29
Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses the ammo stacking issue by modifying the merging logic in the Warehouse, ensuring that spare parts are correctly compared based on their "brand new" state. Key changes include:

  • Replacing reference equality with an equals check for merged parts in Warehouse.addPart.
  • Adding an overloaded checkForExistingSparePart method that considers newness.
  • Updating copyright headers in both Warehouse and Quartermaster.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
MekHQ/src/mekhq/campaign/Warehouse.java Updated merging and spare part checking logic to fix ammo stacking issues.
MekHQ/src/mekhq/campaign/Quartermaster.java Updated copyright header.
Comments suppressed due to low confidence (1)

MekHQ/src/mekhq/campaign/Warehouse.java:89

  • Replacing the intentional reference equality check with an equals comparison may lead to unexpected behavior if the equals method does not match the intended identity check. Verify that this change correctly identifies merged parts without side effects.
if (!mergedPart.equals(part)) {

@Scoppio Scoppio merged commit 0cc2090 into MegaMek:master May 2, 2025
6 checks passed
@IllianiBird IllianiBird deleted the fixAmmoStacking branch June 7, 2025 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Severity: Medium Issues described as medium severity as per the new issue form
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0