Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PedroMPagani/981502151d50190e2a4589e96814e013 to your computer and use it in GitHub Desktop.
Save PedroMPagani/981502151d50190e2a4589e96814e013 to your computer and use it in GitHub Desktop.
This is a packet system that can be used with the RedisAPI in my gist located at:
https://gist.github.com/PedroMPagani/b6fb2a5d54dd2912f98126fff4126051
@PedroMPagani
Copy link
Author

PedroMPagani commented Jun 25, 2022

Base message class:

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import org.bukkit.Bukkit;

public abstract class Packet {

    private int id;

    public Packet(int id) {
        this.id = id;
    }

    public abstract byte[] write();
    public abstract void read(ByteArrayDataInput input);

    public ByteArrayDataOutput newWriter(){
        ByteArrayDataOutput byteArrayDataOutput = ByteStreams.newDataOutput();
        byteArrayDataOutput.writeInt(Bukkit.getServer().getPort());
        byteArrayDataOutput.writeInt(id);
        return byteArrayDataOutput;
    }

}

Example of class:

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.strixmc.crosscore.api.GhostsAPI;
import org.strixmc.messenger.base.Packet;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

@Getter
public class ServerTickPacket extends Packet {

    private List<Player> players = new LinkedList<>();
    public ServerTickPacket(){
        super(0);
    }

    @Override
    public byte[] write(){
        ByteArrayDataOutput writer = newWriter();
        writer.writeInt(players.size());
        return writer.toByteArray();
    }

    @Override
    public void read(ByteArrayDataInput input) {
        int amount  = input.readInt();
    // packet income.
    }

}
// I recommend you have a pubsub where you decompress and read the id of the packet as first int, example:

public class PosseidonChannel extends RedisPubSubAdapter<byte[],byte[]> {

    private Map<Integer,Class<? extends Packet>> packetMap = new ConcurrentHashMap<>();

    public PosseidonChannel(RedisAPI redisAPI){
        redisAPI.registerPubSub(this, "Example".getBytes(StandardCharsets.UTF_8));
        packetMap.put(0, ServerTickPacket.class);
    }

    @Override
    public void message(byte[] channel, byte[] message){
        MainExecutorPool.executorPool.execute(()->{
            ByteArrayDataInput dataInput = ByteStreams.newDataInput(RedisAPI.decompress(message));
            try {
                int port = dataInput.readInt();
                if(Bukkit.getPort() == port){
                    return;
                }
                int packetId = dataInput.readInt();
                Class<? extends Packet> a = packetMap.get(packetId);
                if (a == null) return;
                Packet corePacket = a.newInstance();
                corePacket.read(dataInput); // handle packet..
            } catch (Exception exception){
                exception.printStackTrace();
            }
        });
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment