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 ground item packets to messages #139

Merged
merged 5 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.runejs.client.message.handler.rs435.chat.*;
import org.runejs.client.message.handler.rs435.console.*;
import org.runejs.client.message.handler.rs435.misc.*;
import org.runejs.client.message.handler.rs435.world.item.*;
import org.runejs.client.message.inbound.camera.*;
import org.runejs.client.message.inbound.chat.*;
import org.runejs.client.message.handler.rs435.audio.PlayQuickSongMessageHandler;
Expand All @@ -34,6 +35,7 @@
import org.runejs.client.message.inbound.widget.model.*;
import org.runejs.client.message.inbound.widget.text.*;
import org.runejs.client.message.inbound.widget.visibility.*;
import org.runejs.client.message.inbound.world.item.*;

/**
* A {@link MessageHandlerRegistry} for the RS revision 435 client.
Expand Down Expand Up @@ -124,5 +126,10 @@ public RS435HandlerRegistry() {
register(LoadStandardRegionInboundMessage.class, new LoadStandardRegionMessageHandler());
register(LoadConstructedRegionInboundMessage.class, new LoadConstructedRegionMessageHandler());
register(UpdateReferencePositionInboundMessage.class, new UpdateReferencePositionMessageHandler());

// world
register(AddGroundItemInboundMessage.class, new AddGroundItemMessageHandler());
register(UpdateGroundItemAmountInboundMessage.class, new UpdateGroundItemAmountMessageHandler());
register(RemoveGroundItemInboundMessage.class, new RemoveGroundItemMessageHandler());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.runejs.client.message.handler.rs435.world.item;

import org.runejs.client.LinkedList;
import org.runejs.client.MovedStatics;
import org.runejs.client.cache.def.FramemapDefinition;
import org.runejs.client.cache.def.OverlayDefinition;
import org.runejs.client.media.renderable.Item;
import org.runejs.client.media.renderable.actor.Player;
import org.runejs.client.media.renderable.actor.PlayerAppearance;
import org.runejs.client.message.handler.MessageHandler;
import org.runejs.client.message.inbound.world.item.AddGroundItemInboundMessage;
import org.runejs.client.scene.tile.Wall;

public class AddGroundItemMessageHandler implements MessageHandler<AddGroundItemInboundMessage> {
@Override
public void handle(AddGroundItemInboundMessage message) {
int y = OverlayDefinition.placementY + message.y;
int x = MovedStatics.placementX + message.x;

if (message.excludePlayer != -1 && message.excludePlayer != PlayerAppearance.anInt708) {
return;
}

if (x >= 0 && y >= 0 && x < 104 && y < 104) {
Item item = new Item();
item.itemCount = message.amount;
item.itemId = message.itemId;
if (Wall.groundItems[Player.worldLevel][x][y] == null)
Wall.groundItems[Player.worldLevel][x][y] = new LinkedList();
Wall.groundItems[Player.worldLevel][x][y].addLast(item);
FramemapDefinition.spawnGroundItem(y, x);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.runejs.client.message.handler.rs435.world.item;

import org.runejs.client.LinkedList;
import org.runejs.client.MovedStatics;
import org.runejs.client.cache.def.FramemapDefinition;
import org.runejs.client.cache.def.OverlayDefinition;
import org.runejs.client.media.renderable.Item;
import org.runejs.client.media.renderable.actor.Player;
import org.runejs.client.message.handler.MessageHandler;
import org.runejs.client.message.inbound.world.item.RemoveGroundItemInboundMessage;
import org.runejs.client.scene.tile.Wall;

public class RemoveGroundItemMessageHandler implements MessageHandler<RemoveGroundItemInboundMessage> {
@Override
public void handle(RemoveGroundItemInboundMessage message) {
int y = OverlayDefinition.placementY + message.y;
int x = MovedStatics.placementX + message.x;

if (x >= 0 && y >= 0 && x < 104 && y < 104) {
LinkedList linkedList = Wall.groundItems[Player.worldLevel][x][y];
if (linkedList != null) {
for (Item item = (Item) linkedList.peekFirst(); item != null; item = (Item) linkedList.pollFirst()) {
if ((0x7fff & message.itemId) == item.itemId) {
item.unlink();
break;
}
}
if (linkedList.peekFirst() == null)
Wall.groundItems[Player.worldLevel][x][y] = null;
FramemapDefinition.spawnGroundItem(y, x);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.runejs.client.message.handler.rs435.world.item;

import org.runejs.client.LinkedList;
import org.runejs.client.MovedStatics;
import org.runejs.client.cache.def.FramemapDefinition;
import org.runejs.client.cache.def.OverlayDefinition;
import org.runejs.client.media.renderable.Item;
import org.runejs.client.media.renderable.actor.Player;
import org.runejs.client.message.handler.MessageHandler;
import org.runejs.client.message.inbound.world.item.UpdateGroundItemAmountInboundMessage;
import org.runejs.client.scene.tile.Wall;

public class UpdateGroundItemAmountMessageHandler implements MessageHandler<UpdateGroundItemAmountInboundMessage> {
@Override
public void handle(UpdateGroundItemAmountInboundMessage message) {
int y = OverlayDefinition.placementY + message.y;
int x = MovedStatics.placementX + message.x;

if (x >= 0 && y >= 0 && x < 104 && y < 104) {
LinkedList linkedList = Wall.groundItems[Player.worldLevel][x][y];
if (linkedList != null) {
for (Item item = (Item) linkedList.peekFirst(); item != null; item = (Item) linkedList.pollFirst()) {
if (item.itemId == (message.itemId & 0x7fff) && message.existingAmount == item.itemCount) {
item.itemCount = message.newAmount;
break;
}
}
FramemapDefinition.spawnGroundItem(y, x);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.runejs.client.message.inbound.world.item;

import org.runejs.client.message.InboundMessage;

public class AddGroundItemInboundMessage implements InboundMessage {
public final int itemId;

public final int amount;

public final int x;

public final int y;

/**
* The client will avoid spawning this item if excludePlayer matches the local player.
*
* Set to -1 if no player should be excluded.
*/
public final int excludePlayer;

public AddGroundItemInboundMessage(int itemId, int amount, int x, int y, int excludePlayer) {
this.itemId = itemId;
this.amount = amount;
this.x = x;
this.y = y;
this.excludePlayer = excludePlayer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.runejs.client.message.inbound.world.item;

import org.runejs.client.message.InboundMessage;

public class RemoveGroundItemInboundMessage implements InboundMessage {
public final int itemId;

public final int x;

public final int y;

public RemoveGroundItemInboundMessage(int itemId, int x, int y) {
this.itemId = itemId;
this.x = x;
this.y = y;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.runejs.client.message.inbound.world.item;

import org.runejs.client.message.InboundMessage;

public class UpdateGroundItemAmountInboundMessage implements InboundMessage {
public final int itemId;

public final int x;

public final int y;

public final int existingAmount;

public final int newAmount;

public UpdateGroundItemAmountInboundMessage(int itemId, int x, int y, int existingAmount, int newAmount) {
this.itemId = itemId;
this.x = x;
this.y = y;
this.existingAmount = existingAmount;
this.newAmount = newAmount;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/runejs/client/net/IncomingPackets.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static boolean parseIncomingPackets() {
return true;
}
// object/ground item update packets?
if(opcode == PacketType.PLAY_SOUND_AT_POSITION.getOpcode() || opcode == PacketType.UPDATE_GROUND_ITEM_AMOUNT.getOpcode() || opcode == PacketType.TRANSFORM_PLAYER_TO_OBJECT.getOpcode() || opcode == PacketType.ADD_GROUND_ITEM_EXCLUDE_SOME_PLAYER.getOpcode() || opcode == PacketType.CREATE_STATIONARY_GFX.getOpcode() || opcode == PacketType.CREATE_PROJECTILE.getOpcode() || opcode == PacketType.REMOVE_GROUND_ITEM.getOpcode() || opcode == PacketType.ADD_GROUND_ITEM.getOpcode() || opcode == PacketType.ROTATE_ANIMATE_OBJECT.getOpcode() || opcode == PacketType.REMOVE_OBJECT.getOpcode() || opcode == PacketType.SPAWN_OBJECT.getOpcode()) {
if(opcode == PacketType.PLAY_SOUND_AT_POSITION.getOpcode() || opcode == PacketType.TRANSFORM_PLAYER_TO_OBJECT.getOpcode() || opcode == PacketType.CREATE_STATIONARY_GFX.getOpcode() || opcode == PacketType.CREATE_PROJECTILE.getOpcode() || opcode == PacketType.ROTATE_ANIMATE_OBJECT.getOpcode() || opcode == PacketType.REMOVE_OBJECT.getOpcode() || opcode == PacketType.SPAWN_OBJECT.getOpcode()) {
parseMapIncomingPacket();
opcode = -1;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.runejs.client.net.codec.runejs435.decoder.widget.model.*;
import org.runejs.client.net.codec.runejs435.decoder.widget.text.*;
import org.runejs.client.net.codec.runejs435.decoder.widget.visibility.*;
import org.runejs.client.net.codec.runejs435.decoder.world.item.*;
import org.runejs.client.net.codec.runejs435.encoder.chat.*;
import org.runejs.client.net.codec.runejs435.encoder.console.*;
import org.runejs.client.net.codec.runejs435.encoder.examine.*;
Expand Down Expand Up @@ -185,5 +186,11 @@ private void registerDecoders() {
register(PacketType.CUTSCENE_CAMERA_MOVE_TO.getOpcode(), new CutsceneCameraMoveToMessageDecoder());
register(PacketType.CLOSE_CUTSCENE.getOpcode(), new CutsceneExitMessageDecoder());
register(PacketType.SHAKE_CAMERA.getOpcode(), new ShakeCameraMessageDecoder());

// world
register(PacketType.ADD_GROUND_ITEM.getOpcode(), new AddGroundItemMessageDecoder());
register(PacketType.ADD_GROUND_ITEM_EXCLUDE_SOME_PLAYER.getOpcode(), new AddGroundItemExcludePlayerMessageDecoder());
register(PacketType.UPDATE_GROUND_ITEM_AMOUNT.getOpcode(), new UpdateGroundItemAmountMessageDecoder());
register(PacketType.REMOVE_GROUND_ITEM.getOpcode(), new RemoveGroundItemMessageDecoder());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.runejs.client.net.codec.runejs435.decoder.world.item;

import org.runejs.client.message.inbound.world.item.AddGroundItemInboundMessage;
import org.runejs.client.net.PacketBuffer;
import org.runejs.client.net.codec.MessageDecoder;

public class AddGroundItemExcludePlayerMessageDecoder implements MessageDecoder<AddGroundItemInboundMessage> {
@Override
public AddGroundItemInboundMessage decode(PacketBuffer buffer) {
int amount = buffer.getUnsignedShortLE();
int playerToExclude = buffer.getUnsignedShortLE();
int itemId = buffer.getUnsignedShortBE();
int positionInfo = buffer.getUnsignedByte();
int x = ((0x71 & positionInfo) >> 4);
int y = (0x7 & positionInfo);

return new AddGroundItemInboundMessage(itemId, amount, x, y, playerToExclude);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.runejs.client.net.codec.runejs435.decoder.world.item;

import org.runejs.client.message.inbound.world.item.AddGroundItemInboundMessage;
import org.runejs.client.net.PacketBuffer;
import org.runejs.client.net.codec.MessageDecoder;

public class AddGroundItemMessageDecoder implements MessageDecoder<AddGroundItemInboundMessage> {
@Override
public AddGroundItemInboundMessage decode(PacketBuffer buffer) {
int itemId = buffer.getUnsignedShortLE();
int amount = buffer.getUnsignedShortBE();
int positionInfo = buffer.getUnsignedByte();
int y = (positionInfo & 0x7);
int x = ((0x7a & positionInfo) >> 4);

return new AddGroundItemInboundMessage(itemId, amount, x, y, -1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.runejs.client.net.codec.runejs435.decoder.world.item;

import org.runejs.client.message.inbound.world.item.RemoveGroundItemInboundMessage;
import org.runejs.client.net.PacketBuffer;
import org.runejs.client.net.codec.MessageDecoder;

public class RemoveGroundItemMessageDecoder implements MessageDecoder<RemoveGroundItemInboundMessage> {
@Override
public RemoveGroundItemInboundMessage decode(PacketBuffer buffer) {
int positionInfo = buffer.getUnsignedByte();
int y = (positionInfo & 0x7);
int x = ((0x7a & positionInfo) >> 4);
int itemId = buffer.getUnsignedShortBE();

return new RemoveGroundItemInboundMessage(itemId, x, y);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.runejs.client.net.codec.runejs435.decoder.world.item;

import org.runejs.client.message.inbound.world.item.UpdateGroundItemAmountInboundMessage;
import org.runejs.client.net.PacketBuffer;
import org.runejs.client.net.codec.MessageDecoder;

public class UpdateGroundItemAmountMessageDecoder implements MessageDecoder<UpdateGroundItemAmountInboundMessage> {
@Override
public UpdateGroundItemAmountInboundMessage decode(PacketBuffer buffer) {
int positionInfo = buffer.getUnsignedByte();
int y = (positionInfo & 0x7);
int x = ((0x7a & positionInfo) >> 4);
int itemId = buffer.getUnsignedShortBE();
int existingAmount = buffer.getUnsignedShortBE();
int newAmount = buffer.getUnsignedShortBE();

return new UpdateGroundItemAmountInboundMessage(itemId, x, y, existingAmount, newAmount);
}
}