1.7.3 : One instance only and launching another one spawn a mario

This commit is contained in:
Kleme
2017-09-14 18:44:13 +02:00
parent 4ee557a0b8
commit 9bf97f1847
4 changed files with 84 additions and 1 deletions
+14
View File
@@ -15,6 +15,8 @@ import java.util.Random;
import javax.swing.SwingUtilities;
import fr.klemek.minimario.LocalServer.ConnectionListener;
public abstract class Launch {
private static final String VERSION = "1.7.2";
@@ -32,6 +34,18 @@ public abstract class Launch {
@Override
public void run() {
try {
LocalServer.startServer(new ConnectionListener(){
@Override
public void onConnection() {
try {
new MarioWindow(null, currentFactor, null);
refreshTray();
} catch (IOException e) {
e.printStackTrace();
}
}
});
new MarioWindow(null, currentFactor, null);
addTrayIcon(MarioWindow.getAll().get(0));
} catch (IOException e) {
+68
View File
@@ -0,0 +1,68 @@
package fr.klemek.minimario;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class LocalServer extends Thread {
public static final int[] PORTS = {34881, 41834, 16118, 24326}; //4 random, one should be available
public static final String CONTACT = "MiniMario !";
private ServerSocket serverSocket = null;
private Socket clientSocket = null;
private ConnectionListener listener;
public LocalServer(ConnectionListener listener){
this.listener = listener;
}
@Override
public void run() {
int i = 0;
while(i<PORTS.length){
try {
System.out.println("Starting MiniMario local server on port "+PORTS[i]+" ...");
serverSocket = new ServerSocket(PORTS[i], 1);
System.out.println("Started MiniMario local server on port "+PORTS[i]);
while (true) {
clientSocket = serverSocket.accept();
PrintWriter pw = new PrintWriter(clientSocket.getOutputStream());
pw.println(CONTACT);
pw.flush();
if(listener!=null)
listener.onConnection();
clientSocket.close();
}
} catch (IOException ioe) {
System.out.println("Error in LocalServer: " + ioe);
}
}
System.out.println("Couldn't start MiniMario local server on given ports");
}
@SuppressWarnings("resource")
public static void startServer(ConnectionListener listener){
for(int port:PORTS){
try {
Socket socket = new Socket("localhost", port);
BufferedReader br = new BufferedReader (new InputStreamReader (socket.getInputStream()));
String serverResponse = br.readLine();
if(serverResponse.equals(CONTACT)){
System.out.println("Already running MiniMario local server on port : "+port);
System.exit(0);
}
} catch (Exception e) {}
}
new LocalServer(listener).start();
}
protected interface ConnectionListener{
public void onConnection();
}
}
+2
View File
@@ -112,6 +112,8 @@ public class MarioWindow extends JWindow implements ActionListener {
this.ai.setPos(this.getLocation());
this.refresh.start();
System.out.println("Spawned "+tilesetName+" at ("+this.getX()+","+this.getY()+")");
}
//functions
-1
View File
@@ -28,7 +28,6 @@ public class TilePanel extends JPanel {
this.imageHeight = image.getHeight();
this.columns = this.imageWidth / this.tileWidth;
this.rows = this.imageHeight / this.tileHeight;
System.out.println("Tileset : "+this.columns+"x"+this.rows);
this.setFactor(factor);
this.setBackground(new Color(0, 0, 0, 0));
this.setOpaque(false);