prevent NPE

This commit is contained in:
Anthony Calosa
2025-08-22 16:43:17 +08:00
parent fb624458f0
commit 1ff16ca509
4 changed files with 61 additions and 40 deletions

View File

@@ -61,7 +61,6 @@ public class InventoryScene extends UIScene {
Array<Actor> children = ui.getChildren(); Array<Actor> children = ui.getChildren();
for (int i = 0, n = children.size; i < n; i++) { for (int i = 0, n = children.size; i < n; i++) {
if (children.get(i).getName() != null && children.get(i).getName().startsWith("Equipment")) { if (children.get(i).getName() != null && children.get(i).getName().startsWith("Equipment")) {
String slotName = children.get(i).getName().split("_")[1]; String slotName = children.get(i).getName().split("_")[1];
equipmentSlots.put(slotName, (Button) children.get(i)); equipmentSlots.put(slotName, (Button) children.get(i));
@@ -69,30 +68,32 @@ public class InventoryScene extends UIScene {
slot.addListener(new ChangeListener() { slot.addListener(new ChangeListener() {
@Override @Override
public void changed(ChangeEvent event, Actor actor) { public void changed(ChangeEvent event, Actor actor) {
Button button = ((Button) actor); Button button = ((Button) actor);
if (button.isChecked()) { if (button.isChecked()) {
for (Button otherButton : equipmentSlots.values()) { for (Button otherButton : equipmentSlots.values()) {
if (button != otherButton && otherButton.isChecked()) { if (button != otherButton && otherButton.isChecked()) {
otherButton.setChecked(false); 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;
} }
} }
if (changeButton != null) String item = Current.player().itemInSlot(slotName);
changeButton.setChecked(true); if (item != null && !item.isEmpty()) {
} else { Button changeButton = null;
setSelected(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() { private void repair() {
if (selected == null) if (selected == null)
return; return;
if (itemLocation.get(selected) == null)
return;
ItemData data = itemLocation.get(selected).getRight(); ItemData data = itemLocation.get(selected).getRight();
if (data == null) if (data == null)
return; return;
@@ -166,6 +169,10 @@ public class InventoryScene extends UIScene {
} }
public void delete() { public void delete() {
if (selected == null)
return;
if (itemLocation.get(selected) == null)
return;
ItemData data = itemLocation.get(selected).getRight(); ItemData data = itemLocation.get(selected).getRight();
if (data != null) { if (data != null) {
data.isEquipped = false; data.isEquipped = false;
@@ -176,7 +183,10 @@ public class InventoryScene extends UIScene {
} }
public void equip() { public void equip() {
if (selected == null) return; if (selected == null)
return;
if (itemLocation.get(selected) == null)
return;
ItemData data = itemLocation.get(selected).getRight(); ItemData data = itemLocation.get(selected).getRight();
if (data == null) return; if (data == null) return;
Current.player().equip(data); Current.player().equip(data);
@@ -189,8 +199,10 @@ public class InventoryScene extends UIScene {
} }
private void triggerUse() { private void triggerUse() {
if (selected == null) return; if (selected == null)
return;
if (itemLocation.get(selected) == null)
return;
ItemData data = itemLocation.get(selected).getRight(); ItemData data = itemLocation.get(selected).getRight();
if (data == null) return; if (data == null) return;
Current.player().addShards(-data.shardsNeeded); Current.player().addShards(-data.shardsNeeded);
@@ -324,7 +336,6 @@ public class InventoryScene extends UIScene {
} }
private void updateInventory() { private void updateInventory() {
clearItemDescription();
clearSelectable(); clearSelectable();
inventoryButtons.clear(); inventoryButtons.clear();
inventory.clear(); inventory.clear();
@@ -454,6 +465,7 @@ public class InventoryScene extends UIScene {
@Override @Override
public void enter() { public void enter() {
clearItemDescription();
updateInventory(); updateInventory();
//inventory.add().expand(); //inventory.add().expand();
super.enter(); super.enter();

View File

@@ -470,6 +470,15 @@
"focused": "item_frame_selected", "focused": "item_frame_selected",
"checked": "item_frame_selected", "checked": "item_frame_selected",
"checkedOver": "item_frame_selected_hover" "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": { "ImageTextButtonStyle": {

View File

@@ -20,7 +20,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Ability1", "name": "Equipment_Ability1",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 17, "x": 17,
@@ -29,7 +29,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Ability2", "name": "Equipment_Ability2",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 107, "x": 107,
@@ -38,7 +38,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Neck", "name": "Equipment_Neck",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 62, "x": 62,
@@ -47,7 +47,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Body", "name": "Equipment_Body",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 62, "x": 62,
@@ -56,7 +56,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Boots", "name": "Equipment_Boots",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 62, "x": 62,
@@ -65,7 +65,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Left", "name": "Equipment_Left",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 17, "x": 17,
@@ -74,7 +74,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Right", "name": "Equipment_Right",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 107, "x": 107,

View File

@@ -20,7 +20,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Ability1", "name": "Equipment_Ability1",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 17, "x": 17,
@@ -29,7 +29,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Ability2", "name": "Equipment_Ability2",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 107, "x": 107,
@@ -38,7 +38,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Neck", "name": "Equipment_Neck",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 62, "x": 62,
@@ -47,7 +47,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Body", "name": "Equipment_Body",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 62, "x": 62,
@@ -56,7 +56,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Boots", "name": "Equipment_Boots",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 62, "x": 62,
@@ -65,7 +65,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Left", "name": "Equipment_Left",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 17, "x": 17,
@@ -74,7 +74,7 @@
{ {
"type": "ImageButton", "type": "ImageButton",
"name": "Equipment_Right", "name": "Equipment_Right",
"style": "item_frame", "style": "item_frame_static",
"width": 20, "width": 20,
"height": 20, "height": 20,
"x": 107, "x": 107,