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:
30
plugin-message-api/protocol/build.gradle.kts
Normal file
30
plugin-message-api/protocol/build.gradle.kts
Normal 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"])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package net.limework.pluginmessageapi.protocol;
|
||||
|
||||
public interface PluginMessageHandler {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user