Basic server hosting done
This commit is contained in:
@@ -41,6 +41,7 @@ public class Mindustry extends ModuleCore {
|
||||
module(Vars.control = new Control());
|
||||
module(Vars.renderer = new Renderer());
|
||||
module(Vars.ui = new UI());
|
||||
module(Vars.network = new Network());
|
||||
}
|
||||
|
||||
public void loadBundle(){
|
||||
|
||||
@@ -72,11 +72,15 @@ public class Vars{
|
||||
public static final float multiplier = android ? 3 : 2;
|
||||
|
||||
public static final int tilesize = 8;
|
||||
|
||||
//server port
|
||||
public static final int port = 6567;
|
||||
|
||||
public static Control control;
|
||||
public static Renderer renderer;
|
||||
public static UI ui;
|
||||
public static World world;
|
||||
public static Network network;
|
||||
|
||||
public static Player player;
|
||||
|
||||
|
||||
@@ -1,7 +1,36 @@
|
||||
package io.anuke.mindustry.core;
|
||||
|
||||
import io.anuke.mindustry.Vars;
|
||||
import io.anuke.mindustry.core.GameState.State;
|
||||
import io.anuke.mindustry.net.Net;
|
||||
import io.anuke.ucore.modules.Module;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Network extends Module{
|
||||
//TODO implementation
|
||||
private boolean isHosting;
|
||||
|
||||
public Network(){
|
||||
|
||||
}
|
||||
|
||||
public void update(){
|
||||
if(isHosting && GameState.is(State.menu)){
|
||||
Net.closeServer();
|
||||
isHosting = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void hostServer(int port) throws IOException{
|
||||
if(isHosting){
|
||||
throw new IOException("Already hosting a server!");
|
||||
}
|
||||
|
||||
Net.host(port);
|
||||
isHosting = true;
|
||||
}
|
||||
|
||||
public boolean isHosting(){
|
||||
return isHosting;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ import io.anuke.ucore.scene.builders.label;
|
||||
import io.anuke.ucore.scene.builders.table;
|
||||
import io.anuke.ucore.scene.event.Touchable;
|
||||
import io.anuke.ucore.scene.ui.*;
|
||||
import io.anuke.ucore.scene.ui.TextField.TextFieldFilter;
|
||||
import io.anuke.ucore.scene.ui.Window.WindowStyle;
|
||||
import io.anuke.ucore.scene.ui.layout.Table;
|
||||
import io.anuke.ucore.scene.ui.layout.Unit;
|
||||
@@ -371,12 +372,12 @@ public class UI extends SceneModule{
|
||||
configtable.setVisible(false);
|
||||
}
|
||||
|
||||
public void showTextInput(String title, String text, String def, Consumer<String> confirmed){
|
||||
public void showTextInput(String title, String text, String def, TextFieldFilter filter, Consumer<String> confirmed){
|
||||
new Dialog(title, "dialog"){{
|
||||
content().margin(30);
|
||||
content().add(text).padRight(6f);
|
||||
TextField field = content().addField(def, t->{}).size(170f, 50f).get();
|
||||
field.setTextFieldFilter((f, c) -> field.getText().length() < 12);
|
||||
field.setTextFieldFilter((f, c) -> field.getText().length() < 12 && filter.acceptChar(f, c));
|
||||
Mindustry.platforms.addDialog(field);
|
||||
buttons().defaults().size(120, 54).pad(4);
|
||||
buttons().addButton("$text.ok", () -> {
|
||||
@@ -387,6 +388,10 @@ public class UI extends SceneModule{
|
||||
}}.show();
|
||||
}
|
||||
|
||||
public void showTextInput(String title, String text, String def, Consumer<String> confirmed){
|
||||
showTextInput(title, text, def, (field, c) -> true, confirmed);
|
||||
}
|
||||
|
||||
public void showError(String text){
|
||||
new Dialog("$text.error.title", "dialog"){{
|
||||
content().margin(15);
|
||||
|
||||
@@ -23,8 +23,17 @@ public class Net{
|
||||
|
||||
/**Host a server at an address*/
|
||||
public static void host(int port) throws IOException{
|
||||
active = true;
|
||||
server = true;
|
||||
serverProvider.host(port);
|
||||
}
|
||||
|
||||
/**Closes the server.*/
|
||||
public static void closeServer(){
|
||||
serverProvider.close();
|
||||
server = false;
|
||||
active = false;
|
||||
}
|
||||
|
||||
/**Send an object to all connected clients, or to the server if this is a client.*/
|
||||
public static void send(Object object, SendMode mode){
|
||||
@@ -113,6 +122,8 @@ public class Net{
|
||||
public void sendTo(int id, Object object, SendMode mode);
|
||||
/**Send an object to everyone <i>except</i> a client ID.*/
|
||||
public void sendExcept(int id, Object object, SendMode mode);
|
||||
/**Close the server connection.*/
|
||||
public void close();
|
||||
/**Register classes to be sent.*/
|
||||
public void register(Class<?>... types);
|
||||
}
|
||||
|
||||
16
core/src/io/anuke/mindustry/net/Packets.java
Normal file
16
core/src/io/anuke/mindustry/net/Packets.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package io.anuke.mindustry.net;
|
||||
|
||||
/**Class for storing all packets.*/
|
||||
public class Packets {
|
||||
|
||||
public static class Connect {
|
||||
public int id;
|
||||
public String addressTCP;
|
||||
}
|
||||
|
||||
public static class Disconnect {
|
||||
public int id;
|
||||
public String addressTCP;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package io.anuke.mindustry.net.packets;
|
||||
|
||||
public class Connect {
|
||||
public int id;
|
||||
public String addressTCP;
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package io.anuke.mindustry.net.packets;
|
||||
|
||||
public class Disconnect {
|
||||
public int id;
|
||||
public String addressTCP;
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
package io.anuke.mindustry.net.packets;
|
||||
|
||||
public class Packets {
|
||||
}
|
||||
@@ -10,7 +10,12 @@ import io.anuke.ucore.scene.builders.build;
|
||||
import io.anuke.ucore.scene.builders.imagebutton;
|
||||
import io.anuke.ucore.scene.ui.ConfirmDialog;
|
||||
import io.anuke.ucore.scene.ui.ImageButton;
|
||||
import io.anuke.ucore.scene.ui.TextField.TextFieldFilter.DigitsOnlyFilter;
|
||||
import io.anuke.ucore.scene.ui.layout.Cell;
|
||||
import io.anuke.ucore.util.Bundles;
|
||||
import io.anuke.ucore.util.Strings;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MenuDialog extends FloatingDialog{
|
||||
private SaveDialog save = new SaveDialog();
|
||||
@@ -55,6 +60,24 @@ public class MenuDialog extends FloatingDialog{
|
||||
}
|
||||
|
||||
content().row();
|
||||
|
||||
content().addButton("$text.hostserver", () -> {
|
||||
Vars.ui.showTextInput("$text.hostserver", "$text.server.port", Vars.port + "", new DigitsOnlyFilter(), text -> {
|
||||
int result = Strings.parseInt(text);
|
||||
if(result == Integer.MIN_VALUE || result >= 65535){
|
||||
Vars.ui.showError("$text.server.invalidport");
|
||||
}else{
|
||||
try{
|
||||
Vars.network.hostServer(result);
|
||||
}catch (IOException e){
|
||||
Vars.ui.showError(Bundles.format("text.server.error", Strings.parseException(e, false)));
|
||||
}
|
||||
}
|
||||
});
|
||||
}).disabled(b -> Vars.network.isHosting());
|
||||
|
||||
content().row();
|
||||
|
||||
content().addButton("$text.quit", () -> {
|
||||
new ConfirmDialog("$text.confirm", "$text.quit.confirm", () -> {
|
||||
hide();
|
||||
|
||||
Reference in New Issue
Block a user