#Give player some effect if they touch certain item/have certain item in inventory

34 messages · Page 1 of 1 (latest)

little sand
#

I want to add some radiative things in modpack and integrate them to have give Alex's Cave radiation effect if player is near them or have them in inventory. Is it even possible? I think it is about PlayerEvent, but I am not sure how to implement it, or even it is possible implement it.

vast hemlockBOT
#

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

granite oasis
#
// Contains all Item IDs that should trigger the effect
const checkForIDs = [
    "minecraft:stick"
];

// Runs every tick
PlayerEvents.tick((event)=>{
    let player = event.player;
    let inventory = player.inventory.getAllItems();

    let contains = false;
    // Iterates over the items in the player's inventory, checking for the IDs
    for(const item of inventory){
        // If the currently iterated item is in the checkForIDs array, then set contains to true and break the loop
        if(checkForIDs.includes(item.id)){
            contains = true;
            break;
        }
    }
    // If the player's inventory does not contain any of the IDs, then return
    if(!contains) return;

    // Run a Minecraft command to give the player an effect
    // The effect is "alexsmobs:bug_pheromones" with a duration of 10 seconds and an amplifier of 1
    // The effect will display no particle effects (true/false at the end)
    event.server.runCommandSilent(`effect give ${player.username} alexsmobs:bug_pheromones 10 1 true`)
})

Change the checkForIDs array for items you want to check for, and ofc change the effect at the end to the desired effect you want. This inside of server_scripts folder.

Use this as an example and change it for your specific use case. This is an example of how i'd accomplish this. Though its best to just run this on the PlayerEvents.inventoryChanged event lol

#

Its a similar approach to when touching a block. If by "touch" a block you mean a right click or a left click, there's also events for that. BlockEvents.leftClicked and BlockEvents.rightClicked. The event will contain the block id with event.block.id

bold lark
#

you probably dont want to do this 20 times a second... maybe once every few seconds lol

granite oasis
#

I pulled this script out of my ass a second ago with no thought behind my eyes

bold lark
#

either that or a timer

granite oasis
#

Im not overly familiar with KubeJS, how would a timer work? Run it in the tick event and skip X amount of ticks or does KubeJS support things like setInterval?

bold lark
#

there is a built in timer, but i cant remember the syntax
if not you can just make a standard JS counter

let counter = 0
PlayerEvents.tick(e => {
  if(counter++ % 60 != 0) return
})

which runs once every 3 sec for instance

granite oasis
bold lark
#

@azure pine whats the tick counter thing, im going insane trying to find it

azure pine
#

e.level.time iirc hmmm

azure pine
#

or I'm wrong

bold lark
granite oasis
#

If you dont know, no one does

azure pine
#

ah true despair

pallid sandal
#

server.tickCount()?

bold lark
#

ooooh

#

server.tickCount without brackets

pallid sandal
#

yeah, i forgot it is a bean

bold lark
#

so then its

PlayerEvents.tick(e => {
  if(e.server.tickCount % 60 != 0) return
  // everything else here
})
azure pine
#

oh yea it's that

#

ye I forgor

little sand
#

Sorry for late checking, I will try

little sand
#

Also, I mean touching block means like player walking near them or stepping them.