【发布时间】:2014-07-11 11:04:00
【问题描述】:
好的,我不知道该怎么解释,也不知道怎么做,但我会尝试一步一步地解释我想要什么。
我想创建一个包含 EntitySpawnEvent 对象的 API。它可能看起来像这样:
namespace ExampleAPI
{
class EntitySpawnEvent
{
private bool cancelled;
private Entity entity;
public EntitySpawnEvent(Entity entity)
{
this.entity = entity;
this.cancelled = false;
}
public void SetCancelled(bool cancelled)
{
this.cancelled = cancelled;
}
public bool IsCancelled()
{
return this.cancelled;
}
}
}
然后我将拥有一个使用此 API 的服务器。该服务器还将加载也使用 API 的插件。服务器可能是这样的:
using System.Generics;
using ExampleAPI;
namespace ExampleServer
{
class Server
{
private List<Plugin> plugins;
public OnEnable()
{
LoadPlugins();
}
private void LoadPlugins()
{
// Loop through all "plugins" in the "/plugins" folder.
// Add them all to the list of plugins.
}
}
}
然后当服务器想要生成一个实体时,它会将事件抛出给所有插件,插件可以操纵事件的信息。例如,是否取消事件。插件的事件监听器可能如下所示:
using ExampleAPI;
namespace ExamplePlugin
{
class Plugin : EventListener
{
public void onEntitySpawn(EntitySpawnEvent event)
{
event.SetCancelled(true);
}
}
}
服务器会抛出这样的东西:
using ExampleAPI;
namespace ExampleServer
{
class ExampleEventThrower
{
private Server server;
public ExampleEventThrower(Server server)
{
this.server = server;
SpawnEntity();
}
void SpawnEntity()
{
EntitySpawnEvent event = new EntitySpawnEvent(new Entity()); // Entity would also be part of the API
foreach (Plugin plugin in server.GetPlugins())
{
plugin.onEntitySpawn(event); // Here the plugin could manipulate the values of the EntitySpawnEvent
}
if (!event.IsCancelled())
{
// Spawn entity
}
}
}
}
当然,这些只是非常基本的代码示例,但它们应该有助于解释我想要什么。
基本上,我想知道和做的如下:
我有一个导出的服务器。 服务器有一个 /plugins 文件夹 用户可以使用 API 制作自己的插件,将它们导出并放在 /plugins 文件夹中 服务器将加载插件并让它修改所有事件等。
我的关键问题是,应该如何导出和加载插件,以便它们可以操纵事件等?我是否将它们导出为 DDL?我不知道。 我想这有点类似于 Bukkit 的工作方式,但一切都在 Java 中,你只需将它导出为 .jar 文件。
任何帮助将不胜感激。谢谢!
【问题讨论】:
标签: java c# communication bukkit