Java 使用插件将消息从Bukkit服务器传输到IRC

Java 使用插件将消息从Bukkit服务器传输到IRC,java,plugins,irc,bukkit,Java,Plugins,Irc,Bukkit,不久前,我的朋友让我写一个插件,最近(大约五分钟前),他让我添加一个功能,人们可以登录到esper.net上的特定IRC并在上面聊天。一切都设置好了-我有一个命令,让他们登录,它会让他们加入他们的昵称,他们的用户名在MineCraft。服务器使用聊天机器人来登录用户,这样他们就可以聊天了。我已经设置好了一切,这样当在命令中键入(/irc login)时,他们会向聊天机器人发送正确的消息(特别是,这会让人觉得他们在irc chat中写了/msg identify)。嗯,我让插件设置了一个字符串,将

不久前,我的朋友让我写一个插件,最近(大约五分钟前),他让我添加一个功能,人们可以登录到esper.net上的特定IRC并在上面聊天。一切都设置好了-我有一个命令,让他们登录,它会让他们加入他们的昵称,他们的用户名在MineCraft。服务器使用聊天机器人来登录用户,这样他们就可以聊天了。我已经设置好了一切,这样当在命令中键入(/irc login)时,他们会向聊天机器人发送正确的消息(特别是,这会让人觉得他们在irc chat中写了/msg identify)。嗯,我让插件设置了一个字符串,将所有内容格式化,但我需要知道如何实际将消息发送到IRC。下面是我的代码(当用户输入命令“/irc”时会触发整个事件,“player”是在输入任何命令时设置的player对象)。我还想一些代码来接收什么机器人发送回来,所以我知道如果玩家成功登录或没有

if(args[0].equalsIgnoreCase("login") && args.length == 3) {
    String msg = "/msg <bot name> login " + args[1] + " " + args[2];
    //args[1] is the username and args[2] is the password
    String userName = args[1];
    //Creating the connection with a nickname of userName
    //Here is where I need to send that message through IRC
}
else {
    String msg;
    for(int i = 0; i <= args.length; i++) {
        msg += args[i] + " ";
    }
}
if(args[0].equalsIgnoreCase(“登录”)&&args.length==3){
字符串msg=“/msg login”+args[1]+“”+args[2];
//args[1]是用户名,args[2]是密码
字符串userName=args[1];
//创建昵称为userName的连接
//这里是我需要通过IRC发送信息的地方
}
否则{
串味精;

对于(inti=0;i我认为您应该研究Java套接字连接

但这里有一个基本的IRC连接

class IRC
{
      //Connection Details
      String server = "IRC ADDRESS";
      String nick = "NICKNAME";
      String login = "LOGIN";
      String Pass = "PASS";  
      String channel = "CHANNLE";

  //Socket Stuff
  Socket socket;
  BufferedWriter writer;
  BufferedReader reader;


      public void HandleChat() throws Exception 
      {
           socket = new Socket(server, 6667);

           writer = new BufferedWriter(
               new OutputStreamWriter(socket.getOutputStream( )));

           reader = new BufferedReader(
               new InputStreamReader(socket.getInputStream( )));


           // Log on to the server.      
           writer.write("PASS " + Pass + "\r\n");
           writer.write("NICK " + nick + "\r\n");
           writer.write("USER " + login +"\r\n");
           writer.flush( );

           // Read lines from the server until it tells us we have connected.
           String line = null;

           while (((line = reader.readLine( )) != null))
           {
           }            
      }
}

下面是一些可以连接到服务器的类。您可以使用这些类并根据需要对其进行修改。您所需要做的就是创建一个新的ChatClient实例,并为其提供正确的参数,然后它将连接到服务器

聊天室 聊天服务器线程 接受器
没有冒犯之意,这是一个很好的回答,但早一点就好了:)顺便说一句,谢谢。这似乎是对的,我会在几分钟后测试它。我只需要重新设置我的电脑。我刚度假回来。哦,我只是读台词来聊天吗?我如何关闭连接?抱歉,如果这些问题是基本的,但我对IRC或套接字不太了解…很久以后,我意识到我从来没有接受过答案。你的似乎是最好的,所以在这里,有免费的代表。两件事:一,它通常不会太好,以恢复死亡的问题,但感谢你的答案。二,我发现了一个很好的框架,称为PircBot,它一直在工作令人惊讶。我实际上应该更新我原来的问题。
    package com.weebly.foxgenesis.src;
import java.net.*;
import java.io.*;

public final class ChatClient
{  
    private Socket socket = null;
    private DataOutputStream streamOut = null;
    private ChatClientThread client = null;
    private String serverName = "localhost";
    private int serverPort = -1;
    private final ChatReciever output;

/**
 * Create a new ChatClient that connects to a given server and receives UTF-8 encoded messages
 * @param a Client class
 */
public ChatClient(ChatReciever chatReciever)
{ 
    output = chatReciever;
    serverPort = chatReciever.getPort();
    serverName = chatReciever.getHost();
    connect(serverName, serverPort);
}

private void connect(String serverName, int serverPort)
{  
    output.handleLog("Establishing connection. Please wait ...");
    try
    {  
        socket = new Socket(serverName, serverPort);
        output.handle("Connected to chat server");
        open();
    }
    catch(UnknownHostException uhe)
    {  
        output.handleError("Host unknown: " + uhe.getMessage()); 
    }
    catch(IOException ioe)
    {  
        output.handleError("Unexpected exception: " + ioe.getMessage()); 
    } 
}

/**
 * Sends a message to the server through bytes in UTF-8 encoding 
 * @param msg message to send to the server
 */
public void send(String msg) throws IOException
{  
    streamOut.writeUTF(msg); 
    streamOut.flush();
}

/**
 * forces the ChatReciever to listen to a message (NOTE: this does not send anything to the server)
 * @param msg message to send
 */
public void handle(String msg)
{  
    output.handle(msg);
}

private void open() throws IOException
{   
    streamOut = new DataOutputStream(socket.getOutputStream());
    client = new ChatClientThread(this, socket); 
}

/**
 * tries to close 
 */
public void close() throws IOException
{  
    if (streamOut != null)  
        streamOut.close();
    if (socket    != null)  
        socket.close(); 
}

/**
 * closes the client connection
 */
@SuppressWarnings("deprecation")
public void stop()
{
    if(client != null)
        client.stop();
    client = null;
}

/**
 * checks if the ChatClient is currently connected to the server
 * @return Boolean is connected
 */
public boolean isConnected()
{
    return client == null ?(false) : (true);
}
}
 package com.weebly.foxgenesis.src;
import java.net.*;
import java.io.*;

public final class ChatClientThread extends Thread
{  
    private Socket           socket   = null;
    private ChatClient       client   = null;
    private DataInputStream  streamIn = null;

    public ChatClientThread(ChatClient _client, Socket _socket)
    {  
        client   = _client;
        socket   = _socket;
        open();  
        start();
    }
    public void open()
    {  
        try
        {  
            streamIn  = new DataInputStream(socket.getInputStream());
        }
        catch(IOException ioe)
        {  
            System.out.println("Error getting input stream: " + ioe);
            client.stop();
        }
    }
    public void close()
    {  
        try
        {  
            if (streamIn != null) streamIn.close();
        }
        catch(IOException ioe)
        {  
            System.out.println("Error closing input stream: " + ioe);
        }
    }
    public void run()
    {  
        while (true)
        {  
            try
            {  
                client.handle(streamIn.readUTF());
            }
            catch(IOException ioe)
            { 
                System.out.println("Listening error: " + ioe.getMessage());
                client.stop();
            }
        }
    }
}
package com.weebly.foxgenesis.src;

public interface ChatReciever 
{
    /**
     * gets the IP address of the host
     * @return String IP address
     */
    public String getHost();

    /**
     * gets the port of the host
     * @return Integer port of host
     */
    public int getPort();

    /**
     * sends a message from the server to the implementing class
     * @param msg message from the server
     */
    public void handle(String msg);

    /**
     * sends an error to the implementing class
     * @param errorMsg error message
     */
    public void handleError(String errorMsg);

    /**
     * Sends a message to the log
     * @param msg message to send
     */
    public void handleLog(Object msg);
}