mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-13 08:44:25 +00:00
Clean up FMLEventChannel. Closes #367.
This commit is contained in:
1 parent
03fb1879d7
commit
6bb9b8b953
1 file changed
+91
-11
@@ -6,25 +6,90 @@ import java.util.EnumMap;
|
||||
import net.minecraft.client.network.NetHandlerPlayClient;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.network.NetHandlerPlayServer;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.eventhandler.EventBus;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
|
||||
/**
|
||||
* An event driven network channel, using {@link FMLNetworkEvent.CustomPacketEvent} and {@link FMLNetworkEvent.CustomNetworkEvent}
|
||||
* to deliver messages to an event listener. There is one "bus" for each channel, due to the
|
||||
* impossibility of filtering a bus for specific events.
|
||||
*
|
||||
* This event driven system completely wraps the netty code. Mod code deals with FMLProxyPackets directly. It is not
|
||||
* possible to enhance the netty pipeline, and I would expect highly unexpected results if it were modified reflectively.
|
||||
* Use a real ChannelHandler if you want to use netty.
|
||||
*
|
||||
* @author cpw
|
||||
*
|
||||
*/
|
||||
public class FMLEventChannel {
|
||||
private EnumMap<Side, FMLEmbeddedChannel> channels;
|
||||
private EventBus eventBus;
|
||||
|
||||
/*
|
||||
* This is done this way so that the CLIENT specific code in the factory only loads on the client
|
||||
*/
|
||||
private enum EventFactory
|
||||
{
|
||||
SERVER()
|
||||
{
|
||||
@Override
|
||||
FMLNetworkEvent.CustomPacketEvent<?> make(FMLProxyPacket msg)
|
||||
{
|
||||
FMLNetworkEvent.CustomPacketEvent<?> event = null;
|
||||
if (msg.handler() instanceof NetHandlerPlayServer)
|
||||
{
|
||||
NetHandlerPlayServer server = (NetHandlerPlayServer) msg.handler();
|
||||
event = new FMLNetworkEvent.ServerCustomPacketEvent(server.func_147362_b(), msg);
|
||||
}
|
||||
return event;
|
||||
}
|
||||
},
|
||||
CLIENT()
|
||||
{
|
||||
@Override
|
||||
FMLNetworkEvent.CustomPacketEvent<?> make(FMLProxyPacket msg)
|
||||
{
|
||||
FMLNetworkEvent.CustomPacketEvent<?> event = null;
|
||||
if (msg.handler() instanceof NetHandlerPlayClient)
|
||||
{
|
||||
NetHandlerPlayClient client = (NetHandlerPlayClient) msg.handler();
|
||||
event = new FMLNetworkEvent.ClientCustomPacketEvent(client.func_147298_b(), msg);
|
||||
}
|
||||
else if (msg.handler() instanceof NetHandlerPlayServer)
|
||||
{
|
||||
NetHandlerPlayServer server = (NetHandlerPlayServer) msg.handler();
|
||||
event = new FMLNetworkEvent.ServerCustomPacketEvent(server.func_147362_b(), msg);
|
||||
}
|
||||
return event;
|
||||
}
|
||||
};
|
||||
abstract FMLNetworkEvent.CustomPacketEvent<?> make(FMLProxyPacket msg);
|
||||
}
|
||||
|
||||
private static EventFactory factory = FMLCommonHandler.instance().getSide() == Side.CLIENT ? EventFactory.CLIENT : EventFactory.SERVER;
|
||||
FMLEventChannel(String name)
|
||||
{
|
||||
this.channels = NetworkRegistry.INSTANCE.newChannel(name, new NetworkEventFiringHandler(this));
|
||||
this.eventBus = new EventBus();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an event listener with this channel and bus. See {@link SubscribeEvent}
|
||||
*
|
||||
* @param object
|
||||
*/
|
||||
public void register(Object object)
|
||||
{
|
||||
this.eventBus.register(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister an event listener from the bus.
|
||||
* @param object
|
||||
*/
|
||||
public void unregister(Object object)
|
||||
{
|
||||
this.eventBus.unregister(object);
|
||||
@@ -32,17 +97,7 @@ public class FMLEventChannel {
|
||||
|
||||
void fireRead(FMLProxyPacket msg, ChannelHandlerContext ctx)
|
||||
{
|
||||
FMLNetworkEvent.CustomPacketEvent<?> event = null;
|
||||
if (msg.handler() instanceof NetHandlerPlayClient)
|
||||
{
|
||||
NetHandlerPlayClient client = (NetHandlerPlayClient) msg.handler();
|
||||
event = new FMLNetworkEvent.ClientCustomPacketEvent(client.func_147298_b(), msg);
|
||||
}
|
||||
else if (msg.handler() instanceof NetHandlerPlayServer)
|
||||
{
|
||||
NetHandlerPlayServer server = (NetHandlerPlayServer) msg.handler();
|
||||
event = new FMLNetworkEvent.ServerCustomPacketEvent(server.func_147362_b(), msg);
|
||||
}
|
||||
FMLNetworkEvent.CustomPacketEvent<?> event = factory.make(msg);
|
||||
if (event != null)
|
||||
{
|
||||
this.eventBus.post(event);
|
||||
@@ -60,12 +115,23 @@ public class FMLEventChannel {
|
||||
this.eventBus.post(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a packet to all on the server
|
||||
*
|
||||
* @param pkt
|
||||
*/
|
||||
public void sendToAll(FMLProxyPacket pkt)
|
||||
{
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL);
|
||||
channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send to a specific player
|
||||
*
|
||||
* @param pkt
|
||||
* @param player
|
||||
*/
|
||||
public void sendTo(FMLProxyPacket pkt, EntityPlayerMP player)
|
||||
{
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER);
|
||||
@@ -73,6 +139,11 @@ public class FMLEventChannel {
|
||||
channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send to all around a point
|
||||
* @param pkt
|
||||
* @param point
|
||||
*/
|
||||
public void sendToAllAround(FMLProxyPacket pkt, NetworkRegistry.TargetPoint point)
|
||||
{
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALLAROUNDPOINT);
|
||||
@@ -80,6 +151,11 @@ public class FMLEventChannel {
|
||||
channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send to all in a dimension
|
||||
* @param pkt
|
||||
* @param dimensionId
|
||||
*/
|
||||
public void sendToDimension(FMLProxyPacket pkt, int dimensionId)
|
||||
{
|
||||
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.DIMENSION);
|
||||
@@ -87,6 +163,10 @@ public class FMLEventChannel {
|
||||
channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send to the server
|
||||
* @param pkt
|
||||
*/
|
||||
public void sendToServer(FMLProxyPacket pkt)
|
||||
{
|
||||
channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER);
|
||||
|
||||
Reference in new issue
Block a user