Skip to content

Commit 56493e2

Browse files
committed
PacketHelper#dispatchLocallyAndSendTo and PacketHelper#dispatchLocallyAndToAllTracking
1 parent 375e191 commit 56493e2

4 files changed

Lines changed: 48 additions & 0 deletions

File tree

station-networking-v0/src/main/java/net/modificationstation/stationapi/api/network/packet/PacketHelper.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public static void sendTo(PlayerEntity player, Packet packet) {
4444
INSTANCE.sendTo(player, packet);
4545
}
4646

47+
/**
48+
* On client, ignores the packet if the current game is multiplayer, or handles the packet locally if the current game is singleplayer.
49+
* On server, both handles the packet locally and sends the packet to the player's client.
50+
* @param player the player to send the packet to.
51+
* @param packet the packet to send/handle.
52+
*/
53+
@API
54+
public static void dispatchLocallyAndSendTo(PlayerEntity player, Packet packet) {
55+
INSTANCE.dispatchLocallyAndSendTo(player, packet);
56+
}
57+
4758
/**
4859
* On client, ignores the packet if the current game is multiplayer, or handles the packet locally if the current game is singleplayer.
4960
* On server, sends the packet to all players tracking the given entity.
@@ -55,6 +66,17 @@ public static void sendToAllTracking(Entity entity, Packet packet) {
5566
INSTANCE.sendToAllTracking(entity, packet);
5667
}
5768

69+
/**
70+
* On client, ignores the packet if the current game is multiplayer, or handles the packet locally if the current game is singleplayer.
71+
* On server, both handles the packet locally and sends the packet to all players tracking the given entity.
72+
* @param entity the entity whose tracking players to send the packet to.
73+
* @param packet the packet to send/handle.
74+
*/
75+
@API
76+
public static void dispatchLocallyAndToAllTracking(Entity entity, Packet packet) {
77+
INSTANCE.dispatchLocallyAndToAllTracking(entity, packet);
78+
}
79+
5880
/**
5981
* Registers the given packet.
6082
*

station-networking-v0/src/main/java/net/modificationstation/stationapi/impl/client/network/packet/PacketHelperClientImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,19 @@ public void sendTo(PlayerEntity player, Packet packet) {
2424
packet.apply(packet instanceof ManagedPacket<?> managedPacket ? managedPacket.getType().getHandler().orElse(null) : null);
2525
}
2626

27+
@Override
28+
public void dispatchLocallyAndSendTo(PlayerEntity player, Packet packet) {
29+
sendTo(player, packet);
30+
}
31+
2732
@Override
2833
public void sendToAllTracking(Entity entity, Packet packet) {
2934
//noinspection deprecation
3035
sendTo(((Minecraft) FabricLoader.getInstance().getGameInstance()).player, packet);
3136
}
37+
38+
@Override
39+
public void dispatchLocallyAndToAllTracking(Entity entity, Packet packet) {
40+
sendToAllTracking(entity, packet);
41+
}
3242
}

station-networking-v0/src/main/java/net/modificationstation/stationapi/impl/network/packet/PacketHelperImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ public abstract class PacketHelperImpl {
99

1010
public abstract void sendTo(PlayerEntity player, Packet packet);
1111

12+
public abstract void dispatchLocallyAndSendTo(PlayerEntity player, Packet packet);
13+
1214
public abstract void sendToAllTracking(Entity entity, Packet packet);
15+
16+
public abstract void dispatchLocallyAndToAllTracking(Entity entity, Packet packet);
1317
}

station-networking-v0/src/main/java/net/modificationstation/stationapi/impl/server/network/packet/PacketHelperServerImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,22 @@ public void sendTo(PlayerEntity player, Packet packet) {
2424
((ServerPlayerEntity) player).field_255.method_835(packet);
2525
}
2626

27+
@Override
28+
public void dispatchLocallyAndSendTo(PlayerEntity player, Packet packet) {
29+
send(packet);
30+
sendTo(player, packet);
31+
}
32+
2733
@Override
2834
public void sendToAllTracking(Entity entity, Packet packet) {
2935
//noinspection deprecation
3036
((MinecraftServer) FabricLoader.getInstance().getGameInstance())
3137
.method_2165(entity.world.dimension.id).method_1670(entity, packet);
3238
}
39+
40+
@Override
41+
public void dispatchLocallyAndToAllTracking(Entity entity, Packet packet) {
42+
send(packet);
43+
sendToAllTracking(entity, packet);
44+
}
3345
}

0 commit comments

Comments
 (0)