Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Build api8

on:
push:
branches: [ api8 ]
pull_request:
branches: [ api8 ]

jobs:
build:
name: Build
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
name: Checkout repo

- name: Set up JDK 11 (LTS)
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
cache: gradle

- name: Build with Gradle
run: gradle build

- uses: actions/upload-artifact@v2
name: Upload Artifact
with:
name: PacketGate.jar
path: build/libs/PacketGate-*.jar
83 changes: 73 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,76 @@
# Intellij project files
*.iml
*.ipr
*.iws
.idea/
# Build #
#########
MANIFEST.MF
dependency-reduced-pom.xml

# Compiled #
############
/bin
/build
/dist
/lib
/out
/run
/target
*.com
*.class
*.dll
*.exe
*.o
*.so

# Databases #
#############
*.db
*.sql
*.sqlite

# Packages #
############
*.7z
*.dmg
*.gz
*.iso
*.rar
*.tar
*.zip

#Gradle
.gradletasknamecache
.gradle/
build/
bin/
# Repository #
##############
.git

# Logging #
###########
/logs
*.log

# Misc #
########
*.bak

# System #
##########
.DS_Store
ehthumbs.db
Thumbs.db

# Project #
###########
.classpath
.externalToolBuilders
/.gradle
.idea
.project
.settings
.factorypath
/eclipse
nbproject
atlassian-ide-plugin.xml
build.xml
nb-configuration.xml
*.iml
*.ipr
*.iws
/.apt_generated/
*.launch
/.nb-gradle/
41 changes: 16 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,9 @@ dependencies {
The `PacketGate` library provides a `PacketGate` class that acts as the registry for
`PacketListener`s and provides `PacketConnection`s for all online players.

You can get an instance of PacketGate using Sponge's `ServiceManager`:
You can get an instance of PacketGate using `PacketGateAPI`:
```java
Optional<PacketGate> optional = Sponge.getServiceManager().provide(PacketGate.class);
if (optional.isPresent()) {
PacketGate packetGate = optional.get();
// use PacketGate
} else {
// the PacketGate plugin is not installed on the server
}

PacketGate packetGate = PacketGateAPI.get();
```
### Writing a PacketListener
The `PacketListener` interface exposes two methods: `onPacketWrite` and `onPacketRead` which
Expand All @@ -54,15 +47,15 @@ public class SwearWordListener extends PacketListenerAdapter {

@Override
public void onPacketRead(PacketEvent event, PacketConnection connection) {
if (!(event.getPacket() instanceof CPacketChatMessage)) return;
CPacketChatMessage packet = (CPacketChatMessage)event.getPacket();
if (!(event.packet() instanceof ServerboundChatPacket)) return;
ServerboundChatPacket packet = (ServerboundChatPacket) event.packet();

if (packet.getMessage().contains("shit") || packet.getMessage().contains("damn")) {
// cancel the event so the server will act like the client never sent it
event.setCancelled(true);

// get a Sponge player from the PacketConnection
Player player = Sponge.getServer().getPlayer(connection.getPlayerUUID());
Player player = Sponge.getServer().getPlayer(connection.playerUniqueId());

// send the player some warning words
player.sendMessage(Text.of("Please don't swear!"));
Expand All @@ -74,7 +67,7 @@ public class SwearWordListener extends PacketListenerAdapter {
You can also modify the packet instead of cancelling it, for example:
```java
String censored = packet.getMessage().replaceAll("shit|damn", "****");
event.setPacket(new CPacketChatMessage(censored));
event.setPacket(new ServerboundChatPacket(censored));
```

### Registering a PacketListener
Expand All @@ -84,25 +77,23 @@ the listener should listen to:
```java
packetGate.registerListener(
new ExampleListener(),
ListenerPriority.DEFAULT,
CPacketChatMessage.class,
SPacketChat.class);

ListenerPriority.DEFAULT,
ServerboundChatPacket.class
);
```

A `PacketListener` can be registered globally or for a certain `PacketConnection` only.
To retrieve the `PacketConnection` instance for a certain player, use `PacketGate#connectionByPlayer(Player)`.

```java
public void registerSwearWordListener(Player player) {
Sponge.getServiceManager().provide(PacketGate.class).ifPresent(packetGate -> {
PacketConnection connection = packetGate.connectionByPlayer(player).get();
packetGate.registerListener(
new SwearWordListener(),
ListenerPriority.DEFAULT,
connection,
CPacketChatMessage.class);
}
PacketGate packetGate = PacketGateAPI.get();
PacketConnection connection = packetGate.connectionByPlayer(player).get();
packetGate.registerListener(
new SwearWordListener(),
ListenerPriority.DEFAULT,
connection,
CPacketChatMessage.class);
}
```

Expand Down
45 changes: 0 additions & 45 deletions build.gradle

This file was deleted.

77 changes: 77 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import org.spongepowered.gradle.plugin.config.PluginLoaders
import org.spongepowered.gradle.vanilla.repository.MinecraftPlatform
import org.spongepowered.plugin.metadata.model.PluginDependency

plugins {
`maven-publish`
`java-library`
id("org.spongepowered.gradle.plugin") version "2.0.0"
id("org.spongepowered.gradle.vanilla") version "0.2"
}

group = "eu.crushedpixel.sponge"
version = "0.2.0"

repositories {
mavenCentral()
}

dependencies {
compileOnly("org.spongepowered:mixin:0.8.3")
}

minecraft {
version("1.16.5")
platform(MinecraftPlatform.SERVER)
injectRepositories(false)
}

sponge {
apiVersion("8.0.0")
license("NO_LICENCE_YET")
plugin("packetgate") {
loader {
name(PluginLoaders.JAVA_PLAIN)
version("1.0")
}
displayName("PacketGate")
entrypoint("eu.crushedpixel.sponge.packetgate.plugin.PluginPacketGate")
description("Sponge library to manipulate incoming and outgoing Packets. ")
links {
homepage("https://github.com/CrushedPixel/PacketGate")
source("https://github.com/CrushedPixel/PacketGate")
issues("https://github.com/CrushedPixel/PacketGate/issues")
}
contributor("CrushedPixel") {
description("Lead developer")
}
contributor("Masa") {
description("API8 port")
}
dependency("spongeapi") {
loadOrder(PluginDependency.LoadOrder.AFTER)
optional(false)
}
}
}

tasks.withType<Jar> {
manifest {
attributes["MixinConfigs"] = "mixins.packetgate.json"
}
}

tasks.withType(JavaCompile::class).configureEach {
options.apply {
encoding = "utf-8" // Consistent source file encoding
if (JavaVersion.current().isJava10Compatible) {
release.set(11)
}
}
}

// Make sure all tasks which produce archives (jar, sources jar, javadoc jar, etc) produce more consistent output
tasks.withType(AbstractArchiveTask::class).configureEach {
isReproducibleFileOrder = true
isPreserveFileTimestamps = false
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package eu.crushedpixel.sponge.packetgate.api;

import eu.crushedpixel.sponge.packetgate.api.registry.PacketGate;
import eu.crushedpixel.sponge.packetgate.plugin.PluginPacketGate;

public class PacketGateAPI {

/**
* Get {@link PacketGate}
*
* @return PacketGate instance to use
*/
public static PacketGate get() {
return PluginPacketGate.packetGate;
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package eu.crushedpixel.sponge.packetgate.api.event;

import net.minecraft.network.Packet;

import net.minecraft.network.protocol.Packet;

public class PacketEvent {

public PacketEvent(Packet packet, boolean outgoing) {
public PacketEvent(Packet<?> packet, boolean outgoing) {
this.packet = packet;
this.outgoing = outgoing;
}

private Packet packet;
private Packet<?> packet;

private boolean cancelled = false;

private final boolean outgoing;

public Packet getPacket() {
public Packet<?> packet() {
return packet;
}

public void setPacket(Packet packet) {
public void setPacket(Packet<?> packet) {
this.packet = packet;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

public abstract class ListenerOwner {

protected final Map<Class, List<PacketListenerData>> packetListeners = new ConcurrentHashMap<>();
protected final Map<Class<?>, List<PacketListenerData>> packetListeners = new ConcurrentHashMap<>();

void register(PacketListenerData packetListenerData, Class... classes) {
for (Class clazz : classes) {
void register(PacketListenerData packetListenerData, Class<?>... classes) {
for (Class<?> clazz : classes) {
List<PacketListenerData> list = packetListeners.get(clazz);
if (list == null) list = new CopyOnWriteArrayList<>();
list.add(packetListenerData);
Expand Down
Loading