2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-22 12:18:01 +00:00

unfinished new plugin message system

This commit is contained in:
mohammed jasem alaajel 2024-05-16 02:50:50 +04:00
parent 30540bc15d
commit 2f48aa732f
Signed by: ham1255
GPG Key ID: EF343502046229F4
17 changed files with 339 additions and 0 deletions

View File

@ -0,0 +1,7 @@
Copyright 2024 <COPYRIGHT HOLDER>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,7 @@
Copyright (c) 2013-present RedisBungee contributors
All rights reserved. This program and the accompanying materials
are made available under the terms of the MIT License
which accompanies this api, and is available at
https://opensource.org/license/mit

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;
}
}

View File

@ -0,0 +1,31 @@
plugins {
`java-library`
`maven-publish`
}
dependencies {
compileOnly(project(":Limework-Plugin-Message-API-Protocol"))
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,24 @@
package net.limework.valiobungee.pluginmessage.models;
public class Proxy {
private String proxyId;
private int players;
public Proxy() {
}
public Proxy(String proxyId, int players) {
this.proxyId = proxyId;
this.players = players;
}
public String proxyId() {
return proxyId;
}
public int players() {
return players;
}
}

View File

@ -0,0 +1,11 @@
package net.limework.valiobungee.pluginmessage.protocol;
import net.limework.pluginmessageapi.protocol.PluginMessageHandler;
import net.limework.valiobungee.pluginmessage.protocol.messages.proxybound.RequestProxiesListMessage;
public class ProxyboundHandler implements PluginMessageHandler {
public void handle(RequestProxiesListMessage message) {
}
}

View File

@ -0,0 +1,13 @@
package net.limework.valiobungee.pluginmessage.protocol;
import net.limework.pluginmessageapi.protocol.PluginMessageHandler;
import net.limework.valiobungee.pluginmessage.protocol.messages.servebound.ProxiesListMessage;
public class ServerboundHandler implements PluginMessageHandler {
public void handle(ProxiesListMessage message) {
}
}

View File

@ -0,0 +1,24 @@
package net.limework.valiobungee.pluginmessage.protocol.messages.proxybound;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import net.limework.pluginmessageapi.protocol.PluginMessage;
public abstract class RequestMessage extends PluginMessage {
public RequestMessage() {
}
@Override
public void encode(ByteArrayDataOutput out) {
// do nothing
}
@Override
public void decode(ByteArrayDataInput in) {
// do nothing
}
}

View File

@ -0,0 +1,11 @@
package net.limework.valiobungee.pluginmessage.protocol.messages.proxybound;
import net.limework.pluginmessageapi.protocol.PluginMessageHandler;
import net.limework.valiobungee.pluginmessage.protocol.ProxyboundHandler;
public class RequestProxiesListMessage extends RequestMessage{
@Override
public void handle(PluginMessageHandler handler) {
((ProxyboundHandler) handler).handle(this);
}
}

View File

@ -0,0 +1,46 @@
package net.limework.valiobungee.pluginmessage.protocol.messages.servebound;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import net.limework.pluginmessageapi.protocol.PluginMessage;
import net.limework.pluginmessageapi.protocol.PluginMessageHandler;
import net.limework.valiobungee.pluginmessage.models.Proxy;
import net.limework.valiobungee.pluginmessage.protocol.ServerboundHandler;
public class ProxiesListMessage extends PluginMessage {
private Proxy[] proxies;
public ProxiesListMessage(Proxy[] proxies) {
this.proxies = proxies;
}
public Proxy[] proxies() {
return proxies;
}
@Override
public void decode(ByteArrayDataInput in) {
int proxiesNumber = in.readInt();
this.proxies = new Proxy[proxiesNumber];
for (int i = 0; i < proxiesNumber; i++) {
String id = in.readUTF();
int players = in.readInt();
proxies[i] = new Proxy(id, players);
}
}
@Override
public void encode(ByteArrayDataOutput out) {
out.writeInt(proxies.length);
for (Proxy proxy : proxies) {
out.writeUTF(proxy.proxyId());
out.writeInt(proxy.players());
}
}
@Override
public void handle(PluginMessageHandler handler) {
((ServerboundHandler) handler).handle(this);
}
}

View File

@ -27,6 +27,10 @@ project(":RedisBungee-Velocity").projectDir = file("proxies/velocity/velocity-ap
include(":RedisBungee-Proxy-Velocity")
project(":RedisBungee-Proxy-Velocity").projectDir = file("proxies/velocity")
include(":Limework-Plugin-Message-API-Protocol")
project(":Limework-Plugin-Message-API-Protocol").projectDir = file("plugin-message-api/protocol")
include(":RedisBungee-Plugin-Message-API")
project(":RedisBungee-Plugin-Message-API").projectDir = file("plugin-message-api/redisbungee")