Skip to content
Snippets Groups Projects
Commit fadef223 authored by Miniontoby's avatar Miniontoby :writing_hand_tone1:
Browse files

Working version!

parent 09bea07f
No related branches found
No related tags found
No related merge requests found
Showing
with 247 additions and 11 deletions
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
File added
No preview for this file type
No preview for this file type
No preview for this file type
#!/bin/ksh
nano src/com/miniontoby/MindustrIRC/MindustrIRC.java
if [[ "x$1" == "x" ]]; then
FILE="MindustrIRC"
else
FILE="$1"
fi
nano src/com/miniontoby/MindustrIRC/$FILE.java
package com.miniontoby.MindustrIRC;
import java.io.*;
import java.net.*;
import java.util.*;
import arc.*;
import arc.util.*;
import arc.struct.*;
import mindustry.*;
import mindustry.content.*;
import mindustry.game.EventType.*;
import mindustry.gen.*;
import mindustry.mod.*;
public class IRCBot extends Thread {
private final String NETWORK;
private final int PORT;
private String nickname;
private String defaultChannel;
private Socket connection;
private BufferedReader input;
private BufferedWriter output;
public IRCBot(String NETWORK, int PORT, String nickname, String defaultChannel) {
this.NETWORK = NETWORK;
this.PORT = PORT;
this.nickname = nickname;
this.defaultChannel = defaultChannel;
}
private void connect() throws Exception {
connection = new Socket(NETWORK, PORT);
input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
output = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
}
private void disconnect() throws Exception {
connection.close();
}
private void sendLoginData() throws Exception {
output.write("NICK " + nickname + "\n");
output.write("USER " + nickname + " * * : " + nickname + "\n");
output.flush();
}
private void pingPong(String[] data) throws Exception {
if (data[0].equals("PING")) {
output.write("PONG " + data[1] + "\n");
output.flush();
}
}
private void joinChannel(String channel) throws Exception {
output.write("JOIN " + channel + "\n");
output.flush();
}
public void sendMessage(String to, String message) throws Exception {
output.write("PRIVMSG " + to + " :" + message + "\n");
output.flush();
}
private void verifyMOTD(String[] data) throws Exception {
if (data.length >= 2) {
// 376 is the protocol number (end of MOTD)
if (data[1].equals("376")) {
joinChannel(defaultChannel);
sendMessage(defaultChannel, "[MindustrIRC] Server Started!");
MindustrIRC.ConsoleLog("Connected to IRC!");
}
}
}
private boolean isCommand(String[] data) {
if (data.length >= 4) {
if (data[1].equals("PRIVMSG")) {
if (data[3].substring(1, 2).equals("!")) {
return true;
}
}
}
return false;
}
private void verifyCommand(String[] data) throws Exception {
if (isCommand(data)) {
String from = data[2];
String command = data[3].substring(2);
switch (command) {
case "help":
sendMessage(from, "Available commands: help, players");
break;
case "players":
Seq<String> players = Seq.with(Vars.net.getConnections()).map(con -> con.player.name).removeAll(p -> p == null);
sendMessage(from, "Connected players: " + players.toString(", "));
break;
case "avapro":
sendMessage(from, String.valueOf(Runtime.getRuntime().availableProcessors()));
break;
case "freememory":
sendMessage(from, String.valueOf(Runtime.getRuntime().freeMemory()));
break;
case "totalmemory":
sendMessage(from, String.valueOf(Runtime.getRuntime().totalMemory()));
break;
default:
sendMessage(from, "exception -> unknown function: \"" + command + "\"");
break;
}
}
}
public void run() {
try {
connect();
sendLoginData();
while (true) {
String data = null;
while ((data = input.readLine()) != null) {
String[] dataSplitted = data.split(" ");
pingPong(dataSplitted);
verifyMOTD(dataSplitted);
sendToChat(dataSplitted);
try {
verifyCommand(dataSplitted);
} catch (Exception ex) {
}
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
private void sendToChat(String[] data){
if (data.length >= 4) {
if (data[1].equals("PRIVMSG")) {
String[] split = data[0].split("!");
String user = split[0].substring(1);
String message = data[3].split(":")[1];
for (int i = 4; i < data.length; i++){
message = message + " " + data[i];
}
Call.sendMessage("[red][[[grey]" + user + "@IRC[red]][white] " + message);
} else if (data[1].equals("JOIN")) {
String[] split = data[0].split("!");
String user = split[0].substring(1);
String chn = data[1];
Call.sendMessage("[grey]-!- " + user + " joined " + chn);
} else if (data[1].equals("PART")) {
String[] split = data[0].split("!");
String user = split[0].substring(1);
String chn = data[2];
String reason = data[3];
Call.sendMessage("[grey]-!- " + user + " has left " + chn + " [" + reason + "]");
} else if (data[1].equals("QUIT")) {
String[] split = data[0].split("!");
String user = split[0].substring(1);
String reason = data[2];
Call.sendMessage("[grey]-!- " + user + " has quit [" + reason + "]");
} else if (data[1].equals("NICK")) {
String[] split = data[0].split("!");
String oldUser = split[0].substring(1);
String newUser = data[2];
Call.sendMessage("[grey]-!- " + oldUser + " is now known as " + newUser);
}
}
}
}
package com.miniontoby.MindustrIRC;
import com.miniontoby.MindustrIRC.IRCBot;
import java.net.*;
import java.io.*;
import java.util.*;
import arc.*;
import arc.util.*;
import mindustry.*;
......@@ -7,21 +11,76 @@ import mindustry.content.*;
import mindustry.game.EventType.*;
import mindustry.gen.*;
import mindustry.mod.*;
import mindustry.net.*;
public class MindustrIRC extends Plugin {
public MindustrIRC(){
// Events.on(ServerLoadEvent.class, a -> {
// Log.info("Loaded MindustrIRC constructor.");
// });
Events.on(PlayerChatEvent.class, e -> {
Call.sendChatMessage("Hey");
});
static private IRCBot bot;
static public String server = Core.settings.getString("ircServer");
static public int port = Core.settings.getInt("ircPort");
static public String nickname = Core.settings.getString("ircNickname");
static public String channel = Core.settings.getString("ircChannel");
static public void ConsoleLog(String message){
Log.info("[MindustrIRC] " + message);
}
static public void IRCMessage(String message) {
try {
bot.sendMessage(channel, message);
// bot.sendMessage("#mindustry", "[MindustrIRC] " + message);
} catch (Exception ex){
ConsoleLog("Exception: " + ex);
}
}
@Override
public void init(){
if(Vars.headless){
Log.info("MindustrIRC Mod loaded!");
ConsoleLog("Loaded!");
// if (empty(server) || empty(port) || empty(nickname) || empty(channel)) {
Core.settings.put("ircServer", "irc.ircforever.org");
Core.settings.put("ircPort", 6667);
Core.settings.put("ircNickname", "MindustrIRC");
Core.settings.put("ircChannel", "#mindustry");
String server = Core.settings.getString("ircServer");
port = Core.settings.getInt("ircPort");
nickname = Core.settings.getString("ircNickname");
channel = Core.settings.getString("ircChannel");
// }
setupListeners();
connectToIRC();
}
}
static void connectToIRC() {
Core.app.post(() -> {
bot = new IRCBot(server, port, nickname, channel);
bot.start();
});
}
static private void setupListeners(){
Events.on(GameOverEvent.class, event -> {
IRCMessage("Game over!");
});
Events.on(WinEvent.class, event -> {
IRCMessage("Win Event!");
});
Events.on(LoseEvent.class, event -> {
IRCMessage("Lose Event!");
});
Events.on(PlayerChatEvent.class, event -> {
String playername = event.player.name;
String message = event.message;
IRCMessage("<" + playername + "> " + message);
});
Events.on(PlayerJoin.class, event -> {
String playername = event.player.name;
IRCMessage("*** " + playername + " joined the game");
});
Events.on(PlayerLeave.class, event -> {
String playername = event.player.name;
IRCMessage("*** " + playername + " left the game");
});
}
}
#!/bin/ksh
./gradlew jar
cp ./build/libs/mindustrircDesktop.jar ~/mindustry/config/mods
./gradlew jar || exit 1
cp ./build/libs/mindustrircDesktop.jar ~/mindustry/config/mods || exit 1
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment