NPCScriptManager.java
资源名称:src.rar [点击查看]
上传用户:gwt600
上传日期:2021-06-03
资源大小:704k
文件大小:7k
源码类别:
游戏
开发平台:
Java
- /*
- This file is part of the OdinMS Maple Story Server
- Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
- Matthias Butz <matze@odinms.de>
- Jan Christian Meyer <vimes@odinms.de>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License version 3
- as published by the Free Software Foundation. You may not use, modify
- or distribute this program under any other version of the
- GNU Affero General Public License.
- 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 Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package net.sf.odinms.scripting.npc;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.script.Invocable;
- import net.sf.odinms.client.MapleClient;
- import net.sf.odinms.client.MapleCharacter;
- import net.sf.odinms.scripting.AbstractScriptManager;
- import net.sf.odinms.tools.Pair;
- /**
- *
- * @author Matze
- */
- public class NPCScriptManager extends AbstractScriptManager {
- private Map<MapleClient, NPCConversationManager> cms = new HashMap<MapleClient, NPCConversationManager>();
- private Map<MapleClient, NPCScript> scripts = new HashMap<MapleClient, NPCScript>();
- private static NPCScriptManager instance = new NPCScriptManager();
- public synchronized static NPCScriptManager getInstance() {
- return instance;
- }
- public void start(MapleClient c, int i,String npc) {
- try {
- if (cms.containsKey(c)) {
- return;
- } else {
- }
- } catch (Exception e) {
- System.err.println("NPC脚本发生错误.请检查NPC编号:(" + npc + ")的JS.");
- dispose(c);
- cms.remove(c);
- }
- }
- public void start(MapleClient c, int npc) {
- start(c, npc, null, null);
- }
- public void start(MapleClient c, int npc, String filename, MapleCharacter chr) {
- try {
- NPCConversationManager cm = new NPCConversationManager(c, npc, chr);
- if (cms.containsKey(c)) {
- return;
- }
- cms.put(c, cm);
- Invocable iv = getInvocable("npc/" + npc + ".js", c);
- NPCScriptManager npcsm = NPCScriptManager.getInstance();
- if (filename != null) {
- iv = getInvocable("npc/" + filename + ".js", c);
- }
- if (iv == null || npcsm == null || NPCScriptManager.getInstance() == null) {
- cm.sendSimple("5555,我无法为你服务哦,我编号: (" + npc + ")快去加脚本吧 ");
- cm.dispose();
- return;
- }
- engine.put("cm", cm);
- NPCScript ns = iv.getInterface(NPCScript.class);
- scripts.put(c, ns);
- if (chr != null) {
- ns.start(chr);
- } else {
- ns.start();
- }
- } catch (Exception e) {
- System.err.println("NPC脚本发生错误.请检查NPC编号:(" + npc + ")的JS.");
- dispose(c);
- cms.remove(c);
- }
- }
- public void start(MapleClient c, String filename, int npc, MapleCharacter cha) {
- try {
- NPCConversationManager cm = new NPCConversationManager(c, npc, cha);
- if (cms.containsKey(c)) {
- return;
- }
- cms.put(c, cm);
- Invocable iv = getInvocable("npc/" + filename + ".js", c);
- if (iv == null || NPCScriptManager.getInstance() == null) {
- cm.dispose();
- return;
- }
- engine.put("cm", cm);
- NPCScript ns = iv.getInterface(NPCScript.class);
- scripts.put(c, ns);
- ns.start(cha);
- } catch (Exception e) {
- System.err.println("NPC脚本发生错误.请检查NPC编号:(" + npc + ")的JS.");
- dispose(c);
- cms.remove(c);
- }
- }
- public void action(MapleClient c, byte mode, byte type, int selection) {
- NPCScript ns = scripts.get(c);
- if (ns != null) {
- try {
- ns.action(mode, type, selection);
- } catch (Exception e) {
- System.err.println("执行NPC脚本过程中发生错误.请检查NPC文件.");
- dispose(c);
- }
- }
- }
- public void action(MapleClient c, byte mode, byte type, int selection, int npc ) {
- NPCScript ns = scripts.get(c);
- if (ns != null) {
- try {
- ns.action(mode, type, selection);
- } catch (Exception e) {
- System.err.println("NPC脚本发生错误.请检查NPC编号:(" + npc + ")的JS.");
- dispose(c);
- }
- }
- }
- public void dispose(NPCConversationManager cm) {
- cms.remove(cm.getC());
- scripts.remove(cm.getC());
- if (cm.getFileName() != null) {
- resetContext("npc/" + cm.getFileName() + ".js", cm.getC());
- } else {
- resetContext("npc/" + cm.getNpc() + ".js", cm.getC());
- }
- }
- public void dispose(MapleClient c) {
- NPCConversationManager npccm = cms.get(c);
- if (npccm != null) {
- dispose(npccm);
- }
- }
- public NPCConversationManager getCM(MapleClient c) {
- return cms.get(c);
- }
- private Map<Pair<Integer, Integer>, Integer> npcTalk = new HashMap<Pair<Integer, Integer>, Integer>();
- public int getNpcTalkTimes(int chrid, int npc) {
- Pair<Integer, Integer> pplayer = new Pair<Integer, Integer>(chrid, npc); // first time <3 looks wrong.
- if (!npcTalk.containsKey(pplayer)) {
- npcTalk.put(pplayer, 0);
- }
- return npcTalk.get(pplayer);
- }
- public void addNpcTalkTimes(int chrid, int npc) {
- Pair<Integer, Integer> pplayer = new Pair<Integer, Integer>(chrid, npc);
- if (!npcTalk.containsKey(pplayer)) {
- npcTalk.put(pplayer, 0);
- }
- int talk = 1 + npcTalk.get(pplayer);
- npcTalk.remove(pplayer);
- npcTalk.put(pplayer, talk);
- }
- public void setNpcTalkTimes(int chrid, int npc, int amount) {
- Pair<Integer, Integer> pplayer = new Pair<Integer, Integer>(chrid, npc);
- if (!npcTalk.containsKey(pplayer)) {
- npcTalk.put(pplayer, 0);
- }
- npcTalk.remove(pplayer);
- npcTalk.put(pplayer, amount);
- }
- public List<Integer> listTalkedNpcsByID(int chrid) {
- List<Integer> npcs = new ArrayList<Integer>();
- for (Pair<Integer, Integer> rawr : npcTalk.keySet()) {
- if (rawr.getLeft().equals(chrid)) {
- npcs.add(rawr.getRight());
- }
- }
- return npcs;
- }
- public List<Integer> listAllTalkedNpcs() {
- List<Integer> npcs = new ArrayList<Integer>();
- for (Pair<Integer, Integer> rawr : npcTalk.keySet()) {
- npcs.add(rawr.getRight());
- }
- return npcs;
- }
- public int talkedTimesByNpc(int npc) {
- int i = 0;
- for (Pair<Integer, Integer> rawr : npcTalk.keySet()) {
- if (rawr.getRight().equals(npc)) {
- i += npcTalk.get(rawr);
- }
- }
- return i;
- }
- }