1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-09 17:02:53 +00:00
Files
l2jserver2/src/main/java/com/l2jserver/service/game/template/StaticTemplateService.java
rogiel 51aea46020 Change-Id: I0000000000000000000000000000000000000000
Change-Id: I8636776eaf000fcdfb528cc403710f6d6ee9e73e
Change-Id: Iebc523681d07ecd6d7b7e89343b29a8034558f94
2011-05-07 01:06:17 -03:00

83 lines
2.3 KiB
Java

package com.l2jserver.service.game.template;
import java.util.Map;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.l2jserver.model.id.TemplateID;
import com.l2jserver.model.template.Template;
import com.l2jserver.service.AbstractService;
import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.ServiceStopException;
import com.l2jserver.service.configuration.ConfigurationService;
import com.l2jserver.service.game.scripting.ScriptContext;
import com.l2jserver.service.game.scripting.ScriptingService;
import com.l2jserver.util.factory.CollectionFactory;
public class StaticTemplateService extends AbstractService implements
TemplateService {
private final ScriptingService scriptingService;
private final StaticTemplateServiceConfiguration config;
private final Injector injector;
private ScriptContext context;
@SuppressWarnings("rawtypes")
private Map<TemplateID, Template> templates = CollectionFactory.newMap(
TemplateID.class, Template.class);
@Inject
public StaticTemplateService(ScriptingService scriptingService,
ConfigurationService configService, Injector injector) {
this.scriptingService = scriptingService;
this.injector = injector;
this.config = configService
.get(StaticTemplateServiceConfiguration.class);
}
@Override
public void start() throws ServiceStartException {
if (context == null) {
try {
context = scriptingService.load(config.getTemplateDescriptor())
.get(0);
} catch (Exception e) {
throw new ServiceStartException(e);
}
return;
}
if (context.isInitialized())
context.shutdown();
context.init();
}
@Override
@SuppressWarnings("unchecked")
public <T extends Template<?>> T getTemplate(TemplateID<T> id) {
return (T) templates.get(id);
}
public void addTemplate(Class<? extends Template<?>> t) {
final Template<?> template = injector.getInstance(t);
System.out.println(template.getID() + " -> " + template);
if (template.getID() != null)
templates.put(template.getID(), template);
}
public void removeTemplate(Template<?> t) {
// TODO templates.remove(t);
}
@Override
public void reload() {
context.reload();
}
@Override
public void stop() throws ServiceStopException {
if (context.isInitialized())
context.shutdown();
context = null;
}
}