Skip to content

Commit

Permalink
Added sendToAllTrackingPlayers (#215)
Browse files Browse the repository at this point in the history
* Fixed meshes being broken

* Send a packet to all tracking players :D

* Comment out mesh fix

The bug only affects models created in Blockbench so this will be commented out for a better fix in the future.

* Revert "Comment out mesh fix"

This reverts commit f415f18.

* Revert "Send a packet to all tracking players :D"

This reverts commit ef27862.

* Reapply "Send a packet to all tracking players :D"

This reverts commit 9167a5c.

* Revert mesh fix on this branch
  • Loading branch information
Bomb787 authored Nov 4, 2024
1 parent a8f58fa commit fa59252
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;

import java.util.function.Function;

Expand All @@ -20,6 +21,10 @@ public static void sendToPlayer(Message m, ServerPlayer e) {
INSTANCE.sendToPlayer(m, e);
}

public static void sendToTrackingPlayers(Message m, Entity origin) {
INSTANCE.sendToTrackingPlayers(m, origin);
}

public abstract static class Impl {
protected Impl() {
INSTANCE = this;
Expand All @@ -30,5 +35,7 @@ protected Impl() {
public abstract void sendToServer(Message m);

public abstract void sendToPlayer(Message m, ServerPlayer e);

public abstract void sendToTrackingPlayers(Message m, Entity origin);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import io.netty.buffer.Unpooled;
import net.fabricmc.api.EnvType;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;

import java.util.HashMap;
import java.util.Locale;
Expand Down Expand Up @@ -60,6 +63,15 @@ public void sendToPlayer(Message msg, ServerPlayer e) {
ServerPlayNetworking.send(e, getMessageIdentifier(msg), buf);
}

@Override
public void sendToTrackingPlayers(Message msg, Entity origin) {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
msg.encode(buf);
for(ServerPlayer player : PlayerLookup.tracking(origin)) {
ServerPlayNetworking.send(player, getMessageIdentifier(msg), buf);
}
}

// Fabric's APIs are not side-agnostic.
// We punt this to a separate class file to keep it from being eager-loaded on a server environment.
private static final class ClientProxy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.network.simple.SimpleChannel;
Expand Down Expand Up @@ -47,4 +48,10 @@ public void sendToServer(Message m) {
public void sendToPlayer(Message m, ServerPlayer e) {
channel.send(PacketDistributor.PLAYER.with(() -> e), m);
}

@Override
public void sendToTrackingPlayers(Message m, Entity origin) {
channel.send(PacketDistributor.TRACKING_ENTITY.with(() -> origin), m);
}

}

0 comments on commit fa59252

Please sign in to comment.