Skip to content

Commit

Permalink
1.0.17-beta.2
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticdrew committed Oct 21, 2024
1 parent 124ab71 commit 8bd583a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
13 changes: 13 additions & 0 deletions common/src/main/java/commonnetwork/api/Dispatcher.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commonnetwork.api;

import net.minecraft.core.BlockPos;
import net.minecraft.network.Connection;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -36,6 +37,18 @@ public static <T> void sendToServer(T packet, boolean ignoreCheck)
Network.getNetworkHandler().sendToServer(packet, ignoreCheck);
}

/**
* Sends the packet to the connection.
*
* @param packet - the packet
* @param connection - the connection
* @param <T> - The packet
*/
public static <T> void send(T packet, Connection connection)
{
Network.getNetworkHandler().send(packet, connection);
}

/**
* Sends the packet to the client player, only if the player has the packet registered.
*
Expand Down
10 changes: 10 additions & 0 deletions common/src/main/java/commonnetwork/api/NetworkHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commonnetwork.api;

import net.minecraft.core.BlockPos;
import net.minecraft.network.Connection;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -55,6 +56,15 @@ default <T> void sendToClient(T packet, ServerPlayer player)
*/
<T> void sendToClient(T packet, ServerPlayer player, boolean ignoreCheck);

/**
* Sends the packet to the connection.
*
* @param packet - the packet
* @param connection - the connection
* @param <T> - The packet
*/
<T> void send(T packet, Connection connection);

/**
* Sends the packet to the client players, only if the players has the packet registered.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket;
import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
import net.minecraft.server.level.ServerPlayer;

public class FabricNetworkHandler extends PacketRegistrationHandler
Expand Down Expand Up @@ -84,6 +87,22 @@ protected <T> void registerPacket(PacketContainer<T> container)

}

@Override
public <T> void send(T packet, Connection connection)
{
PacketContainer<T> container = (PacketContainer<T>) PACKET_MAP.get(packet.getClass());
if (container != null)
{
if (this.side == Side.SERVER)
{
connection.send(new ClientboundCustomPayloadPacket(new CommonPacketWrapper<>(container, packet)));
}
else
{
connection.send(new ServerboundCustomPayloadPacket(new CommonPacketWrapper<>(container, packet)));
}
}
}

@SuppressWarnings("unchecked")
public <T> void sendToServer(T packet, boolean ignoreCheck)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ public <T> void sendToClient(T packet, ServerPlayer player, boolean ignoreCheck)
}
}

@Override
public <T> void send(T packet, Connection connection)
{
var message = (Message<T>) CHANNELS.get(packet.getClass());
if (message != null)
{
var channel = message.channel();
channel.send(new CommonPacketWrapper(message.container, packet), connection);
}
}

private static void handle(CustomPacketPayload customPacketPayload, CustomPayloadEvent.Context ctx)
{
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Project
version=1.0.17-beta
version=1.0.17-beta.2
group_id=mysticdrew

curseforge_project_id=806044
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import commonnetwork.networking.data.Side;
import commonnetwork.networking.exceptions.RegistrationException;
import net.minecraft.client.Minecraft;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket;
import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.bus.api.SubscribeEvent;
Expand Down Expand Up @@ -71,6 +73,23 @@ public <T> void sendToServer(T packet, boolean ignoreCheck)
}
}

@Override
public <T> void send(T packet, Connection connection)
{
PacketContainer<T> container = (PacketContainer<T>) PACKET_MAP.get(packet.getClass());
if (container != null)
{
if (this.side == Side.SERVER)
{
connection.send(new ClientboundCustomPayloadPacket(new CommonPacketWrapper<>(container, packet)));
}
else
{
connection.send(new ServerboundCustomPayloadPacket(new CommonPacketWrapper<>(container, packet)));
}
}
}

@SuppressWarnings("unchecked")
public <T> void sendToClient(T packet, ServerPlayer player, boolean ignoreCheck)
{
Expand Down

0 comments on commit 8bd583a

Please sign in to comment.