Custom Packet Send and Receiving?

Discussion in 'Plugin Development' started by Jadar, Mar 31, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Jadar

    Hello. I am making a mod that wants to get data from the server, so I'm making a Bukkit plugin to get that data. The mod is a Forge mod, and I've got a custom packet to send to the client. When the client mod gets the packet i want it to send the server to let it know its there, then the server one sends a final packet with the data. I know there are tutorials for Forge packets, but there are none I've found for Bukkit packets. I'm also relatively new to this too.

    Thanks!
     
  2. Offline

    LucasEmanuel

    I would make the client send the first packet to let the server know there is a client with your mod. Then the server responds with the data. That way your server don't have to check every client if they have the mod, the clients just tell the server they are modded.

    If you just search "packets" here on plugin dev, you'll find a lot of threads discussing it.
     
  3. Offline

    Jadar

    What if the client connects to another server not listening for those packets? Can't there be crashes for unknown packets? And I did but I didn't find anything that was really that useful to understand how Bukkit handles sending and receiving packets.
     
  4. Offline

    LucasEmanuel

  5. Offline

    stirante

    I would do this:
    1.Server changes PlayerConnection of EntityPlayer when player joins server
    2.Server sends packet custom payload with information which requests response.
    3.Client mod which response to that packet
    4.Server sends packet with informations

    So create class which extends PlayerConnection class and override sendPacket method and method which receives packets(forgot name of that method). On PlayerJoinEvent change ((CraftPlayer)e.getPlayer()).playerConnection = new CustomPlayerConnection(params). After that everything should be easy.
     
  6. Offline

    Jadar

    Actually thats exactly what I was thinking. :D
    Thanks, I'll have a look at that. :)
     
  7. Offline

    stirante

    I actually have this system working in my mod and plugin for controlling mod, but it's only for checking if player have mod installed.
     
  8. Offline

    Jadar

    Is your mod open source that I could see and learn how you did it?
     
  9. Offline

    stirante

    It's kinda outdated and I don't use forge, but i'll post some code here.
    Code:
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
    EntityPlayer ep = ((CraftPlayer) e.getPlayer()).getHandle();
    ep.playerConnection = new PacketHandler(ep.server,
    ep.playerConnection.networkManager, ep);
    ep.playerConnection.sendPacket(new Packet250CustomPayload("CW|Init", "some text".getBytes()));
    }
    PacketHandler is class which extends PlayerConnection class
    Boring constructor, nothing too fancy:
    Code:
    public PacketHandler(MinecraftServer minecraftserver,
    INetworkManager inetworkmanager, EntityPlayer entityplayer) {
    super(minecraftserver, inetworkmanager, entityplayer);
    }
    Method which receives Packet250CustomPayload. Again nothing special becouse packet was processed in other class:
    Code:
    @Override
    public void a(Packet250CustomPayload packet){
    if (packet.tag.startsWith("CW|")){
    Mod.getListener().onPacket(packet.tag, packet.data, player.name);
    }
    else {
    super.a(packet);
    }
    }
    In onPacket method there is actually some really cool stuff:
    Code:
    public void onPacket(String name, byte[] data, String nick){
    if (name.equalsIgnoreCase("CW|Init") && !Mod.hasMod(nick)){
    Mod.getInstance().addPlayer(nick);
    lastInitedPlayer = nick;
    for (int i = 0; i < startupPackets.size(); i++) {
    ((CraftPlayer)Bukkit.getPlayerExact(nick)).getHandle().playerConnection.sendPacket(startupPackets.get(i));
    }
    CustomTexture.precacheItem(precachedItems, ((CraftPlayer)Bukkit.getPlayerExact(nick)).getHandle().playerConnection);
    CustomTexture.precacheTexture(precachedTextures, ((CraftPlayer)Bukkit.getPlayerExact(nick)).getHandle().playerConnection);
    CustomTexture.precachePainting(precachedPaintings, ((CraftPlayer)Bukkit.getPlayerExact(nick)).getHandle().playerConnection);
    Messages.sendMessage(nick, "Downloading texture...");
    if (run != null)
    Bukkit.getScheduler().scheduleSyncDelayedTask(Mod.getInstance(), run, 20);
    }
    if (name.equalsIgnoreCase("CW|Key")){
    Player player = Bukkit.getPlayerExact(nick);
    keyMgr.callEvent(data[0], player);
    }
    }
    Mod could change item's texture to texture downloaded from our server, change entitiy model and texture, add new painting, display boss health with custom name and value, achievements also with custom textures and before 1.5 I started custom sounds also downloaded from server :D And i forgot about custom keybinding and a lot of other cool features.
     
  10. Offline

    Jadar

    Hmm, I don't know what the problem is, but I can't seem to get it to work. It seems to send the packet okay, but the client won't print it out.

    Code:
    @EventHandler(priority = EventPriority.NORMAL)
    public void onPlayerLogin(PlayerLoginEvent event) {
    Player player = event.getPlayer();
    System.out.println("Sent message");
    player.sendPluginMessage(this.plugin, "plugin-Out", "u there".getBytes());
    }
    
    Code:
    @Override
    public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) {
    String data = new String(packet.data);
    System.out.println("/n/n/n"+data+"/n/n/n");
    }
    
     
  11. did you registered at the server side at the out coming and incoming channels?
     
  12. Offline

    Jadar

    Yes.

    Code:
    @Override
    public void onEnable() {
     
    Bukkit.getMessenger().registerOutgoingPluginChannel(this, "plugin-Out");
    Bukkit.getMessenger().registerIncomingPluginChannel(this, "plugin-In", packetHandler);
     
     
    this.getServer().getPluginManager().registerEvents(new PlayerLoginListener(this), this);
    }
    
    -
     
  13. does the client also does this?
     
  14. Offline

    Jadar

    Yeah

    in main mod class
    Code:
    @NetworkMod(clientSideRequired=true, serverSideRequired=false,
    channels={"plugin-In", "plugin-Out"}, packetHandler = PacketHandler.class)
    
    in PacketHandler class
    Code:
    import net.minecraft.network.INetworkManager;
    import net.minecraft.network.packet.Packet250CustomPayload;
    import cpw.mods.fml.common.network.IPacketHandler;
    import cpw.mods.fml.common.network.Player;
     
    public class PacketHandler implements IPacketHandler {
     
    @Override
    public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) {
    String data = new String(packet.data);
    System.out.println("/n/n/n"+data+"/n/n/n");
    }
     
    }
    
     
Thread Status:
Not open for further replies.

Share This Page