Skip to content

Commit

Permalink
feat: Added Minestom support
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Oct 18, 2023
1 parent c1e7478 commit cd41f4d
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 0 deletions.
29 changes: 29 additions & 0 deletions backend/minestom/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugins {
alias(libs.plugins.shadow)
}

dependencies {
compileOnly(libs.minestom)
implementation(projects.signedvelocityBackendCommon)
}

tasks {
build {
dependsOn(shadowJar)
}
shadowJar {
archiveBaseName.set("${rootProject.name}-Minestom")
archiveClassifier.set("")
doLast {
copy {
from(archiveFile)
into("${rootProject.projectDir}/build")
}
}
}
processResources {
filesMatching("extension.json") {
expand("version" to project.version)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github._4drian3d.signedvelocity.minestom;

import io.github._4drian3d.signedvelocity.common.SignedQueue;
import io.github._4drian3d.signedvelocity.minestom.listener.PlayerChatListener;
import io.github._4drian3d.signedvelocity.minestom.listener.PlayerCommandListener;
import io.github._4drian3d.signedvelocity.minestom.listener.PluginMessageListener;
import net.minestom.server.event.player.PlayerChatEvent;
import net.minestom.server.event.player.PlayerCommandEvent;
import net.minestom.server.event.player.PlayerDisconnectEvent;
import net.minestom.server.event.player.PlayerPluginMessageEvent;
import net.minestom.server.extensions.Extension;

public final class SignedVelocity extends Extension {
public static final String CHANNEL = "signedvelocity:main";
private final SignedQueue chatQueue = new SignedQueue();
private final SignedQueue commandQueue = new SignedQueue();

@Override
public void initialize() {
getEventNode().addListener(PlayerChatEvent.class, new PlayerChatListener(this));
getEventNode().addListener(PlayerCommandEvent.class, new PlayerCommandListener(this));
getEventNode().addListener(PlayerPluginMessageEvent.class, new PluginMessageListener(this));
getEventNode().addListener(PlayerDisconnectEvent.class, event -> {
final var uuid = event.getPlayer().getUuid();
this.commandQueue.removeData(uuid);
this.chatQueue.removeData(uuid);
});
}

@Override
public void terminate() {
}

public SignedQueue chatQueue() {
return chatQueue;
}

public SignedQueue commandQueue() {
return commandQueue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github._4drian3d.signedvelocity.minestom.listener;

import io.github._4drian3d.signedvelocity.common.SignedQueue;
import io.github._4drian3d.signedvelocity.minestom.SignedVelocity;
import net.minestom.server.entity.Player;
import net.minestom.server.event.player.PlayerChatEvent;

import java.util.function.Consumer;

public final class PlayerChatListener implements Consumer<PlayerChatEvent> {
private final SignedQueue chatQueue;

public PlayerChatListener(final SignedVelocity extension) {
this.chatQueue = extension.chatQueue();
}

@Override
public void accept(final PlayerChatEvent event) {
final Player player = event.getPlayer();
this.chatQueue.dataFrom(player.getUuid())
.nextResult()
.thenAccept(result -> {
if (result.cancelled()) {
event.setCancelled(true);
} else {
final String modifiedChat = result.toModify();
if (modifiedChat != null) {
event.setMessage(modifiedChat);
}
}
}).join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github._4drian3d.signedvelocity.minestom.listener;

import io.github._4drian3d.signedvelocity.common.SignedQueue;
import io.github._4drian3d.signedvelocity.minestom.SignedVelocity;
import net.minestom.server.entity.Player;
import net.minestom.server.event.player.PlayerCommandEvent;

import java.util.function.Consumer;

public final class PlayerCommandListener implements Consumer<PlayerCommandEvent> {
private final SignedQueue commandQueue;

public PlayerCommandListener(final SignedVelocity extension) {
this.commandQueue = extension.commandQueue();
}

@Override
public void accept(final PlayerCommandEvent event) {
final Player player = event.getPlayer();
this.commandQueue.dataFrom(player.getUuid())
.nextResult()
.thenAccept(result -> {
if (result.cancelled()) {
event.setCancelled(true);
} else {
final String modified = result.toModify();
if (modified != null) {
event.setCommand(modified);
}
}
}).join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.github._4drian3d.signedvelocity.minestom.listener;

import io.github._4drian3d.signedvelocity.common.SignedQueue;
import io.github._4drian3d.signedvelocity.common.SignedResult;
import io.github._4drian3d.signedvelocity.minestom.SignedVelocity;
import net.minestom.server.event.player.PlayerPluginMessageEvent;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Consumer;

public final class PluginMessageListener implements Consumer<PlayerPluginMessageEvent> {
private final SignedVelocity extension;

public PluginMessageListener(final SignedVelocity extension) {
this.extension = extension;
}

@Override
public void accept(PlayerPluginMessageEvent event) {
if (!Objects.equals(event.getIdentifier(), SignedVelocity.CHANNEL)) {
return;
}
@SuppressWarnings("UnstableApiUsage")
final DataInput input = new DataInput(event.getMessage());

final UUID playerId = UUID.fromString(input.readUTF());
final String source = input.readUTF();
final String result = input.readUTF();

final SignedQueue queue = switch (source) {
case "COMMAND_RESULT" -> extension.commandQueue();
case "CHAT_RESULT" -> extension.chatQueue();
default -> throw new IllegalArgumentException("Invalid source " + source);
};
final SignedResult resulted = switch (result) {
case "CANCEL" -> SignedResult.cancel();
case "MODIFY" -> SignedResult.modify(input.readUTF());
case "ALLOWED" -> SignedResult.allowed();
default -> throw new IllegalArgumentException("Invalid result " + result);
};
queue.dataFrom(playerId).complete(resulted);
}

private record DataInput(DataInputStream dataStream) {
DataInput(final byte[] data) {
this(new DataInputStream(new ByteArrayInputStream(data)));
}

public String readUTF() {
try {
return dataStream.readUTF();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
6 changes: 6 additions & 0 deletions backend/minestom/src/main/resources/extension.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"entrypoint": "io.github._4drian3d.signedvelocity.minestom.SignedVelocity",
"name": "SignedVelocity",
"version": "${version}",
"externalDependencies": {}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ metadata.format.version = "1.1"
# CompileOnly dependencies
velocity = "3.2.0-SNAPSHOT"
paper = "1.20.1-R0.1-SNAPSHOT"
minestom = "067227421f"

# Gradle Plugins
blossom = "2.1.0"
Expand All @@ -19,6 +20,7 @@ bstats = "3.0.2"
velocity-api = { group = "com.velocitypowered", name = "velocity-api", version.ref = "velocity" }
velocity-proxy = { group = "com.velocitypowered", name = "velocity-proxy", version.ref = "velocity" }
paper = { group = "io.papermc.paper", name = "paper-api", version.ref = "paper" }
minestom = { module = "com.github.Minestom:Minestom", version.ref = "minestom" }

bstats = { module = "org.bstats:bstats-velocity", version.ref = "bstats" }

Expand Down
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencyResolutionManagement {
maven("https://papermc.io/repo/repository/maven-public/")
maven("https://repo.codemc.io/repository/maven-releases/")
maven("https://mvn.exceptionflug.de/repository/exceptionflug-public/")
maven("https://jitpack.io")
}
}

Expand All @@ -22,6 +23,8 @@ include("signedvelocity-backend-common")
project(":signedvelocity-backend-common").projectDir = file("backend/common")
include("signedvelocity-paper")
project(":signedvelocity-paper").projectDir = file("backend/paper")
include("signedvelocity-minestom")
project(":signedvelocity-minestom").projectDir = file("backend/minestom")

include("signedvelocity-sponge-common")
project(":signedvelocity-sponge-common").projectDir = file("backend/sponge/common")
Expand Down

0 comments on commit cd41f4d

Please sign in to comment.