Thread in multiple plugins

Discussion in 'Plugin Development' started by ThrustLP, Aug 3, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    ThrustLP

    Hey it is me again! So I have a very basic concept: Through a socket I send a playername and all servers I send it to should check if the player is online and then do something. To listen to the socket message I start a thread. Now thats my problem. I can only start the thread in one plugin. In the others I obviously get an error. How can I manage that my sendDataToSocket method (see below) sends data, that all servers running that plugin can read using the thread below?
    Code:
        public static void sendDataToSocket(String data){
            Socket client;
            try {
                client = new Socket("localhost",1337);
                DataOutputStream ds = new DataOutputStream(client.getOutputStream());
                ds.writeUTF(data);
                ds.close();
                client.close();
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
              
                e1.printStackTrace();
            }
        }
    Code:
    package me.thruzzd.data;
    
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import me.thruzzd.ontime.Main;
    import me.thruzzd.utils.utils_mysql;
    
    public class ReadThread extends Thread{
    
       
         public void run(){
             while (!Thread.currentThread().isInterrupted()){
                 ServerSocket sock = Main.getInstance().sock;
                
                 Socket client = null;
                 try {
                    
               
                client = sock.accept();
               
                     DataInputStream dis = new DataInputStream(client.getInputStream());
                         String data = dis.readUTF();
                        //TEST
                         System.out.println(data);
    
                         if(Main.getInstance().mysqlontime.containsKey(data)){
                             String[] temp = Main.getInstance().mysqlontime.get(data).split("/");
                             utils_mysql.savePlayers(data, temp[0], temp[1], temp[2]);
                         }
                        
                        
                     dis.close();
                 } catch (IOException e2) {
                     System.out.println("[MafiOntime] Socket closed");
                 }
             }
            
            
         }
        
        
    }
    

    Thank you guys!
     
  2. Offline

    Zombie_Striker

    I don't see why this should cause an error. As long as they are on separate computers, this should not cause any errors. If they are on one computer, then there is no need to use sockets. In that case, just save all the players online into one text document.
     
  3. Offline

    I Al Istannen

    @ThrustLP
    If you know the servers beforehand, add the IPs. If you don't, your clients would have to connect to a webserver of yours and listen for messages send.
    Then you spawn a new Thread for every client that connects to it and send the data you want to the client.

    May I ask what this is for, there may be a nicer way to do this.
     
  4. Offline

    Firestar311

    Could you use a MySQL database that checks every second or so? It is a much nicer way and you can do it in a thread.

    Could you post your error here so that we may see what it is?
     
  5. Offline

    ThrustLP

    @I Al Istannen @Firestar311 @Zombie_Striker Soo I have I plugin installed on all my bungee Servers. That plugin has a method in it, lets say for example:
    Code:
    public void test(String s){
    
    System.out.println(s);
    }
    So if I execute the command /test [String] on one server, that method should be triggerd on every Server running my plugin. How would I do that?
     
  6. Offline

    timtower Administrator Administrator Moderator

    Locked.
    Bungeecord requires offline mode.
    Offline mode is not supported by Bukkit
     
Thread Status:
Not open for further replies.

Share This Page