    001    package cpw.mods.fml.common.network;
    002
    003    import net.minecraft.entity.player.EntityPlayerMP;
    004    import net.minecraft.network.packet.Packet;
    005    import net.minecraft.network.packet.Packet131MapData;
    006    import net.minecraft.network.packet.Packet250CustomPayload;
    007    import net.minecraft.server.MinecraftServer;
    008    import cpw.mods.fml.common.FMLCommonHandler;
    009    import cpw.mods.fml.common.FMLLog;
    010
    011    /**
    012     * A simple utility class to send packet 250 packets around the place
    013     *
    014     * @author cpw
    015     *
    016     */
    017    public class PacketDispatcher
    018    {
    019        public static Packet250CustomPayload getPacket(String type, byte[] data)
    020        {
    021            return new Packet250CustomPayload(type, data);
    022        }
    023
    024        public static void sendPacketToServer(Packet packet)
    025        {
    026            FMLCommonHandler.instance().getSidedDelegate().sendPacket(packet);
    027        }
    028
    029        public static void sendPacketToPlayer(Packet packet, Player player)
    030        {
    031            if (player instanceof EntityPlayerMP)
    032            {
    033                ((EntityPlayerMP)player).playerNetServerHandler.sendPacketToPlayer(packet);
    034            }
    035        }
    036
    037        public static void sendPacketToAllAround(double X, double Y, double Z, double range, int dimensionId, Packet packet)
    038        {
    039            MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
    040            if (server != null)
    041            {
    042                server.getConfigurationManager().sendToAllNear(X, Y, Z, range, dimensionId, packet);
    043            }
    044            else
    045            {
    046                FMLLog.fine("Attempt to send packet to all around without a server instance available");
    047            }
    048        }
    049
    050        public static void sendPacketToAllInDimension(Packet packet, int dimId)
    051        {
    052            MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
    053            if (server != null)
    054            {
    055                server.getConfigurationManager().sendPacketToAllPlayersInDimension(packet, dimId);
    056            }
    057            else
    058            {
    059                FMLLog.fine("Attempt to send packet to all in dimension without a server instance available");
    060            }
    061        }
    062
    063        public static void sendPacketToAllPlayers(Packet packet)
    064        {
    065            MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
    066            if (server != null)
    067            {
    068                server.getConfigurationManager().sendPacketToAllPlayers(packet);
    069            }
    070            else
    071            {
    072                FMLLog.fine("Attempt to send packet to all in dimension without a server instance available");
    073            }
    074        }
    075
    076        public static Packet131MapData getTinyPacket(Object mod, short tag, byte[] data)
    077        {
    078            NetworkModHandler nmh = FMLNetworkHandler.instance().findNetworkModHandler(mod);
    079            return new Packet131MapData((short) nmh.getNetworkId(), tag, data);
    080        }
    081    }