#Item Potion Effect in inventory
19 messages · Page 1 of 1 (latest)
Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!
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
Can I get an examplr
you want a fully working script, or a simple example?
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
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
The second method does not register when the item is no longer in the inventory
[➤](#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);
})
})