{"id":16374357,"url":"https://github.com/raphimc/netminecraft","last_synced_at":"2025-03-21T01:32:04.266Z","repository":{"id":59501701,"uuid":"532105248","full_name":"RaphiMC/NetMinecraft","owner":"RaphiMC","description":"Easy to use low-level networking library for Minecraft","archived":false,"fork":false,"pushed_at":"2024-05-22T16:59:59.000Z","size":400,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-05-22T18:01:38.089Z","etag":null,"topics":["minecraft","minecraft-network","minecraft-protocol","netty","protocol"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RaphiMC.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"ko_fi":"rk_01"}},"created_at":"2022-09-02T23:25:21.000Z","updated_at":"2024-06-05T00:46:19.050Z","dependencies_parsed_at":"2024-01-01T02:26:56.954Z","dependency_job_id":"3d1f5b59-7a46-45cb-a227-df66ee6f0560","html_url":"https://github.com/RaphiMC/NetMinecraft","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RaphiMC%2FNetMinecraft","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RaphiMC%2FNetMinecraft/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RaphiMC%2FNetMinecraft/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RaphiMC%2FNetMinecraft/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RaphiMC","download_url":"https://codeload.github.com/RaphiMC/NetMinecraft/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244097197,"owners_count":20397562,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["minecraft","minecraft-network","minecraft-protocol","netty","protocol"],"created_at":"2024-10-11T03:16:59.607Z","updated_at":"2025-03-21T01:32:04.261Z","avatar_url":"https://github.com/RaphiMC.png","language":"Java","funding_links":["https://ko-fi.com/rk_01"],"categories":[],"sub_categories":[],"readme":"# NetMinecraft\nEasy to use low-level networking library for Minecraft.  \n\n## Releases\n### Gradle/Maven\nTo use NetMinecraft with Gradle/Maven you can get it from [Lenni0451's Maven](https://maven.lenni0451.net/#/releases/net/raphimc/netminecraft) or [Jitpack](https://jitpack.io/#RaphiMC/NetMinecraft).\nYou can also find instructions how to implement it into your build script there.\n\n### Jar File\nIf you just want the latest jar file you can download it from [GitHub Actions](https://github.com/RaphiMC/NetMinecraft/actions/workflows/build.yml) or [Lenni0451's Jenkins](https://build.lenni0451.net/job/NetMinecraft/).\n\n## Usage\n### Client\nTo create a client you can create a new ``NetClient`` instance. The first parameter is a supplier which returns a ``ChannelHandler`` which will be used to handle the packets.  \n\nTo connect to a server with the client you can use the ``connect`` method. (To resolve SRV records you can use the ``MinecraftServerAddress`` class)\n\n### Server\nTo create a server you can create a new ``NetServer`` instance. The first parameter is a supplier which returns a ``ChannelHandler`` which will be used to handle the incoming connections.  \n\nTo bind the server to a port you can use the ``bind`` method.\n\n### Handling packets\nNetMinecraft allows you to either handle packet reading fully yourself or use the built-in packet reading system.  \nTo read packets yourself you don't have to do anything special. NetMinecraft will decompress and decrypt the packets for you and you can read them from the ``ByteBuf`` in the channel handlers.   \n#### Built-in packet reading\nThe built-in packet reading system requires the ``packets`` submodule to be included into your project.  \nIt will allow you to read packets from all version of Minecraft, but only from the Handshake, Status and Login state.\n\nTo use the built-in packet reading system you have to overwrite the ``ChannelInitializer`` of the ``NetClient`` or ``NetServer`` instance.  \n\nHere is an example for a client:\n```java\npublic class ClientChannelInitializer extends MinecraftChannelInitializer {\n\n    public ClientChannelInitializer(final Supplier\u003cChannelHandler\u003e handlerSupplier) {\n        super(handlerSupplier);\n    }\n\n    @Override\n    protected void initChannel(Channel channel) {\n        super.initChannel(channel);\n        channel.attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(new DefaultPacketRegistry(true, protocolVersion));\n    }\n\n}\n```\n\nFrom this point on you will receive ``Packet``s instead of ``ByteBuf``s in your channel handlers.\nYou need to set the connection state of the ``PacketRegistry`` to the correct state yourself.\n\n## Example\n### Client\n```java\nNetClient client = new NetClient(new MinecraftChannelInitializer(new Supplier\u003cChannelHandler\u003e() {\n    @Override\n    public ChannelHandler get() {\n        return new SimpleChannelInboundHandler\u003cByteBuf\u003e() {\n            long start;\n\n            @Override\n            public void handlerAdded(ChannelHandlerContext ctx) throws Exception {\n                super.handlerAdded(ctx);\n                start = System.currentTimeMillis();\n            }\n\n            @Override\n            public void channelActive(ChannelHandlerContext ctx) throws Exception {\n                super.channelActive(ctx);\n                System.out.println(\"connected\");\n            }\n\n            @Override\n            public void channelInactive(ChannelHandlerContext ctx) throws Exception {\n                super.channelInactive(ctx);\n                System.out.println(\"disconnected\");\n            }\n\n            @Override\n            protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {\n                System.out.println(\"got response in \" + (System.currentTimeMillis() - start) + \" ms\");\n                channelHandlerContext.close();\n            }\n        };\n    }\n}));\nByteBuf handshake = Unpooled.buffer();\nPacketTypes.writeVarInt(handshake, 0); // packet id\nPacketTypes.writeVarInt(handshake, 47); // protocol version\nPacketTypes.writeString(handshake, \"localhost\"); // server address\nhandshake.writeShort(25565); // server port\nPacketTypes.writeVarInt(handshake, 1); // next state\n\nByteBuf request = Unpooled.buffer();\nPacketTypes.writeVarInt(request, 0); // packet id\n\nclient.connect(MinecraftServerAddress.ofResolved(\"localhost\")).syncUninterruptibly(); // blocking connect\nclient.getChannel().writeAndFlush(handshake);\nclient.getChannel().writeAndFlush(request);\nclient.getChannel().closeFuture().syncUninterruptibly();\n```\n### Server\n```java\nNetServer server = new NetServer(new MinecraftChannelInitializer(() -\u003e new SimpleChannelInboundHandler\u003cByteBuf\u003e() {\n    @Override\n    public void channelActive(ChannelHandlerContext ctx) throws Exception {\n        super.channelActive(ctx);\n        System.out.println(\"New connection from \" + ctx.channel().remoteAddress());\n    }\n\n    @Override\n    public void channelInactive(ChannelHandlerContext ctx) throws Exception {\n        super.channelInactive(ctx);\n        System.out.println(ctx.channel().remoteAddress() + \" closed connection\");\n    }\n\n    @Override\n    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {\n        // Read packets here\n    }\n}));\nserver.bind(new InetSocketAddress(\"0.0.0.0\", 25565), true);\n```\n### Proxy\n```java\nfinal NetServer server = new NetServer(new ChannelInitializer\u003cChannel\u003e() {\n    @Override\n    protected void initChannel(Channel channel) {\n        channel.pipeline().addLast(new SimpleChannelInboundHandler\u003cByteBuf\u003e() {\n            private Channel otherChannel;\n            private final NetClient client = new NetClient(new ChannelInitializer\u003cChannel\u003e() {\n                @Override\n                protected void initChannel(Channel channel) {\n                    channel.pipeline().addLast(new SimpleChannelInboundHandler\u003cByteBuf\u003e() {\n                        @Override\n                        protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) {\n                            otherChannel.writeAndFlush(byteBuf.retain());\n                        }\n                    });\n                }\n            });\n\n            @Override\n            public void channelActive(ChannelHandlerContext ctx) {\n                this.otherChannel = ctx.channel();\n                this.client.connect(MinecraftServerAddress.ofResolved(\"localhost\")).syncUninterruptibly();\n            }\n\n            @Override\n            protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) {\n                this.client.getChannel().writeAndFlush(byteBuf.retain());\n            }\n\n            @Override\n            public void channelInactive(ChannelHandlerContext ctx) {\n                this.client.getChannel().close().syncUninterruptibly();\n            }\n        });\n    }\n});\nserver.bind(new InetSocketAddress(\"0.0.0.0\", 25565));\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraphimc%2Fnetminecraft","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fraphimc%2Fnetminecraft","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraphimc%2Fnetminecraft/lists"}