#new onJumpAfterEvent using constructors

1 messages ยท Page 1 of 1 (latest)

summer geyser
#

TS ```ts
import { Dimension, Player, system, Vector3, world } from "@minecraft/server";

export class onJumpAfterEvent {
static jumpTag: string = "afterevent.jumped";

constructor(
callback: (args: {
player: Player;
location: Vector3;
dimension: Dimension;
}) => void,
tickDelay?: number
) {
let tick: number = 0;
if (tickDelay) tick = tickDelay;
system.runInterval(() => {
for (const player of world.getAllPlayers()) {
if (player.hasTag(onJumpAfterEvent.jumpTag)) {
if (player.isOnGround) player.removeTag(onJumpAfterEvent.jumpTag);
} else if (player.isJumping) {
player.addTag(onJumpAfterEvent.jumpTag);
callback({
player: player,
location: player.location,
dimension: player.dimension,
});
}
}
}, tick);
}
}


JS ```js
import { system, world } from "@minecraft/server";
export class onJumpAfterEvent {
    constructor(callback, tickDelay) {
        let tick = 0;
        if (tickDelay)
            tick = tickDelay;
        system.runInterval(() => {
            for (const player of world.getAllPlayers()) {
                if (player.hasTag(onJumpAfterEvent.jumpTag)) {
                    if (player.isOnGround)
                        player.removeTag(onJumpAfterEvent.jumpTag);
                }
                else if (player.isJumping) {
                    player.addTag(onJumpAfterEvent.jumpTag);
                    callback({
                        player: player,
                        location: player.location,
                        dimension: player.dimension,
                    });
                }
            }
        }, tick);
    }
}
onJumpAfterEvent.jumpTag = "afterevent.jumped";

Example usage: ```ts
import { onJumpAfterEvent } from "./Events/Jump Event/onJumpAfterEvent";

new onJumpAfterEvent((data) => {
data.player.sendMessage("jumped");
});

#

new onJumpAfterEvent using constructors

#

Keeping the tickrate at 0 will give the best results, but it will also use up more resources.

inner lake
#

Why not update it in the previous thread?

summer geyser
#

ig it leaves options though, the constructor and non-constructor one

#

If i deleted this one now, it would leave the messages, correct?

#

i wanna move it to the other post but i don't wanna leave an empty post with messages under it

rare panther
#

I've made some updates to your js code, most notably:

  • the callback will fire only once per jump press OR when ground has been hit and player is still holding the jump, feels like it makes more sense this way instead of it constantly firing while in air
  • I'm using subscribe/unsubscribe to have parity with existing after events + you can actually unsubscribe and theres less stuff in the constructor
  • its using dynamic properties instead of tags
  • its outputting the player and the block below the player ( player.location and other properties are already a part of the player object and felt slightly unecessary to have them )

Btw, think a better way to call it would be class-based instead of constructor-based. Constructors are just initializing values for a class.

The class:

import { system, world } from "@minecraft/server";

export class onPlayerJumpAfterEvent {
  constructor(_tick = 1) {
    this.tick = _tick;
    this.callback = null;
    this.interval = null;
  }
  subscribe(_callback) {
    this.callback = _callback;
    this.interval = system.runInterval(() => {
      for (const player of world.getAllPlayers()) {
        const hasJumped = player.getDynamicProperty("afterevent:jumped");

        if (!hasJumped && player.isJumping) {
          // Player just started jumping
          player.setDynamicProperty("afterevent:jumped", true);
          if (this.callback) {
            this.callback({
              player: player,
              blockBelow: player.dimension.getBlock(player.location).below(),
            });
          }
        } else if (hasJumped && player.isOnGround) {
          // Player landed on the ground
          player.setDynamicProperty("afterevent:jumped", false);
        }
      }
    }, this.tick);
  }
  unsubscribe() {
    system.clearRun(this.interval);
    this.interval = null;
    this.callback = null;
  }
}
#

Use:

const onPlayerJumpAfterEvent = new onPlayerJumpAfterEvent();
onPlayerJumpAfterEvent.subscribe((event) => {
  const { player, block } = event;
  console.warn(`&{player.id} jumped from &{block.typeId}`);
});
#

hope i helped, it was a good idea to begin with, kudos

rare panther
#

Heres a version that enables identical syntax as existing afterEvents:

#
import { system, world } from "@minecraft/server";

// Ensure the onPlayerJump object exists on world.afterEvents
if (!world.afterEvents.onPlayerJump) {
  world.afterEvents.onPlayerJump = {};
}

// Add the subscribe and unsubscribe methods to world.afterEvents.onPlayerJump
(function () {
  let tick = 1;
  let callback = null;
  let interval = null;

  world.afterEvents.onPlayerJump.subscribe = function (_callback) {
    callback = _callback;
    interval = system.runInterval(() => {
      for (const player of world.getAllPlayers()) {
        const hasJumped = player.getDynamicProperty("afterevent:jumped");

        if (!hasJumped && player.isJumping) {
          // Player just started jumping
          player.setDynamicProperty("afterevent:jumped", true);
          if (callback) {
            callback({
              player: player,
              blockBelow: player.dimension.getBlock(player.location).below(),
            });
          }
        } else if (hasJumped && player.isOnGround) {
          // Player landed on the ground
          player.setDynamicProperty("afterevent:jumped", undefined);
        }
      }
    }, tick);
  };

  world.afterEvents.onPlayerJump.unsubscribe = function () {
    if (interval !== null) {
      system.clearRun(interval);
      interval = null;
      callback = null;
    }
  };
})();
#

Usage:

#
import "./jumpAfterEvent.js";

world.afterEvents.onPlayerJump.subscribe((event) => {
  console.warn(`${event.player.name} jumped and the block below is ${event.blockBelow.typeId}`);
});
summer stirrup
#

object is not extensible at line 5

rare panther
#

you can no longer assign new stuff to the regular world class

#

you need to assign it to some custom variable

fresh totem
#

there is an event to detect the input

#

if its sneak or jump

rare panther
#

There is now - this post is from 2024

#

In any case there is still plenty reason why youd want to create your own event listener, not just for jump or sneak.

#

You can create a listener for when the user inputs a combination of keys, or a specific thing happens within the game

fresh totem
white dew
#

Something like this

#

import { world } from "@minecraft/server";
import "./scriptapi++/index.js";

let x = 0;

world.afterEvents.playerJump.subscribe((data) => {
    // console.warn(`custom event called with data: ${JSON.stringify(data)}`);
    data.player.sendMessage(`you jumped ${x++} times`);
});
#

This is more of a concept project, I never continued it. Its not optimised code

#

Oh this thread is very old ๐Ÿ˜‚

rare panther
#

@fresh totem how so, its no longer extensible

stark fossil
white dew
#

it was just to showcase custom events

rare panther
#

This is a post from 2024 ๐Ÿ˜„ didnt have that back then

main chasm
#

this is kind of useless now considering we have buttonInput event

#

you can detect both sneak and jump and when each button is pushed/released

summer geyser
#

gang we know this

#

but this was made like a year and a half ago

#

when it wasn't a thing

main chasm
fresh totem
#

XD

rare panther
#

It does show how to make an "event listener" - you might want a listener for a combination of keys, or other conditions in the game and its just good code to compartmentalize this behavior in its own little package

summer geyser
#

I've made better since this but yeah I guess

rotund tree
#

The comments were funny. I enjoyed reading them ๐Ÿ˜