ProtocolLib

3M Downloads

Using WrappedParticle to modify particle type only for certain players

Aztlon opened this issue ยท 2 comments

commented

My server is using the ProjectKorra plugin, which is like a magic plugin that displays a lot of particles, and Geyser so that Bedrock players can join. Airbending produces a lot of spell particles, which is very laggy for Bedrock users, but I really like this particle so I would rather not change it for everyone, only Bedrock users, into cloud particles. Anyway, I was successful in doing this with ProtocolLib, but it sometimes changes the particles for Java users as well. It changes about half of them to cloud, seemingly at random, for Java users, while Bedrock users see only cloud. I think it might be because the packets are reused or something, so that once the particle is changed for one person, it's changed for everyone? I'm not sure, but here is my code:

if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null) {
	ProtocolManager manager = ProtocolLibrary.getProtocolManager();
	manager.addPacketListener(new PacketAdapter(this, ListenerPriority.HIGHEST, PacketType.Play.Server.WORLD_PARTICLES) {
		@Override
		public void onPacketSending(PacketEvent event) {
			Player player = event.getPlayer();
			FloodgateApi api = FloodgateApi.getInstance();
			if (api.isFloodgatePlayer(player.getUniqueId())) { // player is a bedrock player
				StructureModifier<WrappedParticle> particles = event.getPacket().getNewParticles();
				if (particles.read(0).getParticle() == Particle.SPELL) {
					particles.write(0, WrappedParticle.create(Particle.CLOUD, 0));
				}
			}
		}
	});
}

Do you have any suggestions for how I should handle this problem? I'm kind of at a loss right now.

commented

I would suggest to deep clone the packet before modifying it, e.g.:

@Override
public void onPacketSending(PacketEvent event) {
        PacketContainer packet = event.getPacket().deepClone();
        // modify 'packet' here
        event.setPacket(packet);
}

Please note that this involves quite some overhead.

commented

@lukalt Thanks king, you saved the day. You dropped this: ๐Ÿ‘‘