Skip to content

Commit

Permalink
Completely redesigned the plugin's network concept and added support …
Browse files Browse the repository at this point in the history
…for 1.21.4.
  • Loading branch information
brunoxkk0 committed Feb 22, 2025
1 parent efbacf2 commit 0df6a48
Show file tree
Hide file tree
Showing 11 changed files with 835 additions and 133 deletions.
44 changes: 25 additions & 19 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
plugins {
id 'java'
id 'com.github.johnrengelman.shadow' version '8.1.1'

id "com.gradleup.shadow" version "9.0.0-beta8"
}

group = 'io.github.cruciblemc'
version = '1.0.0'
version = '1.0.1'

repositories {
mavenCentral()
Expand All @@ -21,35 +20,42 @@ repositories {
name = "codemc"
url = "https://repo.codemc.org/repository/maven-public/"
}
maven {
url = uri("https://repo.codemc.io/repository/nms/")
}

maven {
url = uri("https://hub.spigotmc.org/nexus/content/groups/public/")
}

maven {
url = uri("https://libraries.minecraft.net/")
metadataSources {
mavenPom()
artifact()
ignoreGradleMetadataRedirection()
}
}
}


dependencies {

annotationProcessor 'com.github.bsideup.jabel:jabel-javac-plugin:0.4.2'
compileOnly 'com.github.bsideup.jabel:jabel-javac-plugin:0.4.2'
compileOnly 'org.projectlombok:lombok:1.18.36'
annotationProcessor 'org.projectlombok:lombok:1.18.36'

annotationProcessor 'org.projectlombok:lombok:1.18.28'
compileOnly 'org.projectlombok:lombok:1.18.28'
compileOnly "org.spigotmc:spigot-api:1.21.4-R0.1-SNAPSHOT"
compileOnly "org.spigotmc:spigot:1.21.4-R0.1-SNAPSHOT:remapped-mojang"
compileOnly("io.netty:netty-all:4.0.23.Final")

compileOnly "org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT"
compileOnly "org.jetbrains:annotations:23.0.0"

implementation "de.tr7zw:item-nbt-api:2.11.3"
implementation "de.tr7zw:item-nbt-api:2.14.1"

}

configure([tasks.compileJava]) {
sourceCompatibility = 16 // for the IDE support
options.release = 8
options.encoding = 'UTF-8'

javaCompiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(16)
}
}

compileJava.options.encoding = 'UTF-8'

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Aug 14 11:23:44 BRT 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
30 changes: 16 additions & 14 deletions src/main/java/io/github/cruciblemc/vitatempus/VitaTempus.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,39 @@

import io.github.cruciblemc.vitatempus.core.BukkitPacketDeliver;
import io.github.cruciblemc.vitatempus.necrotempus.NecroTempus;
import lombok.Getter;
import org.bukkit.plugin.java.JavaPlugin;

public final class VitaTempus extends JavaPlugin {

private static final String NecroTempusChannel = "necrotempus:main";
public static final String NecroTempusChannel = "necrotempus:main";

@Getter
private BukkitPacketDeliver necroTempusPacketDeliver;

private static VitaTempus instance;
@Getter
private VitaTempusInterceptor interceptor;

public static VitaTempus getInstance() {
return instance;
}
@Getter
private static VitaTempus instance;

public BukkitPacketDeliver getNecroTempusPacketDeliver() {
return necroTempusPacketDeliver;
}

@Override
public void onEnable() {

instance = this;
necroTempusPacketDeliver = BukkitPacketDeliver.register(this, NecroTempusChannel);
NecroTempus.getInstance().onInit(this, NecroTempusChannel);

this.necroTempusPacketDeliver = new BukkitPacketDeliver(this, NecroTempusChannel);

this.interceptor = new VitaTempusInterceptor(this);

NecroTempus.getInstance();

}

@Override
public void onDisable() {
necroTempusPacketDeliver.onDisable();
NecroTempus.getInstance().onDisable(this, NecroTempusChannel);

interceptor.close();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.github.cruciblemc.vitatempus;

import de.tr7zw.changeme.nbtapi.NBT;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
import io.github.cruciblemc.vitatempus.ext.com.fren_gor.lightInjector.LightInjector;
import io.github.cruciblemc.vitatempus.necrotempus.NecroTempus;
import io.netty.channel.Channel;
import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
import net.minecraft.network.protocol.common.custom.DiscardedPayload;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;

public class VitaTempusInterceptor extends LightInjector {

/**
* Initializes the injector and starts to listen to packets.
* <p>
* Note that, while it is possible to create more than one instance per plugin,
* it is more efficient and recommended to just have only one.
*
* @param plugin The {@link Plugin} which is instantiating this injector.
* @throws NullPointerException If the provided {@code plugin} is {@code null}.
* @throws IllegalStateException When <b>not</b> called from the main thread.
* @throws IllegalArgumentException If the provided {@code plugin} is not enabled.
*/
public VitaTempusInterceptor(@NotNull Plugin plugin) {
super(plugin);
}

@Override
protected @NotNull Object onPacketReceiveAsync(@Nullable Player sender, @NotNull Channel channel, @NotNull Object packet) {

if (packet instanceof ServerboundCustomPayloadPacket(
net.minecraft.network.protocol.common.custom.CustomPacketPayload payload
) && sender != null) {

DiscardedPayload discardedPayload = (DiscardedPayload) payload;

String id = discardedPayload.id().toString();

if (!id.equals(VitaTempus.NecroTempusChannel))
return packet;

var buffer = discardedPayload.data();
byte[] message;

message = new byte[buffer.readableBytes()];
buffer.duplicate().readBytes(message);

ByteBuffer byteBuffer = ByteBuffer.wrap(message);

short size = byteBuffer.getShort(1);
byte[] data = Arrays.copyOfRange(message, 3, size);

ReadWriteNBT nbtContainer = NBT.readNBT(new ByteArrayInputStream(data));

if (nbtContainer.hasTag("version"))
NecroTempus.getInstance().setVersion(sender, nbtContainer.getString("version"));

}

return packet;
}

@Override
protected @NotNull Object onPacketSendAsync(@Nullable Player receiver, @NotNull Channel channel, @NotNull Object packet) {
return packet;
}

}
Original file line number Diff line number Diff line change
@@ -1,60 +1,32 @@
package io.github.cruciblemc.vitatempus.core;

import io.github.cruciblemc.vitatempus.VitaTempus;
import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

public class BukkitPacketDeliver {

private final Plugin plugin;
private final VitaTempus plugin;
private final String channel;

private BukkitPacketDeliver(Plugin plugin, String channel){
public BukkitPacketDeliver(VitaTempus plugin, String channel) {
this.plugin = plugin;
this.channel = channel;
onInit();
}

public static BukkitPacketDeliver register(Plugin plugin, String channel){
return new BukkitPacketDeliver(plugin, channel);
public void deliverTo(Player player, MessagePacket messagePacket) {
Bukkit.getScheduler().runTask(plugin, () -> plugin.getInterceptor().sendPacket(
player,
new ClientboundCustomPayloadPacket(
MessagePacketEncoder.asDiscardedPayload(channel, messagePacket))
));
}

public void deliverTo(Player player, EncodedMessage encodedMessage){
Bukkit.getScheduler().runTask(plugin, () -> {
player.sendPluginMessage(plugin, channel, encodedMessage.getTransformedData());
});
}

public void broadcast(EncodedMessage encodedMessage){
for(Player player : Bukkit.getOnlinePlayers()){
deliverTo(player, encodedMessage);
public void broadcast(MessagePacket messagePacket) {
for (Player player : Bukkit.getOnlinePlayers()) {
deliverTo(player, messagePacket);
}
}

public void deliverTo(Player player, MessagePacket messagePacket){
EncodedMessage encodedMessage = MessagePacketEncoder.encode(messagePacket);

Bukkit.getScheduler().runTask(plugin, () -> {
player.sendPluginMessage(plugin, channel, encodedMessage.getTransformedData());
});
}

public void broadcast(MessagePacket messagePacket){

EncodedMessage encodedMessage = MessagePacketEncoder.encode(messagePacket);

for(Player player : Bukkit.getOnlinePlayers()){
deliverTo(player, encodedMessage);
}
}

public void onInit(){
Bukkit.getServer().getMessenger().registerOutgoingPluginChannel(plugin, channel);
}

public void onDisable(){
Bukkit.getServer().getMessenger().unregisterOutgoingPluginChannel(plugin, channel);
}


}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package io.github.cruciblemc.vitatempus.core;

import io.netty.buffer.Unpooled;
import net.minecraft.network.protocol.common.custom.DiscardedPayload;
import net.minecraft.resources.ResourceLocation;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;

public class MessagePacketEncoder {

public static EncodedMessage encode(MessagePacket messagePacket){
public static ByteBuffer encodeBuffer(MessagePacket messagePacket) {

byte[] data = transform(messagePacket);

Expand All @@ -15,15 +19,19 @@ public static EncodedMessage encode(MessagePacket messagePacket){
byteBuffer.putShort((short) data.length);
byteBuffer.put(data);

return new EncodedMessage(byteBuffer.array());
return byteBuffer;
}

private static byte[] transform(MessagePacket messagePacket){
private static byte[] transform(MessagePacket messagePacket) {

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
messagePacket.writePacket(byteArrayOutputStream);

return byteArrayOutputStream.toByteArray();
}

}
public static DiscardedPayload asDiscardedPayload(String channel, MessagePacket packet) {
return new DiscardedPayload(ResourceLocation.parse(channel), Unpooled.wrappedBuffer(encodeBuffer(packet).flip()));
}

}
Loading

0 comments on commit 0df6a48

Please sign in to comment.