/* * This file is part of Cubic Chunks Mod, licensed under the MIT License (MIT). * * Copyright (c) 2015 contributors * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package cubicchunks.network; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper; import net.minecraftforge.fml.relauncher.Side; import cubicchunks.CubicChunks; /** * Wrapper class for SimpleNetworkWrapper. */ public class PacketDispatcher { // a simple counter will allow us to get rid of 'magic' numbers used during packet registration private static byte packetId = 0; /** * The SimpleNetworkWrapper instance is used both to register and send packets. * Since I will be adding wrapper methods, this field is private, but you should * make it public if you plan on using it directly. */ private static final SimpleNetworkWrapper dispatcher = NetworkRegistry.INSTANCE.newSimpleChannel(CubicChunks.MODID); /** * Registers all packets. Side of a packet is the side on which the packet is handled. */ public static final void registerPackets() { registerMessage(PacketCube.Handler.class, PacketCube.class); registerMessage(PacketColumn.Handler.class, PacketColumn.class); registerMessage(PacketUnloadColumn.Handler.class, PacketUnloadColumn.class); registerMessage(PacketUnloadCube.Handler.class, PacketUnloadCube.class); registerMessage(PacketCubeBlockChange.Handler.class, PacketCubeBlockChange.class); } /** * Registers a message and message handler */ private static final <REQ extends IMessage, REPLY extends IMessage> void registerMessage(Class<? extends IMessageHandler<REQ, REPLY>> handlerClass, Class<REQ> messageClass) { Side side = AbstractClientMessageHandler.class.isAssignableFrom(handlerClass) ? Side.CLIENT : Side.SERVER; PacketDispatcher.dispatcher.registerMessage(handlerClass, messageClass, packetId++, side); } /** * Send this message to the specified player. * See {@link SimpleNetworkWrapper#sendTo(IMessage, EntityPlayerMP)} */ public static final void sendTo(IMessage message, EntityPlayerMP player) { PacketDispatcher.dispatcher.sendTo(message, player); } /** * Send this message to everyone within a certain range of a point. * See {@link SimpleNetworkWrapper#sendToAllAround(IMessage, NetworkRegistry.TargetPoint)} */ public static final void sendToAllAround(IMessage message, NetworkRegistry.TargetPoint point) { PacketDispatcher.dispatcher.sendToAllAround(message, point); } /** * Sends a message to everyone within a certain range of the coordinates in the same dimension. */ public static final void sendToAllAround(IMessage message, int dimension, double x, double y, double z, double range) { PacketDispatcher.sendToAllAround(message, new NetworkRegistry.TargetPoint(dimension, x, y, z, range)); } /** * Sends a message to everyone within a certain range of the player provided. */ public static final void sendToAllAround(IMessage message, EntityPlayer player, double range) { PacketDispatcher.sendToAllAround(message, player.world.provider.getDimension(), player.posX, player.posY, player.posZ, range); } /** * Send this message to everyone within the supplied dimension. * See {@link SimpleNetworkWrapper#sendToDimension(IMessage, int)} */ public static final void sendToDimension(IMessage message, int dimensionId) { PacketDispatcher.dispatcher.sendToDimension(message, dimensionId); } /** * Send this message to the server. * See {@link SimpleNetworkWrapper#sendToServer(IMessage)} */ public static final void sendToServer(IMessage message) { PacketDispatcher.dispatcher.sendToServer(message); } }