generated from 4drian3d/VelocityPluginTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...nd/minestom/src/main/java/io/github/_4drian3d/signedvelocity/minestom/SignedVelocity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...rc/main/java/io/github/_4drian3d/signedvelocity/minestom/listener/PlayerChatListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...main/java/io/github/_4drian3d/signedvelocity/minestom/listener/PlayerCommandListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...main/java/io/github/_4drian3d/signedvelocity/minestom/listener/PluginMessageListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters