Use a vanilla thread instead of Bukkit's wrappers
This commit is contained in:
parent
e4df0128c7
commit
c95354fbb7
|
@ -5,8 +5,9 @@ import org.bukkit.ChatColor;
|
|||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class Bot {
|
||||
public class Bot extends Thread {
|
||||
private final String USERNAME;
|
||||
private final String HOST;
|
||||
private final String CHANNEL;
|
||||
|
@ -28,7 +29,7 @@ public class Bot {
|
|||
this.PORT = PORT;
|
||||
}
|
||||
|
||||
private void init() {
|
||||
public void init() {
|
||||
try {
|
||||
s = new Socket(HOST, PORT);
|
||||
|
||||
|
@ -44,56 +45,50 @@ public class Bot {
|
|||
}
|
||||
}
|
||||
|
||||
private void loop() {
|
||||
final String[] placeholder = {""};
|
||||
public void run() {
|
||||
String line = "";
|
||||
isRunning = true;
|
||||
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(Main.getPlugin(Main.class), () -> {
|
||||
try {
|
||||
if ((placeholder[0] = br.readLine()) != null && isRunning) {
|
||||
String line = placeholder[0].replaceAll("\\x03(?:\\d{1,2}(?:,\\d{1,2})?)|[^\\u0020-\\u007E]", "");
|
||||
|
||||
Main.getPlugin(Main.class).getLogger().info(line);
|
||||
try {
|
||||
while ((line = br.readLine()) != null && isRunning) {
|
||||
line = line.replaceAll("\\x03(?:\\d{1,2}(?:,\\d{1,2})?)|[^\\u0020-\\u007E]", "");
|
||||
|
||||
if (line.contains("PRIVMSG")) {
|
||||
String user = line.substring(1, line.indexOf("!"));
|
||||
String message = line.replaceAll("^[\\w\\W]+ :", "");
|
||||
Main.getPlugin(Main.class).getLogger().info(line);
|
||||
|
||||
Bukkit.broadcastMessage(ChatColor.BLUE + "[IRC] <" + user + "> " + ChatColor.WHITE + message);
|
||||
} else if (line.contains("PING")) {
|
||||
chat.sendToServer(bw, line.replaceFirst("I", "O"));
|
||||
} else if (line.contains("JOIN")) {
|
||||
String user = line.substring(1, line.indexOf("!"));
|
||||
if (line.contains("PRIVMSG")) {
|
||||
String user = line.substring(1, line.indexOf("!"));
|
||||
String message = line.replaceAll("^[\\w\\W]+ :", "");
|
||||
|
||||
Bukkit.broadcastMessage(ChatColor.YELLOW + "" + ChatColor.ITALIC + "[IRC] " + user + " has joined the channel");
|
||||
} else if (line.contains("QUIT") || line.contains("PART")) {
|
||||
String user = line.substring(1, line.indexOf("!"));
|
||||
Bukkit.broadcastMessage(ChatColor.BLUE + "[IRC] <" + user + "> " + ChatColor.WHITE + message);
|
||||
} else if (line.contains("PING")) {
|
||||
chat.sendToServer(bw, line.replaceFirst("I", "O"));
|
||||
} else if (line.contains("JOIN")) {
|
||||
String user = line.substring(1, line.indexOf("!"));
|
||||
|
||||
Bukkit.broadcastMessage(ChatColor.RED + "" + ChatColor.ITALIC + "[IRC] " + user + " has left the channel");
|
||||
}
|
||||
Bukkit.broadcastMessage(ChatColor.YELLOW + "" + ChatColor.ITALIC + "[IRC] " + user + " has joined the channel");
|
||||
} else if (line.contains("QUIT") || line.contains("PART")) {
|
||||
String user = line.substring(1, line.indexOf("!"));
|
||||
|
||||
// This will join the channel once and then not again
|
||||
// Won't run until the bot has responded to the first ping (aka registered)
|
||||
// if (hasRespondedToFirstPing && !hasJoined) {
|
||||
if (line.contains("376") && !hasJoined) {
|
||||
chat.sendToServer(bw, "MODE " + USERNAME + " +B");
|
||||
chat.sendToServer(bw, "JOIN #" + CHANNEL);
|
||||
|
||||
hasJoined = true;
|
||||
}
|
||||
Bukkit.broadcastMessage(ChatColor.RED + "" + ChatColor.ITALIC + "[IRC] " + user + " has left the channel");
|
||||
}
|
||||
|
||||
// This will join the channel once and then not again
|
||||
// Won't run until the bot has responded to the first ping (aka registered)
|
||||
// if (hasRespondedToFirstPing && !hasJoined) {
|
||||
if (line.contains("376") && !hasJoined) {
|
||||
chat.sendToServer(bw, "MODE " + USERNAME + " +B");
|
||||
chat.sendToServer(bw, "JOIN #" + CHANNEL);
|
||||
|
||||
hasJoined = true;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, 0, 0);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void start() {
|
||||
init();
|
||||
loop();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
public void terminate() {
|
||||
try {
|
||||
chat.sendToChat(bw, "Server shutting down!");
|
||||
chat.sendToServer(bw, "PART #" + CHANNEL);
|
||||
|
@ -102,6 +97,8 @@ public class Bot {
|
|||
s.close();
|
||||
bw.close();
|
||||
br.close();
|
||||
this.interrupt();
|
||||
Main.getPlugin(Main.class).getLogger().info("Thread successfully stopped.");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ public class Main extends JavaPlugin {
|
|||
@Override
|
||||
public void onEnable() {
|
||||
super.onEnable();
|
||||
System.out.println("Hello there!");
|
||||
getLogger().info("Hello there!");
|
||||
|
||||
saveDefaultConfig();
|
||||
|
||||
|
@ -19,6 +19,7 @@ public class Main extends JavaPlugin {
|
|||
|
||||
bot = new Bot(username, host, channel, port);
|
||||
|
||||
bot.init();
|
||||
bot.start();
|
||||
|
||||
getServer().getPluginManager().registerEvents(new MinecraftListener(), this);
|
||||
|
@ -27,8 +28,6 @@ public class Main extends JavaPlugin {
|
|||
@Override
|
||||
public void onDisable() {
|
||||
super.onDisable();
|
||||
System.out.println("General Kenobi");
|
||||
|
||||
bot.stop();
|
||||
bot.terminate();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue