mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-06 16:51:37 +00:00
Use Optional in InterfaceSet.
This commit is contained in:
@@ -2,6 +2,7 @@ package org.apollo.game.model.inter;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.apollo.game.message.impl.CloseInterfaceMessage;
|
import org.apollo.game.message.impl.CloseInterfaceMessage;
|
||||||
import org.apollo.game.message.impl.EnterAmountMessage;
|
import org.apollo.game.message.impl.EnterAmountMessage;
|
||||||
@@ -34,12 +35,12 @@ public final class InterfaceSet {
|
|||||||
/**
|
/**
|
||||||
* The current enter amount listener.
|
* The current enter amount listener.
|
||||||
*/
|
*/
|
||||||
private EnterAmountListener amountListener;
|
private Optional<EnterAmountListener> amountListener = Optional.empty();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The current chat box dialogue listener.
|
* The current chat box dialogue listener.
|
||||||
*/
|
*/
|
||||||
private DialogueListener dialogueListener;
|
private Optional<DialogueListener> dialogueListener = Optional.empty();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A map of open interfaces.
|
* A map of open interfaces.
|
||||||
@@ -49,12 +50,12 @@ public final class InterfaceSet {
|
|||||||
/**
|
/**
|
||||||
* The current listener.
|
* The current listener.
|
||||||
*/
|
*/
|
||||||
private InterfaceListener listener;
|
private Optional<InterfaceListener> listener = Optional.empty();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The player whose interfaces are being managed.
|
* The player whose interfaces are being managed.
|
||||||
*/
|
*/
|
||||||
private final Player player; // TODO: maybe switch to a listener system like the inventory?
|
private final Player player;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an interface set.
|
* Creates an interface set.
|
||||||
@@ -72,8 +73,8 @@ public final class InterfaceSet {
|
|||||||
* @return {@code true} if the message handler chain should be broken.
|
* @return {@code true} if the message handler chain should be broken.
|
||||||
*/
|
*/
|
||||||
public boolean buttonClicked(int button) {
|
public boolean buttonClicked(int button) {
|
||||||
if (dialogueListener != null) {
|
if (dialogueListener.isPresent()) {
|
||||||
return dialogueListener.buttonClicked(button);
|
return dialogueListener.get().buttonClicked(button);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -86,21 +87,6 @@ public final class InterfaceSet {
|
|||||||
player.send(new CloseInterfaceMessage());
|
player.send(new CloseInterfaceMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* An internal method for closing the interface, notifying the listener if appropriate, but not sending any
|
|
||||||
* messages.
|
|
||||||
*/
|
|
||||||
private void closeAndNotify() {
|
|
||||||
amountListener = null;
|
|
||||||
dialogueListener = null;
|
|
||||||
|
|
||||||
interfaces.clear();
|
|
||||||
if (listener != null) {
|
|
||||||
listener.interfaceClosed();
|
|
||||||
listener = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if this interface sets contains the specified interface.
|
* Checks if this interface sets contains the specified interface.
|
||||||
*
|
*
|
||||||
@@ -125,8 +111,8 @@ public final class InterfaceSet {
|
|||||||
* Called when the player has clicked the "Click here to continue" button on a dialogue.
|
* Called when the player has clicked the "Click here to continue" button on a dialogue.
|
||||||
*/
|
*/
|
||||||
public void continueRequested() {
|
public void continueRequested() {
|
||||||
if (dialogueListener != null) {
|
if (dialogueListener.isPresent()) {
|
||||||
dialogueListener.continued();
|
dialogueListener.get().continued();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,9 +122,9 @@ public final class InterfaceSet {
|
|||||||
* @param amount The amount.
|
* @param amount The amount.
|
||||||
*/
|
*/
|
||||||
public void enteredAmount(int amount) {
|
public void enteredAmount(int amount) {
|
||||||
if (amountListener != null) {
|
if (amountListener.isPresent()) {
|
||||||
amountListener.amountEntered(amount);
|
amountListener.get().amountEntered(amount);
|
||||||
amountListener = null;
|
amountListener = Optional.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,8 +144,8 @@ public final class InterfaceSet {
|
|||||||
public void openDialogue(DialogueListener listener, int dialogueId) {
|
public void openDialogue(DialogueListener listener, int dialogueId) {
|
||||||
closeAndNotify();
|
closeAndNotify();
|
||||||
|
|
||||||
this.dialogueListener = listener;
|
this.dialogueListener = Optional.ofNullable(listener);
|
||||||
this.listener = listener;
|
this.listener = Optional.ofNullable(listener);
|
||||||
|
|
||||||
interfaces.put(InterfaceType.DIALOGUE, dialogueId);
|
interfaces.put(InterfaceType.DIALOGUE, dialogueId);
|
||||||
player.send(new OpenDialogueInterfaceMessage(dialogueId));
|
player.send(new OpenDialogueInterfaceMessage(dialogueId));
|
||||||
@@ -180,7 +166,7 @@ public final class InterfaceSet {
|
|||||||
* @param listener The enter amount listener.
|
* @param listener The enter amount listener.
|
||||||
*/
|
*/
|
||||||
public void openEnterAmountDialogue(EnterAmountListener listener) {
|
public void openEnterAmountDialogue(EnterAmountListener listener) {
|
||||||
amountListener = listener;
|
amountListener = Optional.of(listener);
|
||||||
player.send(new EnterAmountMessage());
|
player.send(new EnterAmountMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,7 +187,7 @@ public final class InterfaceSet {
|
|||||||
*/
|
*/
|
||||||
public void openWindow(InterfaceListener listener, int windowId) {
|
public void openWindow(InterfaceListener listener, int windowId) {
|
||||||
closeAndNotify();
|
closeAndNotify();
|
||||||
this.listener = listener;
|
this.listener = Optional.ofNullable(listener);
|
||||||
|
|
||||||
interfaces.put(InterfaceType.WINDOW, windowId);
|
interfaces.put(InterfaceType.WINDOW, windowId);
|
||||||
player.send(new OpenInterfaceMessage(windowId));
|
player.send(new OpenInterfaceMessage(windowId));
|
||||||
@@ -226,7 +212,7 @@ public final class InterfaceSet {
|
|||||||
*/
|
*/
|
||||||
public void openWindowWithSidebar(InterfaceListener listener, int windowId, int sidebarId) {
|
public void openWindowWithSidebar(InterfaceListener listener, int windowId, int sidebarId) {
|
||||||
closeAndNotify();
|
closeAndNotify();
|
||||||
this.listener = listener;
|
this.listener = Optional.ofNullable(listener);
|
||||||
|
|
||||||
interfaces.put(InterfaceType.WINDOW, windowId);
|
interfaces.put(InterfaceType.WINDOW, windowId);
|
||||||
interfaces.put(InterfaceType.SIDEBAR, sidebarId);
|
interfaces.put(InterfaceType.SIDEBAR, sidebarId);
|
||||||
@@ -243,4 +229,19 @@ public final class InterfaceSet {
|
|||||||
return interfaces.size();
|
return interfaces.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An internal method for closing the interface, notifying the listener if appropriate, but not sending any
|
||||||
|
* messages.
|
||||||
|
*/
|
||||||
|
private void closeAndNotify() {
|
||||||
|
amountListener = Optional.empty();
|
||||||
|
dialogueListener = Optional.empty();
|
||||||
|
|
||||||
|
interfaces.clear();
|
||||||
|
if (listener.isPresent()) {
|
||||||
|
listener.get().interfaceClosed();
|
||||||
|
listener = Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user