PublicChatHandler.java
上传用户:gwt600
上传日期:2021-06-03
资源大小:704k
文件大小:6k
源码类别:

游戏

开发平台:

Java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package net.sf.odinms.server;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import net.sf.odinms.client.MapleCharacter;
  9. import net.sf.odinms.client.MapleClient;
  10. import net.sf.odinms.client.messages.WhisperMapleClientMessageCallback;
  11. import net.sf.odinms.net.channel.ChannelServer;
  12. import net.sf.odinms.tools.MaplePacketCreator;
  13. import net.sf.odinms.tools.StringUtil;
  14. /**
  15.  *
  16.  * @author XoticStory
  17.  */
  18. public class PublicChatHandler {
  19.     private static Map<Integer, Integer> playerHolder = new HashMap<Integer, Integer>();
  20.     private static void addPlayer(MapleCharacter chr) { // clean up later.
  21.         playerHolder.put(chr.getId(), chr.getClient().getChannel());
  22.         for (int chrIds : playerHolder.keySet()) {
  23.             MapleCharacter chrs = ChannelServer.getInstance(playerHolder.get(chrIds)).getPlayerStorage().getCharacterById(chrIds);
  24.             if (chrs == null) {
  25.                 playerHolder.remove(chrIds);
  26.                 continue;
  27.             }
  28.             chrs.getClient().getSession().write(MaplePacketCreator.multiChat("", chr.getName() + " has been added to the chat.", 2));
  29.         }
  30.     }
  31.     private static void removePlayer(MapleCharacter chr) {
  32.         if (playerHolder.containsKey(chr.getId())) {
  33.             playerHolder.remove(chr.getId());
  34.             for (int chrIds : playerHolder.keySet()) {
  35.                 MapleCharacter chrs = ChannelServer.getInstance(playerHolder.get(chrIds)).getPlayerStorage().getCharacterById(chrIds);
  36.                 if (chrs == null) {
  37.                     playerHolder.remove(chrIds);
  38.                     continue;
  39.                 }
  40.                 chrs.getClient().getSession().write(MaplePacketCreator.multiChat("", chr.getName() + " has left the chat.", 2));
  41.             }
  42.         }
  43.     }
  44.     private static void sendMessage(MapleCharacter chr, String message) {
  45.         if (playerHolder.containsKey(chr.getId())) {
  46.             for (int chrIds : playerHolder.keySet()) {
  47.                 MapleCharacter chrs = ChannelServer.getInstance(playerHolder.get(chrIds)).getPlayerStorage().getCharacterById(chrIds);
  48.                 if (chrs == null) { // changing channels ?
  49.                     playerHolder.remove(chrIds);
  50.                     continue;
  51.                 }
  52.                 chrs.getClient().getSession().write(MaplePacketCreator.multiChat(chr.getName(), message, 3));
  53.             }
  54.         }
  55.     }
  56.     public static Map<Integer, Integer> getPublicChatHolder() {
  57.         return playerHolder;
  58.     }
  59.     public static boolean doChat(MapleClient c, String text) {
  60.         MapleCharacter player = c.getPlayer();
  61.         WhisperMapleClientMessageCallback mc = new WhisperMapleClientMessageCallback("ChatBot", c);
  62.         if (text.charAt(0) == '`') { // ` is much easier than typing ~
  63.             String[] splitted = text.substring(1).split(" ");
  64.             if (splitted[0].equalsIgnoreCase("connect")) {
  65.                 if (playerHolder.containsKey(player.getId())) {
  66.                     mc.dropMessage("You are already in the chat channel.");
  67.                 } else {
  68.                     addPlayer(player); // we dont even need this lmao..
  69.                 }
  70.             } else if (splitted[0].equalsIgnoreCase("leave")) {
  71.                 if (playerHolder.containsKey(player.getId())) {
  72.                     removePlayer(player);
  73.                 } else {
  74.                     mc.dropMessage("You are not in a chat room yet.");
  75.                 }
  76.             } else if (splitted[0].equalsIgnoreCase("online")) {
  77.                 if (playerHolder.containsKey(player.getId())) {
  78.                     mc.dropMessage("There are currently " + playerHolder.size() + " connected people in this channel.");
  79.                 } else {
  80.                     mc.dropMessage("Please make sure you're in the chat room first.");
  81.                 }
  82.             } else if (splitted[0].equalsIgnoreCase("whoson")) {
  83.                 StringBuilder sb = new StringBuilder();
  84.                 c.getSession().write(MaplePacketCreator.multiChat("", "Current people in the chat", 3));
  85.                 c.getSession().write(MaplePacketCreator.serverNotice(6, "")); // new line ? ROFL
  86.                 int i = 0;
  87.                 if (playerHolder.size() == 0) {
  88.                     c.getSession().write(MaplePacketCreator.multiChat("", "No one is in this chat unfortunately", 3));
  89.                 } else {
  90.                     for (int chrIds : playerHolder.keySet()) {
  91.                         MapleCharacter chrs = ChannelServer.getInstance(playerHolder.get(chrIds)).getPlayerStorage().getCharacterById(chrIds);
  92.                         if (chrs == null) { // changing channels ?
  93.                             playerHolder.remove(chrIds);
  94.                             continue;
  95.                         }
  96.                         if (sb.length() > 70) {
  97.                             c.getSession().write(MaplePacketCreator.multiChat("", sb.toString(), 3));
  98.                             sb = new StringBuilder();
  99.                         }
  100.                         sb.append(chrs.getName() + "      ");
  101.                         i++;
  102.                     }
  103.                     c.getSession().write(MaplePacketCreator.multiChat("", sb.toString(), 3));
  104.                     c.getSession().write(MaplePacketCreator.multiChat("", i + " total connected.", 3));
  105.                 }
  106.             } else if (playerHolder.containsKey(player.getId())) {
  107.                 String message = StringUtil.joinStringFrom(splitted, 0).trim();
  108.                 // spam check
  109.                 sendMessage(player, message);
  110.             } else {
  111.                 mc.dropMessage("I did not understand what you just said..");
  112.             }
  113.         } else {
  114.             return false;
  115.         }
  116.         return true;
  117.     }
  118. }