#Effect that gives the player ability to Fly

1 messages · Page 1 of 1 (latest)

desert monolith
#

my code works but after the effect ends the player can still fly, how can i remove this from it, i have some ideas but they all looks ugly AF, like checking for the effect in player.tick. Is there way to trigger something after the effect has ended?

    e.create('flight')
        .displayName("§9Flight")
        // Set a tick event to apply the action
        .effectTick((entity, lvl) => global.flying(entity, lvl))
        .color(Color.BLUE)
        .beneficial()

/**
 * 
 * @param {Internal.LivingEntity} entity 
 */
global.flying = (entity, lvl) => {
    if(!entity.isPlayer()) return

        if(!entity.level.isClientSide()){
            entity.resetFallDistance()
        }else{
            Client.player.abilities.mayfly = true
        }
}```
timber nimbusBOT
#

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

chilly trout
#

Does effect tick tell you how much time is remaining?

#

I wonder if you could use the time remaining to make the last effect tick clear their ability to fly

desert monolith
#

you can acess the duration using the livingEntity

#

is there a equivalent to server scheduleTick for the client? than i could schedule and would happen either way

prime fog
#

I think, was a quick glance

#

think the remove runs at the end of the effect

desert monolith
#

but where do acess this? reflection?

prime fog
#

it's part of the create function

#

ah no, seems like the kjs version is hardcoded only around actual attributes for that

desert monolith
#

how do you use this?

chilly trout
chilly trout
desert monolith
#

i found scheduleTicks in the client

chilly trout
#

Is the mayFly considered a player attribute?

prime fog
#

no, it's an ability

desert monolith
#

and abilities are a Client thing, which complicates a little

desert monolith
#

anything better than this? ```js
/**
*

  • @param {Internal.LivingEntity} entity
    */
    global.flying = (entity, lvl) => {
    if(!entity.isPlayer()) return

     if(!entity.level.isClientSide()){
         entity.resetFallDistance()
             
     }else{
         Client.player.abilities.mayfly = true
             if(!entity.persistentData.flyDuration){
                 entity.persistentData.flyDuration = NBT.i(entity.getEffect('kubejs:flight').duration)
             }  
         let duration = Client.player.persistentData.flyDuration
    
             if(Client.player.getEffect('kubejs:flight').duration == duration){
                 Client.scheduleInTicks(duration, task => {
                     Client.player.abilities.mayfly = false
                     Client.player.tell('Receba')
                     Client.player.persistentData.remove('flyDuration')
                 })
             }
     }
    

}```

#

i think a client only player.tick checking for the effect and changing the mayfly would be better -.-

#

how to remove the fallDamage from the player when flying? right now the player gains immunity to fall damage but that's kind strange

wanton trench
#

I think you are missing onAbilityUpdate or something, check lexxie script on flying command

#

on #1048591172165189632

#

also looks like abilities are server player too

#

you change on server side and call the function to update

#

and the client will receive the new values

desert monolith
wanton trench
#

ServerPlayer is Player too

chilly trout
#

Internal.Player is just Java’s player class

#

ServerPlayer is an extension of Player

wanton trench
#

talking about object, what do you think about asking for a Banana but you receive the whole Amazon with it? @chilly trout heh

desert monolith
chilly trout
wanton trench
#

OOP

wanton trench
chilly trout
wanton trench
#

oh you already is doing it

chilly trout
#

All ServerPlayers are Players.
All Players are Entities.
Not all Entities are Players.
Not all Players are ServerPlayers.

desert monolith
chilly trout
desert monolith
#
/**
 * 
 * @param {Internal.LivingEntity} entity 
 */
```Sorry, i understand now
chilly trout
#

I’m KubeJS, you do not have the type hierarchy restriction that Java gives.
So for example:
If Java returns an Entity in a method, in KubeJS, that object is only required to be at least an entity.
It could be anything that extends from it as well. For example it could be a Zombie, Skeleton, Player, etc…

desert monolith
chilly trout
#

What I’m saying is, because inheritance exists, and in Java you can do things like if (player instanceof ServerPlayer), you can make use of situational properties.

#

For example: if you know your script only runs on the server side, then you know that anything you are given is the server-side variant of itself.
The typings can’t tell you that it’s server side because that event may fire on both client and server

desert monolith
#

okay, i starting to get it now. inheritance play a role here. I don't understand java too much so i didn't consider this

#

idk too much of programming as you guys can see, lol

chilly trout
#

Programming can be a bit confusing to begin with, but when you have one language that is built on top of the other language, you get even more confusion

#

Especially when those two languages interact with each other

desert monolith
#

yeah, this whole JS to Java thing is quite a mystery to me

chilly trout
#

I just call KubeJS: Java in a trench coat

#

It’s basically 90% java logic, with some JS utilities to make things easier

desert monolith
#

Am i missing something?

#
/**
 * 
 * @param {Internal.ServerPlayer} player 
 */
global.flying = (player, lvl) => {
    if(!player.isPlayer()) return

        if(!player.level.isClientSide()){
            player.resetFallDistance()
            player.abilities.mayfly = true
            player.onUpdateAbilities()
        }
}```
wanton trench
#

@desert monolith

#
StartupEvents.registry("mob_effect", (event) => {
  event
    .create("flight")
    .effectTick((entity, level) => global.flying(entity, level))
    .color(Color.BLUE)
    .beneficial()
    .displayName("Flight")
  /**
   *
   * @param {Internal.LivingEntity} entity
   */
})

global.flying = (/** @type {Internal.LivingEntity} */entity, /** @type {number} */lvl) => {
  if (!entity.isPlayer()) return
  if (!entity.level.isClientSide()) {
    /** @type {Internal.ServerPlayer} */
    let player = entity
    if (!player.abilities.mayfly) {
      player.abilities.mayfly = true
      player.onUpdateAbilities()
    }
    let effect = player.getEffect("kubejs:flight")
    if (effect.duration == 1) {
      player.abilities.mayfly = false
      player.abilities.flying = false
      player.onUpdateAbilities()
    }
  }
}

this works for me

#

what is this fall damage about?

chilly trout
#

Likely to reset fall distance caused from flying so they don’t die from flying

wanton trench
#

don't get it hih

chilly trout
#

Flight may just be continuous free fall to the game

desert monolith
#

just tested

chilly trout
#

You could schedule a task, check if there is a task active for that player, and during the schedule, check that the effect is still present, if not, reset flying state

desert monolith
desert monolith
wanton trench
#

@desert monolith going to use the forge event, Expired and Removed

#

it is a lot safer

desert monolith
#

okay, i will try that

wanton trench
#

@desert monolith

#
StartupEvents.registry("mob_effect", (event) => {
  event
    .create("flight")
    .effectTick((entity, level) => global.flying(entity, level))
    .color(Color.BLUE)
    .beneficial()
    .displayName("Flight")
})

global.flying = (/** @type {Internal.LivingEntity} */entity, /** @type {number} */lvl) => {
  if (!entity.isPlayer()) return
  if (!entity.level.isClientSide()) {
    /** @type {Internal.ServerPlayer} */
    let player = entity
    if (!player.abilities.mayfly) {
      player.abilities.mayfly = true
      player.onUpdateAbilities()
    }
  }
}

ForgeEvents.onEvent("net.minecraftforge.event.entity.living.MobEffectEvent$Remove", event => {
  global.clearEffects(event.entity, event.effectInstance)
})

ForgeEvents.onEvent("net.minecraftforge.event.entity.living.MobEffectEvent$Expired", event => {
  global.clearEffects(event.entity, event.effectInstance)
})

global.clearEffects = (/** @type {Internal.LivingEntity} */entity, /** @type {Internal.MobEffectInstance} */ effectInstance) => {
  if (effectInstance.effect.descriptionId == "effect.kubejs.flight") {
    if (entity.isPlayer()) {
      entity.abilities.mayfly = false
      entity.abilities.flying = false
      entity.onUpdateAbilities()
    }
  }
}
wanton trench
desert monolith
#

thanks @wanton trench once again, thanks @chilly trout and congrats on the degree and thanks @prime fog