Skip to content

Commit

Permalink
✨ Added Networking
Browse files Browse the repository at this point in the history
Signed-off-by: XyperCode <xypercode@ultreon.dev>
  • Loading branch information
XyperCode committed Dec 13, 2024
1 parent 8f96cff commit 9c37979
Show file tree
Hide file tree
Showing 51 changed files with 857 additions and 76 deletions.
39 changes: 36 additions & 3 deletions common/src/main/java/dev/ultreon/mods/xinexlib/CommonClass.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package dev.ultreon.mods.xinexlib;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import dev.ultreon.mods.xinexlib.access.IEntityComponentAccess;
import dev.ultreon.mods.xinexlib.components.*;
import dev.ultreon.mods.xinexlib.dev.commands.DevSendMessageCommand;
import dev.ultreon.mods.xinexlib.dev.network.packets.PacketToClient;
import dev.ultreon.mods.xinexlib.dev.network.packets.PacketToServer;
import dev.ultreon.mods.xinexlib.event.JVMShutdownEvent;
import dev.ultreon.mods.xinexlib.event.block.AttemptBlockSetEvent;
import dev.ultreon.mods.xinexlib.event.block.BlockSetEvent;
Expand All @@ -18,9 +24,12 @@
import dev.ultreon.mods.xinexlib.event.system.EventSystem;
import dev.ultreon.mods.xinexlib.item.XinexBlockItem;
import dev.ultreon.mods.xinexlib.nbt.DataKeys;
import dev.ultreon.mods.xinexlib.network.INetworkRegistry;
import dev.ultreon.mods.xinexlib.network.INetworker;
import dev.ultreon.mods.xinexlib.platform.Services;
import dev.ultreon.mods.xinexlib.platform.services.IRegistrar;
import dev.ultreon.mods.xinexlib.platform.services.IRegistrarManager;
import dev.ultreon.mods.xinexlib.registrar.IRegistrar;
import dev.ultreon.mods.xinexlib.registrar.IRegistrarManager;
import net.minecraft.commands.Commands;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -112,7 +121,7 @@ private static void initDev() {
output.accept(new ItemStack(testItem));
output.accept(new ItemStack(testBlockItem));
output.accept(new ItemStack(secondBlockItem));
}).build());
}).title(Component.literal("Test Tab")).build());

Constants.LOG.info("The ID for test_block is {}", testBlock.getId());
Constants.LOG.info("The ID for test_item is {}", testItem.getId());
Expand Down Expand Up @@ -251,5 +260,29 @@ public void load(CompoundTag tag, HolderLookup.Provider registryLookup) {
});

Constants.LOG.info("The developer mode is enabled!");

INetworker networker = Services.createNetworker(Constants.MOD_ID, iNetworkRegistry -> {
iNetworkRegistry.registerClient("packet2client", PacketToClient.class, PacketToClient::read);
// iNetworkRegistry.registerServer("packet2server", PacketToServer.class, PacketToServer::read);
});

Services.registerCommand((dispatcher, registryAccess, environment) -> {
dispatcher.register(Commands.literal("xinex-dev:packets")
.then(Commands.literal("packet")
.then(Commands.argument("message", StringArgumentType.greedyString())
.executes(context -> {
try {
String string = StringArgumentType.getString(context, "message");
PacketToClient packet = new PacketToClient(string);
networker.sendToClient(packet, context.getSource().getPlayerOrException());
} catch (Exception e) {
Constants.LOG.error("Failed to send packet", e);
}
return 1;
})
)
)
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ private Constants() {
public static final String MOD_ID = "xinexlib";
public static final String MOD_NAME = "XinexLib";
public static final Logger LOG = LoggerFactory.getLogger(MOD_NAME);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package dev.ultreon.mods.xinexlib.client;

import dev.ultreon.mods.xinexlib.Env;
import dev.ultreon.mods.xinexlib.EnvExecutor;
import dev.ultreon.mods.xinexlib.client.event.LocalPlayerQuitEvent;
import dev.ultreon.mods.xinexlib.event.system.EventSystem;
import dev.ultreon.mods.xinexlib.platform.Services;
import net.minecraft.client.Minecraft;

public class ClientClass {
public static void init() {
Expand All @@ -12,4 +17,8 @@ public static void init() {
private static void initDev() {
// TODO
}

public static void onDisconnect() {
EnvExecutor.runInEnv(Env.CLIENT, () -> () -> EventSystem.MAIN.publish(new LocalPlayerQuitEvent(Minecraft.getInstance().player)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.ultreon.mods.xinexlib.dev.commands;

public class DevSendMessageCommand {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.ultreon.mods.xinexlib.dev.network.handler;

import dev.ultreon.mods.xinexlib.dev.network.packets.PacketToClient;

public abstract class DevClientPlayHandler {
public abstract void handle(PacketToClient packet);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.ultreon.mods.xinexlib.dev.network.handler;

import dev.ultreon.mods.xinexlib.dev.network.packets.PacketToClient;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;

public class DevClientPlayHandlerImpl extends DevClientPlayHandler {
@Override
public void handle(PacketToClient packet) {
String message = packet.message();
Minecraft.getInstance().player.displayClientMessage(Component.literal(message), true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.ultreon.mods.xinexlib.dev.network.handler;

import dev.ultreon.mods.xinexlib.dev.network.packets.PacketToServer;
import dev.ultreon.mods.xinexlib.network.PacketInfo;
import net.minecraft.network.chat.Component;

import java.util.UUID;

public class DevServerPlayHandler {
public void handle(PacketToServer packet, PacketInfo packetInfo) {
UUID uuid = packet.uuid();
packetInfo.sender().sendSystemMessage(Component.nullToEmpty("Hello, the UUID is: " + uuid));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.ultreon.mods.xinexlib.dev.network.packets;

import dev.ultreon.mods.xinexlib.network.INetworker;
import dev.ultreon.mods.xinexlib.network.packet.IPacketToClient;
import net.minecraft.client.Minecraft;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;

public record PacketToClient(
String message
) implements IPacketToClient<PacketToClient> {

public static PacketToClient read(RegistryFriendlyByteBuf buf) {
Thread.dumpStack();
return new PacketToClient(buf.readUtf());
}

public void write(RegistryFriendlyByteBuf buf) {
buf.writeUtf(message);
}

@Override
public void handle(INetworker networker) {
Minecraft.getInstance().player.displayClientMessage(Component.literal(message), true);
System.out.println(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.ultreon.mods.xinexlib.dev.network.packets;

import dev.ultreon.mods.xinexlib.network.INetworker;
import dev.ultreon.mods.xinexlib.network.packet.IPacketToServer;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;

import java.util.UUID;

public record PacketToServer(
UUID uuid
) implements IPacketToServer<PacketToServer> {
public static PacketToServer read(RegistryFriendlyByteBuf buf) {
return new PacketToServer(buf.readUUID());
}

public void write(RegistryFriendlyByteBuf buf) {
buf.writeUUID(uuid);
}

@Override
public void handle(INetworker connection, ServerPlayer player) {
player.sendSystemMessage(Component.nullToEmpty("Hello, the UUID is: " + uuid));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.ultreon.mods.xinexlib.mixin;

import dev.ultreon.mods.xinexlib.client.ClientClass;
import net.minecraft.network.Connection;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

/// @author XyperCode
/// @since 0.1.0 (December 10, 2024)
@Mixin(Connection.class)
public abstract class MixinClientConnection {

@Inject(at = @At("HEAD"), method = "channelInactive")
private void channelInactive(CallbackInfo ci) {
ClientClass.onDisconnect();
}

@Inject(at = @At("HEAD"), method = "disconnect(Lnet/minecraft/network/DisconnectionDetails;)V")
private void runTick$return(CallbackInfo ci) {
ClientClass.onDisconnect();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.ultreon.mods.xinexlib.network;

import dev.ultreon.mods.xinexlib.network.endpoint.IClientEndpoint;
import dev.ultreon.mods.xinexlib.network.endpoint.IServerEndpoint;
import dev.ultreon.mods.xinexlib.network.packet.IPacket;

public interface INetworkRegistry {
<T extends IPacket<T> & IClientEndpoint> void registerClient(String name, Class<T> clazz, IPacketReader<T> reader);

<T extends IPacket<T> & IServerEndpoint> void registerServer(String name, Class<T> clazz, IPacketReader<T> reader);

<T extends IPacket<T> & IServerEndpoint & IClientEndpoint> void registerBiDirectional(String name, Class<T> clazz, IPacketReader<T> reader);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.ultreon.mods.xinexlib.network;

import dev.ultreon.mods.xinexlib.network.endpoint.IClientEndpoint;
import dev.ultreon.mods.xinexlib.network.endpoint.IServerEndpoint;
import dev.ultreon.mods.xinexlib.network.packet.IPacket;
import net.minecraft.server.level.ServerPlayer;

import java.util.stream.Stream;

public interface INetworker {
<T extends IPacket<T> & IServerEndpoint> void sendToServer(T payload);

<T extends IPacket<T> & IClientEndpoint> void sendToClient(T payload, ServerPlayer player);

default <T extends IPacket<T> & IClientEndpoint> void sendToClients(T payload, ServerPlayer... players) {
for (ServerPlayer player : players) {
sendToClient(payload, player);
}
}

default <T extends IPacket<T> & IClientEndpoint> void sendToClients(T payload, Iterable<ServerPlayer> players) {
for (ServerPlayer player : players) {
sendToClient(payload, player);
}
}

default <T extends IPacket<T> & IClientEndpoint> void sendToClients(T payload, Stream<ServerPlayer> players) {
players.forEach(player -> sendToClient(payload, player));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.ultreon.mods.xinexlib.network;

import dev.ultreon.mods.xinexlib.network.packet.IPacket;
import net.minecraft.network.RegistryFriendlyByteBuf;

@FunctionalInterface
public interface IPacketReader<T extends IPacket<?>> {
T read(RegistryFriendlyByteBuf buffer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.ultreon.mods.xinexlib.network;

import net.minecraft.server.level.ServerPlayer;
import org.jetbrains.annotations.Nullable;

public record PacketInfo(
@Nullable ServerPlayer sender
) {
public PacketInfo() {
this(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.ultreon.mods.xinexlib.network;

import dev.ultreon.mods.xinexlib.network.packet.IPacket;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;

class PayloadWrapper<T extends IPacket<?>> implements CustomPacketPayload {
private final Type<PayloadWrapper<T>> type;
T packet;

PayloadWrapper(Type<PayloadWrapper<T>> type) {
this.type = type;
}

PayloadWrapper(Type<PayloadWrapper<T>> type, T packet) {
this.type = type;
this.packet = packet;
}

@Override
public Type<? extends CustomPacketPayload> type() {
return type;
}

public void write(RegistryFriendlyByteBuf object) {
packet.write(object);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.ultreon.mods.xinexlib.network.endpoint;

import dev.ultreon.mods.xinexlib.network.INetworker;

public interface IClientEndpoint {
void handle(INetworker connection);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.ultreon.mods.xinexlib.network.endpoint;

import dev.ultreon.mods.xinexlib.network.INetworker;
import net.minecraft.server.level.ServerPlayer;

public interface IServerEndpoint {
void handle(INetworker connection, ServerPlayer player);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.ultreon.mods.xinexlib.network.packet;

import net.minecraft.network.RegistryFriendlyByteBuf;

public interface IPacket<T extends IPacket<T>> {
void write(RegistryFriendlyByteBuf buffer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.ultreon.mods.xinexlib.network.packet;

import dev.ultreon.mods.xinexlib.network.endpoint.IClientEndpoint;

public interface IPacketToClient<T extends IPacket<T>> extends IPacket<T>, IClientEndpoint {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.ultreon.mods.xinexlib.network.packet;

import dev.ultreon.mods.xinexlib.network.endpoint.IServerEndpoint;

public interface IPacketToEither<T extends IPacket<T>> extends IPacket<T>, IServerEndpoint {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.ultreon.mods.xinexlib.network.packet;

import dev.ultreon.mods.xinexlib.network.endpoint.IServerEndpoint;

public interface IPacketToServer<T extends IPacket<T>> extends IPacket<T>, IServerEndpoint {

}
Loading

0 comments on commit 9c37979

Please sign in to comment.