mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +00:00
refactor: fix plugin messaging on Fabric
This commit is contained in:
7 files changed
+43
-49
No files matched your search
@@ -27,6 +27,7 @@ import lombok.Setter;
|
||||
import net.fabricmc.api.DedicatedServerModInitializer;
|
||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.fabricmc.loader.api.ModContainer;
|
||||
@@ -321,7 +322,9 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
|
||||
|
||||
@Override
|
||||
public void initializePluginChannels() {
|
||||
ServerPlayNetworking.registerGlobalReceiver(FabricPluginMessage.BUNGEE_CHANNEL_ID, this);
|
||||
PayloadTypeRegistry.playC2S().register(FabricPluginMessage.CHANNEL_ID, FabricPluginMessage.CODEC);
|
||||
PayloadTypeRegistry.playS2C().register(FabricPluginMessage.CHANNEL_ID, FabricPluginMessage.CODEC);
|
||||
ServerPlayNetworking.registerGlobalReceiver(FabricPluginMessage.CHANNEL_ID, this);
|
||||
}
|
||||
|
||||
// When the server receives a plugin message
|
||||
|
||||
@@ -20,27 +20,44 @@
|
||||
package net.william278.huskhomes.network;
|
||||
|
||||
import lombok.*;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.network.RegistryByteBuf;
|
||||
import net.minecraft.network.codec.PacketCodec;
|
||||
import net.minecraft.network.packet.CustomPayload;
|
||||
import net.minecraft.util.Identifier;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class FabricPluginMessage implements CustomPayload {
|
||||
|
||||
public static final Id<FabricPluginMessage> BUNGEE_CHANNEL_ID = new Id<>(
|
||||
public static final PacketCodec<RegistryByteBuf, FabricPluginMessage> CODEC = PacketCodec.of(
|
||||
(value, buf) -> writeBytes(buf, value.getData()),
|
||||
FabricPluginMessage::new
|
||||
);
|
||||
public static final Id<FabricPluginMessage> CHANNEL_ID = new Id<>(
|
||||
Identifier.of("bungeecord", "main")
|
||||
);
|
||||
|
||||
private Id<FabricPluginMessage> id;
|
||||
private byte[] data;
|
||||
|
||||
@NotNull
|
||||
public static FabricPluginMessage of(@NotNull String id, byte[] data) {
|
||||
return new FabricPluginMessage(
|
||||
id.equals("BungeeCord") ? BUNGEE_CHANNEL_ID : new Id<>(Identifier.tryParse(id)), data
|
||||
);
|
||||
private FabricPluginMessage(@NotNull PacketByteBuf buf) {
|
||||
this(getWrittenBytes(buf));
|
||||
}
|
||||
|
||||
private static byte[] getWrittenBytes(@NotNull PacketByteBuf buf) {
|
||||
byte[] bs = new byte[buf.readableBytes()];
|
||||
buf.readBytes(bs);
|
||||
return bs;
|
||||
}
|
||||
|
||||
private static void writeBytes(@NotNull PacketByteBuf buf, byte[] v) {
|
||||
buf.writeBytes(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Id<FabricPluginMessage> getId() {
|
||||
return CHANNEL_ID;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -161,8 +161,8 @@ public class FabricUser extends OnlineUser {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPluginMessage(@NotNull String channel, byte[] message) {
|
||||
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(FabricPluginMessage.of(channel, message)));
|
||||
public void sendPluginMessage(byte[] message) {
|
||||
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(new FabricPluginMessage(message)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user