Add commands to start and stop the bridge.

This commit is contained in:
DaMachinator 2018-01-20 15:10:05 -05:00 committed by Arcanitor
parent f534597c1f
commit a22e084f96
5 changed files with 67 additions and 4 deletions

View File

@ -22,9 +22,10 @@ public class IncomingMessageHandler implements Runnable {
new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = input.readLine( )) != null) {
if(CivilEngineering.incomingMessageThread.isInterrupted()) throw new InterruptedException();
CivilEngineering.logger.debug(line);
ServerChatWriter.messages.add(ApiMessage.decode(line));
Thread.sleep(1);
}
} catch (Exception e) {

View File

@ -26,8 +26,8 @@ public class OutgoingMessageHandler implements Runnable {
CivilEngineering.logger.error("Server returned error "+response);
break;
}
sleep(50);
}
sleep(50);
}
} catch (Exception e) {
if (e instanceof InterruptedException) {
@ -44,7 +44,7 @@ public class OutgoingMessageHandler implements Runnable {
public int postMessage(ApiMessage message) throws IOException {
//open a connection
URL url = new URL(Config.connectURL + "/api/messages");
URL url = new URL(Config.connectURL + "/api/message");
URLConnection urlConnection = url.openConnection();
HttpURLConnection connection = (HttpURLConnection)urlConnection;
@ -68,7 +68,6 @@ public class OutgoingMessageHandler implements Runnable {
post.close();
int response = connection.getResponseCode();
CivilEngineering.logger.info(response);
connection.disconnect();

View File

@ -0,0 +1,60 @@
package arcanitor.civilengineering.command;
import arcanitor.civilengineering.CivilEngineering;
import com.google.common.collect.Lists;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.server.MinecraftServer;
import javax.annotation.Nonnull;
import java.util.List;
public class BridgeCommand extends CommandBase {
private final List<String> aliases;
public BridgeCommand(){
aliases = Lists.newArrayList(CivilEngineering.MODID,"bridge","BRIDGE");
}
@Override
@Nonnull
public String getName() {
return "bridge";
}
@Override
public String getUsage(ICommandSender sender) {
return "bridge <connect|disconnect>";
}
@Override
@Nonnull
public List<String> getAliases() {
return aliases;
}
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) /*throws CommandException*/ {
if (args.length < 1) {
//throw new WrongUsageException("")
return;
}
String cmd = args[0];
if (cmd.toLowerCase().equals("connect")) {
if(!CivilEngineering.incomingMessageThread.isAlive()) {
CivilEngineering.incomingMessageThread.start();
}
if(!CivilEngineering.outgoingMessageThread.isAlive()){
CivilEngineering.outgoingMessageThread.start();
}
} else if (cmd.toLowerCase().equals("disconnect")) {
CivilEngineering.incomingMessageThread.interrupt();
CivilEngineering.outgoingMessageThread.interrupt();
}
}
}

View File

@ -3,6 +3,7 @@ package arcanitor.civilengineering.eventhandlers;
import arcanitor.civilengineering.CivilEngineering;
import arcanitor.civilengineering.Config;
import arcanitor.civilengineering.command.BridgeCommand;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
@ -26,6 +27,7 @@ public class FMLEventHandler {
}
}
public static void serverStarting(FMLServerStartingEvent event) {
event.registerServerCommand(new BridgeCommand());
CivilEngineering.incomingMessageThread.start();
CivilEngineering.outgoingMessageThread.start();
}

View File

@ -23,6 +23,7 @@ public class UserActionHandler {
for(String word:args) {
message = message + " " + word;
}
message = message.trim();
OutgoingMessageHandler.queue.add(new ApiMessage(user,message,"user_action"));
}