Skip to content

Fix AmmoBan dialog and commands not working#152

Open
Raugharr wants to merge 1 commit intomainfrom
fix-ammo-ban-commands
Open

Fix AmmoBan dialog and commands not working#152
Raugharr wants to merge 1 commit intomainfrom
fix-ammo-ban-commands

Conversation

@Raugharr
Copy link
Copy Markdown
Owner

Changes:
AdminListHouseBannedAmmoCommand now works properly.
AdminListFactionBannedAmmoCommand now works properly.
BannedAmmoDialog now checks banned ammo.
BannedAmmoDialog now correctly bans ammo.


//Check box has been selected and should be updated to the server
if (tempBox.isSelected() && !bannedAmmo.containsKey(ammo))
if (tempBox.isSelected() && !bannedAmmo.containsKey(ammo)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since this is essentially checking whether the selection state differs from whether the ammo is banned, you could simplify it to:

if (tempBox.isSelected() != bannedAmmo.containsKey(ammo))

//Check box has been selected and should be updated to the server
if (tempBox.isSelected() && !bannedAmmo.containsKey(ammo))
if (tempBox.isSelected() && !bannedAmmo.containsKey(ammo)
|| !tempBox.isSelected() && bannedAmmo.containsKey(ammo)) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

tempBox.isSelected() can't be null at this point, but what about bannedAmmo? bannedAmmo is from:

mwclient.getData().getServerBannedAmmo()

else if (!tempBox.isSelected() && bannedAmmo.containsKey(ammo))
mwclient.sendChat(GameHost.CAMPAIGN_PREFIX + "c adminsetserverammoban#"
+ munitionTypes.get(tempBox.getText()));
+ munitionTypes.get(tempBox.getText()).ordinal());
Copy link
Copy Markdown
Collaborator

@sandysimonton sandysimonton Mar 31, 2026

Choose a reason for hiding this comment

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

Using .ordinal() is usually avoided because it's brittle - in our case, if the order of enums in this changes:

megamek.common.AmmoType.Munitions;

Then the result of this will change:
munitionTypes.get(tempBox.getText()).ordinal());

The fear is that Munitions is a fairly large enum, written like:

public enum Munitions {
        M_STANDARD,

        // AC Munition Types
        M_CLUSTER,
        ...

But, if the intention is that each should map to a numeral, it should have been written like:

public enum Munitions {
        M_STANDARD(0),

        // AC Munition Types
        M_CLUSTER(1),
        ...

with each numeral accessed through a public getter.

It seems this is all kind of out of our hands unless we use a secondhand mapping enum, so this comment is mostly just for situational awareness. Because we don't necessarily track every change in the megamek project, it is possible that one day someone's innocent reordering/organization of the Munitions enum causes strange and wonky behavior in a downstream project!

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Yup I rather dislike this PR because it uses a lot of values and ordinal which seem to both be bad, but would rather have this feature working than not. Also, after doing this PR I'm under the impression that MegaMek circa 2010's only had Munitions and later came AmmoType which is a list of static final int that solves this issue.
I say this because I noticed in CampaignData we map the enum to string, specifically "LBX Cluster -> M_CLUSTER", however M_CLUSTER but seems to include LBX, Silver bullet Gauss Rifle Ammo, and artillery cluster munitions.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hmm very interesting! Yeah would rather have it working than not, 100%

private Hashtable<String, Integer> commands = new Hashtable<>();
private TreeMap<String, String> planetOpFlags = new TreeMap<>();

private Hashtable<String, AmmoType.Munitions> munitionsByName;
Copy link
Copy Markdown
Collaborator

@sandysimonton sandysimonton Mar 31, 2026

Choose a reason for hiding this comment

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

HashMap<K, V>
Hashtable not used anymore

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Great optimization for sure

Copy link
Copy Markdown
Collaborator

@sandysimonton sandysimonton left a comment

Choose a reason for hiding this comment

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

Some notes on possible things to look at

At your discretion for merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants