From 45cd1d596519aa149be8e11ded8674e0b28174c0 Mon Sep 17 00:00:00 2001 From: kuronekochomusuke Date: Sun, 7 Jan 2024 18:05:38 -0500 Subject: [PATCH 1/2] if unit is invalid show the reason in the mech summary --- megamek/src/megamek/common/MechView.java | 58 +++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/megamek/src/megamek/common/MechView.java b/megamek/src/megamek/common/MechView.java index da062e0f5c0..ee6031b29b5 100644 --- a/megamek/src/megamek/common/MechView.java +++ b/megamek/src/megamek/common/MechView.java @@ -21,6 +21,8 @@ import megamek.common.eras.Era; import megamek.common.eras.Eras; import megamek.common.options.*; +import megamek.common.util.fileUtils.MegaMekFile; +import megamek.common.verifier.*; import megamek.common.weapons.bayweapons.BayWeapon; import megamek.common.weapons.infantry.InfantryWeapon; @@ -78,6 +80,7 @@ interface ViewElement { private List sBasic = new ArrayList<>(); private List sLoadout = new ArrayList<>(); private List sFluff = new ArrayList<>(); + private List sInvalid = new ArrayList<>(); private final boolean html; @@ -529,6 +532,51 @@ public MechView(final Entity entity, final boolean showDetail, final boolean use sFluff.add(new SingleLine()); sFluff.add(new LabeledElement("History", entity.getFluff().getHistory())); } + + EntityVerifier verifier = EntityVerifier.getInstance(new MegaMekFile( + Configuration.unitsDir(), EntityVerifier.CONFIG_FILENAME).getFile()); + StringBuffer sb = new StringBuffer(); + TestEntity testEntity = getTestEntity(entity, verifier); + + if (testEntity != null) { + testEntity.correctEntity(sb, entity.getTechLevel()); + + if (!sb.toString().isEmpty()) { + sInvalid.add(new SingleLine()); + sInvalid.add(new LabeledElement("Invalid Reasons", sb.toString())); + } + } + } + + /** + * copied from megameklab.util.UnitUtil.getEntityVerifier + * @param unit the supplied entity + * @param entityVerifier the entity verifier loaded from a UnitVerifierOptions.xml + * @return a TestEntity instance for the supplied Entity. + */ + public static TestEntity getTestEntity(Entity unit, EntityVerifier entityVerifier) { + // FIXME move the same method from megameklab.util.UnitUtil.getEntityVerifier to common + TestEntity testEntity = null; + if (unit.hasETypeFlag(Entity.ETYPE_MECH)) { + testEntity = new TestMech((Mech) unit, entityVerifier.mechOption, null); + } else if (unit.hasETypeFlag(Entity.ETYPE_PROTOMECH)) { + testEntity = new TestProtomech((Protomech) unit, entityVerifier.protomechOption, null); + } else if (unit.isSupportVehicle()) { + testEntity = new TestSupportVehicle(unit, entityVerifier.tankOption, null); + } else if (unit.hasETypeFlag(Entity.ETYPE_TANK)) { + testEntity = new TestTank((Tank) unit, entityVerifier.tankOption, null); + } else if (unit.hasETypeFlag(Entity.ETYPE_SMALL_CRAFT)) { + testEntity = new TestSmallCraft((SmallCraft) unit, entityVerifier.aeroOption, null); + } else if (unit.hasETypeFlag(Entity.ETYPE_JUMPSHIP)) { + testEntity = new TestAdvancedAerospace((Jumpship) unit, entityVerifier.aeroOption, null); + } else if (unit.hasETypeFlag(Entity.ETYPE_AERO)) { + testEntity = new TestAero((Aero) unit, entityVerifier.aeroOption, null); + } else if (unit.hasETypeFlag(Entity.ETYPE_BATTLEARMOR)) { + testEntity = new TestBattleArmor((BattleArmor) unit, entityVerifier.baOption, null); + } else if (unit.hasETypeFlag(Entity.ETYPE_INFANTRY)) { + testEntity = new TestInfantry((Infantry)unit, entityVerifier.infOption, null); + } + return testEntity; } private String eraText(int startYear, int endYear) { @@ -576,6 +624,14 @@ public String getMechReadoutBasic() { return getReadout(sBasic); } + /** + * The invalid section includes reasons why the unit is invalid + * @return The data from the invalid section + */ + public String getMechReadoutInvalid() { + return getReadout(sInvalid); + } + /** * The loadout includes weapons, ammo, and other equipment broken down by location. * @return The data from the loadout section. @@ -612,7 +668,7 @@ public String getMechReadout(@Nullable String fontName) { } return docStart + getMechReadoutHead() + getMechReadoutBasic() + getMechReadoutLoadout() - + getMechReadoutFluff() + docEnd; + + getMechReadoutFluff() + getMechReadoutInvalid() + docEnd; } private List getInternalAndArmor() { From 7bb00cbbfaefa337dbb41b68f5eebd6784fbb9c7 Mon Sep 17 00:00:00 2001 From: kuronekochomusuke Date: Sun, 7 Jan 2024 18:14:54 -0500 Subject: [PATCH 2/2] code cleanup --- megamek/i18n/megamek/client/messages.properties | 1 + megamek/src/megamek/common/MechView.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/megamek/i18n/megamek/client/messages.properties b/megamek/i18n/megamek/client/messages.properties index ce14a7f0307..c6fba5ba47c 100644 --- a/megamek/i18n/megamek/client/messages.properties +++ b/megamek/i18n/megamek/client/messages.properties @@ -2286,6 +2286,7 @@ MechView.Pod=Pod MechView.Fixed=Fixed MechView.Quirks=Quirks MechView.WeaponQuirks=Weapon Quirks +MechView.InvalidReasons=Invalid Reasons #Minelaying MineDensityDialog.title=Mine Density diff --git a/megamek/src/megamek/common/MechView.java b/megamek/src/megamek/common/MechView.java index ee6031b29b5..7fcf2be9da5 100644 --- a/megamek/src/megamek/common/MechView.java +++ b/megamek/src/megamek/common/MechView.java @@ -543,7 +543,7 @@ public MechView(final Entity entity, final boolean showDetail, final boolean use if (!sb.toString().isEmpty()) { sInvalid.add(new SingleLine()); - sInvalid.add(new LabeledElement("Invalid Reasons", sb.toString())); + sInvalid.add(new LabeledElement(Messages.getString("MechView.InvalidReasons"), sb.toString())); } } }