PublicChatHandler.java
资源名称:src.rar [点击查看]
上传用户:gwt600
上传日期:2021-06-03
资源大小:704k
文件大小:6k
源码类别:
游戏
开发平台:
Java
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package net.sf.odinms.server;
- import java.util.HashMap;
- import java.util.Map;
- import net.sf.odinms.client.MapleCharacter;
- import net.sf.odinms.client.MapleClient;
- import net.sf.odinms.client.messages.WhisperMapleClientMessageCallback;
- import net.sf.odinms.net.channel.ChannelServer;
- import net.sf.odinms.tools.MaplePacketCreator;
- import net.sf.odinms.tools.StringUtil;
- /**
- *
- * @author XoticStory
- */
- public class PublicChatHandler {
- private static Map<Integer, Integer> playerHolder = new HashMap<Integer, Integer>();
- private static void addPlayer(MapleCharacter chr) { // clean up later.
- playerHolder.put(chr.getId(), chr.getClient().getChannel());
- for (int chrIds : playerHolder.keySet()) {
- MapleCharacter chrs = ChannelServer.getInstance(playerHolder.get(chrIds)).getPlayerStorage().getCharacterById(chrIds);
- if (chrs == null) {
- playerHolder.remove(chrIds);
- continue;
- }
- chrs.getClient().getSession().write(MaplePacketCreator.multiChat("", chr.getName() + " has been added to the chat.", 2));
- }
- }
- private static void removePlayer(MapleCharacter chr) {
- if (playerHolder.containsKey(chr.getId())) {
- playerHolder.remove(chr.getId());
- for (int chrIds : playerHolder.keySet()) {
- MapleCharacter chrs = ChannelServer.getInstance(playerHolder.get(chrIds)).getPlayerStorage().getCharacterById(chrIds);
- if (chrs == null) {
- playerHolder.remove(chrIds);
- continue;
- }
- chrs.getClient().getSession().write(MaplePacketCreator.multiChat("", chr.getName() + " has left the chat.", 2));
- }
- }
- }
- private static void sendMessage(MapleCharacter chr, String message) {
- if (playerHolder.containsKey(chr.getId())) {
- for (int chrIds : playerHolder.keySet()) {
- MapleCharacter chrs = ChannelServer.getInstance(playerHolder.get(chrIds)).getPlayerStorage().getCharacterById(chrIds);
- if (chrs == null) { // changing channels ?
- playerHolder.remove(chrIds);
- continue;
- }
- chrs.getClient().getSession().write(MaplePacketCreator.multiChat(chr.getName(), message, 3));
- }
- }
- }
- public static Map<Integer, Integer> getPublicChatHolder() {
- return playerHolder;
- }
- public static boolean doChat(MapleClient c, String text) {
- MapleCharacter player = c.getPlayer();
- WhisperMapleClientMessageCallback mc = new WhisperMapleClientMessageCallback("ChatBot", c);
- if (text.charAt(0) == '`') { // ` is much easier than typing ~
- String[] splitted = text.substring(1).split(" ");
- if (splitted[0].equalsIgnoreCase("connect")) {
- if (playerHolder.containsKey(player.getId())) {
- mc.dropMessage("You are already in the chat channel.");
- } else {
- addPlayer(player); // we dont even need this lmao..
- }
- } else if (splitted[0].equalsIgnoreCase("leave")) {
- if (playerHolder.containsKey(player.getId())) {
- removePlayer(player);
- } else {
- mc.dropMessage("You are not in a chat room yet.");
- }
- } else if (splitted[0].equalsIgnoreCase("online")) {
- if (playerHolder.containsKey(player.getId())) {
- mc.dropMessage("There are currently " + playerHolder.size() + " connected people in this channel.");
- } else {
- mc.dropMessage("Please make sure you're in the chat room first.");
- }
- } else if (splitted[0].equalsIgnoreCase("whoson")) {
- StringBuilder sb = new StringBuilder();
- c.getSession().write(MaplePacketCreator.multiChat("", "Current people in the chat", 3));
- c.getSession().write(MaplePacketCreator.serverNotice(6, "")); // new line ? ROFL
- int i = 0;
- if (playerHolder.size() == 0) {
- c.getSession().write(MaplePacketCreator.multiChat("", "No one is in this chat unfortunately", 3));
- } else {
- for (int chrIds : playerHolder.keySet()) {
- MapleCharacter chrs = ChannelServer.getInstance(playerHolder.get(chrIds)).getPlayerStorage().getCharacterById(chrIds);
- if (chrs == null) { // changing channels ?
- playerHolder.remove(chrIds);
- continue;
- }
- if (sb.length() > 70) {
- c.getSession().write(MaplePacketCreator.multiChat("", sb.toString(), 3));
- sb = new StringBuilder();
- }
- sb.append(chrs.getName() + " ");
- i++;
- }
- c.getSession().write(MaplePacketCreator.multiChat("", sb.toString(), 3));
- c.getSession().write(MaplePacketCreator.multiChat("", i + " total connected.", 3));
- }
- } else if (playerHolder.containsKey(player.getId())) {
- String message = StringUtil.joinStringFrom(splitted, 0).trim();
- // spam check
- sendMessage(player, message);
- } else {
- mc.dropMessage("I did not understand what you just said..");
- }
- } else {
- return false;
- }
- return true;
- }
- }