Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: convert NPC interaction packets to new system #99

Merged
merged 7 commits into from
Feb 28, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.runejs.client.media.renderable.actor.Pathfinding;
import org.runejs.client.media.renderable.actor.Player;
import org.runejs.client.media.renderable.actor.PlayerAppearance;
import org.runejs.client.message.outbound.interactions.ObjectInteractionOutboundMessage;
import org.runejs.client.message.outbound.interactions.*;
import org.runejs.client.net.ISAAC;
import org.runejs.client.net.OutgoingPackets;
import org.runejs.client.net.PacketBuffer;
Expand Down Expand Up @@ -868,8 +868,8 @@ public static void processMenuActions(int arg1) {
ClientScriptRunner.crossX = Class57.clickX;
MovedStatics.crossY = RSString.clickY;
LinkedList.crossType = 2;
SceneCluster.packetBuffer.putPacket(57);
SceneCluster.packetBuffer.putShortBE(npcIdx);

OutgoingPackets.sendMessage(new NPCInteractionOutboundMessage(2, npcIdx));
}
}
if(action == ActionRowType.USE_ITEM_ON_NPC.getId()) {
Expand Down Expand Up @@ -920,8 +920,8 @@ public static void processMenuActions(int arg1) {
LinkedList.crossType = 2;
ClientScriptRunner.crossX = Class57.clickX;
MovedStatics.crossY = RSString.clickY;
SceneCluster.packetBuffer.putPacket(0);
SceneCluster.packetBuffer.putShortBE(npcIdx);

OutgoingPackets.sendMessage(new NPCInteractionOutboundMessage(4, npcIdx));
}
}
if(action == ActionRowType.USE_ITEM_ON_INVENTORY_ITEM.getId()) {
Expand Down Expand Up @@ -1005,8 +1005,8 @@ public static void processMenuActions(int arg1) {
MovedStatics.crossY = RSString.clickY;
OverlayDefinition.crossIndex = 0;
LinkedList.crossType = 2;
SceneCluster.packetBuffer.putPacket(63);
SceneCluster.packetBuffer.putShortLE(npcIdx);

OutgoingPackets.sendMessage(new NPCInteractionOutboundMessage(1, npcIdx));
}
}
if(action == ActionRowType.INTERACT_WITH_WORLD_ITEM_OPTION_1.getId()) {
Expand Down Expand Up @@ -1300,8 +1300,8 @@ public static void processMenuActions(int arg1) {
ClientScriptRunner.crossX = Class57.clickX;
MovedStatics.crossY = RSString.clickY;
OverlayDefinition.crossIndex = 0;
SceneCluster.packetBuffer.putPacket(153);
SceneCluster.packetBuffer.putShortLE(npcIdx);

OutgoingPackets.sendMessage(new NPCInteractionOutboundMessage(5, npcIdx));
}
}
if(action == ActionRowType.USE_ITEM_ON_WORLD_ITEM.getId()) {
Expand Down Expand Up @@ -1345,8 +1345,8 @@ public static void processMenuActions(int arg1) {
ClientScriptRunner.crossX = Class57.clickX;
OverlayDefinition.crossIndex = 0;
MovedStatics.crossY = RSString.clickY;
SceneCluster.packetBuffer.putPacket(116);
SceneCluster.packetBuffer.putShortLE(npcIdx);

OutgoingPackets.sendMessage(new NPCInteractionOutboundMessage(3, npcIdx));
}
}
if(MovedStatics.itemSelected != 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.runejs.client.message.outbound.interactions;

import org.runejs.client.message.OutboundMessage;

/**
* Represents a message sent to the server when a player interacts with an NPC.
*
* e.g. talking, trading or pickpocketing
*/
public class NPCInteractionOutboundMessage implements OutboundMessage {
/**
* Which option on the npc was clicked
*
* i.e. 1 = first option, 2 = second option, etc.
*/
public final int option;

/**
* The index of the npc
*/
public final int npcIndex;

public NPCInteractionOutboundMessage(int option, int npcIndex) {
this.option = option;
this.npcIndex = npcIndex;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.runejs.client.net.codec.runejs435;

import org.runejs.client.message.outbound.interactions.ObjectInteractionOutboundMessage;
import org.runejs.client.message.outbound.*;
import org.runejs.client.net.PacketType;
import org.runejs.client.net.codec.MessagePacketCodec;
import org.runejs.client.net.codec.runejs435.encoder.*;
Expand All @@ -18,6 +18,7 @@ public RuneJS435PacketCodec() {
}

private void registerEncoders() {
register(NPCInteractionOutboundMessage.class, new NPCInteractionMessageEncoder());
register(ObjectInteractionOutboundMessage.class, new ObjectInteractionMessageEncoder());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.runejs.client.net.codec.runejs435.encoder.interactions;

import org.runejs.client.message.outbound.interactions.NPCInteractionOutboundMessage;
import org.runejs.client.net.OutgoingPackets;
import org.runejs.client.net.PacketBuffer;
import org.runejs.client.net.codec.MessageEncoder;

/**
* Encodes an {@link NPCInteractionOutboundMessage} into a {@link PacketBuffer}.
*/
public class NPCInteractionMessageEncoder implements MessageEncoder<NPCInteractionOutboundMessage> {

@Override
public PacketBuffer encode(NPCInteractionOutboundMessage message) {
switch (message.option) {
case 1:
return encodeOption1Interaction(message);
case 2:
return encodeOption2Interaction(message);
case 3:
return encodeOption3Interaction(message);
case 4:
return encodeOption4Interaction(message);
case 5:
return encodeOption5Interaction(message);
default:
throw new RuntimeException("Invalid option: " + message.option);
}
}

/**
* Encodes an interaction for the first option.
* @param message The message to encode
* @return The encoded packet
*/
private PacketBuffer encodeOption1Interaction(NPCInteractionOutboundMessage message) {
PacketBuffer buffer = OutgoingPackets.openFixedSizePacket(2, 63);

buffer.putShortLE(message.npcIndex);

return buffer;
}

/**
* Encodes an interaction for the second option.
* @param message The message to encode
* @return The encoded packet
*/
private PacketBuffer encodeOption2Interaction(NPCInteractionOutboundMessage message) {
PacketBuffer buffer = OutgoingPackets.openFixedSizePacket(2, 57);

buffer.putShortBE(message.npcIndex);

return buffer;
}

/**
* Encodes an interaction for the third option.
* @param message The message to encode
* @return The encoded packet
*/
private PacketBuffer encodeOption3Interaction(NPCInteractionOutboundMessage message) {
PacketBuffer buffer = OutgoingPackets.openFixedSizePacket(2, 116);

buffer.putShortLE(message.npcIndex);

return buffer;
}

/**
* Encodes an interaction for the fourth option.
* @param message The message to encode
* @return The encoded packet
*/
private PacketBuffer encodeOption4Interaction(NPCInteractionOutboundMessage message) {
PacketBuffer buffer = OutgoingPackets.openFixedSizePacket(2, 0);

buffer.putShortBE(message.npcIndex);

return buffer;
}

/**
* Encodes an interaction for the fifth option.
* @param message The message to encode
* @return The encoded packet
*/
private PacketBuffer encodeOption5Interaction(NPCInteractionOutboundMessage message) {
PacketBuffer buffer = OutgoingPackets.openFixedSizePacket(2, 153);

buffer.putShortLE(message.npcIndex);

return buffer;
}
}