forked from FabricMC/fabric
-
Notifications
You must be signed in to change notification settings - Fork 1
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
19 changed files
with
384 additions
and
47 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
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
9 changes: 9 additions & 0 deletions
9
...1/src/main/java/net/fabricmc/fabric/api/networking/v1/FabricCustomPayloadPacketCodec.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,9 @@ | ||
package net.fabricmc.fabric.api.networking.v1; | ||
|
||
import net.fabricmc.fabric.impl.networking.CustomPayloadTypeProvider; | ||
|
||
import net.minecraft.network.PacketByteBuf; | ||
|
||
public interface FabricCustomPayloadPacketCodec<B extends PacketByteBuf> { | ||
void fabric_setPacketCodecProvider(CustomPayloadTypeProvider<B> customPayloadTypeProvider); | ||
} |
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
36 changes: 36 additions & 0 deletions
36
...rking-api-v1/src/main/java/net/fabricmc/fabric/api/networking/v1/PayloadTypeRegistry.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,36 @@ | ||
package net.fabricmc.fabric.api.networking.v1; | ||
|
||
import net.fabricmc.fabric.impl.networking.PayloadTypeRegistryImpl; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import net.minecraft.network.NetworkSide; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.network.codec.PacketCodec; | ||
import net.minecraft.network.codec.RegistryByteBuf; | ||
import net.minecraft.network.packet.CustomPayload; | ||
import net.minecraft.util.Identifier; | ||
|
||
public interface PayloadTypeRegistry<B extends PacketByteBuf> { | ||
<T extends CustomPayload> CustomPayload.Type<B, T> register(CustomPayload.Id<T> id, PacketCodec<B, T> codec); | ||
|
||
@Nullable | ||
CustomPayload.Type<B, ? extends CustomPayload> get(Identifier id); | ||
|
||
@Nullable | ||
<T extends CustomPayload> CustomPayload.Type<B, T> get(CustomPayload.Id<T> id); | ||
|
||
static PayloadTypeRegistry<PacketByteBuf> configuration(NetworkSide side) { | ||
return switch (side) { | ||
case SERVERBOUND -> PayloadTypeRegistryImpl.CONFIGURATION_C2S; | ||
case CLIENTBOUND -> PayloadTypeRegistryImpl.CONFIGURATION_S2C; | ||
}; | ||
} | ||
|
||
static PayloadTypeRegistry<RegistryByteBuf> play(NetworkSide side) { | ||
return switch (side) { | ||
case SERVERBOUND -> PayloadTypeRegistryImpl.PLAY_C2S; | ||
case CLIENTBOUND -> PayloadTypeRegistryImpl.PLAY_S2C; | ||
}; | ||
} | ||
} |
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
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
9 changes: 9 additions & 0 deletions
9
...g-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/CustomPayloadTypeProvider.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,9 @@ | ||
package net.fabricmc.fabric.impl.networking; | ||
|
||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.network.packet.CustomPayload; | ||
import net.minecraft.util.Identifier; | ||
|
||
public interface CustomPayloadTypeProvider<B extends PacketByteBuf> { | ||
CustomPayload.Type<B, ? extends CustomPayload> get(B packetByteBuf, Identifier identifier); | ||
} |
62 changes: 62 additions & 0 deletions
62
...ing-api-v1/src/main/java/net/fabricmc/fabric/impl/networking/PayloadTypeRegistryImpl.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,62 @@ | ||
package net.fabricmc.fabric.impl.networking; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import net.minecraft.network.NetworkSide; | ||
import net.minecraft.network.NetworkState; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.network.codec.PacketCodec; | ||
import net.minecraft.network.codec.RegistryByteBuf; | ||
import net.minecraft.network.packet.CustomPayload; | ||
import net.minecraft.util.Identifier; | ||
|
||
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; | ||
|
||
public class PayloadTypeRegistryImpl<B extends PacketByteBuf> implements PayloadTypeRegistry<B> { | ||
public static PayloadTypeRegistry<PacketByteBuf> CONFIGURATION_C2S = new PayloadTypeRegistryImpl<>(NetworkState.CONFIGURATION, NetworkSide.SERVERBOUND); | ||
public static PayloadTypeRegistry<PacketByteBuf> CONFIGURATION_S2C = new PayloadTypeRegistryImpl<>(NetworkState.CONFIGURATION, NetworkSide.CLIENTBOUND); | ||
public static PayloadTypeRegistry<RegistryByteBuf> PLAY_C2S = new PayloadTypeRegistryImpl<>(NetworkState.PLAY, NetworkSide.SERVERBOUND); | ||
public static PayloadTypeRegistry<RegistryByteBuf> PLAY_S2C = new PayloadTypeRegistryImpl<>(NetworkState.PLAY, NetworkSide.CLIENTBOUND); | ||
|
||
private final Map<Identifier, CustomPayload.Type<B, ? extends CustomPayload>> packetTypes = new HashMap<>(); | ||
private final NetworkState state; | ||
private final NetworkSide side; | ||
|
||
private PayloadTypeRegistryImpl(NetworkState state, NetworkSide side) { | ||
this.state = state; | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public <T extends CustomPayload> CustomPayload.Type<B, T> register(CustomPayload.Id<T> id, PacketCodec<B, T> codec) { | ||
Objects.requireNonNull(id, "id"); | ||
Objects.requireNonNull(codec, "codec"); | ||
|
||
final var payloadType = new CustomPayload.Type<>(id, codec); | ||
|
||
if (packetTypes.containsKey(id.id())) { | ||
throw new IllegalArgumentException("Packet type " + id + " is already registered!"); | ||
} | ||
|
||
packetTypes.put(id.id(), payloadType); | ||
return payloadType; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public CustomPayload.Type<B, ? extends CustomPayload> get(Identifier id) { | ||
return packetTypes.get(id); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public <T extends CustomPayload> CustomPayload.Type<B, T> get(CustomPayload.Id<T> id) { | ||
//noinspection unchecked | ||
return (CustomPayload.Type<B, T>) packetTypes.get(id.id()); | ||
} | ||
} |
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
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
50 changes: 50 additions & 0 deletions
50
...-v1/src/main/java/net/fabricmc/fabric/mixin/networking/CustomPayloadPacketCodecMixin.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,50 @@ | ||
package net.fabricmc.fabric.mixin.networking; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.network.codec.PacketCodec; | ||
import net.minecraft.network.packet.CustomPayload; | ||
import net.minecraft.util.Identifier; | ||
|
||
import net.fabricmc.fabric.api.networking.v1.FabricCustomPayloadPacketCodec; | ||
import net.fabricmc.fabric.impl.networking.CustomPayloadTypeProvider; | ||
|
||
import org.spongepowered.asm.mixin.injection.Coerce; | ||
|
||
@Mixin(targets = "net/minecraft/network/packet/CustomPayload$1") | ||
public abstract class CustomPayloadPacketCodecMixin<B extends PacketByteBuf> implements PacketCodec<B, CustomPayload>, FabricCustomPayloadPacketCodec<B> { | ||
@Unique | ||
private CustomPayloadTypeProvider<B> customPayloadTypeProvider; | ||
|
||
@Override | ||
public void fabric_setPacketCodecProvider(CustomPayloadTypeProvider<B> customPayloadTypeProvider) { | ||
if (this.customPayloadTypeProvider != null) { | ||
throw new IllegalStateException("Payload codec provider is already set!"); | ||
} | ||
|
||
this.customPayloadTypeProvider = customPayloadTypeProvider; | ||
} | ||
|
||
@WrapOperation(method = { | ||
"encode(Lnet/minecraft/network/PacketByteBuf;Lnet/minecraft/network/packet/CustomPayload$Id;Lnet/minecraft/network/packet/CustomPayload;)V", | ||
"decode(Lnet/minecraft/network/PacketByteBuf;)Lnet/minecraft/network/packet/CustomPayload;" | ||
}, at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/CustomPayload$1;getCodec(Lnet/minecraft/util/Identifier;)Lnet/minecraft/network/codec/PacketCodec;")) | ||
private PacketCodec<B, ? extends CustomPayload> wrapGetCodec(@Coerce PacketCodec<B, CustomPayload> instance, Identifier identifier, Operation<PacketCodec<B, CustomPayload>> original, B packetByteBuf) { | ||
if (customPayloadTypeProvider == null) { | ||
throw new IllegalStateException("Payload codec provider is not set!"); | ||
} | ||
|
||
CustomPayload.Type<B, ? extends CustomPayload> payloadType = customPayloadTypeProvider.get(packetByteBuf, identifier); | ||
|
||
if (payloadType != null) { | ||
return payloadType.codec(); | ||
} | ||
|
||
return original.call(instance, identifier); | ||
} | ||
} |
Oops, something went wrong.