mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-06 16:51:37 +00:00
Merge branch 'master' of bitbucket.org:Major-/apollo.
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
<description>Adds support for consumables (e.g. potions, food).</description>
|
<description>Adds support for consumables (e.g. potions, food).</description>
|
||||||
<authors>
|
<authors>
|
||||||
<author>Major</author>
|
<author>Major</author>
|
||||||
<author>Ryley Kimmel</author>
|
<author>Ryley</author>
|
||||||
</authors>
|
</authors>
|
||||||
<scripts>
|
<scripts>
|
||||||
<script>consumable.rb</script>
|
<script>consumable.rb</script>
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
package org.apollo.game.message.handler;
|
package org.apollo.game.message.handler;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Deque;
|
||||||
|
|
||||||
import org.apollo.game.message.Message;
|
import org.apollo.game.message.Message;
|
||||||
import org.apollo.game.model.entity.Player;
|
import org.apollo.game.model.entity.Player;
|
||||||
|
|
||||||
@@ -7,6 +11,7 @@ import org.apollo.game.model.entity.Player;
|
|||||||
* A chain of message handlers.
|
* A chain of message handlers.
|
||||||
*
|
*
|
||||||
* @author Graham
|
* @author Graham
|
||||||
|
* @author Ryley
|
||||||
* @param <M> The type of message handled by this chain.
|
* @param <M> The type of message handled by this chain.
|
||||||
*/
|
*/
|
||||||
public final class MessageHandlerChain<M extends Message> {
|
public final class MessageHandlerChain<M extends Message> {
|
||||||
@@ -14,7 +19,7 @@ public final class MessageHandlerChain<M extends Message> {
|
|||||||
/**
|
/**
|
||||||
* The handlers.
|
* The handlers.
|
||||||
*/
|
*/
|
||||||
private MessageHandler<M>[] handlers;
|
private final Deque<MessageHandler<M>> handlers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the message handler chain.
|
* Creates the message handler chain.
|
||||||
@@ -23,7 +28,7 @@ public final class MessageHandlerChain<M extends Message> {
|
|||||||
*/
|
*/
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public MessageHandlerChain(MessageHandler<M>... handlers) {
|
public MessageHandlerChain(MessageHandler<M>... handlers) {
|
||||||
this.handlers = handlers;
|
this.handlers = new ArrayDeque<>(Arrays.asList(handlers));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,36 +36,24 @@ public final class MessageHandlerChain<M extends Message> {
|
|||||||
*
|
*
|
||||||
* @param handler The handler.
|
* @param handler The handler.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public void addLast(MessageHandler<M> handler) {
|
public void addLast(MessageHandler<M> handler) {
|
||||||
MessageHandler<M>[] old = handlers;
|
handlers.addLast(handler);
|
||||||
handlers = new MessageHandler[old.length + 1];
|
|
||||||
System.arraycopy(old, 0, handlers, 0, old.length);
|
|
||||||
handlers[old.length] = handler;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the message, passing it down the chain until the chain is broken or the message reaches the end of the
|
* Handles the message, passing it down the chain until the chain is broken
|
||||||
* chain.
|
* or the message reaches the end of the chain.
|
||||||
*
|
*
|
||||||
* @param player The player.
|
* @param player The player.
|
||||||
* @param message The message.
|
* @param message The message.
|
||||||
*/
|
*/
|
||||||
public void handle(Player player, M message) {
|
public void handle(Player player, M message) {
|
||||||
final boolean[] running = new boolean[1];
|
MessageHandlerContext context = new MessageHandlerContext();
|
||||||
running[0] = true;
|
|
||||||
|
|
||||||
MessageHandlerContext ctx = new MessageHandlerContext() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void breakHandlerChain() {
|
|
||||||
running[0] = false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
for (MessageHandler<M> handler : handlers) {
|
for (MessageHandler<M> handler : handlers) {
|
||||||
handler.handle(ctx, player, message);
|
handler.handle(context, player, message);
|
||||||
if (!running[0]) {
|
|
||||||
|
if (context.isBroken()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,34 @@
|
|||||||
package org.apollo.game.message.handler;
|
package org.apollo.game.message.handler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides operations specific to a {@link MessageHandler} in a {@link MessageHandlerChain}.
|
* Provides operations specific to a {@link MessageHandler} in a
|
||||||
|
* {@link MessageHandlerChain}.
|
||||||
*
|
*
|
||||||
* @author Graham
|
* @author Graham
|
||||||
|
* @author Ryley
|
||||||
*/
|
*/
|
||||||
public abstract class MessageHandlerContext {
|
public final class MessageHandlerContext {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Denotes whether or not this handler chain is broken.
|
||||||
|
*/
|
||||||
|
private boolean broken;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Breaks the handler chain.
|
* Breaks the handler chain.
|
||||||
*/
|
*/
|
||||||
public abstract void breakHandlerChain();
|
public void breakHandlerChain() {
|
||||||
|
broken = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether or not this handler chain is broken.
|
||||||
|
*
|
||||||
|
* @return {@code true} if this handler chain is broken, otherwise
|
||||||
|
* {@code false}.
|
||||||
|
*/
|
||||||
|
public boolean isBroken() {
|
||||||
|
return broken;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -17,7 +17,7 @@ import org.apollo.util.LanguageUtil;
|
|||||||
*
|
*
|
||||||
* @author Major
|
* @author Major
|
||||||
* @author Graham
|
* @author Graham
|
||||||
* @author Ryley Kimmel <ryley.kimmel@live.com>
|
* @author Ryley
|
||||||
*/
|
*/
|
||||||
public final class EquipItemHandler extends MessageHandler<ItemOptionMessage> {
|
public final class EquipItemHandler extends MessageHandler<ItemOptionMessage> {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user