Qveshn / LightAPI

Bukkit Library for create invisible light source
Other
29 stars 12 forks source link

How to implement LightAPI to a plugin? #23

Closed DropOfAWater closed 3 years ago

DropOfAWater commented 3 years ago

Hello,

I'm new to spigot development, I wanted to create "flashlight" effect, but I don't know how to use LightAPI in my project. Can you tell me how to do that?

Qveshn commented 3 years ago

Hi

1. Modify your pom.xml to include LightAPI into your maven project

    <repositories>
        ...
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/groups/public/</url>
        </repository>
    </repositories>

    <dependencies>
         ...
        <dependency>
            <groupId>io.github.qveshn</groupId>
            <artifactId>LightAPI-fork</artifactId>
            <version>3.4.6-SNAPSHOT</version>
        </dependency>
    </dependencies>

(or just download jar-file and attach it to your project as external library to allow references to it)

2. Try next example to set light.This code is only to learn how to use api. It is not optimized for a lot of sets at one time, but it completely sets the light and updates the chunks on the client (sends notification to client to make him update chunks) for one block position.

import ru.beykerykt.lightapi.LightType;
import ru.beykerykt.lightapi.LightAPI;
...
void setLight(Location location, int lightLevel, LightType lightType) {
    Block block = location.getBlock();
    int oldLightLevel = lightType == LightType.BLOCK ? block.getLightFromBlocks() : block.getLightFromSky();
    if (oldLightLevel != lightLevel) {
        if (lightLevel > 0) {
            LightAPI.createLight(location, lightType, lightLevel, false);
        } else {
            LightAPI.deleteLight(location, lightType, false);
        }
        for (ChunkInfo chunkInfo : LightAPI.collectChunks(location, lightType, Math.max(lightLevel, oldLightLevel))) {
            LightAPI.updateChunk(chunkInfo, lightType);
        }
    }
}

Notes: Methods createLight/deleteLight creates/deletes lights only on server! Player (client) will not see changes until light-chunk-info is updated on client side. So, the next "for" cycle collects all chunk sections (16x16x16) that have changes after creating/deleting light on server and call updateChunk method to notify nearby players to reload light data for each section (chunk)

3. Also, I suggest you look at ShinyItems source code https://github.com/Qveshn/ShinyItems This plugin uses light moving while player walks with torch in hand The main concept is: plugin every n-th tick checks the player position and light level at that block. if it is changed then plugin deletes light at old postion and creates at new position

Notes: LightAPI is only API to set/clear light at the block position. It does not monitor it after creating. Bukkit can destroy it after updating nearby block. Therefore ShinyItems periodically checks its installed light sources, and if they accidentally disappeared, then re-creates them again.

When you will study the code, do not see at LightAPIv5.java. It was compatibility for plugin from original author. But his LightAPI still has bugs. The bridge to my fork is in LightAPIv3.java

DropOfAWater commented 3 years ago

Hi

Modify your pom.xml to include LightAPI into your maven project

    <repositories>
        ...
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/groups/public/</url>
        </repository>
    </repositories>

    <dependencies>
         ...
        <dependency>
            <groupId>io.github.qveshn</groupId>
            <artifactId>LightAPI-fork</artifactId>
            <version>3.4.6-SNAPSHOT</version>
        </dependency>
    </dependencies>

(or just download jar-file and attach it to your project as external library to allow references to it)

Try next example to set light.This code is only to learn how to use api. It is not optimized for a lot of sets at one time, but it completely sets the light and updates the chunks on the client (sends notification to client to make him update chunks) for one block position.

import ru.beykerykt.lightapi.LightType;
import ru.beykerykt.lightapi.LightAPI;
...
void setLight(Location location, int lightLevel, LightType lightType) {
    Block block = location.getBlock();
    int oldLightLevel = lightType == LightType.BLOCK ? block.getLightFromBlocks() : block.getLightFromSky();
    if (oldLightLevel != lightLevel) {
        if (lightLevel > 0) {
            LightAPI.createLight(location, lightType, lightLevel, false);
        } else {
            LightAPI.deleteLight(location, lightType, false);
        }
        for (ChunkInfo chunkInfo : LightAPI.collectChunks(location, lightType, Math.max(lightLevel, oldLightLevel))) {
            LightAPI.updateChunk(chunkInfo, lightType);
        }
    }
}

Notes: Methods createLight/deleteLight creates/deletes lights only on server! Player (client) will not see changes until light-chunk-info is updated on client side. So, the next "for" cycle collects all chunk sections (16x16x16) that have changes after creating/deleting light on server and call updateChunk method to notify nearby players to reload light data for each section (chunk)

Also, I suggest you look at ShinyItems source code https://github.com/Qveshn/ShinyItems This plugin uses light moving while player walks with torch in hand The main concept is: plugin every n-th tick checks the player position and light level at that block. if it is changed then plugin deletes light at old postion and creates at new position

Notes: LightAPI is only API to set/clear light at the block position. It does not monitor it after creating. Bukkit can destroy it after updating nearby block. Therefore ShinyItems periodically checks its installed light sources, and if they accidentally disappeared, then re-creates them again.

When you will study the code, do not see at LightAPIv5.java. It was compatibility for plugin from original author. But his LightAPI still has bugs. The bridge to my fork is in LightAPIv3.java

I had some issues with editing pom.xml file, but importing external library worked, thank you very much!

Qveshn commented 3 years ago

Hmm... Issues? What is wrong with editing pom? Do you use maven project? Issues while editing pom or while compiling? Can you show error?

DropOfAWater commented 3 years ago

Hmm... Issues? What is wrong with editing pom? Do you use maven project? Issues while editing pom or while compiling? Can you show error?

It was probably my fault, I'm new to this stuff.