Share Pastebin
Guest
Public paste!

Ryan/Thing1

By: a guest | Mar 20th, 2010 | Syntax: None | Size: 16.03 KB | Hits: 65 | Expires: Never
Copy text to clipboard
  1. package com.rs2hd.packethandler;
  2.  
  3. import org.apache.mina.common.IoSession;
  4.  
  5. import com.rs2hd.Constants;
  6. import com.rs2hd.event.CoordinateEvent;
  7. import com.rs2hd.model.Bank;
  8. import com.rs2hd.model.Equipment;
  9. import com.rs2hd.model.Inventory;
  10. import com.rs2hd.model.Item;
  11. import com.rs2hd.model.ItemDefinition;
  12. import com.rs2hd.model.Location;
  13. import com.rs2hd.model.NPC;
  14. import com.rs2hd.model.Player;
  15. import com.rs2hd.model.World;
  16. import com.rs2hd.net.Packet;
  17. import com.rs2hd.script.ScriptManager;
  18. import com.rs2hd.util.ItemConfiguration;
  19. import com.rs2hd.util.log.Logger;
  20.  
  21. /**
  22.  * Handles all packets to do with items.
  23.  *
  24.  * @author Graham
  25.  *
  26.  */
  27. public class ItemPacketHandler implements PacketHandler {
  28.  
  29.     private final Logger logger = Logger.getInstance();
  30.  
  31.     @Override
  32.     public void handlePacket(Player player, IoSession session, Packet packet) {
  33.         switch (packet.getId()) {
  34.  
  35.         case 166:
  36.             /*
  37.              * Switch items.
  38.              */
  39.             switchItems(player, session, packet);
  40.             break;
  41.         case 179:
  42.             /*
  43.              * Transfer items between two interfaces.
  44.              */
  45.             switchItems2(player, session, packet);
  46.             break;
  47.         case 124:
  48.             /*
  49.              * Equip.
  50.              */
  51.             equip(player, session, packet);
  52.             break;
  53.         case 241:
  54.             /*
  55.              * Item option 1.
  56.              */
  57.             itemOption1(player, session, packet);
  58.             break;
  59.         case 152:
  60.             /*
  61.              * Item option 2.
  62.              */
  63.             itemOption2(player, session, packet);
  64.             break;
  65.         case 89:
  66.             /*
  67.              * Item select.
  68.              */
  69.             itemSelect(player, session, packet);
  70.             break;
  71.         case 186:
  72.             /*
  73.              * Item operate.
  74.              */
  75.             itemOperate(player, session, packet);
  76.             break;
  77.         case 71:
  78.             /*
  79.              * Drop item.
  80.              */
  81.             drop(player, session, packet);
  82.             break;
  83.         case 105:
  84.             /*
  85.              * Take item.
  86.              */
  87.             take(player, session, packet);
  88.             break;
  89.         case 40:
  90.             /*
  91.              * Item on item.
  92.              */
  93.             itemOnItem(player, session, packet);
  94.             break;
  95.         case 224:
  96.             /*
  97.              * Item on object.
  98.              */
  99.             itemOnObject(player, session, packet);
  100.             break;
  101.         case 131:
  102.             /*
  103.              * Item on player.
  104.              */
  105.             itemOnPlayer(player, session, packet);
  106.             break;
  107.         case 12:
  108.             /*
  109.              * Item on npc.
  110.              */
  111.             itemOnNPC(player, session, packet);
  112.             break;
  113.         }
  114.     }
  115.  
  116.     private void itemOnPlayer(Player player, IoSession session, Packet packet) {
  117.         packet.readInt();// junk
  118.         int playerId = packet.readShort() & 0xFFFF;
  119.         int slot = packet.readLEShortA() & 0xFFFF;
  120.         int id = packet.readShortA() & 0xFFFF;
  121.         if (playerId < 0 || playerId >= Constants.PLAYER_CAP) {
  122.             return;
  123.         }
  124.         Player other = World.getInstance().getPlayerList().get(playerId);
  125.         if (other == null) {
  126.             return;
  127.         }
  128.         if (slot < 0 || slot >= Inventory.SIZE) {
  129.             return;
  130.         }
  131.         Item item = player.getInventory().getContainer().get(slot);
  132.         if (item == null || item.getId() != id) {
  133.             return;
  134.         }
  135.         ScriptManager.getInstance().call("item_on_player_" + id, player, slot,
  136.                 other);
  137.     }
  138.  
  139.     private void itemOnNPC(Player player, IoSession session, Packet packet) {
  140.         packet.readInt(); // junk
  141.         int npcId = packet.readShort() & 0xFFFF;
  142.         int slot = packet.readLEShortA() & 0xFFFF;
  143.         int id = packet.readShortA() & 0xFFFF;
  144.         if (npcId < 0 || npcId >= Constants.NPC_CAP) {
  145.             return;
  146.         }
  147.         NPC npc = World.getInstance().getNpcList().get(npcId);
  148.         if (npc == null) {
  149.             return;
  150.         }
  151.         if (slot < 0 || slot >= Inventory.SIZE) {
  152.             return;
  153.         }
  154.         Item item = player.getInventory().getContainer().get(slot);
  155.         if (item == null || item.getId() != id) {
  156.             return;
  157.         }
  158.         ScriptManager.getInstance()
  159.                 .call("item_on_npc_" + id, player, slot, npc);
  160.     }
  161.  
  162.     private void itemOnObject(Player player, IoSession session, Packet packet) {
  163.         int y = packet.readLEShort();
  164.         int itemId = packet.readShort() & 0xFFFF;
  165.         packet.readShort(); // junk
  166.         packet.readShort(); // junk
  167.         packet.readShort(); // junk
  168.         int objectId = packet.readShortA() & 0xFFFF;
  169.         int x = packet.readShort() & 0xFFFF;
  170.         // TODO can probably get this from the packet
  171.         int slot = player.getInventory().lookupSlot(itemId);
  172.         if (slot == -1) {
  173.             return;
  174.         }
  175.         ScriptManager.getInstance()
  176.                 .call("item_on_object_" + itemId + "_" + objectId, player,
  177.                         slot, x, y);
  178.     }
  179.  
  180.     private void itemOnItem(Player player, IoSession session, Packet packet) {
  181.         int usedWith = packet.readLEShort();
  182.         int itemUsed = packet.readShortA();
  183.         int slot = player.getInventory().lookupSlot(itemUsed);
  184.         if (slot == -1) {
  185.             return;
  186.         }
  187.         int withSlot = player.getInventory().lookupSlot(usedWith);
  188.         if (withSlot == -1) {
  189.             return;
  190.         }
  191.         ScriptManager.getInstance().call(
  192.                 "item_on_item_" + itemUsed + "_" + usedWith, player, slot,
  193.                 withSlot);
  194.     }
  195.  
  196.     private void itemOperate(Player player, IoSession session, Packet packet) {
  197.         int interfaceSet = packet.readInt();
  198.         int interfaceId = interfaceSet >> 16;
  199.         int id = packet.readShortA() & 0xFFFF;
  200.         int slot = packet.readLEShortA() & 0xFFFF;
  201.         if (interfaceId == 7168) {
  202.             if (slot < 0 || slot >= Equipment.SIZE
  203.                     || player.getEquipment().getContainer().get(slot) == null) {
  204.                 return;
  205.             }
  206.             if (player.getEquipment().getContainer().get(slot).getId() != id) {
  207.                 return;
  208.             }
  209.             ScriptManager.getInstance()
  210.                     .call("item_operate_" + id, player, slot);
  211.         } else {
  212.             logger.debug("Unhandled item operate, interface: " + interfaceId
  213.                     + ".");
  214.         }
  215.     }
  216.  
  217.     private void itemSelect(Player player, IoSession session, Packet packet) {
  218.         int id = packet.readLEShortA() & 0xFFFF;
  219.         int interfaceSet = packet.readInt();
  220.         int interfaceId = interfaceSet >> 16;
  221.         int slot = packet.readShortA() & 0xFFFF;
  222.         if (interfaceId == 149) {
  223.             if (slot < 0 || slot >= Inventory.SIZE
  224.                     || player.getInventory().getContainer().get(slot) == null) {
  225.                 return;
  226.             }
  227.             if (player.getInventory().getContainer().get(slot).getId() != id) {
  228.                 return;
  229.             }
  230.             ScriptManager.getInstance().call("item_select_" + id, player, slot);
  231.         } else {
  232.             logger.debug("Unhandled item select, interface: " + interfaceId
  233.                     + ".");
  234.         }
  235.     }
  236.  
  237.     private void itemOption2(Player player, IoSession session, Packet packet) {
  238.         int slot = packet.readLEShortA() & 0xFFFF;
  239.         int id = packet.readShortA() & 0xFFFF;
  240.         int interfaceSet = packet.readInt();
  241.         int interfaceId = interfaceSet >> 16;
  242.         if (interfaceId == 149) {
  243.             if (slot < 0 || slot >= Inventory.SIZE
  244.                     || player.getInventory().getContainer().get(slot) == null) {
  245.                 return;
  246.             }
  247.             if (player.getInventory().getContainer().get(slot).getId() != id) {
  248.                 return;
  249.             }
  250.             ScriptManager.getInstance().call("item_option_2_" + id, player,
  251.                     slot);
  252.         } else {
  253.             logger.debug("Unhandled item option 2, interface: " + interfaceId
  254.                     + ".");
  255.         }
  256.     }
  257.  
  258.     private void take(final Player player, IoSession session, Packet packet) {
  259.         final int id = packet.readShortA() & 0xFFFF;
  260.         int x = packet.readLEShort() & 0xFFFF;
  261.         int y = packet.readLEShort() & 0xFFFF;
  262.         final Location l = Location.location(x, y, player.getLocation().getZ());
  263.         World.getInstance().registerCoordinateEvent(
  264.                 new CoordinateEvent(player, l) {
  265.                     @Override
  266.                     public void run() {
  267.                         if (player.getLocation().withinInteractionDistance(l)) {
  268.                             if (World.getInstance().getItemManager()
  269.                                     .groundItemExists(l, id)) {
  270.                                 int itemAmount = World.getInstance()
  271.                                         .getItemManager().getItemAmount(l, id);
  272.                                 assert itemAmount != -1;
  273.                                 if (player.getInventory().hasRoomFor(id,
  274.                                         itemAmount)) {
  275.                                     World.getInstance().getItemManager()
  276.                                             .destroyGroundItem(l, id);
  277.                                     player.getInventory().addItem(id,
  278.                                             itemAmount);
  279.                                 }
  280.                             }
  281.                         }
  282.                     }
  283.                 });
  284.     }
  285.  
  286.     private void drop(Player player, IoSession session, Packet packet) {
  287.         packet.readLEInt();
  288.         int id = packet.readLEShortA() & 0xFFFF;
  289.         int slot = packet.readLEShort() & 0xFFFF;
  290.         if (slot < 0 || slot >= Inventory.SIZE
  291.                 || player.getInventory().getContainer().get(slot) == null
  292.                 || player.getInventory().getContainer().get(slot).getId() != id) {
  293.             return;
  294.         }
  295.         Item item = player.getInventory().getContainer().get(slot);
  296.         player.getInventory().getContainer().set(slot, null);
  297.         player.getInventory().refresh();
  298.         World.getInstance().getItemManager().createGroundItem(player, item);
  299.     }
  300.  
  301.     private void switchItems2(Player player, IoSession session, Packet packet) {
  302.         int interface1 = packet.readInt() >> 16;
  303.         packet.readInt();
  304.         int fromId = packet.readShort() & 0xFFFF;
  305.         int toId = packet.readLEShort() & 0xFFFF;
  306.         switch (interface1) {
  307.         case 12:
  308.             /*
  309.              * Bank.
  310.              */
  311.             if (fromId < 0 || fromId >= Bank.SIZE || toId < 0
  312.                     || toId >= Bank.SIZE) {
  313.                 break;
  314.             }
  315.             Item temp = player.getBank().getContainer().get(fromId);
  316.             Item temp2 = player.getBank().getContainer().get(toId);
  317.             player.getBank().getContainer().set(fromId, temp2);
  318.             player.getBank().getContainer().set(toId, temp);
  319.             player.getBank().refresh();
  320.             break;
  321.         //case 763:
  322.         case 15:
  323.             /*
  324.              * Inventory.
  325.              */
  326.             if (fromId < 0 || fromId >= Inventory.SIZE || toId < 0
  327.                     || toId >= Inventory.SIZE) {
  328.                 break;
  329.             }
  330.             temp = player.getInventory().getContainer().get(fromId);
  331.             temp2 = player.getInventory().getContainer().get(toId);
  332.             player.getInventory().getContainer().set(fromId, temp2);
  333.             player.getInventory().getContainer().set(toId, temp);
  334.             player.getBank().refresh();
  335.             break;
  336.         }
  337.     }
  338.  
  339.     private void itemOption1(Player player, IoSession session, Packet packet) {
  340.         int slot = packet.readShort();
  341.         int interfaceValue = packet.readLEInt();
  342.         int interfaceId = interfaceValue >> 16;
  343.         @SuppressWarnings("unused")
  344.         int childId = interfaceValue & 0xffff;
  345.         int itemId = packet.readLEShortA();
  346.         if (slot < 0 || itemId < 0) {
  347.             return;
  348.         }
  349.         switch (interfaceId) {
  350.         case 387:
  351.             /*
  352.              * Unequip item.
  353.              */
  354.             if (slot < Equipment.SIZE
  355.                     && player.getEquipment().get(slot) != null) {
  356.                 if (!player.getInventory()
  357.                         .addItem(
  358.                                 player.getEquipment().get(slot).getDefinition()
  359.                                         .getId(),
  360.                                 player.getEquipment().get(slot).getAmount())) {
  361.                     break;
  362.                 }
  363.                 player.getEquipment().set(slot, null);
  364.             }
  365.             break;
  366.         case 465:
  367.             /*
  368.              * Unequip item.
  369.              */
  370.             if (slot < Equipment.SIZE
  371.                     && player.getEquipment().get(slot) != null) {
  372.                 if (!player.getInventory()
  373.                         .addItem(
  374.                                 player.getEquipment().get(slot).getDefinition()
  375.                                         .getId(),
  376.                                 player.getEquipment().get(slot).getAmount())) {
  377.                     break;
  378.                 }
  379.                 player.getEquipment().set(slot, null);
  380.             }
  381.             break;
  382.         case 149:
  383.             if (slot < 0 || slot >= Inventory.SIZE
  384.                     || player.getInventory().getContainer().get(slot) == null) {
  385.                 return;
  386.             }
  387.             if (player.getInventory().getContainer().get(slot).getId() != itemId) {
  388.                 return;
  389.             }
  390.             ScriptManager.getInstance().call("item_option_1_" + itemId, player,
  391.                     slot);
  392.             break;
  393.         default:
  394.             logger.debug("Unhandled item option 1, interface: " + interfaceId
  395.                     + ".");
  396.             break;
  397.         }
  398.     }
  399.  
  400.     private void equip(Player player, IoSession session, Packet packet) {
  401.         int index = packet.readByteA();
  402.         packet.readByte();
  403.         int wearId = packet.readShort();
  404.         packet.readLEInt();
  405.         if (index < 0 || index >= Inventory.SIZE) {
  406.             return;
  407.         }
  408.         Item item = player.getInventory().getContainer().get(index);
  409.         if (item == null) {
  410.             return;
  411.         }
  412.         if (Equipment.isTwoHanded(item.getDefinition())
  413.                 && player.getInventory().getFreeSlots() < 1
  414.                 && player.getEquipment().get(5) != null) {
  415.             player.getActionSender().sendMessage(
  416.                     "Not enough free space in your inventory.");
  417.             return;
  418.         }
  419.         if (item.getDefinition().getId() == wearId) {
  420.             int targetSlot = Equipment.getItemType(wearId);
  421.             if (targetSlot == -1) {
  422.                 return;
  423.             }
  424.             int attLevel = player.getSkills().getLevelForXp(0);
  425.             int defLevel = player.getSkills().getLevelForXp(1);
  426.             int strLevel = player.getSkills().getLevelForXp(2);
  427.             int rangeLevel = player.getSkills().getLevelForXp(4);
  428.             int magicLevel = player.getSkills().getLevelForXp(6);
  429.             int targetAtt = ItemConfiguration.getCLAttack(item.getDefinition()
  430.                     .getId());
  431.             int targetDef = ItemConfiguration.getCLDefence(item.getDefinition()
  432.                     .getId());
  433.             int targetStr = ItemConfiguration.getCLStrength(item
  434.                     .getDefinition().getId());
  435.             int targetRange = ItemConfiguration.getCLRanged(item
  436.                     .getDefinition().getId());
  437.             int targetMagic = ItemConfiguration.getCLMagic(item.getDefinition()
  438.                     .getId());
  439.             boolean stop = false;
  440.             if (attLevel < targetAtt) {
  441.                 stop = true;
  442.                 player.getActionSender().sendMessage(
  443.                         "You need an attack level of " + targetAtt
  444.                                 + " to wield this item.");
  445.             }
  446.             if (strLevel < targetStr) {
  447.                 stop = true;
  448.                 player.getActionSender().sendMessage(
  449.                         "You need an strength level of " + targetStr
  450.                                 + " to wield this item.");
  451.             }
  452.             if (defLevel < targetDef) {
  453.                 stop = true;
  454.                 player.getActionSender().sendMessage(
  455.                         "You need an defence level of " + targetDef
  456.                                 + " to wield this item.");
  457.             }
  458.             if (rangeLevel < targetRange) {
  459.                 stop = true;
  460.                 player.getActionSender().sendMessage(
  461.                         "You need an ranged level of " + targetRange
  462.                                 + " to wield this item.");
  463.             }
  464.             if (magicLevel < targetMagic) {
  465.                 stop = true;
  466.                 player.getActionSender().sendMessage(
  467.                         "You need an magic level of " + targetMagic
  468.                                 + " to wield this item.");
  469.             }
  470.             if (stop) {
  471.                 player
  472.                         .getActionSender()
  473.                         .sendMessage(
  474.                                 "You do not meet the minimum requirements to wield this item.");
  475.                 stop = false;
  476.                 return;
  477.             }
  478.             player.getInventory().deleteItem(item.getDefinition().getId(),
  479.                     item.getAmount());
  480.             if (targetSlot == 3) {
  481.                 if (Equipment.isTwoHanded(ItemDefinition.forId(wearId))
  482.                         && player.getEquipment().get(5) != null) {
  483.                     if (!player.getInventory().addItem(
  484.                             player.getEquipment().get(5).getDefinition()
  485.                                     .getId(),
  486.                             player.getEquipment().get(5).getAmount())) {
  487.                         player.getInventory().addItem(wearId, item.getAmount());
  488.                         return;
  489.                     }
  490.                     player.getEquipment().set(5, null);
  491.                 }
  492.             } else if (targetSlot == 5) {
  493.                 if (player.getEquipment().get(3) != null) {
  494.                     if (Equipment.isTwoHanded(ItemDefinition.forId(player
  495.                             .getEquipment().get(3).getId()))) {
  496.                         if (!player.getInventory().addItem(
  497.                                 player.getEquipment().get(3).getDefinition()
  498.                                         .getId(),
  499.                                 player.getEquipment().get(3).getAmount())) {
  500.                             player.getInventory().addItem(wearId,
  501.                                     item.getAmount());
  502.                             return;
  503.                         }
  504.                         player.getEquipment().set(3, null);
  505.                     }
  506.                 }
  507.             }
  508.             if (player.getEquipment().get(targetSlot) != null
  509.                     && (wearId != player.getEquipment().get(targetSlot)
  510.                             .getDefinition().getId() || !item.getDefinition()
  511.                             .isStackable())) {
  512.                 player.getInventory().addItem(
  513.                         player.getEquipment().get(targetSlot).getDefinition()
  514.                                 .getId(),
  515.                         player.getEquipment().get(targetSlot).getAmount());
  516.                 player.getEquipment().set(targetSlot, null);
  517.             }
  518.             int oldAmt = 0;
  519.             if (player.getEquipment().get(targetSlot) != null) {
  520.                 oldAmt = player.getEquipment().get(targetSlot).getAmount();
  521.             }
  522.             Item item2 = new Item(wearId, oldAmt + item.getAmount());
  523.             player.getEquipment().set(targetSlot, item2);
  524.  
  525.         }
  526.     }
  527.  
  528.     private void switchItems(Player player, IoSession session, Packet packet) {
  529.         int toId = packet.readLEShortA();
  530.         packet.readByteA();
  531.         int interfaceId = packet.readLEInt();
  532.         int fromId = packet.readLEShort() & 0xFFFF;
  533.         switch (interfaceId) {
  534.         case 149:
  535.             /*
  536.              * Switch items in inventory.
  537.              */
  538.             if (fromId < 0 || fromId >= Inventory.SIZE || toId < 0
  539.                     || toId >= Inventory.SIZE) {
  540.                 break;
  541.             }
  542.             Item temp = player.getInventory().getContainer().get(fromId);
  543.             Item temp2 = player.getInventory().getContainer().get(toId);
  544.             player.getInventory().getContainer().set(fromId, temp2);
  545.             player.getInventory().getContainer().set(toId, temp);
  546.             break;
  547.         default:
  548.             logger.debug("Unhandled switch items, interface: " + interfaceId
  549.                     + ".");
  550.             break;
  551.         }
  552.     }
  553.  
  554. }