/******************************************************************************* * AbyssalCraft * Copyright (c) 2012 - 2017 Shinoow. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v3 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl-3.0.txt * * Contributors: * Shinoow - implementation ******************************************************************************/ package com.shinoow.abyssalcraft.common.network; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import com.shinoow.abyssalcraft.AbyssalCraft; import com.shinoow.abyssalcraft.common.network.client.DisruptionMessage; import com.shinoow.abyssalcraft.common.network.client.EvilSheepMessage; import com.shinoow.abyssalcraft.common.network.client.NecroDataCapMessage; import com.shinoow.abyssalcraft.common.network.server.FireMessage; import com.shinoow.abyssalcraft.common.network.server.InterdimensionalCageMessage; import com.shinoow.abyssalcraft.common.network.server.OpenSpellbookMessage; import com.shinoow.abyssalcraft.common.network.server.StaffOfRendingMessage; 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; /** * * This class will house the SimpleNetworkWrapper instance, which I will name 'dispatcher', * as well as give us a logical place from which to register our packets. These two things * could be done anywhere, however, even in your Main class, but I will be adding other * functionality (see below) that gives this class a bit more utility. * * While unnecessary, I'm going to turn this class into a 'wrapper' for SimpleNetworkWrapper * so that instead of writing "PacketDispatcher.dispatcher.{method}" I can simply write * "PacketDispatcher.{method}" All this does is make it quicker to type and slightly shorter; * if you do not care about that, then make the 'dispatcher' field public instead of private, * or, if you do not want to add a new class just for one field and one static method that * you could put anywhere, feel free to put them wherever. * * For further convenience, I have also added two extra sendToAllAround methods: one which * takes an EntityPlayer and one which takes coordinates. * * @author coolAlias */ public class PacketDispatcher { 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(AbyssalCraft.modid); /** * Call this during pre-init or loading and register all of your packets (messages) here */ public static final void registerPackets() { registerMessage(FireMessage.class); registerMessage(EvilSheepMessage.class); registerMessage(DisruptionMessage.class); registerMessage(StaffOfRendingMessage.class); registerMessage(InterdimensionalCageMessage.class); registerMessage(OpenSpellbookMessage.class); registerMessage(NecroDataCapMessage.class); } /** * Registers an {@link AbstractMessage} to the appropriate side(s) */ private static final <T extends AbstractMessage<T> & IMessageHandler<T, IMessage>> void registerMessage(Class<T> clazz) { // We can tell by the message class which side to register it on by using #isAssignableFrom (google it) if (AbstractMessage.AbstractClientMessage.class.isAssignableFrom(clazz)) PacketDispatcher.dispatcher.registerMessage(clazz, clazz, packetId++, Side.CLIENT); else if (AbstractMessage.AbstractServerMessage.class.isAssignableFrom(clazz)) PacketDispatcher.dispatcher.registerMessage(clazz, clazz, packetId++, Side.SERVER); else { PacketDispatcher.dispatcher.registerMessage(clazz, clazz, packetId, Side.CLIENT); PacketDispatcher.dispatcher.registerMessage(clazz, clazz, packetId++, Side.SERVER); } } //========================================================// // The following methods are the 'wrapper' methods; again, // this just makes sending a message slightly more compact // and is purely a matter of stylistic preference //========================================================// /** * 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#sendToDimension(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); } }