SystemMessage.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.network.serverpackets;
  16. import java.util.ArrayList;
  17. import com.l2jserver.gameserver.model.L2Effect;
  18. import com.l2jserver.gameserver.model.L2ItemInstance;
  19. import com.l2jserver.gameserver.model.L2Skill;
  20. import com.l2jserver.gameserver.model.actor.L2Character;
  21. import com.l2jserver.gameserver.model.actor.L2Npc;
  22. import com.l2jserver.gameserver.model.actor.L2Summon;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  24. import com.l2jserver.gameserver.network.SystemMessageId;
  25. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  26. import com.l2jserver.gameserver.templates.item.L2Item;
  27. public final class SystemMessage extends L2GameServerPacket
  28. {
  29. // d d (d S/d d/d dd)
  30. // |--------------> 0 - String 1-number 2-textref npcname (1000000-1002655) 3-textref itemname 4-textref skills 5-??
  31. private static final int TYPE_ZONE_NAME = 7;
  32. private static final int TYPE_ITEM_NUMBER = 6;
  33. private static final int TYPE_FORTRESS = 5; // maybe not only for fortress, rename if needed
  34. private static final int TYPE_SKILL_NAME = 4;
  35. private static final int TYPE_ITEM_NAME = 3;
  36. private static final int TYPE_NPC_NAME = 2;
  37. private static final int TYPE_NUMBER = 1;
  38. private static final int TYPE_TEXT = 0;
  39. private static final String _S__7A_SYSTEMMESSAGE = "[S] 62 SystemMessage";
  40. private int _messageId;
  41. private final ArrayList<SysMsgData> _info = new ArrayList<SysMsgData>();
  42. //private Vector<Integer> _types = new Vector<Integer>();
  43. //private Vector<Object> _values = new Vector<Object>();
  44. private int _skillLvL = 1;
  45. protected class SysMsgData
  46. {
  47. protected final int type;
  48. protected final Object value;
  49. protected SysMsgData(int t, Object val)
  50. {
  51. type = t;
  52. value = val;
  53. }
  54. }
  55. public SystemMessage(SystemMessageId messageId)
  56. {
  57. _messageId = messageId.getId();
  58. }
  59. /**
  60. * Use SystemMessage(SystemMessageId messageId) where possible instead
  61. */
  62. public SystemMessage(int messageId)
  63. {
  64. _messageId = messageId;
  65. }
  66. public static SystemMessage sendString(String msg)
  67. {
  68. SystemMessage sm = new SystemMessage(SystemMessageId.S1);
  69. sm.addString(msg);
  70. return sm;
  71. }
  72. public SystemMessage addString(String text)
  73. {
  74. _info.add(new SysMsgData(TYPE_TEXT, text));
  75. return this;
  76. }
  77. public SystemMessage addFortId(int number)
  78. {
  79. _info.add(new SysMsgData(TYPE_FORTRESS, number));
  80. return this;
  81. }
  82. public SystemMessage addNumber(int number)
  83. {
  84. _info.add(new SysMsgData(TYPE_NUMBER, number));
  85. return this;
  86. }
  87. public SystemMessage addItemNumber(long number)
  88. {
  89. _info.add(new SysMsgData(TYPE_ITEM_NUMBER, number));
  90. return this;
  91. }
  92. public SystemMessage addCharName(L2Character cha)
  93. {
  94. if (cha instanceof L2Npc)
  95. {
  96. if (((L2Npc)cha).getTemplate().serverSideName)
  97. return addString(((L2Npc)cha).getTemplate().name);
  98. else
  99. return addNpcName((L2Npc)cha);
  100. }
  101. if (cha instanceof L2PcInstance)
  102. return addPcName((L2PcInstance)cha);
  103. if (cha instanceof L2Summon)
  104. {
  105. if (((L2Summon)cha).getTemplate().serverSideName)
  106. return addString(((L2Summon)cha).getTemplate().name);
  107. else
  108. return addNpcName((L2Summon)cha);
  109. }
  110. return addString(cha.getName());
  111. }
  112. public SystemMessage addPcName(L2PcInstance pc)
  113. {
  114. return addString(pc.getAppearance().getVisibleName());
  115. }
  116. public SystemMessage addNpcName(L2Npc npc)
  117. {
  118. return addNpcName(npc.getTemplate());
  119. }
  120. public SystemMessage addNpcName(L2Summon npc)
  121. {
  122. return addNpcName(npc.getNpcId());
  123. }
  124. public SystemMessage addNpcName(L2NpcTemplate tpl)
  125. {
  126. if (tpl.isCustom())
  127. return addString(tpl.name);
  128. return addNpcName(tpl.npcId);
  129. }
  130. public SystemMessage addNpcName(int id)
  131. {
  132. _info.add(new SysMsgData(TYPE_NPC_NAME, 1000000+id));
  133. return this;
  134. }
  135. public SystemMessage addItemName(L2ItemInstance item)
  136. {
  137. return addItemName(item.getItem().getItemId());
  138. }
  139. public SystemMessage addItemName(L2Item item)
  140. {
  141. // TODO: template id for items
  142. return addItemName(item.getItemId());
  143. }
  144. public SystemMessage addItemName(int id)
  145. {
  146. _info.add(new SysMsgData(TYPE_ITEM_NAME, id));
  147. return this;
  148. }
  149. public SystemMessage addZoneName(int x, int y, int z)
  150. {
  151. int[] coord = {x, y, z};
  152. _info.add(new SysMsgData(TYPE_ZONE_NAME, coord));
  153. return this;
  154. }
  155. public SystemMessage addSkillName(L2Effect effect)
  156. {
  157. return addSkillName(effect.getSkill());
  158. }
  159. public SystemMessage addSkillName(L2Skill skill)
  160. {
  161. if (skill.getId() != skill.getDisplayId()) //custom skill - need nameId or smth like this.
  162. return addString(skill.getName());
  163. return addSkillName(skill.getId(), skill.getLevel());
  164. }
  165. public SystemMessage addSkillName(int id)
  166. {
  167. return addSkillName(id, 1);
  168. }
  169. public SystemMessage addSkillName(int id, int lvl)
  170. {
  171. _info.add(new SysMsgData(TYPE_SKILL_NAME, id));
  172. _skillLvL = lvl;
  173. return this;
  174. }
  175. @Override
  176. protected final void writeImpl()
  177. {
  178. writeC(0x62);
  179. writeD(_messageId);
  180. writeD(_info.size());
  181. for (SysMsgData data : _info)
  182. {
  183. int t = data.type;
  184. writeD(t);
  185. switch (t)
  186. {
  187. case TYPE_TEXT:
  188. writeS((String)data.value);
  189. break;
  190. case TYPE_ITEM_NUMBER:
  191. writeQ((Long)data.value);
  192. break;
  193. case TYPE_ITEM_NAME:
  194. case TYPE_FORTRESS:
  195. case TYPE_NUMBER:
  196. case TYPE_NPC_NAME:
  197. writeD((Integer)data.value);
  198. break;
  199. case TYPE_SKILL_NAME:
  200. writeD((Integer)data.value); // Skill Id
  201. writeD(_skillLvL); // Skill lvl
  202. break;
  203. case TYPE_ZONE_NAME:
  204. int[] coords = (int[])data.value;
  205. writeD(coords[0]);
  206. writeD(coords[1]);
  207. writeD(coords[2]);
  208. break;
  209. }
  210. }
  211. }
  212. @Override
  213. public String getType()
  214. {
  215. return _S__7A_SYSTEMMESSAGE;
  216. }
  217. public int getMessageID()
  218. {
  219. return _messageId;
  220. }
  221. }