-
Notifications
You must be signed in to change notification settings - Fork 11
Docs: CODE
Alemiz edited this page Aug 31, 2019
·
3 revisions
Here we will show some important parts of code witch is useful to understand.
Full code of Server class can be found here.
/**
* Settings of StarGate protocol, communication services and more
* These services are running on separated threads, so performance will not get DOWN
*/
protected static int port = 47007;
protected static int maxConn = 50;
/**
* Here we are registering new Official Packets, may be useful for DEV
* Every packet Extends @class StarGatePacket*/
private void initPackets(){
GateAPI.RegisterPacket(new WelcomePacket());
GateAPI.RegisterPacket(new PingPacket());
GateAPI.RegisterPacket(new PlayerTransferPacket());
GateAPI.RegisterPacket(new KickPacket());
GateAPI.RegisterPacket(new PlayerOnlinePacket());
}
/* Using these function we can process packet from string to data
* After packet is successfully created we can handle that Packet*/
protected boolean processPacket(String client, String packetString){
String[] data = Convertor.getPacketStringData(packetString);
int PacketId = Integer.decode(data[0]);
if (!packets.containsKey(PacketId) || packets.get(PacketId) == null) return false;
/* Here we decode Packet. Create from String Data*/
StarGatePacket packet = packets.get(PacketId);
String uuid = data[data.length - 1];
packet.decode();
handlePacket(client, packet);
return true;
}
Code of StarGatePacket class can be found here.
/* This class helps you to create your own/custom Packets
* Your packet must extend this class so 'extends StarGatePacket'
* I recommend to look into some official packet to better understanding*/
public abstract class StarGatePacket implements Cloneable{
/* Literally its packet Name*/
protected String type;
/* Every packet must have its own ID
* ID must be unique to prevent crashes or packets rewrites
* Official IDs are registered in @class Packets*/
protected int ID;
public String encoded;
public boolean isEncoded = true;
/* UUID is used for returning response if is needed*/
public String uuid;
/** We use this functions to be able work with string compression
* encode() => Converts data to string and save it tp $encoded
* decode() => Converts from string in $encoded and saves it
* Every packet has custom data, so you must adjust it yourself
* Try to inspire by official packets*/
public abstract void encode();
public abstract void decode();
public abstract StarGatePacket copy() throws CloneNotSupportedException;
/* When you are creating Packet you must define in Constructor Type and ID
* Its really simple: 'super(Type, ID)'*/
public StarGatePacket(String type, int ID){
this.type = type;
this.ID = ID;
}
}
AS example we will use WelcomePacket. Here are shown only needed functions witch must be in every packet.
public void decode() {
/* This(isEncoded) is very important! Server will try to decode packet if it will be not set correctly
* And that can return unupdated packet*/
isEncoded = false;
/* data[0] => ID*/
String[] data = Convertor.getPacketStringData(encoded);
this.server = data[1];
this.tps = Integer.decode(data[2]);
this.usage = Integer.decode(data[3]);
this.players = Integer.decode(data[4]);
}
/* Using @class Convertor we can create packetString from custom data
* It supports custom converting methods
* You must create new Conventor class then using dynamic functions you can putString|putInt|or other data
* For more docs see GitHub documentation*/
##### Encoding:
@Override
public void encode() {
Convertor convertor = new Convertor(getID());
convertor.putString(server);
convertor.putInt(tps);
convertor.putInt(usage);
convertor.putInt(players);
this.encoded = convertor.getPacketString();
isEncoded = true;
}
/* May be useful in feature references
* It helps to clone packet with all its data 'super.clone()'*/
@Override
public StarGatePacket copy() throws CloneNotSupportedException {
WelcomePacket packet = (WelcomePacket) super.clone();
packet.players = players;
packet.server = server;
packet.tps = tps;
packet.usage = usage;
return packet;
}