source
Source for my MapleSolaxiaV2 (v83 MapleStory).
This commit is contained in:
201
src/scripting/event/EventManager.java
Normal file
201
src/scripting/event/EventManager.java
Normal file
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
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 as
|
||||
published by the Free Software Foundation 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 scripting.event;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import net.server.channel.Channel;
|
||||
import net.server.world.MapleParty;
|
||||
import server.TimerManager;
|
||||
import server.expeditions.MapleExpedition;
|
||||
import server.maps.MapleMap;
|
||||
import client.MapleCharacter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Matze
|
||||
*/
|
||||
public class EventManager {
|
||||
private Invocable iv;
|
||||
private Channel cserv;
|
||||
private Map<String, EventInstanceManager> instances = new HashMap<String, EventInstanceManager>();
|
||||
private Properties props = new Properties();
|
||||
private String name;
|
||||
private ScheduledFuture<?> schedule = null;
|
||||
|
||||
public EventManager(Channel cserv, Invocable iv, String name) {
|
||||
this.iv = iv;
|
||||
this.cserv = cserv;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
try {
|
||||
iv.invokeFunction("cancelSchedule", (Object) null);
|
||||
} catch (ScriptException ex) {
|
||||
ex.printStackTrace();
|
||||
} catch (NoSuchMethodException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void schedule(String methodName, long delay) {
|
||||
schedule(methodName, null, delay);
|
||||
}
|
||||
|
||||
public void schedule(final String methodName, final EventInstanceManager eim, long delay) {
|
||||
schedule = TimerManager.getInstance().schedule(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
iv.invokeFunction(methodName, eim);
|
||||
} catch (ScriptException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} catch (NoSuchMethodException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}, delay);
|
||||
}
|
||||
|
||||
public void cancelSchedule() {
|
||||
schedule.cancel(true);
|
||||
}
|
||||
|
||||
public ScheduledFuture<?> scheduleAtTimestamp(final String methodName, long timestamp) {
|
||||
return TimerManager.getInstance().scheduleAtTimestamp(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
iv.invokeFunction(methodName, (Object) null);
|
||||
} catch (ScriptException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} catch (NoSuchMethodException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}, timestamp);
|
||||
}
|
||||
|
||||
public Channel getChannelServer() {
|
||||
return cserv;
|
||||
}
|
||||
|
||||
public EventInstanceManager getInstance(String name) {
|
||||
return instances.get(name);
|
||||
}
|
||||
|
||||
public Collection<EventInstanceManager> getInstances() {
|
||||
return Collections.unmodifiableCollection(instances.values());
|
||||
}
|
||||
|
||||
public EventInstanceManager newInstance(String name) {
|
||||
EventInstanceManager ret = new EventInstanceManager(this, name);
|
||||
instances.put(name, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void disposeInstance(String name) {
|
||||
instances.remove(name);
|
||||
}
|
||||
|
||||
public Invocable getIv() {
|
||||
return iv;
|
||||
}
|
||||
|
||||
public void setProperty(String key, String value) {
|
||||
props.setProperty(key, value);
|
||||
}
|
||||
|
||||
public String getProperty(String key) {
|
||||
return props.getProperty(key);
|
||||
}
|
||||
|
||||
public void setProperty(String key, int value) {
|
||||
props.setProperty(key, value + "");
|
||||
}
|
||||
|
||||
public int getIntProperty(String key) {
|
||||
return Integer.parseInt(props.getProperty(key));
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
//Expedition method: starts an expedition
|
||||
public void startInstance(MapleExpedition exped) {
|
||||
try {
|
||||
EventInstanceManager eim = (EventInstanceManager) (iv.invokeFunction("setup", (Object) null));
|
||||
eim.registerExpedition(exped);
|
||||
exped.start();
|
||||
} catch (ScriptException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} catch (NoSuchMethodException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
//Regular method: player
|
||||
public void startInstance(MapleCharacter chr) {
|
||||
try {
|
||||
EventInstanceManager eim = (EventInstanceManager) (iv.invokeFunction("setup", (Object) null));
|
||||
eim.registerPlayer(chr);
|
||||
} catch (ScriptException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} catch (NoSuchMethodException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
//PQ method: starts a PQ
|
||||
public void startInstance(MapleParty party, MapleMap map) {
|
||||
try {
|
||||
EventInstanceManager eim = (EventInstanceManager) (iv.invokeFunction("setup", (Object) null));
|
||||
eim.registerParty(party, map);
|
||||
} catch (ScriptException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} catch (NoSuchMethodException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
//non-PQ method for starting instance
|
||||
public void startInstance(EventInstanceManager eim, String leader) {
|
||||
try {
|
||||
iv.invokeFunction("setup", eim);
|
||||
eim.setProperty("leader", leader);
|
||||
} catch (ScriptException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} catch (NoSuchMethodException ex) {
|
||||
Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user