|
|
@@ -15,9 +15,9 @@ import lombok.RequiredArgsConstructor;
|
|
|
public class EquipItemController {
|
|
|
|
|
|
public static void accept(final DDInventory inv, final DDItem item) {
|
|
|
- if ( item.getUnequippedCount() == 0 ) { return; }
|
|
|
- if ( inv.canEquip(item) || new Helper(inv).getReplaceItem(item.getSlot()) ) {
|
|
|
- item.setCountEquipped(item.getCountEquipped() + 1);
|
|
|
+ if (item.getUnequippedCount() == 0) { return; }
|
|
|
+ if (inv.canEquip(item) || new Helper(inv).getReplaceItem(item.getSlot())) {
|
|
|
+ item.adjustCounEquipped(+1);
|
|
|
inv.equipNext(item);
|
|
|
ObserverDispatch.notifySubscribers(inv, null);
|
|
|
}
|
|
|
@@ -26,32 +26,28 @@ public class EquipItemController {
|
|
|
@RequiredArgsConstructor
|
|
|
private static class Helper {
|
|
|
final DDInventory inv;
|
|
|
+ private static final String QUERY_REPLACE = "Replace Equipped Item";
|
|
|
+ private static final String REPLACE_EITHER = "Which of the following items would you like to replace?";
|
|
|
+ private static final String REPLACE_BOTH = "Do you want to replace both of the following items?";
|
|
|
+ private static final String REPLACE_ONE = "Do you want to replace the following item?";
|
|
|
|
|
|
private boolean getReplaceItem(final EquipmentSlot slot) {
|
|
|
- switch ( slot ) {
|
|
|
- case TWO_HANDS: return selectToReplaceAllOf( TWO_HANDS, MAIN_HAND, OFF_HAND );
|
|
|
- case ONE_HAND: return selectToReplaceOneOf( MAIN_HAND, OFF_HAND );
|
|
|
- case RING: return selectToReplaceOneOf( RING1, RING2 );
|
|
|
- default: return selectToReplace( slot );
|
|
|
+ switch (slot) {
|
|
|
+ case TWO_HANDS: return selectToReplaceAllOf(TWO_HANDS, MAIN_HAND, OFF_HAND);
|
|
|
+ case ONE_HAND: return selectToReplaceOneOf(MAIN_HAND, OFF_HAND);
|
|
|
+ case RING: return selectToReplaceOneOf(RING1, RING2);
|
|
|
+ default: return selectToReplace(slot);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private boolean selectToReplaceAllOf(final EquipmentSlot base,
|
|
|
final EquipmentSlot slot1, final EquipmentSlot slot2) {
|
|
|
- final EquipItemDialog panel;
|
|
|
- if ( inv.getEquipment().get(slot1).getSlot() == base ) {
|
|
|
- panel = new EquipItemDialog("Do you want to replace the following item?",
|
|
|
- slot1, inv.getEquipment().get(slot1));
|
|
|
- } else {
|
|
|
- panel = new EquipItemDialog("Do you want to replace both of the following items?",
|
|
|
- new EquipItemDialog.Tuple(slot1, inv.getEquipment().get(slot1)),
|
|
|
- new EquipItemDialog.Tuple(slot2, inv.getEquipment().get(slot2)));
|
|
|
- }
|
|
|
- if ( JOptionPane.showConfirmDialog(null,
|
|
|
- panel, "Replace Equipped Item", JOptionPane.YES_NO_OPTION)
|
|
|
- == JOptionPane.YES_OPTION ) {
|
|
|
- inv.unequip( slot1 );
|
|
|
- inv.unequip( slot2 );
|
|
|
+ if (inv.getEquipment().get(slot1).getSlot() == base) {
|
|
|
+ return selectToReplace(slot1);
|
|
|
+ } else if (JOptionPane.showConfirmDialog(null, createDialogTwoSlots(REPLACE_BOTH, slot1, slot2),
|
|
|
+ QUERY_REPLACE, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
|
|
|
+ doUnequip(slot1);
|
|
|
+ doUnequip(slot2);
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
@@ -59,32 +55,42 @@ public class EquipItemController {
|
|
|
|
|
|
private boolean selectToReplaceOneOf(final EquipmentSlot slot1,
|
|
|
final EquipmentSlot slot2) {
|
|
|
- final int choice = JOptionPane.showOptionDialog(null,
|
|
|
- new EquipItemDialog("Which of the following items would you like to replace?",
|
|
|
- new EquipItemDialog.Tuple(slot1, inv.getEquipment().get(slot1)),
|
|
|
- new EquipItemDialog.Tuple(slot2, inv.getEquipment().get(slot2))),
|
|
|
- "Replace Equipped Item", JOptionPane.YES_NO_CANCEL_OPTION,
|
|
|
- JOptionPane.QUESTION_MESSAGE, null,
|
|
|
- new String[] {"Cancel", slot1.toString(), slot2.toString()}, null );
|
|
|
- if ( choice == JOptionPane.YES_OPTION ) {
|
|
|
- inv.unequip( slot1 );
|
|
|
+ final int choice = JOptionPane.showOptionDialog(null, createDialogTwoSlots(REPLACE_EITHER, slot1, slot2),
|
|
|
+ QUERY_REPLACE, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
|
|
|
+ new String[] {"Cancel", slot1.toString(), slot2.toString()}, null);
|
|
|
+ if (choice == JOptionPane.YES_OPTION) {
|
|
|
+ doUnequip(slot1);
|
|
|
return true;
|
|
|
- } else if ( choice == JOptionPane.NO_OPTION ) {
|
|
|
- inv.unequip( slot2 );
|
|
|
+ } else if (choice == JOptionPane.NO_OPTION) {
|
|
|
+ doUnequip(slot2);
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
private boolean selectToReplace(final EquipmentSlot slot) {
|
|
|
- if ( JOptionPane.showConfirmDialog(null,
|
|
|
- new EquipItemDialog("Do you want to replace the following item?", slot, inv.getEquipment().get(slot)),
|
|
|
- "Replace Equipped Item", JOptionPane.YES_NO_OPTION)
|
|
|
- == JOptionPane.YES_OPTION ) {
|
|
|
- inv.unequip( slot );
|
|
|
+ if (JOptionPane.showConfirmDialog(null, createDialogOneSlot(slot), QUERY_REPLACE, JOptionPane.YES_NO_OPTION)
|
|
|
+ == JOptionPane.YES_OPTION) {
|
|
|
+ doUnequip(slot);
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
+ private void doUnequip(final EquipmentSlot slot) {
|
|
|
+ inv.getEquipment().get(slot).adjustCounEquipped(-1);
|
|
|
+ inv.unequip(slot);
|
|
|
+ }
|
|
|
+
|
|
|
+ private EquipItemDialog createDialogOneSlot(final EquipmentSlot slot) {
|
|
|
+ return new EquipItemDialog(REPLACE_ONE, slot, inv.getEquipment().get(slot));
|
|
|
+ }
|
|
|
+
|
|
|
+ private EquipItemDialog createDialogTwoSlots(final String message,
|
|
|
+ final EquipmentSlot slot1, final EquipmentSlot slot2) {
|
|
|
+ return new EquipItemDialog(message,
|
|
|
+ new EquipItemDialog.Tuple(slot1, inv.getEquipment().get(slot1)),
|
|
|
+ new EquipItemDialog.Tuple(slot2, inv.getEquipment().get(slot2)));
|
|
|
+ }
|
|
|
}
|
|
|
}
|