Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ajnin.game;
- import com.ajnin.game.serverobjects.TypeMessage;
- import com.cubes.BlockTerrainControl;
- import com.cubes.Vector3Int;
- import com.cubes.network.CubesSerializer;
- import com.cubes.test.CubesTestAssets;
- import com.cubes.test.blocks.Block_Grass;
- import com.jme3.app.SimpleApplication;
- import com.jme3.network.ConnectionListener;
- import com.jme3.network.HostedConnection;
- import com.jme3.network.Message;
- import com.jme3.network.MessageListener;
- import com.jme3.network.Network;
- import com.jme3.network.Server;
- import com.jme3.network.serializing.Serializer;
- import com.jme3.system.JmeContext;
- import java.io.IOException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Ajnin
- */
- public class MainServer extends SimpleApplication implements MessageListener<HostedConnection>, ConnectionListener {
- //private BlockTerrainControl map;
- //byte[] chunk;
- Server myServer;
- public static void main(String[] args) throws IOException {
- MainServer app = new MainServer();
- app.start(JmeContext.Type.Headless);
- }
- @Override
- public void simpleInitApp() {
- Serializer.registerClass(TypeMessage.class);
- try {
- System.out.println("Starting Server...");
- myServer = Network.createServer(7777);
- myServer.addConnectionListener(this);
- myServer.start();
- System.out.println("Server Started!");
- } catch (IOException ex) {
- Logger.getLogger(MainServer.class.getName()).log(Level.SEVERE, null, ex);
- }
- //Ignore For Now
- //map = new BlockTerrainControl(CubesTestAssets.getSettings(this), new Vector3Int(64, 1, 64));
- //map.setBlocksFromNoise(new Vector3Int(0, 0, 0), new Vector3Int(64, 16, 64), 0.5f, Block_Grass.class);
- //chunk = CubesSerializer.writeToBytes(map.getChunk(new Vector3Int(0, 0, 0)));
- }
- @Override
- public void messageReceived(HostedConnection source, Message message) {
- if (message instanceof TypeMessage) {
- TypeMessage information = (TypeMessage) message;
- System.out.println("Message: " + information.getMessage());
- if (source.getAttribute("Verified") == "True") {
- if (information.getType().equals("Message")) {
- TypeMessage text = (TypeMessage) message;
- System.out.println("Server Recieved '" + text.getMessage() + "' from client #" + source.getId());
- }
- } else {
- if (information.getType().equals("Game Identity")) {
- String name = information.getMessage().split("|")[0];
- Integer version = Integer.valueOf(information.getMessage().split(",")[1]);
- if (name.equals(myServer.getGameName()) && version.equals(myServer.getVersion())) {
- source.setAttribute("Verified", "True");
- System.out.println("Client # " + source.getId() + " is now verified!");
- } else {
- System.out.println("Vertification Failed");
- source.close("Verification Failed. Game Name or Version does not match.");
- }
- }
- }
- }
- }
- @Override
- public void simpleUpdate(float tpf) {
- }
- @Override
- public void destroy() {
- myServer.close();
- super.destroy();
- }
- @Override
- public void connectionAdded(Server server, HostedConnection conn) {
- System.out.println("Client Connected # " + conn);
- }
- @Override
- public void connectionRemoved(Server server, HostedConnection conn) {
- System.out.println("Removed Client: " + conn);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement