Java Code Examples for com.comphenix.protocol.events.PacketEvent#setCancelled()

The following examples show how to use com.comphenix.protocol.events.PacketEvent#setCancelled() . 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: EntityHider.java    From BetonQuest with GNU General Public License v3.0 10 votes vote down vote up
/**
 * Construct the packet listener that will be used to intercept every
 * entity-related packet.
 *
 * @param plugin - the parent plugin.
 * @return The packet listener.
 */
private PacketAdapter constructProtocol(Plugin plugin) {
    return new PacketAdapter(plugin, ENTITY_PACKETS) {

        @Override
        public void onPacketSending(PacketEvent event) {
            int index = event.getPacketType() == COMBAT_EVENT ? 1 : 0;

            Integer entityID = event.getPacket().getIntegers().readSafely(index);
            if (entityID != null) {
                if (!isVisible(event.getPlayer(), entityID)) {
                    event.setCancelled(true);
                }
            }
        }
    };
}
 
Example 2
Source File: RemoveEffectPacketListener.java    From WorldGuardExtraFlagsPlugin with MIT License 5 votes vote down vote up
@Override
public void onPacketSending(PacketEvent event)
{
	if (!event.isCancelled())
	{
		Player player = event.getPlayer();
		if (!player.isValid()) //Work around, getIfPresent is broken inside WG due to using LocalPlayer as key instead of CacheKey
		{
			return;
		}

		try
		{
			Session session = WorldGuardExtraFlagsPlugin.getPlugin().getWorldGuardCommunicator().getSessionManager().get(player);
			
			GiveEffectsFlagHandler giveEffectsHandler = session.getHandler(GiveEffectsFlagHandler.class);
			if (giveEffectsHandler.isSupressRemovePotionPacket())
			{
				event.setCancelled(true);
			}
		}
		catch(IllegalStateException wgBug)
		{
			
		}
	}
}
 
Example 3
Source File: CFIPacketListener.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
private void handleBlockEvent(PacketEvent event, boolean relative, RunnableVal3<PacketEvent, VirtualWorld, Vector> task) {
    VirtualWorld gen = getGenerator(event);
    if (gen != null) {
        Vector pt = getRelPos(event, gen);
        if (pt != null) {
            if (relative) pt = getRelative(event, pt);
            if (gen.contains(pt)) {
                event.setCancelled(true);
                task.run(event, gen, pt);
            }
        }
    }
}
 
Example 4
Source File: PacketInterceptor.java    From BetonQuest with GNU General Public License v3.0 5 votes vote down vote up
public PacketInterceptor(Conversation conv, String playerID) {
    this.conv = conv;
    this.player = PlayerConverter.getPlayer(playerID);

    // Intercept Packets
    packetAdapter = new PacketAdapter(BetonQuest.getInstance(), ListenerPriority.HIGHEST,
            PacketType.Play.Server.CHAT

    ) {
        @Override
        public void onPacketSending(PacketEvent event) {
            if (event.getPlayer() != player) {
                return;
            }

            if (event.getPacketType().equals(PacketType.Play.Server.CHAT)) {
                PacketContainer packet = event.getPacket();
                BaseComponent[] bc = (BaseComponent[]) packet.getModifier().read(1);
                if (bc != null && bc.length > 0 && ((TextComponent) bc[0]).getText().contains("_bq_")) {
                    packet.getModifier().write(1, Arrays.copyOfRange(bc, 1, bc.length));
                    event.setPacket(packet);
                    return;
                }

                // Else save message to replay later
                WrapperPlayServerChat chat = new WrapperPlayServerChat(event.getPacket());
                event.setCancelled(true);
                messages.add(chat);
            }
        }
    };

    ProtocolLibrary.getProtocolManager().addPacketListener(packetAdapter);
}
 
Example 5
Source File: TabCompletePacketAdapter.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onPacketReceiving(PacketEvent event) {
    if (event.getPacketType() == PacketType.Play.Client.TAB_COMPLETE) {
        try {
            if (!playerCache.isAuthenticated(event.getPlayer().getName())) {
                event.setCancelled(true);
            }
        } catch (FieldAccessException e) {
            logger.logException("Couldn't access field:", e);
        }
    }
}
 
Example 6
Source File: InventoryPacketAdapter.java    From AuthMeReloaded with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onPacketSending(PacketEvent packetEvent) {
    Player player = packetEvent.getPlayer();
    PacketContainer packet = packetEvent.getPacket();

    int windowId = packet.getIntegers().read(0);
    if (windowId == PLAYER_INVENTORY && shouldHideInventory(player.getName())) {
        packetEvent.setCancelled(true);
    }
}