Advertisement
Guest User

Untitled

a guest
May 9th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.91 KB | None | 0 0
  1. package com.osroyale.net.packet.in;
  2.  
  3. import com.osroyale.content.event.EventDispatcher;
  4. import com.osroyale.content.event.impl.ItemOnItemInteractionEvent;
  5. import com.osroyale.content.event.impl.ItemOnObjectInteractionEvent;
  6. import com.osroyale.content.itemaction.ItemActionRepository;
  7. import com.osroyale.game.event.impl.ItemOnItemEvent;
  8. import com.osroyale.game.event.impl.ItemOnNpcEvent;
  9. import com.osroyale.game.event.impl.ItemOnObjectEvent;
  10. import com.osroyale.game.event.impl.ItemOnPlayerEvent;
  11. import com.osroyale.game.plugin.PluginManager;
  12. import com.osroyale.game.world.InterfaceConstants;
  13. import com.osroyale.game.world.World;
  14. import com.osroyale.game.world.entity.mob.data.PacketType;
  15. import com.osroyale.game.world.entity.mob.player.Player;
  16. import com.osroyale.game.world.entity.mob.player.PlayerRight;
  17. import com.osroyale.game.world.items.Item;
  18. import com.osroyale.game.world.object.GameObject;
  19. import com.osroyale.game.world.position.Position;
  20. import com.osroyale.game.world.region.Region;
  21. import com.osroyale.net.codec.ByteModification;
  22. import com.osroyale.net.codec.ByteOrder;
  23. import com.osroyale.net.packet.ClientPackets;
  24. import com.osroyale.net.packet.GamePacket;
  25. import com.osroyale.net.packet.PacketListener;
  26. import com.osroyale.net.packet.PacketListenerMeta;
  27. import com.osroyale.net.packet.out.SendMessage;
  28. import com.osroyale.util.MessageColor;
  29.  
  30. /**
  31. * The {@link GamePacket}'s responsible for an items "use" option.
  32. *
  33. * @author Daniel
  34. */
  35. @PacketListenerMeta({ClientPackets.ITEM_ON_NPC, ClientPackets.ITEM_ON_ITEM, ClientPackets.ITEM_ON_OBJECT, ClientPackets.ITEM_ON_GROUND_ITEM, ClientPackets.ITEM_ON_PLAYER})
  36. public class UseItemPacketListener implements PacketListener {
  37.  
  38. @Override
  39. public void handlePacket(Player player, GamePacket packet) {
  40. if (player.isDead()) return;
  41. if (player.locking.locked(PacketType.USE_ITEM))
  42. return;
  43.  
  44. switch (packet.getOpcode()) {
  45. case ClientPackets.ITEM_ON_GROUND_ITEM:
  46. handleItemOnGround(player, packet);
  47. break;
  48. case ClientPackets.ITEM_ON_ITEM:
  49. handleItemOnItem(player, packet);
  50. break;
  51. case ClientPackets.ITEM_ON_NPC:
  52. handleItemOnNpc(player, packet);
  53. break;
  54. case ClientPackets.ITEM_ON_OBJECT:
  55. handleItemOnObject(player, packet);
  56. break;
  57. case ClientPackets.ITEM_ON_PLAYER:
  58. handleItemOnPlayer(player, packet);
  59. break;
  60. }
  61. }
  62.  
  63. /** Handles an randomevent when a player uses the "Use" option of an item with an item on the ground. */
  64. private void handleItemOnGround(Player player, GamePacket packet) {
  65. final int a1 = packet.readShort(ByteOrder.LE);
  66. final int itemUsed = packet.readShort(false, ByteModification.ADD);
  67. final int groundItem = packet.readShort();
  68. final int gItemY = packet.readShort(false, ByteModification.ADD);
  69. final int itemUsedSlot = packet.readShort(ByteOrder.LE, ByteModification.ADD);
  70. final int gItemX = packet.readShort();
  71.  
  72. if (PlayerRight.isDeveloper(player) && player.debug) {
  73. player.send(new SendMessage("[ItemUsed] - " + itemUsed + " groundItem: " + groundItem + " itemUsedSlot: " + itemUsedSlot + " gItemX: " + gItemX + " gItemY: " + gItemY + " a1: " + a1, MessageColor.DEVELOPER));
  74. }
  75.  
  76. player.send(new SendMessage("Nothing interesting happens."));
  77. }
  78.  
  79. /** Handles an randomevent when a player uses the "Use" option of an item with another item in a players inventory. */
  80. private void handleItemOnItem(Player player, GamePacket packet) {
  81. final int usedWithSlot = packet.readShort();
  82. final int itemUsedSlot = packet.readShort(ByteModification.ADD);
  83. final Item used = player.inventory.get(itemUsedSlot);
  84. final Item with = player.inventory.get(usedWithSlot);
  85.  
  86. if (used != null && with != null) {
  87. if (EventDispatcher.execute(player, new ItemOnItemInteractionEvent(used, with, usedWithSlot, itemUsedSlot))) {
  88. return;
  89. }
  90. if (ItemActionRepository.itemOnItem(player, used, with)) {
  91. return;
  92. }
  93.  
  94. if (PluginManager.getDataBus().publish(player, new ItemOnItemEvent(used, usedWithSlot, with, usedWithSlot))) {
  95. return;
  96. }
  97.  
  98. player.send(new SendMessage("Nothing interesting happens."));
  99.  
  100. }
  101. }
  102.  
  103. /** Handles an randomevent when a player uses the "Use" option of an item with an in-game npc. */
  104. private void handleItemOnNpc(Player player, GamePacket packet) {
  105. final int itemId = packet.readShort(false, ByteModification.ADD);
  106. final int index = packet.readShort(false, ByteModification.ADD);
  107. final int slot = packet.readShort(ByteOrder.LE);
  108. final Item used = player.inventory.get(slot);
  109.  
  110. if (used == null) return;
  111. World.getNpcBySlot(index).ifPresent(npc -> {
  112. Position position = npc.getPosition();
  113. Region region = World.getRegions().getRegion(position);
  114. if (!region.containsNpc(position.getHeight(), npc)) return;
  115. if (!npc.isValid()) return;
  116. player.walkTo(npc, () -> {
  117. player.face(position);
  118.  
  119. if (PluginManager.getDataBus().publish(player, new ItemOnNpcEvent(npc, used, slot))) {
  120. return;
  121. }
  122.  
  123. player.send(new SendMessage("Nothing interesting happens."));
  124. });
  125. });
  126. }
  127.  
  128. /** Handles an randomevent when a player uses the "Use" option of an item with an in-game object. */
  129. private void handleItemOnObject(Player player, GamePacket packet) {
  130. int interfaceType = packet.readShort();
  131. final int objectId = packet.readShort(ByteOrder.LE);
  132. final int y = packet.readShort(ByteOrder.LE, ByteModification.ADD);
  133. final int slot = packet.readShort(ByteOrder.LE);
  134. final int x = packet.readShort(ByteOrder.LE, ByteModification.ADD);
  135. final int itemId = packet.readShort();
  136. final Item used = player.inventory.get(slot);
  137.  
  138. if (used == null)
  139. return;
  140.  
  141. Position position = new Position(x, y);
  142. Region region = World.getRegions().getRegion(position);
  143. GameObject object = region.getGameObject(objectId, position);
  144.  
  145. if (object == null)
  146. return;
  147.  
  148. player.walkTo(object, () -> {
  149. if (interfaceType == InterfaceConstants.INVENTORY_INTERFACE) {
  150. player.face(object);
  151.  
  152. if (EventDispatcher.execute(player, new ItemOnObjectInteractionEvent(used, object))) {
  153. return;
  154. }
  155.  
  156. if (PluginManager.getDataBus().publish(player, new ItemOnObjectEvent(used, slot, object))) {
  157. return;
  158. }
  159.  
  160. player.send(new SendMessage("Nothing interesting happens."));
  161. }
  162. });
  163. }
  164.  
  165. /** Handles an randomevent when a player uses the "Use" option of an item with an in-game player. */
  166. private void handleItemOnPlayer(Player player, GamePacket packet) {
  167. final int interfaceId = packet.readShort(ByteModification.ADD);
  168. final int slot = packet.readShort();
  169. final int item = packet.readShort();
  170. final int itemSlot = packet.readShort(ByteOrder.LE);
  171. final Item used = player.inventory.get(itemSlot);
  172. if (used == null)
  173. return;
  174. World.getPlayerBySlot(slot).ifPresent(other -> player.walkTo(other, () -> {
  175. player.face(other.getPosition());
  176.  
  177. if (PluginManager.getDataBus().publish(player, new ItemOnPlayerEvent(other, used, itemSlot))) {
  178. return;
  179. }
  180.  
  181. player.send(new SendMessage("Nothing interesting happens."));
  182. }));
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement