Java Code Examples for net.minecraftforge.fml.network.NetworkEvent#Context

The following examples show how to use net.minecraftforge.fml.network.NetworkEvent#Context . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndexedMessageCodec.java    From patchwork-api with GNU Lesser General Public License v2.1 6 votes vote down vote up
void consume(PacketByteBuf payload, int payloadIndex, NetworkEvent.Context context) {
	if (payload == null) {
		LOGGER.error(SIMPLENET, "Received empty payload on channel {}", channelName);
		return;
	}

	short discriminator = payload.readUnsignedByte();
	final MessageHandler<?> messageHandler = indices.get(discriminator);

	if (messageHandler == null) {
		LOGGER.error(SIMPLENET, "Received invalid discriminator byte {} on channel {}", discriminator, channelName);
		return;
	}

	tryDecode(payload, context, payloadIndex, messageHandler);
}
 
Example 2
Source File: PacketChangeRange.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static void handle(PacketChangeRange msg, Supplier<NetworkEvent.Context> ctx) {
    ctx.get().enqueueWork(() -> {
        ServerPlayerEntity player = ctx.get().getSender();
        if (player == null)
            return;

        ItemStack stack = MiningGadget.getGadget(player);
        MiningProperties.setBeamRange(stack, msg.range);
    });

    ctx.get().setPacketHandled(true);
}
 
Example 3
Source File: PacketCustomChannelBuilder.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SubscribeEvent
public void onClientPayload(NetworkEvent.LoginPayloadEvent event) {
    PacketCustom packet = new PacketCustom(event.getPayload());
    NetworkEvent.Context ctx = event.getSource().get();
    INetHandler netHandler = ctx.getNetworkManager().getNetHandler();
    ctx.setPacketHandled(true);
    if (netHandler instanceof ClientLoginNetHandler) {
        ClientLoginNetHandler nh = (ClientLoginNetHandler) netHandler;
        packetHandler.handleLoginPacket(packet, Minecraft.getInstance(), nh, ctx);
        //For _some_ reason sending this response packet in FML is private. So just spoof the packet :D
        ctx.getPacketDispatcher().sendPacket(new ResourceLocation("fml:handshake"), new PacketBuffer(Unpooled.buffer().writeByte(99)));
    }
}
 
Example 4
Source File: PacketOpenFilterContainer.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static void handle(PacketOpenFilterContainer msg, Supplier<NetworkEvent.Context> ctx) {
    ctx.get().enqueueWork(() -> {
        ServerPlayerEntity sender = ctx.get().getSender();
        if (sender == null)
            return;

        Container container = sender.openContainer;
        if (container == null)
            return;

        ItemStack stack = MiningGadget.getGadget(sender);
        if( stack.isEmpty() )
            return;

        ItemStackHandler ghostInventory = new ItemStackHandler(30) {
            @Override
            protected void onContentsChanged(int slot) {
                stack.getOrCreateTag().put(MiningProperties.KEY_FILTERS, serializeNBT());
            }
        };

        ghostInventory.deserializeNBT(stack.getOrCreateChildTag(MiningProperties.KEY_FILTERS));
        sender.openContainer(new SimpleNamedContainerProvider(
                (windowId, playerInventory, playerEntity) -> new FilterContainer(windowId, playerInventory, ghostInventory), new StringTextComponent("")
        ));
    });

    ctx.get().setPacketHandled(true);
}
 
Example 5
Source File: PacketCustomChannelBuilder.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SubscribeEvent
public void onServerPayload(NetworkEvent.ClientCustomPayloadEvent event) {
    PacketCustom packet = new PacketCustom(event.getPayload());
    NetworkEvent.Context ctx = event.getSource().get();
    INetHandler netHandler = ctx.getNetworkManager().getNetHandler();
    ctx.setPacketHandled(true);
    if (netHandler instanceof ServerPlayNetHandler) {
        ServerPlayNetHandler nh = (ServerPlayNetHandler) netHandler;
        ctx.enqueueWork(() -> packetHandler.handlePacket(packet, nh.player, nh));
    }
}
 
Example 6
Source File: PacketChangeMiningSize.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static void handle(PacketChangeMiningSize msg, Supplier<NetworkEvent.Context> ctx) {
    ctx.get().enqueueWork(() -> {
        ServerPlayerEntity player = ctx.get().getSender();
        if (player == null)
            return;

        ItemStack stack = MiningGadget.getGadget(player);
        MiningGadget.changeRange(stack);
    });

    ctx.get().setPacketHandled(true);
}
 
Example 7
Source File: PacketChangeVolume.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static void handle(PacketChangeVolume msg, Supplier<NetworkEvent.Context> ctx) {
    ctx.get().enqueueWork(() -> {
        ServerPlayerEntity player = ctx.get().getSender();
        if (player == null)
            return;

        ItemStack stack = MiningGadget.getGadget(player);

        // Active toggle feature
        MiningProperties.setVolume(stack, msg.volume);
    });

    ctx.get().setPacketHandled(true);
}
 
Example 8
Source File: PacketChangeFreezeDelay.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static void handle(PacketChangeFreezeDelay msg, Supplier<NetworkEvent.Context> ctx) {
    ctx.get().enqueueWork(() -> {
        ServerPlayerEntity player = ctx.get().getSender();
        if (player == null)
            return;

        ItemStack stack = MiningGadget.getGadget(player);

        // Active toggle feature
        MiningProperties.setFreezeDelay(stack, msg.freezeDelay);
    });

    ctx.get().setPacketHandled(true);
}
 
Example 9
Source File: PacketChangeBreakType.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static void handle(PacketChangeBreakType msg, Supplier<NetworkEvent.Context> ctx) {
    ctx.get().enqueueWork(() -> {
        ServerPlayerEntity player = ctx.get().getSender();
        if (player == null)
            return;

        ItemStack stack = MiningGadget.getGadget(player);
        MiningProperties.nextBreakType(stack);
    });

    ctx.get().setPacketHandled(true);
}
 
Example 10
Source File: PacketTogglePrecision.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static void handle(PacketTogglePrecision msg, Supplier<NetworkEvent.Context> ctx) {
    ctx.get().enqueueWork(() -> {
        ServerPlayerEntity player = ctx.get().getSender();
        if (player == null)
            return;

        ItemStack stack = MiningGadget.getGadget(player);

        // Active toggle feature
        MiningProperties.setPrecisionMode(stack, !MiningProperties.getPrecisionMode(stack));
    });

    ctx.get().setPacketHandled(true);
}
 
Example 11
Source File: SimpleChannel.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Function returning a boolean "packet handled" indication, for simpler channel building.
 * @param handler a handler
 * @return this
 */
public MessageBuilder<M> consumer(ToBooleanBiFunction<M, Supplier<NetworkEvent.Context>> handler) {
	this.consumer = (message, context) -> {
		boolean handled = handler.applyAsBool(message, context);
		context.get().setPacketHandled(handled);
	};
	return this;
}
 
Example 12
Source File: PacketToggleFilters.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static void handle(PacketToggleFilters msg, Supplier<NetworkEvent.Context> ctx) {
    ctx.get().enqueueWork(() -> {
        ServerPlayerEntity player = ctx.get().getSender();
        if (player == null)
            return;

        ItemStack stack = MiningGadget.getGadget(player);

        // Active toggle feature
        MiningProperties.setWhitelist(stack, !MiningProperties.getWhiteList(stack));
    });

    ctx.get().setPacketHandled(true);
}
 
Example 13
Source File: IndexedMessageCodec.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
MessageHandler(int index, Class<M> messageType, BiConsumer<M, PacketByteBuf> encoder, Function<PacketByteBuf, M> decoder, BiConsumer<M, Supplier<NetworkEvent.Context>> messageConsumer) {
	this.index = index;
	this.messageType = messageType;
	this.encoder = encoder;
	this.decoder = decoder;
	this.messageConsumer = messageConsumer;
	this.loginIndexGetter = null;
	this.loginIndexSetter = null;
	indices.put((short) (index & 0xff), this);
	types.put(messageType, this);
}
 
Example 14
Source File: ScrapingMessage.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void handle(Supplier<NetworkEvent.Context> ctx)
{
    ClientEvents.handleScrapingMessage(this);
}
 
Example 15
Source File: SimpleChannel.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
public <M> void reply(M msgToReply, NetworkEvent.Context context) {
	context.getPacketDispatcher().sendPacket(channelName, toBuffer(msgToReply).getLeft());
}
 
Example 16
Source File: SimpleChannel.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void onPacket(ICustomPacket<?> packet, NetworkEvent.Context context) {
	this.indexedCodec.consume(packet.getInternalData(), packet.getIndex(), context);
}
 
Example 17
Source File: IndexedMessageCodec.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
<M> MessageHandler<M> addCodecIndex(int index, Class<M> messageType, BiConsumer<M, PacketByteBuf> encoder, Function<PacketByteBuf, M> decoder, BiConsumer<M, Supplier<NetworkEvent.Context>> messageConsumer) {
	return new MessageHandler<>(index, messageType, encoder, decoder, messageConsumer);
}
 
Example 18
Source File: PacketDurabilitySync.java    From MiningGadgets with MIT License 4 votes vote down vote up
public static void handle(PacketDurabilitySync msg, Supplier<NetworkEvent.Context> ctx) {
    ctx.get().enqueueWork(() -> DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> clientPacketHandler(msg)));
    ctx.get().setPacketHandled(true);
}
 
Example 19
Source File: ICustomPacketHandler.java    From CodeChickenLib with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Called on the client to handle a login packet provided through {@link #gatherLoginPackets}.
 * This method unlike the other handlers, does not sync to the main thread for processing, instead
 * it is fired on the network thread, this is to allow mods to choose if the packet is important
 * in the handshake cycle and must be handled before anything else can continue. If the data isn't
 * critical to the handshake process, feel free to use {@link NetworkEvent.Context#enqueueWork(Runnable)}
 * to throw things on the main thread.
 *
 * @param packet  The packet to handle.
 * @param mc      The Minecraft instance.
 * @param handler Vanilla's NetHandler.
 * @param context The network context.
 */
@OnlyIn (Dist.CLIENT)
void handleLoginPacket(PacketCustom packet, Minecraft mc, IClientLoginNetHandler handler, NetworkEvent.Context context);
 
Example 20
Source File: ListenableChannel.java    From patchwork-api with GNU Lesser General Public License v2.1 votes vote down vote up
void onPacket(ICustomPacket<?> packet, NetworkEvent.Context context);