chat -> MC bridge
This commit is contained in:
parent
2c644deb5a
commit
df357d7817
|
@ -1,7 +1,9 @@
|
||||||
package arcanitor.civilengineering;
|
package arcanitor.civilengineering;
|
||||||
|
|
||||||
import arcanitor.civilengineering.eventhandlers.FMLEventHandler;
|
import arcanitor.civilengineering.eventhandlers.FMLEventHandler;
|
||||||
import arcanitor.civilengineering.network.NetworkHandler;
|
import arcanitor.civilengineering.bridge.MessageHandler;
|
||||||
|
import arcanitor.civilengineering.server.ServerChatWriter;
|
||||||
|
import net.minecraftforge.common.MinecraftForge;
|
||||||
import net.minecraftforge.fml.common.Mod;
|
import net.minecraftforge.fml.common.Mod;
|
||||||
import net.minecraftforge.fml.common.event.*;
|
import net.minecraftforge.fml.common.event.*;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
@ -23,8 +25,7 @@ public class CivilEngineering {
|
||||||
public static CivilEngineering instance;
|
public static CivilEngineering instance;
|
||||||
|
|
||||||
public static Logger logger;
|
public static Logger logger;
|
||||||
public static Thread networkThread = new Thread(new NetworkHandler());
|
public static Thread networkThread = new Thread(new MessageHandler());
|
||||||
|
|
||||||
|
|
||||||
@Mod.EventHandler
|
@Mod.EventHandler
|
||||||
public void preInit(FMLPreInitializationEvent event) {
|
public void preInit(FMLPreInitializationEvent event) {
|
||||||
|
@ -41,6 +42,7 @@ public class CivilEngineering {
|
||||||
@Mod.EventHandler
|
@Mod.EventHandler
|
||||||
public void postInit(FMLPostInitializationEvent event) {
|
public void postInit(FMLPostInitializationEvent event) {
|
||||||
FMLEventHandler.postInit(event);
|
FMLEventHandler.postInit(event);
|
||||||
|
MinecraftForge.EVENT_BUS.register(ServerChatWriter.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Mod.EventHandler
|
@Mod.EventHandler
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package arcanitor.civilengineering;
|
package arcanitor.civilengineering;
|
||||||
|
|
||||||
import arcanitor.civilengineering.eventhandlers.FMLEventHandler;
|
import arcanitor.civilengineering.eventhandlers.FMLEventHandler;
|
||||||
import arcanitor.civilengineering.network.NetworkHandler;
|
|
||||||
import net.minecraftforge.common.config.Configuration;
|
import net.minecraftforge.common.config.Configuration;
|
||||||
import org.apache.logging.log4j.Level;
|
import org.apache.logging.log4j.Level;
|
||||||
|
|
||||||
|
@ -13,6 +12,8 @@ public class Config {
|
||||||
public static boolean relayAdvancements = false; //unused for now
|
public static boolean relayAdvancements = false; //unused for now
|
||||||
|
|
||||||
public static String connectURL = "localhost";
|
public static String connectURL = "localhost";
|
||||||
|
public static String authToken = "";
|
||||||
|
public static String gateway = "";
|
||||||
|
|
||||||
public static void readConfig() {
|
public static void readConfig() {
|
||||||
Configuration config = FMLEventHandler.config;
|
Configuration config = FMLEventHandler.config;
|
||||||
|
@ -43,12 +44,26 @@ public class Config {
|
||||||
false,
|
false,
|
||||||
"This option does nothing as advancement relays are not implemented."
|
"This option does nothing as advancement relays are not implemented."
|
||||||
);
|
);
|
||||||
|
|
||||||
connectURL = cfg.getString(
|
connectURL = cfg.getString(
|
||||||
"connectURL",
|
"connectURL",
|
||||||
CATEGORY_CONNECTION,
|
CATEGORY_CONNECTION,
|
||||||
"localhost",
|
"http://example.com:1234",
|
||||||
"The URL or IP address of the bridge server"
|
"The URL or IP address of the bridge server, ex. http://example.com:1234"
|
||||||
);
|
);
|
||||||
|
authToken = cfg.getString(
|
||||||
|
"auth_token",
|
||||||
|
CATEGORY_CONNECTION,
|
||||||
|
"",
|
||||||
|
"Auth token used to connect to the bridge server"
|
||||||
|
);
|
||||||
|
gateway = cfg.getString(
|
||||||
|
"gateway",
|
||||||
|
CATEGORY_CONNECTION,
|
||||||
|
"",
|
||||||
|
"MatterBridge gateway"
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
package arcanitor.civilengineering.bridge;
|
||||||
|
|
||||||
|
import arcanitor.civilengineering.CivilEngineering;
|
||||||
|
import arcanitor.civilengineering.Config;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
|
public class ApiMessage {
|
||||||
|
private String text = "";
|
||||||
|
private String channel = "";
|
||||||
|
private String username = "";
|
||||||
|
private String userid = "";
|
||||||
|
private String avatar = "";
|
||||||
|
private String gateway = "";
|
||||||
|
private String event = "";
|
||||||
|
|
||||||
|
public ApiMessage(String user, String msg) {
|
||||||
|
this.username = user;
|
||||||
|
this.text = msg;
|
||||||
|
this.gateway = Config.gateway;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ApiMessage decode(String json) {
|
||||||
|
Gson gson = new Gson();
|
||||||
|
ApiMessage msg = gson.fromJson(json, ApiMessage.class);
|
||||||
|
return msg;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String encode() {
|
||||||
|
Gson gson = new Gson();
|
||||||
|
return gson.toJson(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return this.username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return this.text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEvent() {
|
||||||
|
return this.event;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package arcanitor.civilengineering.bridge;
|
||||||
|
|
||||||
|
import arcanitor.civilengineering.CivilEngineering;
|
||||||
|
import arcanitor.civilengineering.Config;
|
||||||
|
import arcanitor.civilengineering.server.ServerChatWriter;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
|
||||||
|
public class MessageHandler implements Runnable {
|
||||||
|
private HttpURLConnection connection = null;
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
CivilEngineering.logger.info("Network Thread starting.");
|
||||||
|
try {
|
||||||
|
connect(Config.connectURL,Config.authToken);
|
||||||
|
BufferedReader input =
|
||||||
|
new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||||
|
String line;
|
||||||
|
while ((line = input.readLine( )) != null) {
|
||||||
|
CivilEngineering.logger.debug(line);
|
||||||
|
ServerChatWriter.messages.add(ApiMessage.decode(line));
|
||||||
|
Thread.sleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
if (e instanceof InterruptedException) {
|
||||||
|
connection.disconnect(); //close the connection
|
||||||
|
CivilEngineering.logger.info("Bridge demolished!");
|
||||||
|
} else if (e instanceof IOException) {
|
||||||
|
CivilEngineering.logger.error("Bridge construction failed!");
|
||||||
|
CivilEngineering.logger.error(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void connect(String serverURL, String token) throws IOException {
|
||||||
|
URL url = new URL(serverURL+"/api/stream"); //parse the server URL
|
||||||
|
URLConnection urlConnection = url.openConnection();
|
||||||
|
|
||||||
|
//set connection properties
|
||||||
|
connection = (HttpURLConnection)urlConnection;
|
||||||
|
connection.setAllowUserInteraction(false);
|
||||||
|
connection.setInstanceFollowRedirects(true);
|
||||||
|
|
||||||
|
if (token != null) {
|
||||||
|
connection.setRequestProperty ("Authorization", "Bearer " + token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,7 +3,6 @@ package arcanitor.civilengineering.eventhandlers;
|
||||||
|
|
||||||
import arcanitor.civilengineering.CivilEngineering;
|
import arcanitor.civilengineering.CivilEngineering;
|
||||||
import arcanitor.civilengineering.Config;
|
import arcanitor.civilengineering.Config;
|
||||||
import arcanitor.civilengineering.network.NetworkHandler;
|
|
||||||
import net.minecraftforge.common.config.Configuration;
|
import net.minecraftforge.common.config.Configuration;
|
||||||
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
|
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
|
||||||
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
package arcanitor.civilengineering.network;
|
|
||||||
|
|
||||||
import arcanitor.civilengineering.CivilEngineering;
|
|
||||||
|
|
||||||
public class NetworkHandler implements Runnable {
|
|
||||||
public void run() {
|
|
||||||
CivilEngineering.logger.info("Network Thread");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
package arcanitor.civilengineering.server;
|
||||||
|
|
||||||
|
import arcanitor.civilengineering.CivilEngineering;
|
||||||
|
import arcanitor.civilengineering.bridge.ApiMessage;
|
||||||
|
import net.minecraft.util.text.TextComponentString;
|
||||||
|
import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||||
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||||
|
import net.minecraftforge.fml.common.gameevent.TickEvent;
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
|
public class ServerChatWriter {
|
||||||
|
public static ConcurrentLinkedQueue<ApiMessage> messages = new ConcurrentLinkedQueue();
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
public static void onServerUpdate(TickEvent.ServerTickEvent event) {
|
||||||
|
ApiMessage nextMessage = messages.poll();
|
||||||
|
|
||||||
|
if (nextMessage != null) {
|
||||||
|
CivilEngineering.logger.info("Processing message");
|
||||||
|
String user = nextMessage.getUsername();
|
||||||
|
String text = nextMessage.getMessage().trim();
|
||||||
|
|
||||||
|
String message;
|
||||||
|
|
||||||
|
if (!text.isEmpty()) {
|
||||||
|
if (nextMessage.getEvent() == "user_action") {
|
||||||
|
message = "* " + user + " " + text;
|
||||||
|
} else {
|
||||||
|
message = "<" + user + "> " + text;
|
||||||
|
}
|
||||||
|
FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().sendMessage(new TextComponentString(message));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue