Skip to content

Commit

Permalink
KyoyuPacketPayload fabric/neoforge
Browse files Browse the repository at this point in the history
  • Loading branch information
topi-banana committed Nov 30, 2024
1 parent ca575da commit 7f03931
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/vulpeus/kyoyu/CompatibleUtils.java
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); */
//?}
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/vulpeus/kyoyu/Kyoyu.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,17 @@ public class Kyoyu {
public static final String MOD_VERSION = /*$ mod_version*/ "unknown";

public static final Logger LOGGER = LogManager.getLogger(MOD_ID);

private static boolean isClient = false;

public static void setEnv(boolean isClient) {
Kyoyu.isClient = isClient;
}

public static boolean isClient() {
return isClient;
}
public static boolean isServer() {
return !isClient;
}
}
27 changes: 23 additions & 4 deletions src/main/java/com/vulpeus/kyoyu/KyoyuMod.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vulpeus.kyoyu;

import com.vulpeus.kyoyu.net.packets.KyoyuPacketPayload;

//? if FORGE
/* import net.minecraftforge.fml.common.Mod; */

Expand All @@ -8,7 +10,7 @@


//? if FORGE || NEOFORGE
/* @Mod("kyoyu") */
/* @Mod(Kyoyu.MOD_ID) */
public class KyoyuMod
//? if FABRIC
implements net.fabricmc.api.ModInitializer
Expand All @@ -18,13 +20,30 @@ public class KyoyuMod
{
//? if FABRIC
@Override public void onInitialize()

//? if FORGE || NEOFORGE
/* public KyoyuMod() */
//? if NEOFORGE
/* public KyoyuMod(net.neoforged.bus.api.IEventBus modBus) */
//? if FORGE
/* public KyoyuMod(net.minecraftforge.eventbus.api.IEventBus modBus) */
//? if PAPER
/* @Override public void onEnable() */
{
Kyoyu.LOGGER.info("Hello, World!");
Kyoyu.LOGGER.info("KyoyuMod Version : {}", Kyoyu.MOD_VERSION);

Kyoyu.setEnv(
//? if FABRIC
net.fabricmc.loader.api.FabricLoader.getInstance().getEnvironmentType() == net.fabricmc.api.EnvType.CLIENT
//? if NEOFORGE
/* net.neoforged.fml.loading.FMLEnvironment.dist == net.neoforged.api.distmarker.Dist.CLIENT */
//? if FORGE
/* net.minecraftforge.fml.loading.FMLEnvironment.dist == net.minecraftforge.api.distmarker.Dist.CLIENT */
//? if PAPER
/* false */
);

//? if FABRIC
KyoyuPacketPayload.register();
//? if NEOFORGE
/* net.neoforged.neoforge.common.NeoForge.EVENT_BUS.register(KyoyuPacketPayload.class); */
}
}
181 changes: 181 additions & 0 deletions src/main/java/com/vulpeus/kyoyu/net/packets/KyoyuPacketPayload.java
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);
//?}
}
}

0 comments on commit 7f03931

Please sign in to comment.