⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commandset.java

📁 使用工具jublider开发的一个聊天室实现基本功能,
💻 JAVA
字号:
/**
 * Copyright (C) 2003  Manfred Andres
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 * Created on 28.09.2003
 */

package freecs.commands;
import freecs.interfaces.ICommand;
import freecs.interfaces.IReloadable;
import freecs.util.FileMonitor;
import freecs.content.MessageState;
import freecs.Server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;

/**
 * @author Manfred Andres
 *
 * freecs.commands
 */
public class CommandSet implements IReloadable {
	public static final byte UNKNOWN_COMMAND=-1;
	public static final byte TRUE=1;
	public static final byte FALSE=0;
	public static final byte INTERRUPTED = Byte.MIN_VALUE;
	private static final CommandSet cs = new CommandSet();
	private HashMap allCmds, availableCmds;
	private Properties props;	
	private File 	cfgFile = null;
	private boolean cfgFilePresent = false;
	private long	cfgFileLastModified;

	private CommandSet () {
		props = new Properties();
		allCmds 		= initAllCommands();
		availableCmds 	= initAvailableCommands (allCmds);
	}
	
	private HashMap initAvailableCommands (HashMap all) {
		cfgFile = new File (Server.BASE_PATH + "/config", "command.properties");
		HashMap available = checkActivatedCommands ();
		FileMonitor.getFileMonitor().addReloadable (this);
		return available;
	}
	
	private HashMap checkActivatedCommands () {
		if (cfgFile.exists() && cfgFile.isFile()) try {
			cfgFilePresent = true;
			cfgFileLastModified = cfgFile.lastModified();
			FileInputStream in = new FileInputStream(cfgFile);
			props.load(in);
			in.close();
		} catch (FileNotFoundException fnfe) {
			// never
		} catch (IOException ioe) {
			Server.log(this, "Unable to read command.properties", Server.MSG_ERROR, Server.LVL_MAJOR);
		} else {
			cfgFilePresent = false;
		}
		if (props == null)
			return allCmds;
		HashMap available = new HashMap();
		for (Iterator i= allCmds.keySet ().iterator (); i.hasNext() ;) {
			String curr = (String) i.next();
			String key = curr.substring(1).toLowerCase();
			String value = curr = props.getProperty (key);
			if (curr != null 
				&& (curr.equals("off") 
					|| curr.equals ("false")))
				continue;
			key = "/" + key;
			ICommand cmd = (ICommand) allCmds.get(key);
			available.put(key, cmd);
		}
		return available;
	}
	
	// FIXME: should be done automatically (placing a class which name
	// starts with Cmd into freecs.commands should automatically load
	// this class on startup)
	private HashMap initAllCommands() {
		HashMap all = new HashMap ();
		all.put ("/ack",	CmdAck.getInstance());
		all.put ("/a", 		CmdAccept.getInstance());
		all.put ("/me", 	CmdAct.getInstance());
		all.put ("/away", 	CmdAway.getInstance());
		all.put ("/ban", 	CmdBan.getInstance());
		all.put ("/wban",	CmdListBan.getInstance());
		all.put ("/col", 	CmdChangeColor.getInstance());
        all.put ("/fcol",   CmdChangeForeignColor.getInstance());
		all.put ("/c", 		CmdClear.getInstance());
		all.put ("/td", 	CmdHitDice.getInstance());
		all.put ("/ig", 	CmdIgnore.getInstance());
		all.put ("/rig", 	CmdRespectUser.getInstance());
		all.put ("/i", 		CmdInvite.getInstance());
		all.put ("/ia", 	CmdInviteAll.getInstance());
		all.put ("/j", 		CmdJoin.getInstance());
		all.put ("/jclosed",CmdJoinClosed.getInstance());
		all.put ("/ju", 	CmdJoinUser.getInstance());
		all.put ("/k", 		CmdKick.getInstance());
		all.put ("/kh", 	CmdKickHard.getInstance());
		all.put ("/kc", 	CmdKickToRoom.getInstance());
		all.put ("/fl", 	CmdListAllFriends.getInstance());
		all.put ("/f", 		CmdListOnlineFriends.getInstance());
		all.put ("/wc", 	CmdListUsers.getInstance());
		all.put ("/vip", 	CmdListVips.getInstance());
		all.put ("/l", 		CmdLock.getInstance());
		all.put ("/mycol", 	CmdMyCol.getInstance());
		all.put ("/m", 		CmdPrivateMessage.getInstance());
		all.put ("/gag", 	CmdPunish.getInstance());
		all.put ("/aq", 	CmdQuestion.getInstance());
        all.put ("/raq",    CmdResetQuestioncounter.getInstance());
		all.put ("/q", 		CmdQuit.getInstance());
		all.put ("/", 		CmdRepeatedPrivateMessage.getInstance());
		all.put ("/r", 		CmdReplyMessage.getInstance());
		all.put ("/sys", 	CmdSys.getInstance());
		all.put ("/rsu", 	CmdRSu.getInstance());
		all.put ("/sepa", 	CmdSepa.getInstance());
		all.put ("/t", 		CmdSetTheme.getInstance());
		all.put ("/s", 		CmdShout.getInstance());
		all.put ("/ip", 	CmdShowIp.getInstance());
		all.put ("/time", 	CmdShowTime.getInstance());
		all.put ("/w", 		CmdShowUserDetail.getInstance());
		all.put ("/su", 	CmdSu.getInstance());
		all.put ("/th", 	CmdThink.getInstance());
		all.put ("/uban", 	CmdUnBan.getInstance());
		all.put ("/ul", 	CmdUnlock.getInstance());
		all.put ("/rgag", 	CmdUnPunish.getInstance());
		all.put ("/rc", 	CmdRightChange.getInstance());
		return all;
	}
	
	public static CommandSet getCommandSet () {
		return cs;
	}
	
	public ICommand getCommand (String cmd) {
		return (ICommand) allCmds.get(cmd);
	} 

	public byte evaluate (String cmd, MessageState msgState, String param) {
        try {
            return evaluate (cmd, msgState, param, false);
        } catch (Exception e) {
            Server.debug(cmd, "evaluation caused exception", e, Server.MSG_ERROR, Server.LVL_MAJOR);
        }
        return -1;
	}
	public byte evaluate (String cmd, MessageState msgState, String param, boolean moderated) {
        try {
    		if (!msgState.cb.isValid())
    			return (INTERRUPTED);
    		ICommand cmdObj = (ICommand) availableCmds.get(cmd);
    		if (cmdObj == null) 
    			return (UNKNOWN_COMMAND);
    		return (cmdObj.execute(msgState, param) ? TRUE : FALSE);
        } catch (Exception e) {
            Server.debug (cmd, "evaluation caused exception", e, Server.MSG_ERROR, Server.LVL_MAJOR);
        }
        return -1;
	}

	/**
	 * Interface IReloadable's methods are deffined below here
	 */
	public boolean filePresent() {
		return cfgFilePresent;
	}

	public File getFile() {
		return cfgFile;
	}

	public long lastModified() {
		return cfgFileLastModified;
	}

	public void changed() {
		Server.log (this, "changed: reloaded commandset", Server.MSG_STATE, Server.LVL_MINOR);
		availableCmds=checkActivatedCommands ();
	}

	public void removed() {
		Server.log (this, "removed: removed commandset", Server.MSG_STATE, Server.LVL_MINOR);
		availableCmds=checkActivatedCommands ();
	}
	
	public void created() {
		Server.log (this, "created: loaded commandset", Server.MSG_STATE, Server.LVL_MINOR);
		availableCmds=checkActivatedCommands ();
	}
	public String toString () {
		return ("[CommandSet]");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -