trying to set up a matrix bridge
This commit is contained in:
@@ -2,23 +2,33 @@ package com.alttd.velocitychat;
|
||||
|
||||
import com.alttd.chat.ChatAPI;
|
||||
import com.alttd.chat.ChatImplementation;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.database.DatabaseConnection;
|
||||
import com.alttd.chat.managers.ChatUserManager;
|
||||
import com.alttd.chat.managers.PartyManager;
|
||||
import com.alttd.chat.objects.ChatUser;
|
||||
import com.alttd.chat.objects.chat_log.ChatLogHandler;
|
||||
import com.alttd.chat.util.ALogger;
|
||||
import com.alttd.matrix.MatrixConfig;
|
||||
import com.alttd.matrix.MatrixRuntime;
|
||||
import com.alttd.matrix.bridge.DefaultMatrixBridge;
|
||||
import com.alttd.matrix.interfaces.MatrixAppServiceServer;
|
||||
import com.alttd.matrix.interfaces.MatrixBridge;
|
||||
import com.alttd.matrix.interfaces.MatrixClient;
|
||||
import com.alttd.velocitychat.commands.*;
|
||||
import com.alttd.chat.config.Config;
|
||||
import com.alttd.chat.database.DatabaseConnection;
|
||||
import com.alttd.velocitychat.handlers.ChatHandler;
|
||||
import com.alttd.velocitychat.handlers.ServerHandler;
|
||||
import com.alttd.velocitychat.listeners.ChatListener;
|
||||
import com.alttd.velocitychat.listeners.LiteBansListener;
|
||||
import com.alttd.velocitychat.listeners.ProxyPlayerListener;
|
||||
import com.alttd.velocitychat.listeners.PluginMessageListener;
|
||||
import com.alttd.chat.util.ALogger;
|
||||
import com.alttd.velocitychat.listeners.ProxyPlayerListener;
|
||||
import com.alttd.velocitychat.matrix.MatrixRoomMap;
|
||||
import com.alttd.velocitychat.matrix.VelocityMatrixInbound;
|
||||
import com.alttd.velocitychat.matrix.VelocityMatrixOutbound;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.event.EventManager;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
|
||||
import com.velocitypowered.api.plugin.Dependency;
|
||||
@@ -36,14 +46,16 @@ import java.nio.file.Path;
|
||||
description = "A chat plugin for Altitude Minecraft Server",
|
||||
authors = {"destro174", "teri"},
|
||||
dependencies = {@Dependency(id = "luckperms"), @Dependency(id = "litebans"), @Dependency(id = "proxydiscordlink")}
|
||||
)
|
||||
)
|
||||
public class VelocityChat {
|
||||
|
||||
private static VelocityChat plugin;
|
||||
private final ProxyServer server;
|
||||
private final Logger logger;
|
||||
private final Path dataDirectory;
|
||||
private final EventManager eventManager;
|
||||
|
||||
private MatrixAppServiceServer matrixAsServer;
|
||||
private ChatAPI chatAPI;
|
||||
private ChatHandler chatHandler;
|
||||
private ServerHandler serverHandler;
|
||||
@@ -51,11 +63,12 @@ public class VelocityChat {
|
||||
private ChannelIdentifier channelIdentifier;
|
||||
|
||||
@Inject
|
||||
public VelocityChat(ProxyServer proxyServer, Logger proxyLogger, @DataDirectory Path proxydataDirectory) {
|
||||
public VelocityChat(ProxyServer proxyServer, Logger proxyLogger, @DataDirectory Path proxydataDirectory, EventManager eventManager) {
|
||||
plugin = this;
|
||||
server = proxyServer;
|
||||
logger = proxyLogger;
|
||||
dataDirectory = proxydataDirectory;
|
||||
this.eventManager = eventManager;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
@@ -80,6 +93,7 @@ public class VelocityChat {
|
||||
ChatUser console = new ChatUser(Config.CONSOLEUUID, -1, null);
|
||||
console.setDisplayName(Config.CONSOLENAME);
|
||||
ChatUserManager.addUser(console);
|
||||
initMatrix();
|
||||
}
|
||||
|
||||
public void reloadConfig() {
|
||||
@@ -92,6 +106,42 @@ public class VelocityChat {
|
||||
getProxy().getAllServers().stream().forEach(registeredServer -> registeredServer.sendPluginMessage(getChannelIdentifier(), buf.toByteArray()));
|
||||
}
|
||||
|
||||
//TODO call initMatrix
|
||||
private void initMatrix() {
|
||||
MatrixConfig cfg = new MatrixConfig(
|
||||
Config.MATRIX_SERVER,
|
||||
Config.MATRIX_AS_TOKEN,
|
||||
Config.MATRIX_HS_TOKEN,
|
||||
"matrix.alttd.com",
|
||||
"mc_",
|
||||
"127.0.0.1",
|
||||
9000
|
||||
);
|
||||
|
||||
MatrixAppServiceServer asServer;
|
||||
try {
|
||||
asServer = MatrixRuntime.startAppService(cfg);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to start Matrix AppService server", e);
|
||||
return;
|
||||
}
|
||||
|
||||
MatrixClient client = MatrixRuntime.createClient(cfg);
|
||||
|
||||
initMatrix(cfg, asServer, client);
|
||||
}
|
||||
|
||||
public void initMatrix(MatrixConfig matrixConfig, MatrixAppServiceServer asServer, MatrixClient client) {
|
||||
MatrixRoomMap roomMap = new MatrixRoomMap();
|
||||
roomMap.put("bayou", "!bayou:matrix.alttd.com");
|
||||
|
||||
MatrixBridge matrix = new DefaultMatrixBridge(matrixConfig, asServer, client);
|
||||
matrix.setRoomResolver(roomMap);
|
||||
matrix.setInboundHandler(new VelocityMatrixInbound(server, roomMap));
|
||||
|
||||
eventManager.register(this, new VelocityMatrixOutbound(matrix));
|
||||
}
|
||||
|
||||
public File getDataDirectory() {
|
||||
return dataDirectory.toFile();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.alttd.velocitychat.matrix;
|
||||
|
||||
import com.alttd.matrix.interfaces.MatrixBridge;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public final class MatrixRoomMap implements MatrixBridge.RoomResolver {
|
||||
private final Map<String, String> serverToRoom = new ConcurrentHashMap<>();
|
||||
private final Map<String, String> roomToServer = new ConcurrentHashMap<>();
|
||||
|
||||
public void put(String serverName, String roomId) {
|
||||
serverToRoom.put(serverName, roomId);
|
||||
roomToServer.put(roomId, serverName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String roomIdForServer(String serverName) {
|
||||
return serverToRoom.get(serverName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serverForRoomId(String roomId) {
|
||||
return roomToServer.get(roomId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.alttd.velocitychat.matrix;
|
||||
|
||||
import com.alttd.matrix.interfaces.MatrixBridge;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public final class VelocityMatrixInbound implements MatrixBridge.InboundHandler {
|
||||
private final ProxyServer proxy;
|
||||
private final MatrixRoomMap rooms;
|
||||
|
||||
public VelocityMatrixInbound(ProxyServer proxy, MatrixRoomMap rooms) {
|
||||
this.proxy = proxy;
|
||||
this.rooms = rooms;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMatrixChat(String roomId, String senderMxid, String body) {
|
||||
final String serverName = rooms.serverForRoomId(roomId);
|
||||
if (serverName == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// route to backend server players connected to that server
|
||||
Optional<RegisteredServer> rs = proxy.getServer(serverName);
|
||||
if (rs.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// minimal formatting; adjust to your chat format
|
||||
String sender = senderMxid != null ? senderMxid : "matrix";
|
||||
Component msg = Component.text("[M] " + sender + ": " + body);
|
||||
|
||||
rs.get().getPlayersConnected().forEach(p -> p.sendMessage(msg));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.alttd.velocitychat.matrix;
|
||||
|
||||
import com.alttd.matrix.interfaces.MatrixBridge;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.player.PlayerChatEvent;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
|
||||
public final class VelocityMatrixOutbound {
|
||||
private final MatrixBridge matrix;
|
||||
|
||||
public VelocityMatrixOutbound(MatrixBridge matrix) {
|
||||
this.matrix = matrix;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onChat(PlayerChatEvent e) {
|
||||
if (!e.getResult().isAllowed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Player p = e.getPlayer();
|
||||
String serverName = p.getCurrentServer().map(s -> s.getServerInfo().getName()).orElse(null);
|
||||
if (serverName == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String msg = PlainTextComponentSerializer.plainText().serialize(e.getMessage());
|
||||
if (msg == null || msg.isBlank()) {
|
||||
return;
|
||||
}
|
||||
|
||||
matrix.sendChat(p.getUniqueId(), p.getUsername(), serverName, msg);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user