{"id":15660298,"url":"https://github.com/crushedpixel/packetgate","last_synced_at":"2025-05-05T20:12:18.762Z","repository":{"id":48465314,"uuid":"84126276","full_name":"CrushedPixel/PacketGate","owner":"CrushedPixel","description":"Sponge library to manipulate incoming and outgoing Packets.","archived":false,"fork":false,"pushed_at":"2022-01-15T12:30:39.000Z","size":96,"stargazers_count":20,"open_issues_count":5,"forks_count":11,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-05T20:12:14.100Z","etag":null,"topics":["minecraft","packets","sponge","sponge-library","sponge-plugin"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CrushedPixel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-06T22:12:37.000Z","updated_at":"2023-04-03T11:38:07.000Z","dependencies_parsed_at":"2022-08-19T12:12:00.794Z","dependency_job_id":null,"html_url":"https://github.com/CrushedPixel/PacketGate","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrushedPixel%2FPacketGate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrushedPixel%2FPacketGate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrushedPixel%2FPacketGate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrushedPixel%2FPacketGate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CrushedPixel","download_url":"https://codeload.github.com/CrushedPixel/PacketGate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252569646,"owners_count":21769517,"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","packets","sponge","sponge-library","sponge-plugin"],"created_at":"2024-10-03T13:20:57.695Z","updated_at":"2025-05-05T20:12:18.745Z","avatar_url":"https://github.com/CrushedPixel.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PacketGate\nA Sponge library allowing plugins to listen to, modify and intercept incoming and outgoing packets.\n\n## Build\nTo build `PacketGate` on your own machine, clone or download the repository, navigate into the directory and\nrun `./gradlew setupDecompWorkspace` to set up ForgeGradle and the Access Transformers.  \n\nRun `./gradlew build` to simply build the jar.\n\nAlternatively, run `./gradlew install` to build the jar and deploy it to your local maven repository to use it as a dependency in other projects.  \nAs the `PacketGate` library is in no public maven repository yet, this is the best approach to use \n`PacketGate` as a dependency as of now.\n\n## Installation\nTo use `PacketGate` in one of your plugins, you need to use ForgeGradle and add `PacketGate` as a compile-time dependency.\nWe suggest using [jitpack.io](https://jitpack.io) to depend on `PacketGate` like so:\n```gradle\nrepositories {\n    maven { url 'https://jitpack.io' }\n}\n\ndependencies {\n    compile 'com.github.CrushedPixel:PacketGate:0.1.2'\n}\n```\n\n## Usage\nThe `PacketGate` library provides a `PacketGate` class that acts as the registry for \n`PacketListener`s and provides `PacketConnection`s for all online players.\n\nYou can get an instance of PacketGate using Sponge's `ServiceManager`:\n```java\nOptional\u003cPacketGate\u003e optional = Sponge.getServiceManager().provide(PacketGate.class);\nif (optional.isPresent()) {\n    PacketGate packetGate = optional.get();\n    // use PacketGate\n} else {\n    // the PacketGate plugin is not installed on the server\n}\n\n```\n### Writing a PacketListener\nThe `PacketListener` interface exposes two methods: `onPacketWrite` and `onPacketRead` which \nare called whenever a packet of a type the listener is registered to is received from/sent to a client.\n\nFor convenience reasons, the abstract class `PacketListenerAdapter` implements those methods, \nso you only have to override the methods if you actually need them.\n\n```java\n/**\n * An example PacketListener filtering certain swear words sent by the client.\n */\npublic class SwearWordListener extends PacketListenerAdapter {\n    \n    @Override\n    public void onPacketRead(PacketEvent event, PacketConnection connection) {\n        if (!(event.getPacket() instanceof CPacketChatMessage)) return;\n        CPacketChatMessage packet = (CPacketChatMessage)event.getPacket();\n        \n        if (packet.getMessage().contains(\"shit\") || packet.getMessage().contains(\"damn\")) {\n            // cancel the event so the server will act like the client never sent it\n            event.setCancelled(true);\n            \n            // get a Sponge player from the PacketConnection\n            Player player = Sponge.getServer().getPlayer(connection.getPlayerUUID());\n            \n            // send the player some warning words\n            player.sendMessage(Text.of(\"Please don't swear!\"));\n        }\n    }\n}\n```\n\nYou can also modify the packet instead of cancelling it, for example:\n```java\nString censored = packet.getMessage().replaceAll(\"shit|damn\", \"****\");\nevent.setPacket(new CPacketChatMessage(censored));\n```\n\n### Registering a PacketListener\nWhen registering a `PacketListener`, you have to provide a `ListenerPriority` and the Packet classes\nthe listener should listen to:\n\n```java\npacketGate.registerListener(\n    new ExampleListener(), \n    ListenerPriority.DEFAULT, \n    CPacketChatMessage.class, \n    SPacketChat.class);\n\n```\n\nA `PacketListener` can be registered globally or for a certain `PacketConnection` only.\nTo retrieve the `PacketConnection` instance for a certain player, use `PacketGate#connectionByPlayer(Player)`.\n\n```java\npublic void registerSwearWordListener(Player player) {\n    Sponge.getServiceManager().provide(PacketGate.class).ifPresent(packetGate -\u003e {\n        PacketConnection connection = packetGate.connectionByPlayer(player).get();\n        packetGate.registerListener(\n            new SwearWordListener(),\n            ListenerPriority.DEFAULT, \n            connection,\n            CPacketChatMessage.class);\n    }\n}\n```\n\nTo unregister a `PacketListener`, simply call `PacketGate#unregisterListener(PacketListener)` and it won't listen to\nany packets sent out to any `PacketConnection` anymore.\n\nNote that a `PacketConnection` can only be retrieved from a `Player` instance after the server has sent out the \n`SPacketLoginSuccess` packet to the client, so any packet sent before the `Play` state may only be handled \non a global scale!\n\n### Sending packets\nYou can send any Packet to a client using `PacketConnection#sendPacket(Packet)`.  \nThis can be used to send packets without the need to modify a `PacketEvent` in a `PacketListener`.\n\n## Examples\nYou can find a full example plugin using PacketGate at \n[CrushedPixel/PacketGateExamples](https://github.com/CrushedPixel/PacketGateExamples).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrushedpixel%2Fpacketgate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrushedpixel%2Fpacketgate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrushedpixel%2Fpacketgate/lists"}