Search in sources :

Example 1 with IMixinNetPlayHandler

use of org.spongepowered.mod.interfaces.IMixinNetPlayHandler in project SpongeForge by SpongePowered.

the class SpongeModNetworkManager method onCustomPacketRegistration.

@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onCustomPacketRegistration(CustomPacketRegistrationEvent<?> event) {
    Set<String> channels = ((IMixinNetPlayHandler) event.getHandler()).getRegisteredChannels();
    ;
    final boolean isMainThread = Sponge.isServerAvailable() && Sponge.getServer().isMainThread();
    try (final CauseStackManager.StackFrame frame = isMainThread ? Sponge.getCauseStackManager().pushCauseFrame() : null) {
        if (isMainThread) {
            if (event.getHandler() instanceof NetHandlerPlayServer) {
                Sponge.getCauseStackManager().pushCause(((NetHandlerPlayServer) event.getHandler()).player);
            }
            Sponge.getCauseStackManager().addContext(NET_HANDLER, event.getHandler());
        }
        if (event.getOperation().equals("REGISTER")) {
            channels.addAll(event.getRegistrations());
            for (String channel : event.getRegistrations()) {
                final Cause currentCause = isMainThread ? Sponge.getCauseStackManager().getCurrentCause() : Cause.of(EventContext.empty(), Sponge.getGame());
                SpongeImpl.postEvent(SpongeEventFactory.createChannelRegistrationEventRegister(currentCause, channel));
            }
        } else if (event.getOperation().equals("UNREGISTER")) {
            channels.removeAll(event.getRegistrations());
            for (String channel : event.getRegistrations()) {
                final Cause currentCause = isMainThread ? Sponge.getCauseStackManager().getCurrentCause() : Cause.of(EventContext.empty(), Sponge.getGame());
                SpongeImpl.postEvent(SpongeEventFactory.createChannelRegistrationEventUnregister(currentCause, channel));
            }
        }
    }
}
Also used : CauseStackManager(org.spongepowered.api.event.CauseStackManager) Cause(org.spongepowered.api.event.cause.Cause) NetHandlerPlayServer(net.minecraft.network.NetHandlerPlayServer) IMixinNetPlayHandler(org.spongepowered.mod.interfaces.IMixinNetPlayHandler) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Aggregations

NetHandlerPlayServer (net.minecraft.network.NetHandlerPlayServer)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 Cause (org.spongepowered.api.event.cause.Cause)1 IMixinNetPlayHandler (org.spongepowered.mod.interfaces.IMixinNetPlayHandler)1