    001/*
    002 * Forge Mod Loader
    003 * Copyright (c) 2012-2013 cpw.
    004 * All rights reserved. This program and the accompanying materials
    005 * are made available under the terms of the GNU Lesser Public License v2.1
    006 * which accompanies this distribution, and is available at
    007 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    008 *
    009 * Contributors:
    010 *     cpw - implementation
    011 */
    012
    013package cpw.mods.fml.common.network;
    014
    015import net.minecraft.entity.player.EntityPlayerMP;
    016import net.minecraft.network.packet.Packet;
    017import net.minecraft.network.packet.Packet131MapData;
    018import net.minecraft.network.packet.Packet250CustomPayload;
    019import net.minecraft.server.MinecraftServer;
    020import cpw.mods.fml.common.FMLCommonHandler;
    021import cpw.mods.fml.common.FMLLog;
    022
    023/**
    024 * A simple utility class to send packet 250 packets around the place
    025 *
    026 * @author cpw
    027 *
    028 */
    029public class PacketDispatcher
    030{
    031    public static Packet250CustomPayload getPacket(String type, byte[] data)
    032    {
    033        return new Packet250CustomPayload(type, data);
    034    }
    035
    036    public static void sendPacketToServer(Packet packet)
    037    {
    038        FMLCommonHandler.instance().getSidedDelegate().sendPacket(packet);
    039    }
    040
    041    public static void sendPacketToPlayer(Packet packet, Player player)
    042    {
    043        if (player instanceof EntityPlayerMP)
    044        {
    045            ((EntityPlayerMP)player).playerNetServerHandler.sendPacketToPlayer(packet);
    046        }
    047    }
    048
    049    public static void sendPacketToAllAround(double X, double Y, double Z, double range, int dimensionId, Packet packet)
    050    {
    051        MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
    052        if (server != null)
    053        {
    054            server.getConfigurationManager().sendToAllNear(X, Y, Z, range, dimensionId, packet);
    055        }
    056        else
    057        {
    058            FMLLog.fine("Attempt to send packet to all around without a server instance available");
    059        }
    060    }
    061
    062    public static void sendPacketToAllInDimension(Packet packet, int dimId)
    063    {
    064        MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
    065        if (server != null)
    066        {
    067            server.getConfigurationManager().sendPacketToAllPlayersInDimension(packet, dimId);
    068        }
    069        else
    070        {
    071            FMLLog.fine("Attempt to send packet to all in dimension without a server instance available");
    072        }
    073    }
    074
    075    public static void sendPacketToAllPlayers(Packet packet)
    076    {
    077        MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
    078        if (server != null)
    079        {
    080            server.getConfigurationManager().sendPacketToAllPlayers(packet);
    081        }
    082        else
    083        {
    084            FMLLog.fine("Attempt to send packet to all in dimension without a server instance available");
    085        }
    086    }
    087
    088    public static Packet131MapData getTinyPacket(Object mod, short tag, byte[] data)
    089    {
    090        NetworkModHandler nmh = FMLNetworkHandler.instance().findNetworkModHandler(mod);
    091        return new Packet131MapData((short) nmh.getNetworkId(), tag, data);
    092    }
    093}