#Item Potion Effect in inventory

19 messages · Page 1 of 1 (latest)

cobalt kestrel
#

I was wondering how you would apply a potion effect if an item enters the player hotbar or inventory? plus if it is still in the inventory to continuously provide the potion effects?

uneven marshBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

granite carbon
#

You could do it through PlayerEvents.tick and just continuously check the inventory for the item but I think a better way would be to apply a potion effect with an infinite timer with ItemEvents.pickedup and then clear the effect with Itemevents.dropped

potent wagon
#

replace full armor check with an inventory find check

cobalt kestrel
#

Can I get an examplr

potent wagon
#

you want a fully working script, or a simple example?

potent wagon
#

nvm, the inventory change event doesn't work in this case

#
const itemEffects = {
    'minecraft:diamond': {
        'minecraft:speed': 0,
        'minecraft:jump_boost': 0
    }
}
const checkTime = 40

PlayerEvents.tick(e=> {
    const player = e.player;
    if(player.age % checkTime) return;

    for(const item in itemEffects){
        const effects = itemEffects[item];
        if(player.inventory.find(item) == -1) for(const id in effects) player.removeEffect(id);
        else for(const id in effects) player.potionEffects.add(id, checkTime, effects[id]);
    }
})
#

the most inefficient thing is using player tick event

potent wagon
#

a new way to implement this:

const itemEffects = {
    'minecraft:diamond': {
        'minecraft:speed': 0,
        'minecraft:jump_boost': 0
    }
};
const INFINITE = Java.loadClass('java.lang.Integer').MAX_VALUE;

Object.keys(itemEffects).forEach(item => {
    function findLoop(player) {
        player.server.scheduleInTicks(5, callback => {
            if(!player || player.inventory.find(item) == -1) for(const id in effects) player.removeEffect(id);
            else callback.reschedule();
        })
    };
    const effects = itemEffects[item];
    PlayerEvents.inventoryChanged(item, e => {
        const player = e.player;
        for(const id in effects) player.potionEffects.add(id, INFINITE, effects[id]);
        findLoop(player);
    });
    PlayerEvents.loggedIn(e => {
        const player = e.player;
        if(player.inventory.find(item) != -1) findLoop(player);
    });
})
#

this time using a scedule method to check, just like player tick

#

but it won't trigger until player have that item in inventory

#

the check interval is 5, but it can be higher for slightly better performance

#

also, if it's in server, once the player is being offline, the loop for him stops

cobalt kestrel
#

The second method does not register when the item is no longer in the inventory

potent wagon
#

register? you mean being effective?

#

use this:

flat jacinthBOT
#

[➤](#1255456379532087308 message)
Having specific item in inventory gives effects, should be performant.

const itemEffects = {
    'minecraft:diamond': {
        'minecraft:speed': 0,
        'minecraft:jump_boost': 0
    }
};

Object.keys(itemEffects).forEach(item => {
    const effects = itemEffects[item];
    function findLoop(player) {
        player.server.scheduleInTicks(5, callback => {
            if(!player || player.inventory.find(item) == -1) for(const id in effects) player.removeEffect(id);
            else callback.reschedule();
        })
    };
    PlayerEvents.inventoryChanged(item, e => {
        const player = e.player;
        for(const id in effects) player.potionEffects.add(id, Infinity, effects[id]);
        findLoop(player);
    });
    PlayerEvents.loggedIn(e => {
        const player = e.player;
        if(player.inventory.find(item) != -1) findLoop(player);
    })
})