Skip to content

Commit

Permalink
feat: ✨ remove net.deechael.khl and self handling kook
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Dec 16, 2022
1 parent eb122a3 commit edf4f27
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 155 deletions.
26 changes: 8 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,14 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.deechael</groupId>
<artifactId>khl</artifactId>
<version>1.09.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</exclusion>
</exclusions>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.21</version>
</dependency>
</dependencies>

Expand Down
54 changes: 18 additions & 36 deletions src/main/java/com/zhanganzhi/chathub/ChatHub.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.zhanganzhi.chathub;

import java.io.File;
import java.util.HashSet;
import java.nio.file.Path;
import java.lang.reflect.Field;

import org.slf4j.Logger;
import com.google.inject.Inject;
Expand All @@ -13,9 +10,6 @@
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import net.deechael.khl.bot.KaiheilaBot;
import net.deechael.khl.bot.KaiheilaBotBuilder;
import net.deechael.khl.event.MessageHandler;

import com.zhanganzhi.chathub.core.Config;
import com.zhanganzhi.chathub.core.EventHub;
Expand All @@ -32,70 +26,58 @@
authors = {"Andy Zhang", "ZhuRuoLing"}
)
public class ChatHub {
private final ProxyServer server;
private final ProxyServer proxyServer;
private final Logger logger;
private final Path dataDirectory;
private KaiheilaBot kaiheilaBot;
private EventHub eventHub;
private KookReceiver kookReceiver;

@Inject
public ChatHub(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
this.server = server;
public ChatHub(ProxyServer proxyServer, Logger logger, @DataDirectory Path dataDirectory) {
this.proxyServer = proxyServer;
this.logger = logger;
this.dataDirectory = dataDirectory;
}

public ProxyServer getServer() {
return server;
public ProxyServer getProxyServer() {
return proxyServer;
}

public Logger getLogger() {
return logger;
}

public KaiheilaBot getKaiheilaBot() {
return kaiheilaBot;
public EventHub getEventHub() {
return eventHub;
}

@Subscribe
public void onInitialize(ProxyInitializeEvent event) {
// core
Config.getInstance().loadConfig(dataDirectory);
kaiheilaBot = (KaiheilaBot) KaiheilaBotBuilder.builder().createDefault(Config.getInstance().getKookToken()).build();
EventHub eventHub = new EventHub(this);
eventHub = new EventHub(this);

// command
server.getCommandManager().register(
server.getCommandManager().metaBuilder("chathub").plugin(this).build(),
new Command(server)
proxyServer.getCommandManager().register(
proxyServer.getCommandManager().metaBuilder("chathub").plugin(this).build(),
new Command(proxyServer)
);

// velocity receiver
server.getEventManager().register(this, new VelocityReceiver(eventHub));
proxyServer.getEventManager().register(this, new VelocityReceiver(this));

// kook receiver
if (Config.getInstance().isKookEnabled()) {
new Thread(() -> {
try {
Field messageHandlersField = ChatHub.this.kaiheilaBot.getEventManager().getClass().getDeclaredField("messageHandlers");
messageHandlersField.setAccessible(true);
messageHandlersField.set(ChatHub.this.kaiheilaBot.getEventManager(), new HashSet<MessageHandler>());
} catch (Exception exception) {
throw new RuntimeException(exception);
}
ChatHub.this.kaiheilaBot.getEventManager().registerMessageHandler(new KookReceiver(eventHub));
ChatHub.this.kaiheilaBot.start();
}).start();
logger.info("Kook enabled");
kookReceiver = new KookReceiver(this);
kookReceiver.start();
}
}

@Subscribe
public void onProxyShutdown(ProxyShutdownEvent event) {
if (Config.getInstance().isKookEnabled()) {
kaiheilaBot.shutdown();
File sessionFile = new File("session.dat");
if (sessionFile.exists()) {
sessionFile.delete();
}
kookReceiver.shutdown();
}
}
}
23 changes: 19 additions & 4 deletions src/main/java/com/zhanganzhi/chathub/core/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class Config {
private static final Config config = new Config();
private Toml configToml;
private boolean tempIsKookEnabled;

private Config() {
}
Expand All @@ -31,11 +32,16 @@ public void loadConfig(Path dataDirectory) {
try {
Files.copy(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("config.toml")), configFile.toPath());
} catch (IOException e) {
throw new RuntimeException(e);
e.printStackTrace();
}
}

configToml = new Toml().read(configFile);
tempIsKookEnabled = configToml.getBoolean("kook.enable");
}

public void setIsKookEnabled(boolean isKookEnabled) {
this.tempIsKookEnabled = isKookEnabled;
}

public boolean isCompleteTakeoverMode() {
Expand Down Expand Up @@ -103,7 +109,7 @@ public String getMinecraftListMessage(String server, int count, String[] playerL
}

public boolean isKookEnabled() {
return configToml.getBoolean("kook.enable");
return tempIsKookEnabled;
}

public String getKookToken() {
Expand All @@ -114,11 +120,12 @@ public String getKookChannelId() {
return configToml.getString("kook.channelId");
}

public String getKookChatMessage(String server, String name) {
public String getKookChatMessage(String server, String name, String message) {
return configToml
.getString("kook.message.chat")
.replace("{server}", getPlainServername(server))
.replace("{name}", name);
.replace("{name}", name)
.replace("{message}", message);
}

public String getKookJoinMessage(String server, String name) {
Expand All @@ -141,4 +148,12 @@ public String getKookSwitchMessage(String name, String serverFrom, String server
.replace("{serverFrom}", getPlainServername(serverFrom))
.replace("{serverTo}", getPlainServername(serverTo));
}

public String getKookListMessage(String server, int count, String[] playerList) {
return configToml
.getString("kook.message.list")
.replace("{server}", getPlainServername(server))
.replace("{count}", String.valueOf(count))
.replace("{playerList}", String.join(", ", playerList));
}
}
13 changes: 9 additions & 4 deletions src/main/java/com/zhanganzhi/chathub/core/EventHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,36 @@ public EventHub(ChatHub chatHub) {
kookSender = new KookSender(chatHub);
}

public KookSender getKookSender() {
return kookSender;
}

public void onMinecraftMessage(String server, String name, String message) {
velocitySender.sendChatMessage(server, name, message);
if (Config.getInstance().isKookEnabled()) {
kookSender.sendChatMessage(server, name, message);
new Thread(() -> kookSender.sendChatMessage(server, name, message)).start();
}
}

public void onMinecraftJoinServer(String server, String name) {
velocitySender.sendJoinMessage(server, name);
if (Config.getInstance().isKookEnabled()) {
kookSender.sendJoinMessage(server, name);
new Thread(() -> kookSender.sendJoinMessage(server, name)).start();
}
}

public void onMinecraftLeaveServer(String name) {
velocitySender.sendLeaveMessage(name);
if (Config.getInstance().isKookEnabled()) {
kookSender.sendLeaveMessage(name);
new Thread(() -> kookSender.sendLeaveMessage(name)).start();
}
}

public void onMinecraftSwitchServer(String name, String serverFrom, String serverTo) {
velocitySender.sendSwitchMessage(name, serverFrom, serverTo);
if (Config.getInstance().isKookEnabled()) {
kookSender.sendSwitchMessage(name, serverFrom, serverTo);
new Thread(() -> kookSender.sendSwitchMessage(name, serverFrom, serverTo)).start();

}
}

Expand Down

This file was deleted.

Loading

0 comments on commit edf4f27

Please sign in to comment.