max1mde / HologramAPI

Packet based text display hologram api with minimessage and emoji support
https://www.spigotmc.org/resources/111746
GNU General Public License v3.0
6 stars 2 forks source link

HologramManager is null & line width is 0 #1

Closed 7FULL closed 7 months ago

7FULL commented 1 year ago

``@EventHandler public void onPlayerJoin(PlayerJoinEvent event) { Player player = event.getPlayer();

    //Para que no salga cuando se une un jugador
    event.setJoinMessage("");

    TextHologram hologram = new TextHologram(player.getUniqueId().toString())
            .setText(ChatColor.AQUA + "Hello world!")
            .addLine(ChatColor.RED + "Second line")
            .addLine("Third line")
            .addLine("Another line...") // You can add as many lines as you want
            .setBillboard(Display.Billboard.CENTER)
            .setTextShadow(true)
            .setBackgroundColor(Color.fromARGB(0/*Opacity*/, 255/*Red*/, 236/*Green*/, 222/*Blue*/)) // You can use the https://htmlcolorcodes.com/color-picker/ to get the RGB color you want!
            .spawn(player.getLocation().add(0, 2, 0));

    hologramManager = HologramAPI.getHologramManager();
}

//Jugador se va del servidor
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) {
    Player player = event.getPlayer();

    hologramManager.removeAll(player.getUniqueId().toString());
}
``
7FULL commented 1 year ago

Caused by: java.lang.NullPointerException: Cannot invoke "com.maximfiedler.hologramapi.hologram.HologramManager.removeAll(String)" because "this.hologramManager" is null at com.prueba.prueba.listeners.PlayerListeners.onPlayerLeave(PlayerListeners.java:62) ~[?:?] at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-api-1.19.4-R0.1-SNAPSHOT.jar:?]

max1mde commented 1 year ago

So you have a global variable hologramManager right? Are you sure that first someone joined (to initilize the hologramManager variable) before someone left? You can just init that variable on the top because the plugin is anyways loaded after the api... private HologramManager hologramManager = HologramAPI.getHologramManager(); (on top) Also add HologramAPI to your plugin.yml (depend: [HologramAPI]) and make sure that you have also uploaded the plugin jar from the releases or spigotmc on to your server.

max1mde commented 1 year ago

image

max1mde commented 1 year ago

I described how to do everything in the wiki https://github.com/MaximFiedler/HologramAPI/wiki

7FULL commented 1 year ago

I have It all. The plugon the dependency, all. The holograma is shown whe Simeone joins but when Simeone leaves It tells that. Sorry for my english btw im fron Spain

max1mde commented 1 year ago

And are you sure that you did not compile the api directly into your plugin (for example by using the shadow plugin)? You can open the .jar using winrar for example and look you have there a com package which contains a maximfiedler packages and if so remove it. Can you maybe also show me your pom or build.gradle file?

Reason why this is a problem: If you use the TextHologram class there is no problem because you create a new Instance of that class But if you use the HologramAPI.getHologramManager() method and it uses the code in your jar it will return null because its inizilized in the onEnable() method which gets only called if it only runs on the server thats why you need to use the data from the plugin instead of the code in the jar so look that you only compile your plugin with the api but the jar should not contain the code of the hologram api

max1mde commented 1 year ago

You can also take a look at my example plugin which uses the hologram api https://github.com/MaximFiedler/ExampleHologramPlugin

max1mde commented 1 year ago

If you want i can add a check to the getter method and initilize the hologram api variable if its null even without the onEnable So after that it should work without removing it from the jar

But the animations will still not work because they need the plugin instance

max1mde commented 1 year ago

I have It all. The plugon the dependency, all. The holograma is shown whe Simeone joins but when Simeone leaves It tells that. Sorry for my english btw im fron Spain

If thats to complicated you can maybe just sent me your pom.xml file or build.gradle file and i can take a look

max1mde commented 1 year ago

¿Todavía estás aquí? Creo que tengo una solución para ti: Actualiza a la última versión de la api (1.1.4) en pom.xml o build.gradle o actualiza el jar que usas como librería en tu IDE/editor de código y elimina el plugin hologramapi de tu servidor.

7FULL commented 1 year ago

Than you so much for the effort or answering me Ill be back home in 20 min and Ill let you know

7FULL commented 1 year ago

Code: ` HologramManager hologramManager = HologramAPI.getHologramManager();

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
    Player player = event.getPlayer();

    //Para que no salga cuando se une un jugador
    event.setJoinMessage("");

    TextHologram hologram = new TextHologram(player.getUniqueId().toString())
            .setText(ChatColor.AQUA + "Hello world!")
            .addLine(ChatColor.RED + "Second line")
            .addLine("Third line")
            .addLine("Another line...") // You can add as many lines as you want
            .setBillboard(Display.Billboard.VERTICAL)
            .setTextShadow(true)
            .setBackgroundColor(Color.fromARGB(0/*Opacity*/, 255/*Red*/, 236/*Green*/, 222/*Blue*/)) // You can use the https://htmlcolorcodes.com/color-picker/ to get the RGB color you want!
            .spawn(player.getLocation().add(0, 2, 0));

    player.sendMessage("Hola " + player.getName());

    if (player.hasPlayedBefore()){
        //Displayname basicamente es el nombre que se muestra en el chat pero puede que haya sido personalizado
        player.sendMessage("Bienvenido de nuevo " + player.getDisplayName());
    } else {
        player.sendMessage("Bienvenido por primera vez");
    }
}

//Jugador se va del servidor
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event) {
    Player player = event.getPlayer();

    System.out.println("Adios " + player.getName());

    event.setQuitMessage("");

    player.sendMessage("Adios " + player.getName());

    hologramManager.removeAll(player.getUniqueId().toString());
}`

Pom.xml `<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0
<groupId>com.prueba</groupId>
<artifactId>Prueba</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Prueba</name>

<properties>
    <java.version>16</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <plugins>
        <!-- Configuración del plugin maven-compiler-plugin para compilar el código fuente -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>16</source>
                <target>16</target>
            </configuration>
        </plugin>

        <!-- Configuración del plugin maven-shade-plugin para crear un JAR sombreado (shaded JAR) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- Configuración del plugin maven-resources-plugin para copiar el JAR generado -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-plugin</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <!-- Aquí se define la carpeta de destino, reemplaza la ruta a tu carpeta de plugins -->
                        <outputDirectory>C:/Users/phgfu/staff/MCServers/1.19.4/plugins</outputDirectory>
                        <resources>
                            <resource>
                                <!-- El archivo JAR generado se copiará a la carpeta de destino con el mismo nombre -->
                                <directory>${project.build.directory}</directory>
                                <includes>
                                    <include>Prueba-1.0-SNAPSHOT.jar</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

<repositories>
    <repository>
        <id>spigotmc-repo</id>
        <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
    </repository>
    <repository>
        <id>sonatype</id>
        <url>https://oss.sonatype.org/content/groups/public/</url>
    </repository>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.spigotmc</groupId>
        <artifactId>spigot-api</artifactId>
        <version>1.19.4-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.github.MaximFiedler</groupId>
        <artifactId>HologramAPI</artifactId>
        <version>1.1.4</version>
    </dependency>
</dependencies>

`

Error: org.bukkit.event.EventException: null at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-api-1.19.4-R0.1-SNAPSHOT.jar:?] at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-api-1.19.4-R0.1-SNAPSHOT.jar:?] at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot-api-1.19.4-R0.1-SNAPSHOT.jar:?] at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot-api-1.19.4-R0.1-SNAPSHOT.jar:?] at net.minecraft.server.players.PlayerList.remove(PlayerList.java:521) ~[spigot-1.19.4-R0.1-SNAPSHOT.jar:3763-Spigot-7d7b241-5a5e43e] at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1790) ~[spigot-1.19.4-R0.1-SNAPSHOT.jar:3763-Spigot-7d7b241-5a5e43e] at net.minecraft.network.NetworkManager.m(NetworkManager.java:444) ~[spigot-1.19.4-R0.1-SNAPSHOT.jar:3763-Spigot-7d7b241-5a5e43e] at net.minecraft.server.network.ServerConnection.c(ServerConnection.java:195) ~[spigot-1.19.4-R0.1-SNAPSHOT.jar:3763-Spigot-7d7b241-5a5e43e] at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:1326) ~[spigot-1.19.4-R0.1-SNAPSHOT.jar:3763-Spigot-7d7b241-5a5e43e] at net.minecraft.server.dedicated.DedicatedServer.b(DedicatedServer.java:388) ~[spigot-1.19.4-R0.1-SNAPSHOT.jar:3763-Spigot-7d7b241-5a5e43e] at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:1200) ~[spigot-1.19.4-R0.1-SNAPSHOT.jar:3763-Spigot-7d7b241-5a5e43e] at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:1017) ~[spigot-1.19.4-R0.1-SNAPSHOT.jar:3763-Spigot-7d7b241-5a5e43e] at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:304) ~[spigot-1.19.4-R0.1-SNAPSHOT.jar:3763-Spigot-7d7b241-5a5e43e] at java.lang.Thread.run(Thread.java:833) ~[?:?] Caused by: java.lang.NullPointerException: Cannot invoke "com.maximfiedler.hologramapi.hologram.HologramManager.removeAll(String)" because "this.hologramManager" is null at com.prueba.prueba.listeners.PlayerListeners.onPlayerLeave(PlayerListeners.java:60) ~[?:?] at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-api-1.19.4-R0.1-SNAPSHOT.jar:?] ... 13 more

7FULL commented 1 year ago

Btw with that code I see them like this 2023-08-04_14 02 47

Forgot to mention when compiling this appears in console

image

max1mde commented 1 year ago

Ok one moment please i need to read and understand everything first...

7FULL commented 1 year ago

Sorry

max1mde commented 1 year ago

Okay so... you use the shade plugin you just need to exclude my api from it

max1mde commented 1 year ago

image

max1mde commented 1 year ago
<createDependencyReducedPom>false</createDependencyReducedPom>
<artifactSet>
    <excludes>
        <!-- Exclude HologramAPI from shading -->
        <exclude>com.github.MaximFiedler:HologramAPI</exclude>
    </excludes>
</artifactSet>

put this into the < configuration > tag

max1mde commented 1 year ago

Also if you already removed the hologramapi plugin jar from your server add it again

max1mde commented 1 year ago

Here is the full edited pom.xml (I did not tested it so if there is a problem tell me)

<groupId>com.prueba</groupId>
<artifactId>Prueba</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Prueba</name>

<properties>
    <java.version>16</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <plugins>
        <!-- Configuración del plugin maven-compiler-plugin para compilar el código fuente -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>16</source>
                <target>16</target>
            </configuration>
        </plugin>

        <!-- Configuración del plugin maven-shade-plugin para crear un JAR sombreado (shaded JAR) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                        <artifactSet>
                            <excludes>
                                <!-- Exclude HologramAPI from shading -->
                                <exclude>com.github.MaximFiedler:HologramAPI</exclude>
                            </excludes>
                        </artifactSet>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- Configuración del plugin maven-resources-plugin para copiar el JAR generado -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-plugin</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <!-- Aquí se define la carpeta de destino, reemplaza la ruta a tu carpeta de plugins -->
                        <outputDirectory>C:/Users/phgfu/staff/MCServers/1.19.4/plugins</outputDirectory>
                        <resources>
                            <resource>
                                <!-- El archivo JAR generado se copiará a la carpeta de destino con el mismo nombre -->
                                <directory>${project.build.directory}</directory>
                                <includes>
                                    <include>Prueba-1.0-SNAPSHOT.jar</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

<repositories>
    <repository>
        <id>spigotmc-repo</id>
        <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
    </repository>
    <repository>
        <id>sonatype</id>
        <url>https://oss.sonatype.org/content/groups/public/</url>
    </repository>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.spigotmc</groupId>
        <artifactId>spigot-api</artifactId>
        <version>1.19.4-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.github.MaximFiedler</groupId>
        <artifactId>HologramAPI</artifactId>
        <version>1.1.4</version>
    </dependency>
</dependencies>
7FULL commented 1 year ago

WORKS PERFECTLY!!!! Thank you so much and excuse me for bothering you. Btw any idea why they look like that?

max1mde commented 1 year ago

No problem! Actually i dont know why they look like that... which client are you using? Can you add this line after the hologram creation for debugging in the join event? player.sendMessage(hologram.getText()); and show me that message?

7FULL commented 1 year ago

image

max1mde commented 1 year ago

Thats how it should look... can i join your server maybe and look by myself?

max1mde commented 1 year ago

Like how the holograms are there using my client

max1mde commented 1 year ago

Or can you stand nearby a hologram (max 2 blocks away) and use this command: /data get entity @e[type=minecraft:text_display,distance=..2,limit=1]

7FULL commented 1 year ago

It is running in localhost how do I change the ip

7FULL commented 1 year ago

image

It doesnt found the entity

max1mde commented 1 year ago

image

Increase the distance in the command from 2 to like 10

max1mde commented 1 year ago

/data get entity @e[type=minecraft:text_display,distance=..10,limit=1]

max1mde commented 1 year ago

It is running in localhost how do I change the ip

You can use ngrok (port tunnel), open the port on the router or find a host/rent or buy a root, v or dedicated server.. But you dont have to do that I just thought the server is already public that's why I asked

max1mde commented 1 year ago

I mean i could host the server for you as long as i still have server capacity in surplus (but it would be a bit complicated i would need to give you access in some way)

7FULL commented 1 year ago

image

If you need the server anyways after this Ill do it

image

max1mde commented 1 year ago

I will try to solve this problem tomorrow then I come back here again

max1mde commented 1 year ago

I think i have found the problem... image In the screenshot you have sent me the line width is set to 0 and if i spawn such a display manually it looks like your display. On the left a display with the line width of 0 and on the right side a display with the line width of 200 image

max1mde commented 1 year ago

Add this line to your code: .setLineWidth(200)

Or

hologram.setLineWidth(200);
hologram.update();

Also use /version HologramAPI and check the version of the plugin if its not 1.1.4 update it.

7FULL commented 1 year ago

Still the same and it is 1.1.4

max1mde commented 1 year ago

Can you share the code for spawning the hologram again?

7FULL commented 1 year ago
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
    Player player = event.getPlayer();

    //Para que no salga cuando se une un jugador

    // Create the event here
    CustomEvent customEvent = new CustomEvent("Sample Message");
    // Call the event
    Bukkit.getServer().getPluginManager().callEvent(customEvent);
    // Now you do the event
    Bukkit.getServer().broadcastMessage(customEvent.getMessage());

    event.setJoinMessage("");

    ScoreboardManager manager = Bukkit.getScoreboardManager();
    Scoreboard scoreboard = manager.getNewScoreboard();

    Objective objective = scoreboard.registerNewObjective("Title", "dummy");
    objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    objective.setDisplayName(ChatColor.BLUE + "Scoreboard Title");

    Score score = objective.getScore(ChatColor.GOLD + "Money: $" + ChatColor.GREEN + 1); //create a line for the board
    Score s2 = objective.getScore(""); //blank space
    Score s3 = objective.getScore(ChatColor.DARK_PURPLE + "https://google.com");

    score.setScore(3);
    s2.setScore(2);
    s3.setScore(1);
    s2.setScore(0);

    player.setScoreboard(scoreboard);

    TextHologram hologram = new TextHologram(player.getUniqueId().toString())
            .setText(ChatColor.AQUA + "Hello world!")
            .addLine(ChatColor.RED + "Second line")
            .addLine("Third line")
            .addLine("Another line...") // You can add as many lines as you want
            .setBillboard(Display.Billboard.VERTICAL)
            .setTextShadow(true)
            .setBackgroundColor(Color.fromARGB(0/*Opacity*/, 255/*Red*/, 236/*Green*/, 222/*Blue*/)) // You can use the https://htmlcolorcodes.com/color-picker/ to get the RGB color you want!
            .spawn(player.getLocation().add(0, 2, 0))
            .setLineWidth(200);

    player.sendMessage(hologram.getText());

    player.sendMessage("Hola " + player.getName());

    if (player.hasPlayedBefore()){
        //Displayname basicamente es el nombre que se muestra en el chat pero puede que haya sido personalizado
        player.sendMessage("Bienvenido de nuevo " + player.getDisplayName());
    } else {
        player.sendMessage("Bienvenido por primera vez");
    }
}
max1mde commented 1 year ago

You need to set the line width before the spawn method call because otherwise it will not be updated anymore unless you call the .update() method

.setLineWidth(200)
.spawn(player.getLocation().add(0, 2, 0));