Solved Knowing what the light level is and adding potion effect when within a range

Discussion in 'Spigot Plugin Development' started by NeverFlame42, Jun 25, 2021.

  1. I'm pretty new to Spigot and am using intellij for programming plugins. To practice coding, a friend gave me a few ideas for plugins. One of them was a reverse vampire. To cover as much as I could with this one, I want to give the player bad potion effects when they are in a light level below 8, and add a couple commands and maybe an enchantment or two. I don't know how to detect the light level for where the player is standing, or for when they move out of the range. I know of Block.getLightLevel() but don't really know how to use it. Any Ideas?
  2. I see two ways to go about checking the player's light level (automatically)..

    1: PlayerMoveEvent
    Get the event location, event block, and then check the light level. If it is <8, apply your potion effects and enchantments. However, I don't know if you want that event being called because it will call a lot. And the more complex your code gets for the Vampire and the more players, the more lag introduced.

    2: Loop All Players
    Loop all players every 3-4 seconds and then get the player location, location block, and check the light level. Do the same logic as above. This would be more ideal in terms of performance I believe, but less accurate to when the light level changes.
    • Like Like x 1
  3. For the first one, how would you use that to get the light level into its own variable to be able to be compared? Also, could you compare this number with an If statement?
  4. You can compare anything with an if statement.
    Code (Java):
        public void checkLight(PlayerMoveEvent event) {
            Block block = event.getTo().getBlock();
            int lightLevel = block.getLightLevel();
            if (lightLevel < 8) {
                // Do your vampire stuff here
    • Like Like x 1
  5. Thank you so much, I should be able to get it from here.
  6. No problem. If you need help, feel free to DM me.
  7. I may in the next day or two, thanks again!
  8. One thought that I had is a potential edge case where a player stands still in the sun for a long time. Since the light level is only checked on move, and the player isn't moving, eventually the negative effects will expire.
    Two solutions:
    1. Make the potion durations obscenely long.
    2. Instead of setting the potions in the event handler, instead just change the value of a boolean, and have a repeating task that applies the potion effects if the boolean is true.
    • Like Like x 1
  9. The more efficient method would be to use packets. Listen to FLYING, POSITION, POSITION_LOOK, and LOOK asynchronously. Create a location using your coordinates provided by those packets and grab the block from there and handle whatever logic necessary. You’ll need to call potion effect adding on the main thread though.