-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
ca575da
commit 7f03931
Showing
4 changed files
with
232 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.vulpeus.kyoyu; | ||
|
||
import net.minecraft.resources.ResourceLocation; | ||
|
||
public class CompatibleUtils { | ||
public static ResourceLocation identifier(String namespace, String path) { | ||
//? if >1.20.6 { | ||
return ResourceLocation.fromNamespaceAndPath(namespace, path); | ||
//?} elif =1.20.6 { | ||
/* return ResourceLocation.tryBuild(namespace, path); */ | ||
//?} else { | ||
/* return new ResourceLocation(namespace, path); */ | ||
//?} | ||
} | ||
} |
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
181 changes: 181 additions & 0 deletions
181
src/main/java/com/vulpeus/kyoyu/net/packets/KyoyuPacketPayload.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,181 @@ | ||
package com.vulpeus.kyoyu.net.packets; | ||
|
||
import com.vulpeus.kyoyu.CompatibleUtils; | ||
import com.vulpeus.kyoyu.Kyoyu; | ||
import net.minecraft.resources.ResourceLocation; | ||
|
||
//? if >=1.20.6 { | ||
import net.minecraft.network.codec.ByteBufCodecs; | ||
import net.minecraft.network.codec.StreamCodec; | ||
import net.minecraft.network.protocol.common.custom.CustomPacketPayload; | ||
import net.minecraft.network.RegistryFriendlyByteBuf; | ||
//?} else { | ||
/* | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.server.network.ServerGamePacketListenerImpl; | ||
*/ | ||
//?} | ||
|
||
|
||
//? if FABRIC { | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; | ||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; | ||
//? if >1.20.4 { | ||
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; | ||
//?} else { | ||
/* | ||
import io.netty.buffer.Unpooled; | ||
import net.fabricmc.fabric.api.networking.v1.PacketSender; | ||
import net.minecraft.client.multiplayer.ClientPacketListener; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.server.MinecraftServer; | ||
*/ | ||
//?} | ||
//?} elif NEOFORGE { | ||
/* | ||
import net.neoforged.bus.api.SubscribeEvent; | ||
import net.neoforged.neoforge.network.PacketDistributor; | ||
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent; | ||
import net.neoforged.neoforge.network.handling.DirectionalPayloadHandler; | ||
import net.neoforged.neoforge.network.handling.IPayloadContext; | ||
import net.neoforged.neoforge.network.registration.PayloadRegistrar; | ||
*/ | ||
//?} | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class KyoyuPacketPayload | ||
//? if >1.20.4 | ||
implements CustomPacketPayload | ||
{ | ||
|
||
public final byte[] content; | ||
public KyoyuPacketPayload(byte[] content) { | ||
this.content = content; | ||
} | ||
public byte[] content() { | ||
return this.content; | ||
} | ||
|
||
|
||
private static final ResourceLocation identifier = CompatibleUtils.identifier(Kyoyu.MOD_ID, "kyoyu"); | ||
|
||
//? if >1.20.4 { | ||
private static final Type<KyoyuPacketPayload> TYPE = new Type<>(identifier); | ||
private static final StreamCodec<RegistryFriendlyByteBuf, KyoyuPacketPayload> CODEC = StreamCodec.composite( | ||
ByteBufCodecs.BYTE_ARRAY, KyoyuPacketPayload::content, KyoyuPacketPayload::new | ||
); | ||
@Override | ||
public @NotNull Type<? extends CustomPacketPayload> type() { | ||
return TYPE; | ||
} | ||
//?} | ||
|
||
//? FABRIC { | ||
public static void register() { | ||
//? if >1.20.4 { | ||
PayloadTypeRegistry.playC2S().register(TYPE, CODEC); | ||
PayloadTypeRegistry.playS2C().register(TYPE, CODEC); | ||
//?} | ||
|
||
if (Kyoyu.isClient()) { | ||
ClientPlayNetworking.registerGlobalReceiver( | ||
//? if >1.20.4 { | ||
TYPE, KyoyuPacketPayload::onPacketClient | ||
//?} else { | ||
/* | ||
identifier, (client, handler, buf, responseSender) -> { | ||
KyoyuPacketPayload packetPayload = new KyoyuPacketPayload(buf.readByteArray()); | ||
packetPayload.onPacketClient(client, handler, responseSender); | ||
} | ||
*/ | ||
//?} | ||
); | ||
} else { | ||
ServerPlayNetworking.registerGlobalReceiver( | ||
//? if >1.20.4 { | ||
TYPE, KyoyuPacketPayload::onPacketServer | ||
//?} else { | ||
/* | ||
identifier, (server, player, handler, buf, responseSender) -> { | ||
KyoyuPacketPayload packetPayload = new KyoyuPacketPayload(buf.readByteArray()); | ||
packetPayload.onPacketServer(server, player, handler, responseSender); | ||
} | ||
*/ | ||
//?} | ||
); | ||
} | ||
} | ||
//?} elif NEOFORGE { | ||
/* | ||
@SubscribeEvent | ||
public static void register(final RegisterPayloadHandlersEvent event) { | ||
final PayloadRegistrar registrar = event.registrar("1"); | ||
registrar.playBidirectional( | ||
KyoyuPacketPayload.TYPE, | ||
KyoyuPacketPayload.CODEC, | ||
new DirectionalPayloadHandler<>( | ||
KyoyuPacketPayload::onPacketServer, | ||
KyoyuPacketPayload::onPacketClient | ||
) | ||
); | ||
} | ||
*/ | ||
//?} | ||
|
||
public void sendC2S() { | ||
//? FABRIC { | ||
//? >=1.20.6 { | ||
ClientPlayNetworking.send(this); | ||
//?} else { | ||
/* | ||
FriendlyByteBuf packetbb = new FriendlyByteBuf(Unpooled.buffer()); | ||
packetbb.writeByteArray(this.content); | ||
ClientPlayNetworking.send(identifier, packetbb); | ||
*/ | ||
//?} | ||
//?} elif NEOFORGE { | ||
/* PacketDistributor.sendToServer(this); */ | ||
//?} | ||
} | ||
|
||
public void onPacketServer( | ||
//? FABRIC { | ||
//? if >1.20.4 { | ||
ServerPlayNetworking.Context context | ||
//?} else { | ||
/* MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl handler, PacketSender sender */ | ||
//?} | ||
//?} elif NEOFORGE { | ||
/* IPayloadContext context */ | ||
//?} else { | ||
/* Object context */ | ||
//?} | ||
) { | ||
//? if FABRIC && <=1.20.4 { | ||
/* Kyoyu.LOGGER.info("onPacketServer {} {} {} {}", server, player, handler, sender); */ | ||
//?} else { | ||
Kyoyu.LOGGER.info("onPacketServer {}", context); | ||
//?} | ||
} | ||
|
||
public void onPacketClient( | ||
//? FABRIC { | ||
//? if >1.20.4 { | ||
ClientPlayNetworking.Context context | ||
//?} else { | ||
/* Minecraft client, ClientPacketListener handler, PacketSender sender */ | ||
//?} | ||
//?} elif NEOFORGE { | ||
/* IPayloadContext context */ | ||
//?} else { | ||
/* Object context */ | ||
//?} | ||
) { | ||
//? if FABRIC && <=1.20.4 { | ||
/* Kyoyu.LOGGER.info("onPacketClient {} {} {}", client, handler, sender); */ | ||
//?} else { | ||
Kyoyu.LOGGER.info("onPacketClient {}", context); | ||
//?} | ||
} | ||
} |