Skip to content

fix(PVPModule): handle exceptions in onTickModule and improve key conditions logic#269

Merged
dm94 merged 4 commits intomasterfrom
fix/217
Feb 2, 2026
Merged

fix(PVPModule): handle exceptions in onTickModule and improve key conditions logic#269
dm94 merged 4 commits intomasterfrom
fix/217

Conversation

@dm94
Copy link
Copy Markdown
Owner

@dm94 dm94 commented Jan 9, 2026

  • Add try-catch block in PVPModule's onTickModule to prevent crashes
  • Refactor useKeyWithConditions in ShipAttacker for better readability and conditions handling
  • Expand healthEnemyRange default values in ExtraKeyConditions

#217

Summary by Sourcery

Improve PVP combat module robustness and refine extra key condition handling during ship attacks.

Bug Fixes:

  • Prevent PVPModule onTickModule crashes by safely handling unexpected runtime exceptions.

Enhancements:

  • Refine ShipAttacker extra key usage conditions to use clearer, more flexible health and custom condition checks.
  • Allow extra key conditions to apply across the full enemy health range by expanding default enemy health thresholds.

…ditions logic

- Add try-catch block in PVPModule's onTickModule to prevent crashes
- Refactor useKeyWithConditions in ShipAttacker for better readability and conditions handling
- Expand healthEnemyRange default values in ExtraKeyConditions
@dm94 dm94 self-assigned this Jan 9, 2026
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jan 9, 2026

Reviewer's Guide

Adds exception handling around PVPModule.onTickModule to prevent crashes, refactors ShipAttacker.useKeyWithConditions for clearer, more robust condition evaluation using PluginAPI and configurable conditions, and broadens the default enemy health range in ExtraKeyConditions so extra key logic can apply across the full HP spectrum.

Sequence diagram for PVPModule onTickModule with exception handling

sequenceDiagram
participant PVPModule as PVPModule
participant pet as pet
participant safety as safety
participant antiPushLogic as antiPushLogic
participant shipAttacker as shipAttacker

PVPModule->>PVPModule: onTickModule()
activate PVPModule
PVPModule->>pet: setEnabled(true)
alt movementDisabledOrSafeAndOnCorrectMap
    PVPModule->>PVPModule: hasTarget()
    alt hasTarget
        PVPModule->>PVPModule: attackLogic()
    else noTarget
        PVPModule->>PVPModule: set target to null
        PVPModule->>shipAttacker: resetDefenseData()
        PVPModule->>PVPModule: roamingLogic()
    end
end
PVPModule->>antiPushLogic: registerTarget(target)
opt exceptionThrown
    PVPModule->>PVPModule: catch Exception
    PVPModule->>PVPModule: set target to null
end
deactivate PVPModule
Loading

Sequence diagram for ShipAttacker useKeyWithConditions decision flow

sequenceDiagram
participant shipAttacker as shipAttacker
participant extra as extra
participant api as api
participant heroapi as heroapi
participant target as target
participant conditionsManagement as conditionsManagement
participant items as items

shipAttacker->>shipAttacker: useKeyWithConditions(extra, selectableItem)
alt extraDisabled
    shipAttacker-->>shipAttacker: return false
else extraEnabled
    shipAttacker->>shipAttacker: check keyDelay >= 500ms
    alt recentKeyUse
        shipAttacker-->>shipAttacker: return false
    end

    alt selectableItemNullAndExtraKeyNotNull
        shipAttacker->>items: getItem(extra.key)
        items-->>shipAttacker: selectableItem
    end

    alt selectableItemStillNull
        shipAttacker-->>shipAttacker: return false
    end

    alt extraConditionNotNull
        shipAttacker->>extra: condition.get(api)
        extra-->>shipAttacker: conditionResult
        alt conditionDisallows
            shipAttacker-->>shipAttacker: return false
        end
    end

    shipAttacker->>heroapi: getHealth().hpPercent()
    alt heroHealthOutsideRange
        shipAttacker-->>shipAttacker: return false
    end

    shipAttacker->>heroapi: getLocalTarget()
    heroapi-->>shipAttacker: target
    alt targetNotNull
        shipAttacker->>target: getHealth().hpPercent()
        alt enemyHealthOutsideRange
            shipAttacker-->>shipAttacker: return false
        end
    end

    shipAttacker->>conditionsManagement: useSelectableReadyWhenReady(selectableItem)
    conditionsManagement-->>shipAttacker: used
    alt selectableUsed
        shipAttacker->>shipAttacker: update keyDelay
        shipAttacker-->>shipAttacker: return true
    else notUsed
        shipAttacker-->>shipAttacker: return false
    end
end
Loading

Class diagram for updated ShipAttacker, PVPModule, and ExtraKeyConditions

classDiagram

class ShipAttacker {
  -HeroAPI heroapi
  -MovementAPI movement
  -ConfigAPI configAPI
  -GroupAPI group
  -PluginAPI api
  -SettingsProxy settingsProxy
  -Collection~Player~ allShips
  -Collection~Portal~ allPortals
  -AmmoConfig ammoConfig
  -Items items
  -ConditionsManagement conditionsManagement
  -Humanizer humanizerConfig
  -char attackLaserKey
  -long keyDelay
  +ShipAttacker(PluginAPI api, AmmoConfig ammoConfig, Humanizer humanizerConfig)
  +Ship getTarget()
  +boolean useKeyWithConditions(ExtraKeyConditions extra, SelectableItem selectableItem)
}

class ExtraKeyConditions {
  +boolean enable
  +SelectableItemKey key
  +PercentRange healthRange
  +PercentRange healthEnemyRange
  +Condition condition
}

class PVPModule {
  -PluginAPI api
  -HeroAPI heroapi
  -PetAPI pet
  -PVPConfig pvpConfig
  -Safety safety
  -AntiPushLogic antiPushLogic
  -ExtraMovementLogic extraMovementLogic
  -ExtraCChangerLogic extraConfigChangerLogic
  -ShipAttacker shipAttacker
  -Player target
  +void setup()
  +void onTickModule()
  -void attackLogic()
  -void roamingLogic()
  -boolean hasTarget()
}

ShipAttacker --> ExtraKeyConditions
PVPModule --> ShipAttacker
PVPModule --> AntiPushLogic
Loading

File-Level Changes

Change Details Files
Refactor ShipAttacker key-usage logic to be clearer, more robust, and support additional conditions via PluginAPI.
  • Store PluginAPI instance on ShipAttacker for later use in conditions evaluation.
  • Simplify useKeyWithConditions by returning early when feature is disabled, key was used too recently, item cannot be resolved, or configured condition disallows usage.
  • Switch hero/enemy health checks from combined range checks to early-return style guard clauses, and make enemy-health check optional if there is no local target.
  • Invoke an optional ExtraKeyConditions.condition via condition.get(api).allows() before attempting to use the selectable item.
  • Only call conditionsManagement.useSelectableReadyWhenReady after all preconditions pass, updating keyDelay and returning success when the item is used.
src/main/java/com/deeme/types/ShipAttacker.java
Protect PVPModule update loop from runtime exceptions and preserve anti-push behavior registration.
  • Wrap onTickModule body in a try-catch block so unexpected exceptions do not crash the module.
  • Reset target to null in the catch block to ensure a clean state after failures.
  • Keep pet enabling, movement/attack vs roaming branching, and antiPushLogic.registerTarget(target) inside the guarded section, with anti-push registration executed after targeting/roaming logic when no exception occurs.
src/main/java/com/deeme/modules/PVPModule.java
Broaden default enemy health range for extra key conditions to cover the full 0–100% spectrum.
  • Change ExtraKeyConditions.healthEnemyRange default from PercentRange(0.2, 0.4) to PercentRange(0, 1) so the extra key can trigger regardless of enemy HP unless configured otherwise.
src/main/java/com/deeme/types/config/ExtraKeyConditions.java

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 3 issues, and left some high level feedback:

  • In PVPModule.onTickModule, the broad catch (Exception e) silently swallows all errors and only resets target; consider at least logging the exception or narrowing the catch so unexpected failures are visible during debugging.
  • The new logic in useKeyWithConditions allows the key to be used even when heroapi.getLocalTarget() is null (enemy health range is effectively ignored in that case), whereas previously a local target was required; please confirm this behavior change is intentional.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `PVPModule.onTickModule`, the broad `catch (Exception e)` silently swallows all errors and only resets `target`; consider at least logging the exception or narrowing the catch so unexpected failures are visible during debugging.
- The new logic in `useKeyWithConditions` allows the key to be used even when `heroapi.getLocalTarget()` is null (enemy health range is effectively ignored in that case), whereas previously a local target was required; please confirm this behavior change is intentional.

## Individual Comments

### Comment 1
<location> `src/main/java/com/deeme/modules/PVPModule.java:146-155` </location>
<code_context>
-                target = null;
-                shipAttacker.resetDefenseData();
-                roamingLogic();
+        try {
+            pet.setEnabled(true);
+            if (!pvpConfig.move || (safety.tick() && checkMap())) {
+                if (hasTarget()) {
+                    attackLogic();
+                } else {
+                    target = null;
+                    shipAttacker.resetDefenseData();
+                    roamingLogic();
+                }
             }
+            antiPushLogic.registerTarget(target);
+        } catch (Exception e) {
+            target = null;
         }
</code_context>

<issue_to_address>
**issue (bug_risk):** Catching raw `Exception` without any logging may hide real issues and make debugging difficult.

Wrapping the entire `onTickModule` in a broad `catch (Exception)` and only resetting `target` will silently hide any runtime errors in movement/attack logic. Please either log the exception (via your existing logger or `api`) so recurring failures are visible, or narrow the catch to specific, expected exception types for this loop.
</issue_to_address>

### Comment 2
<location> `src/main/java/com/deeme/modules/PVPModule.java:146` </location>
<code_context>
-                target = null;
-                shipAttacker.resetDefenseData();
-                roamingLogic();
+        try {
+            pet.setEnabled(true);
+            if (!pvpConfig.move || (safety.tick() && checkMap())) {
</code_context>

<issue_to_address>
**issue (review_instructions):** Catching the generic Exception in onTickModule without any logging or handling beyond resetting target reduces code cleanliness and makes diagnosing issues difficult.

Wrapping the whole onTickModule body in a broad `try { ... } catch (Exception e) { target = null; }` silently swallows all unexpected errors.

For a clean and maintainable plugin, it’s better to either:
- Catch specific expected exceptions, or
- At least log the exception via Darkbot’s logging facilities so issues can be detected and debugged.

As written, any bug in `attackLogic`, `roamingLogic`, or `antiPushLogic.registerTarget` will be hidden, which works against clean, diagnosable code in a plugin context.

<details>
<summary>Review instructions:</summary>

**Path patterns:** `*.java`

**Instructions:**
It is a plugin for the darkbot bot in java, check that the code is clean and efficient.

</details>
</issue_to_address>

### Comment 3
<location> `src/main/java/com/deeme/modules/PVPModule.java:158` </location>
<code_context>
+                }
             }
+            antiPushLogic.registerTarget(target);
+        } catch (Exception e) {
+            target = null;
         }
</code_context>

<issue_to_address>
**suggestion (review_instructions):** The catch block only nulls the target and does not log or handle the underlying error, which is not clean error handling for a plugin.

Inside this `catch (Exception e)` you only set `target = null;` and then ignore the exception. For a Darkbot plugin, this makes runtime failures hard to discover and can leave the bot in an inconsistent state while appearing to work.

Please consider logging the exception (or rethrowing/propagating it as appropriate), or narrowing the catch to specific expected failures and handling them explicitly.

<details>
<summary>Review instructions:</summary>

**Path patterns:** `*.java`

**Instructions:**
It is a plugin for the darkbot bot in java, check that the code is clean and efficient.

</details>
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/main/java/com/deeme/modules/PVPModule.java
Comment thread src/main/java/com/deeme/modules/PVPModule.java
Comment thread src/main/java/com/deeme/modules/PVPModule.java
@dm94 dm94 merged commit 87b5907 into master Feb 2, 2026
8 checks passed
@dm94 dm94 deleted the fix/217 branch February 2, 2026 12:33
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.

1 participant