From 1ff16ca5099fade65d0c66d2323868b5cd28b76e Mon Sep 17 00:00:00 2001 From: Anthony Calosa Date: Fri, 22 Aug 2025 16:43:17 +0800 Subject: [PATCH] prevent NPE --- .../forge/adventure/scene/InventoryScene.java | 62 +++++++++++-------- .../res/adventure/common/skin/ui_skin.json | 9 +++ .../res/adventure/common/ui/inventory.json | 14 ++--- .../common/ui/inventory_portrait.json | 16 ++--- 4 files changed, 61 insertions(+), 40 deletions(-) diff --git a/forge-gui-mobile/src/forge/adventure/scene/InventoryScene.java b/forge-gui-mobile/src/forge/adventure/scene/InventoryScene.java index 4484688568d..7a2bed04a26 100644 --- a/forge-gui-mobile/src/forge/adventure/scene/InventoryScene.java +++ b/forge-gui-mobile/src/forge/adventure/scene/InventoryScene.java @@ -61,7 +61,6 @@ public class InventoryScene extends UIScene { Array children = ui.getChildren(); for (int i = 0, n = children.size; i < n; i++) { - if (children.get(i).getName() != null && children.get(i).getName().startsWith("Equipment")) { String slotName = children.get(i).getName().split("_")[1]; equipmentSlots.put(slotName, (Button) children.get(i)); @@ -69,30 +68,32 @@ public class InventoryScene extends UIScene { slot.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { - Button button = ((Button) actor); - if (button.isChecked()) { - for (Button otherButton : equipmentSlots.values()) { - if (button != otherButton && otherButton.isChecked()) { - otherButton.setChecked(false); - } - } - String item = Current.player().itemInSlot(slotName); - if (item != null && !item.isEmpty()) { - Button changeButton = null; - for (Button invButton : inventoryButtons) { - if (itemLocation.get(invButton) != null && itemLocation.get(invButton).equals(item)) { - changeButton = invButton; - break; + Button button = ((Button) actor); + if (button.isChecked()) { + for (Button otherButton : equipmentSlots.values()) { + if (button != otherButton && otherButton.isChecked()) { + otherButton.setChecked(false); } } - if (changeButton != null) - changeButton.setChecked(true); - } else { - setSelected(null); + String item = Current.player().itemInSlot(slotName); + if (item != null && !item.isEmpty()) { + Button changeButton = null; + for (Button invButton : inventoryButtons) { + if(itemLocation.get(invButton) == null) + continue; + ItemData data = itemLocation.get(invButton).getRight(); + if (data != null && item.equals(data.equipmentSlot)) { + changeButton = invButton; + break; + } + } + if (changeButton != null) + changeButton.setChecked(true); + } else { + setSelected(null); + } } } - - } }); } } @@ -121,6 +122,8 @@ public class InventoryScene extends UIScene { private void repair() { if (selected == null) return; + if (itemLocation.get(selected) == null) + return; ItemData data = itemLocation.get(selected).getRight(); if (data == null) return; @@ -166,6 +169,10 @@ public class InventoryScene extends UIScene { } public void delete() { + if (selected == null) + return; + if (itemLocation.get(selected) == null) + return; ItemData data = itemLocation.get(selected).getRight(); if (data != null) { data.isEquipped = false; @@ -176,7 +183,10 @@ public class InventoryScene extends UIScene { } public void equip() { - if (selected == null) return; + if (selected == null) + return; + if (itemLocation.get(selected) == null) + return; ItemData data = itemLocation.get(selected).getRight(); if (data == null) return; Current.player().equip(data); @@ -189,8 +199,10 @@ public class InventoryScene extends UIScene { } private void triggerUse() { - if (selected == null) return; - + if (selected == null) + return; + if (itemLocation.get(selected) == null) + return; ItemData data = itemLocation.get(selected).getRight(); if (data == null) return; Current.player().addShards(-data.shardsNeeded); @@ -324,7 +336,6 @@ public class InventoryScene extends UIScene { } private void updateInventory() { - clearItemDescription(); clearSelectable(); inventoryButtons.clear(); inventory.clear(); @@ -454,6 +465,7 @@ public class InventoryScene extends UIScene { @Override public void enter() { + clearItemDescription(); updateInventory(); //inventory.add().expand(); super.enter(); diff --git a/forge-gui/res/adventure/common/skin/ui_skin.json b/forge-gui/res/adventure/common/skin/ui_skin.json index 7bc293b04b9..637bfdc7b89 100644 --- a/forge-gui/res/adventure/common/skin/ui_skin.json +++ b/forge-gui/res/adventure/common/skin/ui_skin.json @@ -470,6 +470,15 @@ "focused": "item_frame_selected", "checked": "item_frame_selected", "checkedOver": "item_frame_selected_hover" + }, + "item_frame_static": { + "imageCheckedOver": "item_frame", + "up": "item_frame", + "over": "item_frame", + "focused": "item_frame", + "checked": "item_frame", + "checkedOver": "item_frame", + "checkedDown": "item_frame" } }, "ImageTextButtonStyle": { diff --git a/forge-gui/res/adventure/common/ui/inventory.json b/forge-gui/res/adventure/common/ui/inventory.json index 125d9830e65..4f01b0c796a 100644 --- a/forge-gui/res/adventure/common/ui/inventory.json +++ b/forge-gui/res/adventure/common/ui/inventory.json @@ -20,7 +20,7 @@ { "type": "ImageButton", "name": "Equipment_Ability1", - "style": "item_frame", + "style": "item_frame_static", "width": 20, "height": 20, "x": 17, @@ -29,7 +29,7 @@ { "type": "ImageButton", "name": "Equipment_Ability2", - "style": "item_frame", + "style": "item_frame_static", "width": 20, "height": 20, "x": 107, @@ -38,7 +38,7 @@ { "type": "ImageButton", "name": "Equipment_Neck", - "style": "item_frame", + "style": "item_frame_static", "width": 20, "height": 20, "x": 62, @@ -47,7 +47,7 @@ { "type": "ImageButton", "name": "Equipment_Body", - "style": "item_frame", + "style": "item_frame_static", "width": 20, "height": 20, "x": 62, @@ -56,7 +56,7 @@ { "type": "ImageButton", "name": "Equipment_Boots", - "style": "item_frame", + "style": "item_frame_static", "width": 20, "height": 20, "x": 62, @@ -65,7 +65,7 @@ { "type": "ImageButton", "name": "Equipment_Left", - "style": "item_frame", + "style": "item_frame_static", "width": 20, "height": 20, "x": 17, @@ -74,7 +74,7 @@ { "type": "ImageButton", "name": "Equipment_Right", - "style": "item_frame", + "style": "item_frame_static", "width": 20, "height": 20, "x": 107, diff --git a/forge-gui/res/adventure/common/ui/inventory_portrait.json b/forge-gui/res/adventure/common/ui/inventory_portrait.json index 3d83a3f6494..60b39b65b8b 100644 --- a/forge-gui/res/adventure/common/ui/inventory_portrait.json +++ b/forge-gui/res/adventure/common/ui/inventory_portrait.json @@ -19,8 +19,8 @@ }, { "type": "ImageButton", - "name": "Equipment_Ability1", - "style": "item_frame", + "name": "Equipment_Ability1", + "style": "item_frame_static", "width": 20, "height": 20, "x": 17, @@ -29,7 +29,7 @@ { "type": "ImageButton", "name": "Equipment_Ability2", - "style": "item_frame", + "style": "item_frame_static", "width": 20, "height": 20, "x": 107, @@ -38,7 +38,7 @@ { "type": "ImageButton", "name": "Equipment_Neck", - "style": "item_frame", + "style": "item_frame_static", "width": 20, "height": 20, "x": 62, @@ -47,7 +47,7 @@ { "type": "ImageButton", "name": "Equipment_Body", - "style": "item_frame", + "style": "item_frame_static", "width": 20, "height": 20, "x": 62, @@ -56,7 +56,7 @@ { "type": "ImageButton", "name": "Equipment_Boots", - "style": "item_frame", + "style": "item_frame_static", "width": 20, "height": 20, "x": 62, @@ -65,7 +65,7 @@ { "type": "ImageButton", "name": "Equipment_Left", - "style": "item_frame", + "style": "item_frame_static", "width": 20, "height": 20, "x": 17, @@ -74,7 +74,7 @@ { "type": "ImageButton", "name": "Equipment_Right", - "style": "item_frame", + "style": "item_frame_static", "width": 20, "height": 20, "x": 107,