Skip to content

fix: guard MapMeta#getMapView against IllegalStateException#154

Open
HyacinthHaru wants to merge 1 commit intoLOOHP:mainfrom
HyacinthHaru:fix/mapmeta-getmapview-illegalstate
Open

fix: guard MapMeta#getMapView against IllegalStateException#154
HyacinthHaru wants to merge 1 commit intoLOOHP:mainfrom
HyacinthHaru:fix/mapmeta-getmapview-illegalstate

Conversation

@HyacinthHaru
Copy link

On Leaves/Paper 1.21.10, MapMeta#getMapView() can throw -
IllegalStateException: Item does not have map associated - check hasMapView() first!
even after hasMapView() checks in some cases (invalid/corrupted map meta state).

This caused event handler errors in:

  • EntityPickupItemEvent
  • InventoryClickEvent

So, I changed

  • Added safe MapView retrieval with try/catch IllegalStateException in:
    • ImageFilledMapUtils
    • MapUtils#getItemMapView
  • Added null/bounds guards in ImageFilledMapUtils for stale map info.
  • Reused MapUtils#getItemMapView in MapMarkerEditManager to avoid direct unsafe calls.

plugin now skips malformed map metadata gracefully instead of throwing and spamming console errors.

Copilot AI review requested due to automatic review settings February 11, 2026 05:40
Copy link

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

Guards map metadata access against a Paper/Leaves edge case where MapMeta#getMapView() can throw IllegalStateException even after hasMapView() checks, preventing event/task spam and improving resilience to corrupted map meta.

Changes:

  • Hardened MapUtils#getItemMapView with try/catch (IllegalStateException) and reused it from rayTraceTargetImageMap.
  • Added safe MapView retrieval and additional null/bounds guards in ImageFilledMapUtils to handle stale/corrupt map info.
  • Replaced direct MapMeta#getMapView() usage in MapMarkerEditManager with MapUtils#getItemMapView to avoid unsafe calls.

Reviewed changes

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

File Description
common/src/main/java/com/loohp/imageframe/utils/MapUtils.java Adds exception-safe MapView extraction and reuses it in raytracing.
common/src/main/java/com/loohp/imageframe/utils/ImageFilledMapUtils.java Adds safe MapView retrieval + defensive guards for invalid/stale filled-map info.
common/src/main/java/com/loohp/imageframe/objectholders/MapMarkerEditManager.java Avoids unsafe direct map meta access by using MapUtils#getItemMapView.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 109 to 112
ItemStack itemStack = itemFrame.getItem();
if (itemStack == null || itemStack.getType().equals(Material.AIR)) {
continue;
}
if (!itemStack.hasItemMeta()) {
continue;
}
ItemMeta itemMeta = itemStack.getItemMeta();
if (!(itemMeta instanceof MapMeta)) {
continue;
}
MapMeta mapMeta = (MapMeta) itemMeta;
if (!mapMeta.hasMapView()) {
continue;
}
MapView mapView = mapMeta.getMapView();
MapView mapView = MapUtils.getItemMapView(itemStack);
if (mapView == null) {
continue;
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

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

MapUtils.getItemMapView() performs tryDeleteBlankDataFile(...) (filesystem existence/length checks) when Settings.TryDeleteBlankMapFiles is enabled. Calling it from this per-tick edit task can introduce repeated IO and potential lag under that setting. Consider adding/using a variant that only does the safe MapView extraction (no blank-file cleanup) for hot paths like marker editing/raytracing.

Copilot uses AI. Check for mistakes.
Comment on lines +94 to +103
private static MapView getMapViewSafely(MapMeta mapMeta) {
try {
if (!mapMeta.hasMapView()) {
return null;
}
return mapMeta.getMapView();
} catch (IllegalStateException ignored) {
return null;
}
}
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

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

getMapViewSafely(MapMeta) duplicates the same hasMapView() + getMapView() try/catch logic now present in MapUtils.getItemMapView(...). To avoid the two implementations drifting over time, consider centralizing this into a shared helper (e.g., a MapUtils.getMapViewSafely(MapMeta) or an overload of getItemMapView that accepts MapMeta / skips cleanup).

Copilot uses AI. Check for mistakes.
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

Comments