Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ subprojects {
compileOnly("org.slf4j:slf4j-api:2.0.17")
compileOnly("org.graalvm.js:js:22.0.0.2")

// plugins
implementation("org.pf4j:pf4j:3.6.0")

// storage
compileOnly("dev.neovoxel.nsapi:NeoStorageAPI:1.1.0")
compileOnly("com.zaxxer:HikariCP:4.0.3")
Expand Down
27 changes: 27 additions & 0 deletions bukkit/src/main/java/dev/neovoxel/neobot/NeoBotBukkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import dev.neovoxel.neobot.config.ScriptConfig;
import dev.neovoxel.neobot.event.BukkitEventManager;
import dev.neovoxel.neobot.game.GameEventListener;
import dev.neovoxel.neobot.misc.EventListener;
import dev.neovoxel.neobot.extension.BukkitExtensionsManager;
import dev.neovoxel.neobot.scheduler.ScheduledTask;
import dev.neovoxel.neobot.script.ScriptProvider;
import dev.neovoxel.neobot.script.ScriptScheduler;
Expand All @@ -24,6 +26,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Getter
public class NeoBotBukkit extends JavaPlugin implements NeoBot {
Expand All @@ -35,6 +38,10 @@ public class NeoBotBukkit extends JavaPlugin implements NeoBot {
@Setter
private ScriptProvider scriptProvider;

@Getter
@Setter
private BukkitExtensionsManager extensionsManager;

@Getter(onMethod_ = {@HostAccess.Export})
@Setter
private StorageProvider storageProvider;
Expand Down Expand Up @@ -68,12 +75,32 @@ public NeoLogger getNeoLogger() {
return new BukkitLogger(getLogger());
}

public void initExtensionsManager() {
extensionsManager = new BukkitExtensionsManager();
}

@Override
public void setGameEventListener(GameEventListener listener) {
Bukkit.getPluginManager().registerEvents(new BukkitEventManager(this), this);
this.gameEventListener = listener;
}

@Override
public Map<String, EventListener> getListenerMap(){
return extensionsManager.getListenerMap();
}

@Override
public void loadExtensions(NeoBot plugin) {
extensionsManager.loadExtensions(plugin);
}

@Override
public void unloadExtensions() {
extensionsManager.unloadExtensions();
}


@Override
public ScheduledTask submit(Runnable task) {
return new BukkitScheduledTask(Bukkit.getScheduler().runTask(this, task));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.neovoxel.neobot.extension;

import dev.neovoxel.neobot.NeoBot;

public class BukkitExtensionsManager extends ExtensionsManager {
@Override
public void loadExtensions(NeoBot plugin) {
super.loadExtensions(plugin);
}
}
16 changes: 15 additions & 1 deletion common/src/main/java/dev/neovoxel/neobot/NeoBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.neovoxel.neobot.game.GameEventListener;
import dev.neovoxel.neobot.game.GameProvider;
import dev.neovoxel.neobot.library.LibraryProvider;
import dev.neovoxel.neobot.extension.ExtensionsProvider;
import dev.neovoxel.neobot.scheduler.SchedulerProvider;
import dev.neovoxel.neobot.script.ScriptProvider;
import dev.neovoxel.neobot.script.ScriptScheduler;
Expand All @@ -17,7 +18,7 @@

import java.io.File;

public interface NeoBot extends ConfigProvider, GameProvider, LibraryProvider, SchedulerProvider {
public interface NeoBot extends ExtensionsProvider, ConfigProvider, GameProvider, LibraryProvider, SchedulerProvider {
default void enable() {
try {
getNeoLogger().info("Loading libraries...");
Expand All @@ -35,6 +36,9 @@ default void enable() {
BotProvider botProvider = new BotProvider(this);
setBotProvider(botProvider);
getBotProvider().loadBot(this);
getNeoLogger().info("Loading extensions...");
this.initExtensionsManager();
loadExtensions(this);
getNeoLogger().info("Loading script system...");
submitAsync(() -> {
try {
Expand All @@ -61,6 +65,8 @@ default void disable() {
getScriptProvider().unloadScript();
getBotProvider().getBotListener().reset();
getGameEventListener().reset();
getNeoLogger().info("Unloading all extensions...");
unloadExtensions();
getNeoLogger().info("Cancelling all the tasks...");
cancelAllTasks();
getScriptScheduler().clear();
Expand All @@ -85,6 +91,12 @@ default void reload(CommandSender sender) {
getGameEventListener().reset();
getNeoLogger().info("Reloading scripts...");
getScriptProvider().unloadScript();

getNeoLogger().info("Reloading extensions...");
unloadExtensions();
this.initExtensionsManager();
loadExtensions(this);

getScriptProvider().loadScript(this);
getGameEventListener().onPluginReloaded();
} catch (Throwable e) {
Expand All @@ -101,6 +113,8 @@ default void reload(CommandSender sender) {

File getDataFolder();

void initExtensionsManager();

void setGameEventListener(GameEventListener listener);

GameEventListener getGameEventListener();
Expand Down
75 changes: 22 additions & 53 deletions common/src/main/java/dev/neovoxel/neobot/bot/BotListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import dev.neovoxel.nbapi.listener.NBotEventHandler;
import dev.neovoxel.nbapi.listener.NBotListener;
import dev.neovoxel.neobot.NeoBot;
import dev.neovoxel.neobot.misc.EventListener;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.Value;
import org.json.JSONArray;
Expand All @@ -22,39 +23,24 @@
import java.util.List;
import java.util.Map;

public class BotListener implements NBotListener {
private final NeoBot plugin;

private final Map<Value, String> map = new LinkedHashMap<>();
public class BotListener extends EventListener implements NBotListener {

public BotListener(NeoBot plugin) {
this.plugin = plugin;
super(plugin);
}

@HostAccess.Export
public void sendGroupMessage(long groupId, String message) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new SendGroupMessage(groupId, new JSONArray(message)));
}
});
}

public void clearUuidContext(String uuid) {
List<Value> toRemove = new ArrayList<>();
for (Map.Entry<Value, String> entry : map.entrySet()) {
if (entry.getKey().getContext().getBindings("js").getMember("__uuid__").asString().equals(uuid)) {
toRemove.add(entry.getKey());
}
}
for (Value value : toRemove) {
map.remove(value);
}
}

@HostAccess.Export
public void sendPrivateMessage(long userId, String message) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new SendPrivateMessage(userId, new JSONArray(message)));
}
Expand All @@ -63,7 +49,7 @@ public void sendPrivateMessage(long userId, String message) {

@HostAccess.Export
public void renameGroupMember(long groupId, long userId, String name) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new SetGroupCard(groupId, userId, name));
}
Expand All @@ -72,7 +58,7 @@ public void renameGroupMember(long groupId, long userId, String name) {

@HostAccess.Export
public void muteGroupMember(long groupId, long userId, int duration) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new SetGroupBan(groupId, userId, duration));
}
Expand All @@ -81,7 +67,7 @@ public void muteGroupMember(long groupId, long userId, int duration) {

@HostAccess.Export
public void muteAllGroupMember(long groupId) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new SetGroupWholeBan(groupId, true));
}
Expand All @@ -90,7 +76,7 @@ public void muteAllGroupMember(long groupId) {

@HostAccess.Export
public void unMuteAllGroupMember(long groupId) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new SetGroupWholeBan(groupId, false));
}
Expand All @@ -99,7 +85,7 @@ public void unMuteAllGroupMember(long groupId) {

@HostAccess.Export
public void kickGroupMember(long groupId, long userId) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new SetGroupKick(groupId, userId));
}
Expand All @@ -108,7 +94,7 @@ public void kickGroupMember(long groupId, long userId) {

@HostAccess.Export
public void approveGroupRequest(String flag, String type) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new SetGroupAddRequest(flag, GroupRequestType.from(type)));
}
Expand All @@ -117,7 +103,7 @@ public void approveGroupRequest(String flag, String type) {

@HostAccess.Export
public void rejectGroupRequest(String flag, String type) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new SetGroupAddRequest(flag, GroupRequestType.from(type), false));
}
Expand All @@ -126,7 +112,7 @@ public void rejectGroupRequest(String flag, String type) {

@HostAccess.Export
public void approveFriendRequest(String flag) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new SetFriendAddRequest(flag));
}
Expand All @@ -135,7 +121,7 @@ public void approveFriendRequest(String flag) {

@HostAccess.Export
public void rejectFriendRequest(String flag) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new SetFriendAddRequest(flag, false));
}
Expand All @@ -144,7 +130,7 @@ public void rejectFriendRequest(String flag) {

@HostAccess.Export
public void setGroupSpecialTitle(long groupId, long userId, String title, long duration) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new SetGroupSpecialTitle(groupId, userId, title, duration));
}
Expand All @@ -153,7 +139,7 @@ public void setGroupSpecialTitle(long groupId, long userId, String title, long d

@HostAccess.Export
public void setGroupWholeBan(long groupId, boolean enable) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new SetGroupWholeBan(groupId, enable));
}
Expand All @@ -162,7 +148,7 @@ public void setGroupWholeBan(long groupId, boolean enable) {

@HostAccess.Export
public void recallMessage(long messageId) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new DeleteMessage(messageId));
}
Expand All @@ -172,7 +158,7 @@ public void recallMessage(long messageId) {
@HostAccess.Export
public void getGroupMemberInfo(long groupId, long userId, Value method) {
if (method.canExecute()) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new GetGroupMemberInfo(groupId, userId), method::execute);
} else {
Expand All @@ -185,7 +171,7 @@ public void getGroupMemberInfo(long groupId, long userId, Value method) {
@HostAccess.Export
public void getGroupMemberList(long groupId, Value method) {
if (method.canExecute()) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new GetGroupMemberList(groupId), method::execute);
} else method.execute(new ArrayList<>());
Expand All @@ -196,7 +182,7 @@ public void getGroupMemberList(long groupId, Value method) {
@HostAccess.Export
public void getFriendList(Value method) {
if (method.canExecute()) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new GetFriendList(), method::execute);
} else method.execute(new ArrayList<>());
Expand All @@ -207,7 +193,7 @@ public void getFriendList(Value method) {
@HostAccess.Export
public void getGroupList(Value method) {
if (method.canExecute()) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new GetGroupList(), method::execute);
} else method.execute(new ArrayList<>());
Expand All @@ -218,30 +204,13 @@ public void getGroupList(Value method) {
@HostAccess.Export
public void getGroupInfo(long groupId, Value method) {
if (method.canExecute()) {
plugin.getBotProvider().getBot().forEach(client -> {
getPlugin().getBotProvider().getBot().forEach(client -> {
if (client.isConnected()) {
client.action(new GetGroupInfo(groupId), method::execute);
} else method.execute(new ArrayList<>());
});
}
}

@HostAccess.Export
public void register(String eventName, Value method) {
if (method.canExecute()) map.put(method, eventName);
}

public void reset() {
map.clear();
}

private void fireEvent(String eventName, NEvent event) {
for (Map.Entry<Value, String> entry : map.entrySet()) {
if (entry.getValue().equals(eventName)) {
entry.getKey().execute(event);
}
}
}

@NBotEventHandler
private void onGroupMessage(GroupMessageEvent event) {
Expand Down
Loading