[Add][Change] Framework-API, Core 添加 FrameworkContext 用于向 Framework 传递与 Bot 有关的相关对象;

[Add] FrameworkContext, DefaultFrameworkContext 添加 FrameworkContext 和一个默认实现;
[Change] Framework, FrameworkFactory 调整代码以支持传递 FrameworkContext;
This commit is contained in:
2020-11-05 00:58:07 +08:00
parent 4a2337afd7
commit 2875426f72
4 changed files with 114 additions and 5 deletions

View File

@ -31,6 +31,7 @@ import java.io.File;
public abstract class Framework extends Plugin {
private final File dataFolder;
private final FrameworkContext context;
/**
* 由 FrameworkManager 执行的构造方法.
@ -38,8 +39,9 @@ public abstract class Framework extends Plugin {
*
* @param wrapper 包含框架运行期间需要使用对象的包装器.
*/
public Framework(PluginWrapper wrapper, File dataFolder) {
public Framework(PluginWrapper wrapper, File dataFolder, FrameworkContext context) {
super(wrapper);
this.context = context;
if (!(wrapper.getDescriptor() instanceof FrameworkDescriptor)) {
throw new IllegalStateException("Invalid description object");
}
@ -86,4 +88,11 @@ public abstract class Framework extends Plugin {
throw new IllegalStateException("无法转换 Descriptor 的类型, 框架管理器可能遭到修改!");
}
/**
* 获取当前框架对象与所属 ContentGrabbingJiBot 的上下文.
* @return 返回上下文对象.
*/
protected FrameworkContext getContext() {
return context;
}
}

View File

@ -0,0 +1,45 @@
/*
* Copyright (C) 2020 LamGC
*
* ContentGrabbingJi 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, either version 3 of the
* License, or (at your option) any later version.
*
* ContentGrabbingJi 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 <https://www.gnu.org/licenses/>.
*/
package net.lamgc.cgj.bot.framework;
import net.lamgc.cgj.bot.cache.CacheStoreBuilder;
import net.lamgc.cgj.bot.event.EventExecutor;
/**
* 框架上下文对象.
* <p> 由于 ContentGrabbingJi 按实例分配资源,
* 故 Context 可获取专属于框架所属实例的相关可用资源以供框架使用.
* @author LamGC
*/
public interface FrameworkContext {
/**
* 获取事件执行器.
* <p> 事件执行器用于将事件提交给 CGJ 进行处理.
* @return 返回事件执行器.
*/
EventExecutor getEventExecutor();
/**
* 获取缓存存储容器构造器.
* <p> 如需使用缓存, 可通过 {@link CacheStoreBuilder} 获取独立的缓存容器.
* @return 返回构造器.
*/
CacheStoreBuilder getCacheStoreBuilder();
}