NpcSay.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.network.serverpackets;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import com.l2jserver.gameserver.model.actor.L2Npc;
  23. import com.l2jserver.gameserver.network.NpcStringId;
  24. /**
  25. * @author Kerberos
  26. */
  27. public final class NpcSay extends L2GameServerPacket
  28. {
  29. private final int _objectId;
  30. private final int _textType;
  31. private final int _npcId;
  32. private String _text;
  33. private final int _npcString;
  34. private List<String> _parameters;
  35. /**
  36. * @param objectId
  37. * @param messageType
  38. * @param npcId
  39. * @param text
  40. */
  41. public NpcSay(int objectId, int messageType, int npcId, String text)
  42. {
  43. _objectId = objectId;
  44. _textType = messageType;
  45. _npcId = 1000000 + npcId;
  46. _npcString = -1;
  47. _text = text;
  48. }
  49. public NpcSay(L2Npc npc, int messageType, String text)
  50. {
  51. _objectId = npc.getObjectId();
  52. _textType = messageType;
  53. _npcId = 1000000 + npc.getId();
  54. _npcString = -1;
  55. _text = text;
  56. }
  57. public NpcSay(int objectId, int messageType, int npcId, NpcStringId npcString)
  58. {
  59. _objectId = objectId;
  60. _textType = messageType;
  61. _npcId = 1000000 + npcId;
  62. _npcString = npcString.getId();
  63. }
  64. public NpcSay(L2Npc npc, int messageType, NpcStringId npcString)
  65. {
  66. _objectId = npc.getObjectId();
  67. _textType = messageType;
  68. _npcId = 1000000 + npc.getId();
  69. _npcString = npcString.getId();
  70. }
  71. /**
  72. * @param text the text to add as a parameter for this packet's message (replaces S1, S2 etc.)
  73. * @return this NpcSay packet object
  74. */
  75. public NpcSay addStringParameter(String text)
  76. {
  77. if (_parameters == null)
  78. {
  79. _parameters = new ArrayList<>();
  80. }
  81. _parameters.add(text);
  82. return this;
  83. }
  84. /**
  85. * @param params a list of strings to add as parameters for this packet's message (replaces S1, S2 etc.)
  86. * @return this NpcSay packet object
  87. */
  88. public NpcSay addStringParameters(String... params)
  89. {
  90. if ((params != null) && (params.length > 0))
  91. {
  92. if (_parameters == null)
  93. {
  94. _parameters = new ArrayList<>();
  95. }
  96. for (String item : params)
  97. {
  98. if ((item != null) && (item.length() > 0))
  99. {
  100. _parameters.add(item);
  101. }
  102. }
  103. }
  104. return this;
  105. }
  106. @Override
  107. protected final void writeImpl()
  108. {
  109. writeC(0x30);
  110. writeD(_objectId);
  111. writeD(_textType);
  112. writeD(_npcId);
  113. writeD(_npcString);
  114. if (_npcString == -1)
  115. {
  116. writeS(_text);
  117. }
  118. else if (_parameters != null)
  119. {
  120. for (String s : _parameters)
  121. {
  122. writeS(s);
  123. }
  124. }
  125. }
  126. }