From c205a52389aa170c1ec57660975e9c8449e0fdcc Mon Sep 17 00:00:00 2001 From: thispixel Date: Fri, 27 Jun 2014 12:52:53 +0100 Subject: [PATCH] Fix 317 revision npc interaction packets, fixed npc indexes being +1 and added npc action verification. --- data/events.xml | 6 ++++ .../impl/NpcActionVerificationHandler.java | 33 +++++++++++++++++++ .../game/event/impl/NpcActionEvent.java | 2 +- .../r317/FirstNpcActionEventDecoder.java | 7 ++-- .../apollo/net/release/r317/Release317.java | 6 ++-- .../r317/SecondNpcActionEventDecoder.java | 7 ++-- .../r317/ThirdNpcActionEventDecoder.java | 4 +-- src/org/apollo/util/MobRepository.java | 14 ++++++++ 8 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 src/org/apollo/game/event/handler/impl/NpcActionVerificationHandler.java diff --git a/data/events.xml b/data/events.xml index e544028c..93017663 100644 --- a/data/events.xml +++ b/data/events.xml @@ -90,4 +90,10 @@ org.apollo.game.event.handler.impl.ItemVerificationHandler + + org.apollo.game.event.impl.NpcActionEvent + + org.apollo.game.event.handler.impl.NpcActionVerificationHandler + + \ No newline at end of file diff --git a/src/org/apollo/game/event/handler/impl/NpcActionVerificationHandler.java b/src/org/apollo/game/event/handler/impl/NpcActionVerificationHandler.java new file mode 100644 index 00000000..f6a9972b --- /dev/null +++ b/src/org/apollo/game/event/handler/impl/NpcActionVerificationHandler.java @@ -0,0 +1,33 @@ +package org.apollo.game.event.handler.impl; + +import org.apollo.game.event.handler.EventHandler; +import org.apollo.game.event.handler.EventHandlerContext; +import org.apollo.game.event.impl.NpcActionEvent; +import org.apollo.game.model.Npc; +import org.apollo.game.model.Player; +import org.apollo.game.model.World; +import org.apollo.game.model.WorldConstants; + +/** + * An {@link EventHandler} that verifies {@link org.apollo.game.event.impl.NpcActionEvent} + * + * @author Stuart + */ +public class NpcActionVerificationHandler extends EventHandler { + + @Override + public void handle(EventHandlerContext ctx, Player player, NpcActionEvent event) { + if(event.getIndex() < 0 || event.getIndex() > WorldConstants.MAXIMUM_NPCS) { + ctx.breakHandlerChain(); + return; + } + + Npc npc = (Npc)World.getWorld().getNpcRepository().get(event.getIndex()); + + if(npc == null || !player.getPosition().isWithinDistance(npc.getPosition(), 15)) { + ctx.breakHandlerChain(); + return; + } + } + +} \ No newline at end of file diff --git a/src/org/apollo/game/event/impl/NpcActionEvent.java b/src/org/apollo/game/event/impl/NpcActionEvent.java index 5196139c..7cc247fc 100644 --- a/src/org/apollo/game/event/impl/NpcActionEvent.java +++ b/src/org/apollo/game/event/impl/NpcActionEvent.java @@ -29,7 +29,7 @@ public abstract class NpcActionEvent extends Event { */ public NpcActionEvent(int option, int index) { this.option = option; - this.index = index; + this.index = index - 1; } /** diff --git a/src/org/apollo/net/release/r317/FirstNpcActionEventDecoder.java b/src/org/apollo/net/release/r317/FirstNpcActionEventDecoder.java index b032e894..0eeda0c6 100644 --- a/src/org/apollo/net/release/r317/FirstNpcActionEventDecoder.java +++ b/src/org/apollo/net/release/r317/FirstNpcActionEventDecoder.java @@ -1,10 +1,7 @@ package org.apollo.net.release.r317; import org.apollo.game.event.impl.FirstNpcActionEvent; -import org.apollo.net.codec.game.DataTransformation; -import org.apollo.net.codec.game.DataType; -import org.apollo.net.codec.game.GamePacket; -import org.apollo.net.codec.game.GamePacketReader; +import org.apollo.net.codec.game.*; import org.apollo.net.release.EventDecoder; /** @@ -17,7 +14,7 @@ public final class FirstNpcActionEventDecoder extends EventDecoder implements Iterable { return false; } + /** + * Gets the mob at the given index + * + * @param index The index of the mob + * @return The mob instance + */ + public Mob get(int index) { + if (index < 0 || index >= mobs.length) { + throw new IndexOutOfBoundsException("Mob index is out of bounds."); + } + + return mobs[index]; + } + /** * Gets the size of this repository. *