Skip to content

Commit 2fa0a86

Browse files
author
bb-dev
committed
refactor: organize Java custom payload messaging
1 parent 950313e commit 2fa0a86

10 files changed

Lines changed: 674 additions & 40 deletions

src/main/java/org/itxtech/synapseapi/SynapseAPI.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import cn.nukkit.Server;
55
import cn.nukkit.event.EventHandler;
66
import cn.nukkit.event.Listener;
7+
import cn.nukkit.event.player.PlayerQuitEvent;
8+
import cn.nukkit.event.plugin.PluginDisableEvent;
79
import cn.nukkit.event.server.BatchPacketsEvent;
810
import cn.nukkit.item.Item;
911
import cn.nukkit.item.RuntimeItemPaletteInterface;
@@ -23,6 +25,8 @@
2325
import org.itxtech.synapseapi.filtertext.FilterTextService;
2426
import org.itxtech.synapseapi.messaging.Messenger;
2527
import org.itxtech.synapseapi.messaging.StandardMessenger;
28+
import org.itxtech.synapseapi.messaging.java.JavaCustomPayloadMessenger;
29+
import org.itxtech.synapseapi.messaging.java.StandardJavaCustomPayloadMessenger;
2630
import org.itxtech.synapseapi.multiprotocol.AbstractProtocol;
2731
import org.itxtech.synapseapi.multiprotocol.PacketRegister;
2832
import org.itxtech.synapseapi.multiprotocol.utils.*;
@@ -50,6 +54,7 @@ public class SynapseAPI extends PluginBase implements Listener {
5054
private boolean recordPacketStack = false;
5155
private final Map<String, SynapseEntry> synapseEntries = new Object2ObjectOpenHashMap<>();
5256
private Messenger messenger;
57+
private JavaCustomPayloadMessenger javaCustomPayloadMessenger;
5358
private boolean networkBroadcastPlayerMove;
5459
private int blobCacheChunkSendPreTick;
5560

@@ -81,6 +86,7 @@ public void onEnable() {
8186
Player.setViolationListener(new SynapsePlayerViolationListener());
8287

8388
this.messenger = new StandardMessenger();
89+
this.javaCustomPayloadMessenger = new StandardJavaCustomPayloadMessenger();
8490
loadEntries();
8591

8692
this.getServer().getPluginManager().registerEvents(this, this);
@@ -372,6 +378,26 @@ public Messenger getMessenger() {
372378
return messenger;
373379
}
374380

381+
public JavaCustomPayloadMessenger getJavaCustomPayloadMessenger() {
382+
return this.javaCustomPayloadMessenger;
383+
}
384+
385+
@EventHandler
386+
public void onPlayerQuit(final PlayerQuitEvent event) {
387+
if (this.javaCustomPayloadMessenger != null && event.getPlayer() instanceof SynapsePlayer player) {
388+
this.javaCustomPayloadMessenger.unregisterPlayerChannels(player);
389+
}
390+
}
391+
392+
@EventHandler
393+
public void onPluginDisable(final PluginDisableEvent event) {
394+
if (this.javaCustomPayloadMessenger == null) {
395+
return;
396+
}
397+
this.javaCustomPayloadMessenger.unregisterIncomingPluginChannel(event.getPlugin());
398+
this.javaCustomPayloadMessenger.unregisterOutgoingPluginChannel(event.getPlugin());
399+
}
400+
375401
@EventHandler
376402
public void onBatchPackets(BatchPacketsEvent e) {
377403
e.setCancelled();

src/main/java/org/itxtech/synapseapi/SynapsePlayer.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.itxtech.synapseapi.event.player.SynapsePlayerPreChatEvent;
4444
import org.itxtech.synapseapi.event.player.SynapsePlayerTransferEvent;
4545
import org.itxtech.synapseapi.event.player.SynapsePlayerUnexpectedBehaviorEvent;
46+
import org.itxtech.synapseapi.messaging.java.JavaCustomPayloadMessenger;
4647
import org.itxtech.synapseapi.multiprotocol.AbstractProtocol;
4748
import org.itxtech.synapseapi.multiprotocol.common.camera.CameraInstruction;
4849
import org.itxtech.synapseapi.multiprotocol.common.drawer.Shape;
@@ -58,7 +59,7 @@
5859
import org.itxtech.synapseapi.multiprotocol.protocol121130.protocol.TextPacket121130;
5960
import org.itxtech.synapseapi.multiprotocol.protocol12120.protocol.ChangeDimensionPacket12120;
6061
import org.itxtech.synapseapi.multiprotocol.protocol11810.protocol.ScriptMessagePacket11810;
61-
import org.itxtech.synapseapi.java.JavaCustomPayloadEnvelope;
62+
import org.itxtech.synapseapi.messaging.java.JavaCustomPayloadEnvelope;
6263
import org.itxtech.synapseapi.multiprotocol.protocol126.protocol.TextPacket126;
6364
import org.itxtech.synapseapi.multiprotocol.protocol14.protocol.PlayerActionPacket14;
6465
import org.itxtech.synapseapi.multiprotocol.protocol14.protocol.TextPacket14;
@@ -1462,8 +1463,12 @@ public void onRun() {
14621463
break;
14631464
}
14641465
JavaCustomPayloadEnvelope.decode(scriptMessagePacket.messageId, scriptMessagePacket.value)
1465-
.ifPresent(payload -> this.server.getPluginManager().callEvent(
1466-
new SynapsePlayerJavaCustomPayloadEvent(this, payload.channel(), payload.payload())));
1466+
.ifPresent(payload -> {
1467+
JavaCustomPayloadMessenger messenger = SynapseAPI.getInstance().getJavaCustomPayloadMessenger();
1468+
messenger.dispatchIncomingMessage(this, payload.channel(), payload.payload());
1469+
this.server.getPluginManager().callEvent(
1470+
new SynapsePlayerJavaCustomPayloadEvent(this, payload.channel(), payload.payload()));
1471+
});
14671472
break;
14681473
/*case ProtocolInfo.LEVEL_SOUND_EVENT_PACKET:
14691474
if (!callPacketReceiveEvent(packet)) break;

src/main/java/org/itxtech/synapseapi/java/JavaCustomPayloadBridge.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/main/java/org/itxtech/synapseapi/java/JavaCustomPayloadEnvelope.java renamed to src/main/java/org/itxtech/synapseapi/messaging/java/JavaCustomPayloadEnvelope.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.itxtech.synapseapi.java;
1+
package org.itxtech.synapseapi.messaging.java;
22

33
import java.nio.ByteBuffer;
44
import java.nio.charset.CharacterCodingException;
@@ -15,13 +15,21 @@
1515
public final class JavaCustomPayloadEnvelope {
1616

1717
public static final String SCRIPT_MESSAGE_ID = "easecation:java_custom_payload_v1";
18-
public static final int MAX_CHANNEL_BYTES = 128;
19-
public static final int MAX_PAYLOAD_BYTES = 8 * 1024;
18+
public static final int MAX_CHANNEL_BYTES = JavaCustomPayloadMessenger.MAX_CHANNEL_SIZE;
19+
public static final int MAX_PAYLOAD_BYTES = JavaCustomPayloadMessenger.MAX_MESSAGE_SIZE;
2020
private static final Pattern CHANNEL_PATTERN = Pattern.compile("[a-z0-9_.-]+:[a-z0-9/._-]+");
2121

2222
private JavaCustomPayloadEnvelope() {
2323
}
2424

25+
public static boolean isValidChannel(final String channel) {
26+
if (channel == null || !CHANNEL_PATTERN.matcher(channel).matches()) {
27+
return false;
28+
}
29+
final byte[] channelBytes = channel.getBytes(StandardCharsets.UTF_8);
30+
return channelBytes.length > 0 && channelBytes.length <= MAX_CHANNEL_BYTES;
31+
}
32+
2533
public static Optional<Payload> decode(final String messageId, final String encodedValue) {
2634
if (!SCRIPT_MESSAGE_ID.equals(messageId)) {
2735
return Optional.empty();
@@ -54,11 +62,11 @@ public static Optional<Payload> decode(final String messageId, final String enco
5462
}
5563

5664
public static Optional<String> encode(final String channel, final byte[] payload) {
57-
if (channel == null || payload == null || !CHANNEL_PATTERN.matcher(channel).matches()) {
65+
if (!isValidChannel(channel) || payload == null) {
5866
return Optional.empty();
5967
}
6068
final byte[] channelBytes = channel.getBytes(StandardCharsets.UTF_8);
61-
if (channelBytes.length == 0 || channelBytes.length > MAX_CHANNEL_BYTES || payload.length > MAX_PAYLOAD_BYTES) {
69+
if (payload.length > MAX_PAYLOAD_BYTES) {
6270
return Optional.empty();
6371
}
6472
final byte[] envelope = new byte[1 + channelBytes.length + payload.length];
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.itxtech.synapseapi.messaging.java;
2+
3+
import org.itxtech.synapseapi.SynapsePlayer;
4+
5+
/**
6+
* Java 客户端自定义载荷监听器。
7+
*/
8+
@FunctionalInterface
9+
public interface JavaCustomPayloadListener {
10+
11+
void onJavaCustomPayloadReceived(SynapsePlayer player, String channel, byte[] payload);
12+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.itxtech.synapseapi.messaging.java;
2+
3+
import cn.nukkit.plugin.Plugin;
4+
5+
import java.util.Objects;
6+
7+
/**
8+
* Java 客户端自定义载荷监听器注册记录。
9+
*/
10+
public final class JavaCustomPayloadListenerRegistration {
11+
12+
private final JavaCustomPayloadMessenger messenger;
13+
private final Plugin plugin;
14+
private final String channel;
15+
private final JavaCustomPayloadListener listener;
16+
17+
JavaCustomPayloadListenerRegistration(final JavaCustomPayloadMessenger messenger, final Plugin plugin,
18+
final String channel, final JavaCustomPayloadListener listener) {
19+
this.messenger = Objects.requireNonNull(messenger, "messenger");
20+
this.plugin = Objects.requireNonNull(plugin, "plugin");
21+
this.channel = Objects.requireNonNull(channel, "channel");
22+
this.listener = Objects.requireNonNull(listener, "listener");
23+
}
24+
25+
public Plugin getPlugin() {
26+
return this.plugin;
27+
}
28+
29+
public String getChannel() {
30+
return this.channel;
31+
}
32+
33+
public JavaCustomPayloadListener getListener() {
34+
return this.listener;
35+
}
36+
37+
public boolean isValid() {
38+
return this.messenger.isRegistrationValid(this);
39+
}
40+
41+
@Override
42+
public boolean equals(final Object object) {
43+
if (this == object) {
44+
return true;
45+
}
46+
if (!(object instanceof JavaCustomPayloadListenerRegistration registration)) {
47+
return false;
48+
}
49+
return this.messenger.equals(registration.messenger)
50+
&& this.plugin.equals(registration.plugin)
51+
&& this.channel.equals(registration.channel)
52+
&& this.listener.equals(registration.listener);
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
return Objects.hash(this.messenger, this.plugin, this.channel, this.listener);
58+
}
59+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.itxtech.synapseapi.messaging.java;
2+
3+
import cn.nukkit.plugin.Plugin;
4+
import org.itxtech.synapseapi.SynapsePlayer;
5+
6+
import java.util.Set;
7+
8+
/**
9+
* Java 客户端自定义载荷的插件频道注册与分发接口。
10+
*/
11+
public interface JavaCustomPayloadMessenger {
12+
13+
int MAX_CHANNEL_SIZE = 128;
14+
int MAX_MESSAGE_SIZE = 8 * 1024;
15+
String REGISTER_CHANNEL = "minecraft:register";
16+
String UNREGISTER_CHANNEL = "minecraft:unregister";
17+
18+
boolean isReservedChannel(String channel);
19+
20+
void registerOutgoingPluginChannel(Plugin plugin, String channel);
21+
22+
void unregisterOutgoingPluginChannel(Plugin plugin, String channel);
23+
24+
void unregisterOutgoingPluginChannel(Plugin plugin);
25+
26+
JavaCustomPayloadListenerRegistration registerIncomingPluginChannel(Plugin plugin, String channel,
27+
JavaCustomPayloadListener listener);
28+
29+
void unregisterIncomingPluginChannel(Plugin plugin, String channel, JavaCustomPayloadListener listener);
30+
31+
void unregisterIncomingPluginChannel(Plugin plugin, String channel);
32+
33+
void unregisterIncomingPluginChannel(Plugin plugin);
34+
35+
Set<String> getOutgoingChannels();
36+
37+
Set<String> getOutgoingChannels(Plugin plugin);
38+
39+
Set<String> getIncomingChannels();
40+
41+
Set<String> getIncomingChannels(Plugin plugin);
42+
43+
Set<JavaCustomPayloadListenerRegistration> getIncomingChannelRegistrations(Plugin plugin);
44+
45+
Set<JavaCustomPayloadListenerRegistration> getIncomingChannelRegistrations(String channel);
46+
47+
Set<JavaCustomPayloadListenerRegistration> getIncomingChannelRegistrations(Plugin plugin, String channel);
48+
49+
boolean isRegistrationValid(JavaCustomPayloadListenerRegistration registration);
50+
51+
boolean isIncomingChannelRegistered(Plugin plugin, String channel);
52+
53+
boolean isOutgoingChannelRegistered(Plugin plugin, String channel);
54+
55+
boolean sendPluginMessage(Plugin source, SynapsePlayer player, String channel, byte[] payload);
56+
57+
void dispatchIncomingMessage(SynapsePlayer player, String channel, byte[] payload);
58+
59+
Set<String> getListeningChannels(SynapsePlayer player);
60+
61+
void unregisterPlayerChannels(SynapsePlayer player);
62+
}

0 commit comments

Comments
 (0)