<-- Main Page <-- KubeJS-Journey

Adding Potion Effects to Players

I have no idea why this has such poor documentation online, but it really is quite simple.

Potion effects can be easily applied to players with the following code:


    player.potionEffects.add([EFFECT_HERE], [DURATION], [EFFECT_LEVEL], true/false, true/false)
    

Yes, it really is that simple.

As an example, here is some code I wrote to make it so that players can only use one backpack at a time:


    // Tag items that will be counted as "big bags"
    ServerEvents.tags('item', event => {
      // Big bags
      event.add('materials/big_bag', 'examplemod:example_bag_item_id')
    });

    ServerEvents.tick(event => {
      // Apply effect to players with 2 or more backpacks
      event.server.players.forEach(player => {
        // Check if player is dead, in spectator mode, or in creative mode and if so pass
        if (!player.alive || player.isSpectator() || !player.isCreative()) return;
        // Counting number of items tagged "#materials/big_bag"
        const bigBag = player.inventory.count('#materials/big_bag');
        // If number of materials tagged is greater than 1, add potion effect
        if (bigBag > 1) {
            // Applying effect
            player.potionEffects.add([EFFECT_HERE], [DURATION], [LEVEL], true/false, true/false)
        }
      });
    });
    

I also created my own potion effect to apply using KubeJS, but there are numerous tutorials out there for that.

Always plenty to learn, huh?