Skip to content

Commit

Permalink
target 24w03a
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Jan 18, 2024
1 parent b38d84f commit c4dda20
Show file tree
Hide file tree
Showing 23 changed files with 236 additions and 251 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
org.gradle.jvmargs = -Xmx1G

minecraft = 1.20.4
minecraft = 24w03a

archiveBaseName = badpackets
group = lol.bai
majorVersion = 0

fabricLoader = 0.15.3
fabricLoader = 0.15.6
fabricApi = 0.93.1+1.20.4
forge = 49.0.19
neo = 20.4.96-beta
Expand Down
2 changes: 1 addition & 1 deletion platform/fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
mappings(loom.officialMojangMappings())

modImplementation("net.fabricmc:fabric-loader:${rootProp["fabricLoader"]}")
modImplementation("net.fabricmc.fabric-api:fabric-api:${rootProp["fabricApi"]}")
// modImplementation("net.fabricmc.fabric-api:fabric-api:${rootProp["fabricApi"]}")
}

loom {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
package lol.bai.badpackets.impl.fabric;

import lol.bai.badpackets.impl.Constants;
import lol.bai.badpackets.impl.platform.PlatformProxy;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.Packet;

public class FabricProxy extends PlatformProxy {

private static final boolean HAS_FABRIC_API = FabricLoader.getInstance().isModLoaded("fabric-networking-api-v1");

@Override
public Packet<?> createVanillaRegisterConfigS2CPacket(FriendlyByteBuf buf) {
return HAS_FABRIC_API
? ServerConfigurationNetworking.createS2CPacket(Constants.MC_REGISTER_CHANNEL, buf)
: super.createVanillaRegisterConfigS2CPacket(buf);
}

@Override
public Packet<?> createVanillaRegisterConfigC2SPacket(FriendlyByteBuf buf) {
return HAS_FABRIC_API
? ClientConfigurationNetworking.createC2SPacket(Constants.MC_REGISTER_CHANNEL, buf)
: super.createVanillaRegisterConfigC2SPacket(buf);
}

@Override
public Packet<?> createVanillaRegisterPlayS2CPacket(FriendlyByteBuf buf) {
return HAS_FABRIC_API
? ServerPlayNetworking.createS2CPacket(Constants.MC_REGISTER_CHANNEL, buf)
: super.createVanillaRegisterPlayS2CPacket(buf);
}

@Override
public Packet<?> createVanillaRegisterPlayC2SPacket(FriendlyByteBuf buf) {
return HAS_FABRIC_API
? ClientPlayNetworking.createC2SPacket(Constants.MC_REGISTER_CHANNEL, buf)
: super.createVanillaRegisterPlayC2SPacket(buf);
}
// TODO
// @Override
// public Packet<?> createVanillaRegisterConfigS2CPacket(FriendlyByteBuf buf) {
// return HAS_FABRIC_API
// ? ServerConfigurationNetworking.createS2CPacket(Constants.MC_REGISTER_CHANNEL, buf)
// : super.createVanillaRegisterConfigS2CPacket(buf);
// }

// @Override
// public Packet<?> createVanillaRegisterConfigC2SPacket(FriendlyByteBuf buf) {
// return HAS_FABRIC_API
// ? ClientConfigurationNetworking.createC2SPacket(Constants.MC_REGISTER_CHANNEL, buf)
// : super.createVanillaRegisterConfigC2SPacket(buf);
// }

// @Override
// public Packet<?> createVanillaRegisterPlayS2CPacket(FriendlyByteBuf buf) {
// return HAS_FABRIC_API
// ? ServerPlayNetworking.createS2CPacket(Constants.MC_REGISTER_CHANNEL, buf)
// : super.createVanillaRegisterPlayS2CPacket(buf);
// }

// @Override
// public Packet<?> createVanillaRegisterPlayC2SPacket(FriendlyByteBuf buf) {
// return HAS_FABRIC_API
// ? ClientPlayNetworking.createC2SPacket(Constants.MC_REGISTER_CHANNEL, buf)
// : super.createVanillaRegisterPlayC2SPacket(buf);
// }

}
2 changes: 1 addition & 1 deletion platform/fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"license" : "Apache-2.0",
"mixins" : ["badpackets.mixins.json"],
"depends" : {
"minecraft" : ">=1.20.4-",
"minecraft" : ">=1.20.5-",
"fabricloader": ">=0.14.22"
},
"custom" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import lol.bai.badpackets.impl.Constants;
import lol.bai.badpackets.test.BadPacketTest;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

public class FabricBadPacketTest implements ModInitializer {
Expand All @@ -16,11 +13,12 @@ public class FabricBadPacketTest implements ModInitializer {
public void onInitialize() {
BadPacketTest.server();

ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> {
if (!ServerConfigurationNetworking.canSend(handler, FABRIC_TEST)) {
handler.disconnect(Component.literal("fabric test channel not found"));
}
});
// TODO
// ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> {
// if (!ServerConfigurationNetworking.canSend(handler, FABRIC_TEST)) {
// handler.disconnect(Component.literal("fabric test channel not found"));
// }
// });
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import lol.bai.badpackets.test.BadPacketTest;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;

public class FabricBadPacketTestClient implements ClientModInitializer {

@Override
public void onInitializeClient() {
BadPacketTest.client();

ClientConfigurationNetworking.registerGlobalReceiver(FabricBadPacketTest.FABRIC_TEST, (client, handler, buf, responseSender) -> {});
// TODO
// ClientConfigurationNetworking.registerGlobalReceiver(FabricBadPacketTest.FABRIC_TEST, (client, handler, buf, responseSender) -> {});
}

}
4 changes: 2 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ fun platform(name: String) {
platform("mojmap")

platform("fabric")
platform("forge")
platform("neo")
//platform("forge")
//platform("neo")
19 changes: 16 additions & 3 deletions src/main/java/lol/bai/badpackets/api/PacketSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lol.bai.badpackets.impl.payload.UntypedPayload;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.PacketSendListener;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -26,7 +27,7 @@ public interface PacketSender {
* <b>Only available when on game.</b>
*
* @see PlayPackets#registerServerReceiver(ResourceLocation, ServerPlayPacketReceiver)
* @see PlayPackets#registerServerReceiver(ResourceLocation, FriendlyByteBuf.Reader, ServerPlayPacketReceiver)
* @see PlayPackets#registerServerReceiver(CustomPacketPayload.Type, StreamCodec, ServerPlayPacketReceiver)
*/
@ApiSide.ClientOnly
static PacketSender c2s() {
Expand All @@ -39,7 +40,7 @@ static PacketSender c2s() {
* @param player the player that we want to send packets to.
*
* @see PlayPackets#registerClientReceiver(ResourceLocation, ClientPlayPacketReceiver)
* @see PlayPackets#registerClientReceiver(ResourceLocation, FriendlyByteBuf.Reader, ClientPlayPacketReceiver)
* @see PlayPackets#registerClientReceiver(CustomPacketPayload.Type, StreamCodec, ClientPlayPacketReceiver)
*/
@ApiSide.ServerOnly
static PacketSender s2c(ServerPlayer player) {
Expand All @@ -56,6 +57,18 @@ static PacketSender s2c(ServerPlayer player) {
*/
boolean canSend(ResourceLocation id);

/**
* Returns whether the target can receive a packet with the specified id.
* <p>
* <b>Note:</b> Only works for Bad Packets channels.
*
* @see PlayPackets#registerClientReadyCallback(ClientPlayPacketReadyCallback)
* @see PlayPackets#registerServerReadyCallback(ServerPlayPacketReadyCallback)
*/
default boolean canSend(CustomPacketPayload.Type<?> type) {
return canSend(type.id());
}

/**
* Send a packet to the target.
*/
Expand Down Expand Up @@ -83,7 +96,7 @@ default void send(ResourceLocation id, FriendlyByteBuf buf) {
* @param callback a callback in which will be called after the packet sent to the target.
*/
default void send(ResourceLocation id, FriendlyByteBuf buf, @Nullable PacketSendListener callback) {
send(new UntypedPayload(id, buf), callback);
send(new UntypedPayload(UntypedPayload.type(id), buf), callback);
}

}
21 changes: 11 additions & 10 deletions src/main/java/lol/bai/badpackets/api/config/ConfigPackets.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.client.multiplayer.ClientConfigurationPacketListenerImpl;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -44,7 +45,7 @@ public static void registerTask(ResourceLocation id, ConfigTaskExecutor executor
* @see ServerConfigPacketReceiver#receive
*/
public static void registerServerReceiver(ResourceLocation id, ServerConfigPacketReceiver<FriendlyByteBuf> receiver) {
registerServerReceiver(id, UntypedPayload.reader(id), (server, handler, payload, responseSender, taskFinisher) ->
registerServerReceiver(UntypedPayload.type(id), UntypedPayload.codec(id), (server, handler, payload, responseSender, taskFinisher) ->
receiver.receive(server, handler, payload.buffer(), responseSender, taskFinisher));
}

Expand All @@ -53,15 +54,15 @@ public static void registerServerReceiver(ResourceLocation id, ServerConfigPacke
* <p>
* Typed packet receiver is run on the main server thread.
*
* @param id the {@linkplain CustomPacketPayload#id() packet id}
* @param reader the payload reader
* @param type the {@linkplain CustomPacketPayload#type() packet type}
* @param codec the payload codec
* @param receiver the receiver
*
* @see ServerConfigPacketReceiver#receive
*/
@SuppressWarnings("unchecked")
public static <P extends CustomPacketPayload> void registerServerReceiver(ResourceLocation id, FriendlyByteBuf.Reader<P> reader, ServerConfigPacketReceiver<P> receiver) {
ChannelRegistry.CONFIG_C2S.register(id, reader, (ServerConfigPacketReceiver<CustomPacketPayload>) receiver);
public static <P extends CustomPacketPayload> void registerServerReceiver(CustomPacketPayload.Type<P> type, StreamCodec<? super FriendlyByteBuf, P> codec, ServerConfigPacketReceiver<P> receiver) {
ChannelRegistry.CONFIG_C2S.register(type, codec, (ServerConfigPacketReceiver<CustomPacketPayload>) receiver);
}

/**
Expand Down Expand Up @@ -90,7 +91,7 @@ public static void registerServerReadyCallback(ServerConfigPacketReadyCallback c
*/
@ApiSide.ClientOnly
public static void registerClientReceiver(ResourceLocation id, ClientConfigPacketReceiver<FriendlyByteBuf> receiver) {
registerClientReceiver(id, UntypedPayload.reader(id), (client, handler, payload, responseSender) ->
registerClientReceiver(UntypedPayload.type(id), UntypedPayload.codec(id), (client, handler, payload, responseSender) ->
receiver.receive(client, handler, payload.buffer(), responseSender));
}

Expand All @@ -99,17 +100,17 @@ public static void registerClientReceiver(ResourceLocation id, ClientConfigPacke
* <p>
* Typed packet receiver is run on the main client thread.
*
* @param id the {@linkplain CustomPacketPayload#id() packet id}
* @param reader the payload reader
* @param type the {@linkplain CustomPacketPayload#type() packet type}
* @param codec the payload codec
* @param receiver the receiver
*
* @see ClientConfigPacketReceiver#receive
* @see #disconnect
*/
@ApiSide.ClientOnly
@SuppressWarnings("unchecked")
public static <P extends CustomPacketPayload> void registerClientReceiver(ResourceLocation id, FriendlyByteBuf.Reader<P> reader, ClientConfigPacketReceiver<P> receiver) {
ChannelRegistry.CONFIG_S2C.register(id, reader, (ClientConfigPacketReceiver<CustomPacketPayload>) receiver);
public static <P extends CustomPacketPayload> void registerClientReceiver(CustomPacketPayload.Type<P> type, StreamCodec<? super FriendlyByteBuf, P> codec, ClientConfigPacketReceiver<P> receiver) {
ChannelRegistry.CONFIG_S2C.register(type, codec, (ClientConfigPacketReceiver<CustomPacketPayload>) receiver);
}

/**
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/lol/bai/badpackets/api/play/PlayPackets.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lol.bai.badpackets.impl.registry.ChannelRegistry;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
Expand All @@ -26,7 +28,7 @@ public final class PlayPackets {
* @param receiver the receiver
*/
public static void registerServerReceiver(ResourceLocation id, ServerPlayPacketReceiver<FriendlyByteBuf> receiver) {
registerServerReceiver(id, UntypedPayload.reader(id), (server, player, handler, payload, responseSender) ->
registerServerReceiver(UntypedPayload.type(id), UntypedPayload.codec(id), (server, player, handler, payload, responseSender) ->
receiver.receive(server, player, handler, payload.buffer(), responseSender));
}

Expand All @@ -35,13 +37,13 @@ public static void registerServerReceiver(ResourceLocation id, ServerPlayPacketR
* <p>
* Typed packet receiver is run on the main server thread.
*
* @param id the {@linkplain CustomPacketPayload#id() packet id}
* @param reader the payload reader
* @param type the {@linkplain CustomPacketPayload#type() packet type}
* @param codec the payload codec
* @param receiver the receiver
*/
@SuppressWarnings("unchecked")
public static <P extends CustomPacketPayload> void registerServerReceiver(ResourceLocation id, FriendlyByteBuf.Reader<P> reader, ServerPlayPacketReceiver<P> receiver) {
ChannelRegistry.PLAY_C2S.register(id, reader, (ServerPlayPacketReceiver<CustomPacketPayload>) receiver);
public static <P extends CustomPacketPayload> void registerServerReceiver(CustomPacketPayload.Type<P> type, StreamCodec<? super RegistryFriendlyByteBuf, P> codec, ServerPlayPacketReceiver<P> receiver) {
ChannelRegistry.PLAY_C2S.register(type, codec, (ServerPlayPacketReceiver<CustomPacketPayload>) receiver);
}

/**
Expand All @@ -66,7 +68,7 @@ public static void registerServerReadyCallback(ServerPlayPacketReadyCallback cal
*/
@ApiSide.ClientOnly
public static void registerClientReceiver(ResourceLocation id, ClientPlayPacketReceiver<FriendlyByteBuf> receiver) {
registerClientReceiver(id, UntypedPayload.reader(id), (client, handler, payload, responseSender) ->
registerClientReceiver(UntypedPayload.type(id), UntypedPayload.codec(id), (client, handler, payload, responseSender) ->
receiver.receive(client, handler, payload.buffer(), responseSender));
}

Expand All @@ -75,14 +77,14 @@ public static void registerClientReceiver(ResourceLocation id, ClientPlayPacketR
* <p>
* Typed packet receiver is run on the main client thread.
*
* @param id the {@linkplain CustomPacketPayload#id() packet id}
* @param reader the payload reader
* @param type the {@linkplain CustomPacketPayload#type() packet type}
* @param codec the payload codec
* @param receiver the receiver
*/
@ApiSide.ClientOnly
@SuppressWarnings("unchecked")
public static <P extends CustomPacketPayload> void registerClientReceiver(ResourceLocation id, FriendlyByteBuf.Reader<P> reader, ClientPlayPacketReceiver<P> receiver) {
ChannelRegistry.PLAY_S2C.register(id, reader, (ClientPlayPacketReceiver<CustomPacketPayload>) receiver);
public static <P extends CustomPacketPayload> void registerClientReceiver(CustomPacketPayload.Type<P> type, StreamCodec<? super RegistryFriendlyByteBuf, P> codec, ClientPlayPacketReceiver<P> receiver) {
ChannelRegistry.PLAY_S2C.register(type, codec, (ClientPlayPacketReceiver<CustomPacketPayload>) receiver);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;

public abstract class AbstractPacketHandler<T> implements PacketSender {
public abstract class AbstractPacketHandler<R> implements PacketSender {

protected final ChannelRegistry<T> registry;
protected final ChannelRegistry<?, R> registry;
protected final Logger logger;

private final Function<CustomPacketPayload, Packet<?>> packetFactory;
Expand All @@ -39,7 +39,7 @@ public abstract class AbstractPacketHandler<T> implements PacketSender {

private boolean initialized = false;

protected AbstractPacketHandler(String desc, ChannelRegistry<T> registry, Function<CustomPacketPayload, Packet<?>> packetFactory, BlockableEventLoop<?> eventLoop, Connection connection) {
protected AbstractPacketHandler(String desc, ChannelRegistry<?, R> registry, Function<CustomPacketPayload, Packet<?>> packetFactory, BlockableEventLoop<?> eventLoop, Connection connection) {
this.logger = LogManager.getLogger(desc);
this.registry = registry;
this.packetFactory = packetFactory;
Expand Down Expand Up @@ -69,12 +69,8 @@ private void receiveChannelSyncPacket(FriendlyByteBuf buf) {
}
}

public static void addChannelSyncReader(Map<ResourceLocation, FriendlyByteBuf.Reader<? extends CustomPacketPayload>> map) {
map.put(Constants.CHANNEL_SYNC, UntypedPayload.reader(Constants.CHANNEL_SYNC));
}

public boolean receive(CustomPacketPayload payload) {
ResourceLocation id = payload.id();
ResourceLocation id = payload.type().id();

if (id.equals(Constants.CHANNEL_SYNC)) {
UntypedPayload untyped = (UntypedPayload) payload;
Expand All @@ -84,7 +80,7 @@ public boolean receive(CustomPacketPayload payload) {

if (registry.has(id)) {
try {
T receiver = registry.get(id);
R receiver = registry.get(id);

if (payload instanceof UntypedPayload || eventLoop.isSameThread()) {
receiveUnsafe(receiver, payload);
Expand All @@ -103,7 +99,7 @@ public boolean receive(CustomPacketPayload payload) {

protected abstract void onInitialChannelSyncPacketReceived();

protected abstract void receiveUnsafe(T receiver, CustomPacketPayload payload);
protected abstract void receiveUnsafe(R receiver, CustomPacketPayload payload);

public void sendInitialChannelSyncPacket() {
if (!initialized) {
Expand Down
Loading

0 comments on commit c4dda20

Please sign in to comment.