SystemMessage.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. /**
  28. * This class ...
  29. *
  30. * @version $Revision: 1.18.2.5.2.8 $ $Date: 2005/04/05 19:41:08 $
  31. */
  32. public final class SystemMessage extends L2GameServerPacket
  33. {
  34. // d d (d S/d d/d dd)
  35. // |--------------> 0 - String 1-number 2-textref npcname (1000000-1002655) 3-textref itemname 4-textref skills 5-??
  36. private static final int TYPE_ZONE_NAME = 7;
  37. private static final int TYPE_ITEM_NUMBER = 6;
  38. private static final int TYPE_FORTRESS = 5; // maybe not only for fortress, rename if needed
  39. private static final int TYPE_SKILL_NAME = 4;
  40. private static final int TYPE_ITEM_NAME = 3;
  41. private static final int TYPE_NPC_NAME = 2;
  42. private static final int TYPE_NUMBER = 1;
  43. private static final int TYPE_TEXT = 0;
  44. private static final String _S__7A_SYSTEMMESSAGE = "[S] 62 SystemMessage";
  45. private int _messageId;
  46. private final ArrayList<SysMsgData> _info = new ArrayList<SysMsgData>();
  47. //private Vector<Integer> _types = new Vector<Integer>();
  48. //private Vector<Object> _values = new Vector<Object>();
  49. private int _skillLvL = 1;
  50. protected class SysMsgData
  51. {
  52. protected final int type;
  53. protected final Object value;
  54. protected SysMsgData(int t, Object val)
  55. {
  56. type = t;
  57. value = val;
  58. }
  59. }
  60. public SystemMessage(SystemMessageId messageId)
  61. {
  62. _messageId = messageId.getId();
  63. }
  64. /**
  65. * Use SystemMessage(SystemMessageId messageId) where possible instead
  66. */
  67. public SystemMessage(int messageId)
  68. {
  69. _messageId = messageId;
  70. }
  71. public static SystemMessage sendString(String msg)
  72. {
  73. SystemMessage sm = new SystemMessage(SystemMessageId.S1);
  74. sm.addString(msg);
  75. return sm;
  76. }
  77. public SystemMessage addString(String text)
  78. {
  79. _info.add(new SysMsgData(TYPE_TEXT, text));
  80. return this;
  81. }
  82. public SystemMessage addFortId(int number)
  83. {
  84. _info.add(new SysMsgData(TYPE_FORTRESS, number));
  85. return this;
  86. }
  87. public SystemMessage addNumber(int number)
  88. {
  89. _info.add(new SysMsgData(TYPE_NUMBER, number));
  90. return this;
  91. }
  92. public SystemMessage addItemNumber(long number)
  93. {
  94. _info.add(new SysMsgData(TYPE_ITEM_NUMBER, number));
  95. return this;
  96. }
  97. public SystemMessage addCharName(L2Character cha)
  98. {
  99. if (cha instanceof L2Npc)
  100. {
  101. if (((L2Npc)cha).getTemplate().serverSideName)
  102. return addString(((L2Npc)cha).getTemplate().name);
  103. else
  104. return addNpcName((L2Npc)cha);
  105. }
  106. if (cha instanceof L2PcInstance)
  107. return addPcName((L2PcInstance)cha);
  108. if (cha instanceof L2Summon)
  109. {
  110. if (((L2Summon)cha).getTemplate().serverSideName)
  111. return addString(((L2Summon)cha).getTemplate().name);
  112. else
  113. return addNpcName((L2Summon)cha);
  114. }
  115. return addString(cha.getName());
  116. }
  117. public SystemMessage addPcName(L2PcInstance pc)
  118. {
  119. return addString(pc.getAppearance().getVisibleName());
  120. }
  121. public SystemMessage addNpcName(L2Npc npc)
  122. {
  123. return addNpcName(npc.getTemplate());
  124. }
  125. public SystemMessage addNpcName(L2Summon npc)
  126. {
  127. return addNpcName(npc.getNpcId());
  128. }
  129. public SystemMessage addNpcName(L2NpcTemplate tpl)
  130. {
  131. if (tpl.isCustom())
  132. return addString(tpl.name);
  133. return addNpcName(tpl.npcId);
  134. }
  135. public SystemMessage addNpcName(int id)
  136. {
  137. _info.add(new SysMsgData(TYPE_NPC_NAME, 1000000+id));
  138. return this;
  139. }
  140. public SystemMessage addItemName(L2ItemInstance item)
  141. {
  142. return addItemName(item.getItem().getItemId());
  143. }
  144. public SystemMessage addItemName(L2Item item)
  145. {
  146. // TODO: template id for items
  147. return addItemName(item.getItemId());
  148. }
  149. public SystemMessage addItemName(int id)
  150. {
  151. _info.add(new SysMsgData(TYPE_ITEM_NAME, id));
  152. return this;
  153. }
  154. public SystemMessage addZoneName(int x, int y, int z)
  155. {
  156. int[] coord = {x, y, z};
  157. _info.add(new SysMsgData(TYPE_ZONE_NAME, coord));
  158. return this;
  159. }
  160. public SystemMessage addSkillName(L2Effect effect)
  161. {
  162. return addSkillName(effect.getSkill());
  163. }
  164. public SystemMessage addSkillName(L2Skill skill)
  165. {
  166. if (skill.getId() != skill.getDisplayId()) //custom skill - need nameId or smth like this.
  167. return addString(skill.getName());
  168. return addSkillName(skill.getId(), skill.getLevel());
  169. }
  170. public SystemMessage addSkillName(int id)
  171. {
  172. return addSkillName(id, 1);
  173. }
  174. public SystemMessage addSkillName(int id, int lvl)
  175. {
  176. _info.add(new SysMsgData(TYPE_SKILL_NAME, id));
  177. _skillLvL = lvl;
  178. return this;
  179. }
  180. @Override
  181. protected final void writeImpl()
  182. {
  183. writeC(0x62);
  184. writeD(_messageId);
  185. writeD(_info.size());
  186. for (SysMsgData data : _info)
  187. {
  188. int t = data.type;
  189. writeD(t);
  190. switch (t)
  191. {
  192. case TYPE_TEXT:
  193. writeS((String)data.value);
  194. break;
  195. case TYPE_ITEM_NUMBER:
  196. writeQ((Long)data.value);
  197. break;
  198. case TYPE_ITEM_NAME:
  199. case TYPE_FORTRESS:
  200. case TYPE_NUMBER:
  201. case TYPE_NPC_NAME:
  202. writeD((Integer)data.value);
  203. break;
  204. case TYPE_SKILL_NAME:
  205. writeD((Integer)data.value); // Skill Id
  206. writeD(_skillLvL); // Skill lvl
  207. break;
  208. case TYPE_ZONE_NAME:
  209. int[] coords = (int[])data.value;
  210. writeD(coords[0]);
  211. writeD(coords[1]);
  212. writeD(coords[2]);
  213. break;
  214. }
  215. }
  216. }
  217. /* (non-Javadoc)
  218. * @see com.l2jserver.gameserver.serverpackets.ServerBasePacket#getType()
  219. */
  220. @Override
  221. public String getType()
  222. {
  223. return _S__7A_SYSTEMMESSAGE;
  224. }
  225. public int getMessageID()
  226. {
  227. return _messageId;
  228. }
  229. }