ChatHandler.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.handler;
  16. import gnu.trove.map.hash.TIntObjectHashMap;
  17. import java.util.logging.Logger;
  18. import com.l2jserver.Config;
  19. /**
  20. * This class handles all chat handlers
  21. *
  22. * @author durgus
  23. */
  24. public class ChatHandler
  25. {
  26. private static Logger _log = Logger.getLogger(ChatHandler.class.getName());
  27. private final TIntObjectHashMap<IChatHandler> _datatable;
  28. public static ChatHandler getInstance()
  29. {
  30. return SingletonHolder._instance;
  31. }
  32. /**
  33. * Singleton constructor
  34. */
  35. protected ChatHandler()
  36. {
  37. _datatable = new TIntObjectHashMap<>();
  38. }
  39. /**
  40. * Register a new chat handler
  41. * @param handler
  42. */
  43. public void registerHandler(IChatHandler handler)
  44. {
  45. int[] ids = handler.getChatTypeList();
  46. for (int i = 0; i < ids.length; i++)
  47. {
  48. if (Config.DEBUG)
  49. _log.fine("Adding handler for chat type " + ids[i]);
  50. _datatable.put(ids[i], handler);
  51. }
  52. }
  53. /**
  54. * Get the chat handler for the given chat type
  55. * @param chatType
  56. * @return
  57. */
  58. public IChatHandler getHandler(int chatType)
  59. {
  60. return _datatable.get(chatType);
  61. }
  62. /**
  63. * Returns the size
  64. * @return
  65. */
  66. public int size()
  67. {
  68. return _datatable.size();
  69. }
  70. private static class SingletonHolder
  71. {
  72. protected static final ChatHandler _instance = new ChatHandler();
  73. }
  74. }