2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2026-04-25 16:00:27 +00:00

unfinished new plugin message system

This commit is contained in:
2024-05-16 02:50:50 +04:00
parent 131d89129f
commit 81ae05b6a4
17 changed files with 339 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
plugins {
`java-library`
`maven-publish`
}
dependencies {
compileOnly(libs.guava)
}
tasks {
compileJava {
options.encoding = Charsets.UTF_8.name()
options.release.set(8) // use java 8 for shit servers that still stuck on 1.8
}
javadoc {
options.encoding = Charsets.UTF_8.name()
}
processResources {
filteringCharset = Charsets.UTF_8.name()
}
}
publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
}
}
}

View File

@@ -0,0 +1,31 @@
package net.limework.pluginmessageapi.protocol;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
public class MessageEncoderDecoder {
private final PluginMessageRegistry registry;
public MessageEncoderDecoder(PluginMessageRegistry registry) {
this.registry = registry;
}
public byte[] encode(PluginMessage pluginMessage) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
// write packet id
out.write(registry.idByMessage(pluginMessage.getClass()));
pluginMessage.encode(out);
return out.toByteArray();
}
public PluginMessage decode(byte[] data) {
ByteArrayDataInput in = ByteStreams.newDataInput(data);
int messageId = in.readInt();
PluginMessage pluginMessage = registry.messageById(messageId);
pluginMessage.decode(in);
return pluginMessage;
}
}

View File

@@ -0,0 +1,16 @@
package net.limework.pluginmessageapi.protocol;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
public abstract class PluginMessage {
public abstract void decode(ByteArrayDataInput in);
public abstract void encode(ByteArrayDataOutput out);
public abstract void handle(PluginMessageHandler handler);
}

View File

@@ -0,0 +1,9 @@
package net.limework.pluginmessageapi.protocol;
public interface PluginMessageHandler {
}

View File

@@ -0,0 +1,34 @@
package net.limework.pluginmessageapi.protocol;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
public class PluginMessageRegistry {
private final Map<Integer, Supplier<PluginMessage>> messageById = new HashMap<>();
private final Map<Class<? extends PluginMessage>, Integer> idByMessage = new HashMap<>();
public void register(int id, Class<? extends PluginMessage> clazz, Supplier<PluginMessage> messageSupplier) {
messageById.put(id, messageSupplier);
idByMessage.put(clazz, id);
}
public PluginMessage messageById(int id) {
if (!messageById.containsKey(id)) {
throw new IllegalArgumentException("plugin Message id " + id + " does not exists in the registry");
}
return messageById.get(id).get();
}
public int idByMessage(Class<? extends PluginMessage> clazz) {
if (!idByMessage.containsKey(clazz)) {
throw new IllegalArgumentException("plugin Message clazz " + clazz + " does not exists in the registry");
}
return idByMessage.get(clazz);
}
}

View File

@@ -0,0 +1,41 @@
package net.limework.pluginmessageapi.protocol.messages;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import net.limework.pluginmessageapi.protocol.PluginMessageHandler;
import net.limework.pluginmessageapi.protocol.PluginMessage;
// example plugin message class
public class HelloMessage extends PluginMessage {
private String helloWorld;
private static final String DEFAULT_MESSAGE = "hello from Limework!";
public HelloMessage(String helloWorld) {
this.helloWorld = helloWorld != null ? helloWorld : DEFAULT_MESSAGE;
}
@Override
public void decode(ByteArrayDataInput in) {
this.helloWorld = in.readUTF();
}
@Override
public void encode(ByteArrayDataOutput out) {
out.writeUTF(this.helloWorld);
}
@Override
public void handle(PluginMessageHandler messageHandler) {
// messageHandler.handle(this);
}
public void setHelloWorldMessage(String helloWorld) {
this.helloWorld = helloWorld != null ? helloWorld : DEFAULT_MESSAGE;
}
public String helloWorld() {
return helloWorld;
}
}