Sending data from the Spigot server to the Fabric 1.21 client #4430
Replies: 4 comments 1 reply
|
Do you think you could provide a bit more information about your server? BungeeCord (and forks) can redirect plugin messages through |
|
I too am facing the same issue, just want to check if you found any work around. |
|
At the time of my previous comment I was also actually running into this same issue, but I have developed a temporary solution until Fabric fixes this bug. I had to manually mixin Step 1: Mixin /**
* @author JoshieGemFinder
* @reason Aggressively hook our packet codec into the clientbound codec to receive packets from bukkit/velocity plugins
*/
@SuppressWarnings("rawtypes") // only have to worry about RegistryFriendlyByteBuf/FriendlyByteBuf here
@ModifyArg(
method = "<clinit>",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/network/protocol/common/custom/CustomPacketPayload;codec(Lnet/minecraft/network/protocol/common/custom/CustomPacketPayload$FallbackProvider;Ljava/util/List;)Lnet/minecraft/network/codec/StreamCodec;"
//, ordinal = 0 /* for targeting only the PLAY phase */
//, ordinal = 1 /* for targeting only the CONFIGURATION phase */
// No ordinal means it'll target both PLAY and CONFIGURATION
),
index = 0
)
private static FallbackProvider replaceFallbackProvider(FallbackProvider original) {
return (resourceLocation) -> {
// YourPacket.CHANNEL is a non-null ResourceLocation of your packet id
if(YourPacket.CHANNEL.equals(resourceLocation)) {
// YourPacket.CODEC should be a StreamCodec<FriendlyByteBuf, YourPacket>
return YourPacket.CODEC;
} else {
return original.create(resourceLocation);
}
};
}Alternatively, you could mixin inject the Step 2: Now the packet is being decoded we have to also add an inject to actually handle it, because in the current state fabric will just discard it. @Shadow
public CustomPacketPayload payload() { return null; }
/**
* @author JoshieGemFinder
* @reason Handle our packet early, because fabric will discard it it's from a bukkit/velocity plugin
*/
@Inject(method = "handle(Lnet/minecraft/network/protocol/common/ClientCommonPacketListener;)V", at = @At("HEAD"), cancellable = true)
private void handleCustomPayload(ClientCommonPacketListener clientCommonPacketListener, CallbackInfo ci) {
if(this.payload() instanceof YourPacket payload) {
// any type of method to handle your custom packet payload, I'm assuming you probably have one of these already
YourPacketHandler.handleYourPacket(clientCommonPacketListener, payload);
ci.cancel();
}
}Finally, putting it all together: package your.package.for.mixins;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.network.protocol.common.ClientCommonPacketListener;
import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload.FallbackProvider;
/**
* @author JoshieGemFinder
* @reason Due to issues with fabric api, we have to aggressively hook our packet in to receive data from bukkit/velocity plugins
*/
@Mixin(ClientboundCustomPayloadPacket.class)
public class ClientboundCustomPayloadPacketMixin {
/**
* @author JoshieGemFinder
* @reason Aggressively hook our packet codec into the clientbound codec to receive packets from bukkit/velocity plugins
*/
@SuppressWarnings("rawtypes") // only have to worry about RegistryFriendlyByteBuf/FriendlyByteBuf here
@ModifyArg(
method = "<clinit>",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/network/protocol/common/custom/CustomPacketPayload;codec(Lnet/minecraft/network/protocol/common/custom/CustomPacketPayload$FallbackProvider;Ljava/util/List;)Lnet/minecraft/network/codec/StreamCodec;"
//, ordinal = 0 /* for targeting only the PLAY phase */
//, ordinal = 1 /* for targeting only the CONFIGURATION phase */
// No ordinal means it'll target both PLAY and CONFIGURATION
),
index = 0
)
private static FallbackProvider replaceFallbackProvider(FallbackProvider original) {
return (resourceLocation) -> {
// YourPacket.CHANNEL is a non-null ResourceLocation of your packet id
if(YourPacket.CHANNEL.equals(resourceLocation)) {
// YourPacket.CODEC should be a StreamCodec<FriendlyByteBuf, YourPacket>
return YourPacket.CODEC;
} else {
return original.create(resourceLocation);
}
};
}
@Shadow
public CustomPacketPayload payload() { return null; }
/**
* @author JoshieGemFinder
* @reason Handle our packet early, because fabric will discard it it's from a bukkit/velocity plugin
*/
@Inject(method = "handle(Lnet/minecraft/network/protocol/common/ClientCommonPacketListener;)V", at = @At("HEAD"), cancellable = true)
private void handleCustomPayload(ClientCommonPacketListener clientCommonPacketListener, CallbackInfo ci) {
if(this.payload() instanceof YourPacket payload) {
// any type of method to handle your custom packet payload, I'm assuming you probably have one of these already
YourPacketHandler.handleYourPacket(clientCommonPacketListener, payload);
ci.cancel();
}
}
} |
|
I a relatively new to the fabric networking stuff, but I wanted to check if I am doing something wrong here, as I still am able to send the data but not receive. And the plugin is I don't know what I am doing wrong here. |
Uh oh!
There was an error while loading. Please reload this page.
Description
Hi! I'm new to this topic. I want to make a connection between the server on Spigot and the Fabric client mod (API 1.21). I tried to follow the information from the wiki, but in my case, only the transfer from the client to the server works, but the data does not arrive from the server to the client. Does anyone know what the problem is?
Fabric (client):
Spigot:
All reactions