Attempt 1: MC -> chat bridge
This commit is contained in:
parent
df357d7817
commit
0cc9707980
|
@ -1,7 +1,8 @@
|
||||||
package arcanitor.civilengineering;
|
package arcanitor.civilengineering;
|
||||||
|
|
||||||
|
import arcanitor.civilengineering.bridge.OutgoingMessageHandler;
|
||||||
import arcanitor.civilengineering.eventhandlers.FMLEventHandler;
|
import arcanitor.civilengineering.eventhandlers.FMLEventHandler;
|
||||||
import arcanitor.civilengineering.bridge.MessageHandler;
|
import arcanitor.civilengineering.bridge.IncomingMessageHandler;
|
||||||
import arcanitor.civilengineering.server.ServerChatWriter;
|
import arcanitor.civilengineering.server.ServerChatWriter;
|
||||||
import net.minecraftforge.common.MinecraftForge;
|
import net.minecraftforge.common.MinecraftForge;
|
||||||
import net.minecraftforge.fml.common.Mod;
|
import net.minecraftforge.fml.common.Mod;
|
||||||
|
@ -25,7 +26,8 @@ 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 MessageHandler());
|
public static Thread incomingMessageThread = new Thread(new IncomingMessageHandler());
|
||||||
|
public static Thread outgoingMessageThread = new Thread(new OutgoingMessageHandler());
|
||||||
|
|
||||||
@Mod.EventHandler
|
@Mod.EventHandler
|
||||||
public void preInit(FMLPreInitializationEvent event) {
|
public void preInit(FMLPreInitializationEvent event) {
|
||||||
|
|
|
@ -19,6 +19,11 @@ public class ApiMessage {
|
||||||
this.gateway = Config.gateway;
|
this.gateway = Config.gateway;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public ApiMessage(String user, String msg, String event) {
|
||||||
|
this.username = user;
|
||||||
|
this.text = msg;
|
||||||
|
this.event = event;
|
||||||
|
}
|
||||||
|
|
||||||
public static ApiMessage decode(String json) {
|
public static ApiMessage decode(String json) {
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
|
@ -44,4 +49,6 @@ public class ApiMessage {
|
||||||
return this.event;
|
return this.event;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,11 +11,11 @@ import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
|
|
||||||
public class MessageHandler implements Runnable {
|
public class IncomingMessageHandler implements Runnable {
|
||||||
private HttpURLConnection connection = null;
|
private HttpURLConnection connection = null;
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
CivilEngineering.logger.info("Network Thread starting.");
|
CivilEngineering.logger.info("Receiving Network Thread starting.");
|
||||||
try {
|
try {
|
||||||
connect(Config.connectURL,Config.authToken);
|
connect(Config.connectURL,Config.authToken);
|
||||||
BufferedReader input =
|
BufferedReader input =
|
||||||
|
@ -31,9 +31,10 @@ public class MessageHandler implements Runnable {
|
||||||
|
|
||||||
if (e instanceof InterruptedException) {
|
if (e instanceof InterruptedException) {
|
||||||
connection.disconnect(); //close the connection
|
connection.disconnect(); //close the connection
|
||||||
CivilEngineering.logger.info("Bridge demolished!");
|
CivilEngineering.logger.info("Receiving connection closed.");
|
||||||
} else if (e instanceof IOException) {
|
} else if (e instanceof IOException) {
|
||||||
CivilEngineering.logger.error("Bridge construction failed!");
|
CivilEngineering.outgoingMessageThread.interrupt();
|
||||||
|
CivilEngineering.logger.error("Error connecting to bridge server!");
|
||||||
CivilEngineering.logger.error(e.getMessage());
|
CivilEngineering.logger.error(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
package arcanitor.civilengineering.bridge;
|
||||||
|
|
||||||
|
import arcanitor.civilengineering.CivilEngineering;
|
||||||
|
import arcanitor.civilengineering.Config;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
|
import static java.lang.Thread.sleep;
|
||||||
|
|
||||||
|
public class OutgoingMessageHandler implements Runnable {
|
||||||
|
public static ConcurrentLinkedQueue<ApiMessage> queue = new ConcurrentLinkedQueue();
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
CivilEngineering.logger.info("Sending network thread starting");
|
||||||
|
try {
|
||||||
|
while(true) {
|
||||||
|
ApiMessage nextMessage = queue.poll();
|
||||||
|
if (nextMessage!=null) {
|
||||||
|
int response = postMessage(nextMessage);
|
||||||
|
if (response != 200) {
|
||||||
|
CivilEngineering.logger.error("Server returned error "+response);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sleep(50);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (e instanceof InterruptedException) {
|
||||||
|
CivilEngineering.logger.info("Sending connection closed.");
|
||||||
|
} else if (e instanceof IOException) {
|
||||||
|
CivilEngineering.incomingMessageThread.interrupt();
|
||||||
|
CivilEngineering.logger.error("Error connecting to bridge server!");
|
||||||
|
CivilEngineering.logger.error(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int postMessage(ApiMessage message) throws IOException {
|
||||||
|
|
||||||
|
//open a connection
|
||||||
|
URL url = new URL(Config.connectURL + "api/messages");
|
||||||
|
URLConnection urlConnection = url.openConnection();
|
||||||
|
HttpURLConnection connection = (HttpURLConnection)urlConnection;
|
||||||
|
|
||||||
|
//configure the connection
|
||||||
|
connection.setAllowUserInteraction(false);
|
||||||
|
connection.setInstanceFollowRedirects(true);
|
||||||
|
connection.setRequestProperty("Content-Type","application/json");
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
if (Config.authToken != null) {
|
||||||
|
connection.setRequestProperty ("Authorization", "Bearer " + Config.authToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
//encode the ApiMessage for sending
|
||||||
|
String json = message.encode();
|
||||||
|
|
||||||
|
//send the message
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
DataOutputStream post = new DataOutputStream(connection.getOutputStream());
|
||||||
|
post.writeBytes(json);
|
||||||
|
post.flush();
|
||||||
|
post.close();
|
||||||
|
|
||||||
|
int response = connection.getResponseCode();
|
||||||
|
CivilEngineering.logger.info(response);
|
||||||
|
|
||||||
|
connection.disconnect();
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,8 @@
|
||||||
package arcanitor.civilengineering.eventhandlers;
|
package arcanitor.civilengineering.eventhandlers;
|
||||||
|
|
||||||
import arcanitor.civilengineering.CivilEngineering;
|
import arcanitor.civilengineering.CivilEngineering;
|
||||||
|
import arcanitor.civilengineering.bridge.ApiMessage;
|
||||||
|
import arcanitor.civilengineering.bridge.OutgoingMessageHandler;
|
||||||
import net.minecraftforge.event.ServerChatEvent;
|
import net.minecraftforge.event.ServerChatEvent;
|
||||||
import net.minecraftforge.fml.common.Mod;
|
import net.minecraftforge.fml.common.Mod;
|
||||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||||
|
@ -9,6 +11,8 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||||
public class ChatMessageHandler {
|
public class ChatMessageHandler {
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void handleServerChatEvent (ServerChatEvent event) {
|
public static void handleServerChatEvent (ServerChatEvent event) {
|
||||||
CivilEngineering.logger.info("Message on server: "+event.getMessage()+" sent by "+event.getUsername());
|
String message = event.getMessage().trim();
|
||||||
|
if (!message.isEmpty())
|
||||||
|
OutgoingMessageHandler.queue.add(new ApiMessage(event.getUsername(),message));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ package arcanitor.civilengineering.eventhandlers;
|
||||||
|
|
||||||
import arcanitor.civilengineering.CivilEngineering;
|
import arcanitor.civilengineering.CivilEngineering;
|
||||||
import arcanitor.civilengineering.Config;
|
import arcanitor.civilengineering.Config;
|
||||||
|
import arcanitor.civilengineering.bridge.ApiMessage;
|
||||||
|
import arcanitor.civilengineering.bridge.OutgoingMessageHandler;
|
||||||
import net.minecraft.entity.EntityLivingBase;
|
import net.minecraft.entity.EntityLivingBase;
|
||||||
import net.minecraft.entity.player.EntityPlayer;
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
import net.minecraftforge.event.entity.living.LivingDeathEvent;
|
import net.minecraftforge.event.entity.living.LivingDeathEvent;
|
||||||
|
@ -16,11 +18,8 @@ public class DeathEventHandler {
|
||||||
EntityLivingBase entity = event.getEntityLiving();
|
EntityLivingBase entity = event.getEntityLiving();
|
||||||
if (entity instanceof EntityPlayer) {
|
if (entity instanceof EntityPlayer) {
|
||||||
String message = entity.getCombatTracker().getDeathMessage().getUnformattedText();
|
String message = entity.getCombatTracker().getDeathMessage().getUnformattedText();
|
||||||
CivilEngineering.logger.info(message);
|
OutgoingMessageHandler.queue.add(new ApiMessage("Server",message));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,9 +26,11 @@ public class FMLEventHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static void serverStarting(FMLServerStartingEvent event) {
|
public static void serverStarting(FMLServerStartingEvent event) {
|
||||||
CivilEngineering.networkThread.start();
|
CivilEngineering.incomingMessageThread.start();
|
||||||
|
CivilEngineering.outgoingMessageThread.start();
|
||||||
}
|
}
|
||||||
public static void serverStopping(FMLServerStoppingEvent event) {
|
public static void serverStopping(FMLServerStoppingEvent event) {
|
||||||
CivilEngineering.networkThread.interrupt();
|
CivilEngineering.incomingMessageThread.interrupt();
|
||||||
|
CivilEngineering.outgoingMessageThread.interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
package arcanitor.civilengineering.eventhandlers;
|
||||||
|
|
||||||
|
import arcanitor.civilengineering.CivilEngineering;
|
||||||
|
import arcanitor.civilengineering.bridge.ApiMessage;
|
||||||
|
import arcanitor.civilengineering.bridge.OutgoingMessageHandler;
|
||||||
|
import net.minecraft.command.server.CommandEmote;
|
||||||
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
|
import net.minecraft.entity.player.EntityPlayerMP;
|
||||||
|
import net.minecraftforge.event.CommandEvent;
|
||||||
|
import net.minecraftforge.fml.common.Mod;
|
||||||
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||||
|
|
||||||
|
@Mod.EventBusSubscriber
|
||||||
|
public class UserActionHandler {
|
||||||
|
@SubscribeEvent
|
||||||
|
public static void handleCommandEvent(CommandEvent event) {
|
||||||
|
if(event.getCommand() instanceof CommandEmote && event.getSender() instanceof EntityPlayer) {
|
||||||
|
String[] args = event.getParameters();
|
||||||
|
|
||||||
|
String user = event.getSender().getName();
|
||||||
|
String message = "";
|
||||||
|
|
||||||
|
for(String word:args) {
|
||||||
|
message = message + " " + word;
|
||||||
|
}
|
||||||
|
|
||||||
|
OutgoingMessageHandler.queue.add(new ApiMessage(user,message,"user_action"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,14 +17,13 @@ public class ServerChatWriter {
|
||||||
ApiMessage nextMessage = messages.poll();
|
ApiMessage nextMessage = messages.poll();
|
||||||
|
|
||||||
if (nextMessage != null) {
|
if (nextMessage != null) {
|
||||||
CivilEngineering.logger.info("Processing message");
|
|
||||||
String user = nextMessage.getUsername();
|
String user = nextMessage.getUsername();
|
||||||
String text = nextMessage.getMessage().trim();
|
String text = nextMessage.getMessage().trim();
|
||||||
|
|
||||||
String message;
|
String message;
|
||||||
|
|
||||||
if (!text.isEmpty()) {
|
if (!text.isEmpty()) {
|
||||||
if (nextMessage.getEvent() == "user_action") {
|
if (nextMessage.getEvent().equals("user_action")) {
|
||||||
message = "* " + user + " " + text;
|
message = "* " + user + " " + text;
|
||||||
} else {
|
} else {
|
||||||
message = "<" + user + "> " + text;
|
message = "<" + user + "> " + text;
|
||||||
|
|
Loading…
Reference in New Issue