Hello,
I hope this code helps you. [edited 06072020]
Over heads is about 10-20mb of ram but is worth it becuase of how flexable it is.
How to use
new Packets
Packets are usefull because you can send data and both sides know what is being sent and how to use it. Examples of packets I have made mouse packet that takes an x and a y and a mouse button and when executed will click on the screen. The example below is from my login packet takes a name, password and a world. The server will send this to a client and using code in the script will log in.
Goto src/com/bacon/eco/server/shared/packets
Make a new Packet that "implements IPacket"
Go to PacketType enum add this line
ClassName(last number ++ , com.bacon.eco.server.shared.packets.ClassName.class),
You will see 4 override methods echo, send, receive, execute in the new Class you just made
Make 2 constructors like This. One needs to have 0 args taken, other can take what ever you want.
public LoginPacket() {
}
public LoginPacket(final String username,
final String password,
final int world){
this.username = username;
this.password = password;
this.world = world;
}
echo -has a boolean and if the server ever gets this packet it will send the info to everone- good for pking and ge merching.
send - Inside send you need to use connection.writeString, connection.writeInt, ... and others in the order that you want to send them
@Override
public void send(ActiveConnection connection) {
connection.writeString( this.username);
connection.writeString( this.password);
connection.writeInt( this.world );
}
receive - Inside receive use connection.readString,connection.writeInt in the same order as above.
@Override
public void receive(ActiveConnection connection) {
this.username =connection.readString();
this.password=connection.readString();
this.world =connection.readInt();
}
execute - put what you want your bot to do here
@Override
public void execute(ActiveConnection connection)
{
if ( Client.mp !=null) Client.mp.loginToAccount( username,password );
if ( Client.mp !=null) Client.mp.hopto( world );
}
This will does the same this as excute inside the packet (just rember to leave execute blank or it will do both). make getters and inside Client -> handlePacket -> add code like this
if (clazz == LoginPacket.class) {
final LoginPacket packet = (LoginPacket)Ipacket;
if ( Client.mp !=null) Client.mp.loginToAccount( packet.username() ,packet.password() );
if ( Client.mp !=null) Client.mp.hopto( packet.world() );
return;
}
How to add P2P messages
Make a new Class
Add it to the Enum PacketType
Goto com.bacon.eco.server.server.network.Controller
under handlePacket make code like this
Test your code (example P2PMessagePacket)
go to TestClient write some code like this. compile and run
Change the code to un comment to see them talk to each other.
out put shoud look like
Adding useful info
AbstractPingPongPacket these send a request for data [Advance]
PingPacket this updates the server ever 3 seconds.
info that could go in here
userloged in
username [example script you need write this your self]
current script runnign
User location
much,much more
Sever-side Tuning
Basicly you can make methods that can be controlled by a gui or CLA with a scanner :
Example
This tells all accounts to log out or just some amount.
public void sendlogouts(int logoutAmount) {
final Set<ActiveConnection> clientSet = clients.keySet();
int count = 0;
for (final ActiveConnection connection : clientSet) {
if (connection.getType() == 1 && (count < logoutAmount || logoutAmount == -1)) {
count++;
connection.addPacket(new LogoutPacket());
}
}
}
Adding it to your scripts
Examples from my scripts
This is code was my frist draft on networking.Code update to add change in How to use [06072020]
Things that can be added:
if your connection drops out then try to reconnect.
Better way to drop old conections- at the moment if you get sent 10 null messages in a row the connetion is droped.
Things that can be ran
TestClient
Mainserver
Example (script)
Any questions or help just ask.
update [06072020]
Username is example script needs to be defined in some way by you.
code_06072020.zip