Solved Not able to create and open a book

Discussion in 'Spigot Plugin Development' started by JoshC, Mar 11, 2020.

  1. Hi,

    I'm making a plugin which makes use of a custom book with clickable interface. So before starting I went to look on the docs of spigot and found the following for org.bukkit.entity.player : void openBook(@NotNull ItemStack book). So I went to my IDE and tried it out except for the fact that the player instance doesn't seem to contain this method: "Cannot resolve method 'openBook' in 'Player'". The codeI used was this:
    Code (Java):
    player.openBook(book);
    But after searching around on the forums I couldn't find anything related to this issue. I did find that some people still use packages which are sent to the player.

    So I wanted to do the same but first I wanted to create a test custom book in code via BookMeta. So by following the example code I found:
    Code (Java):
    ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
                BookMeta bookMeta = (BookMeta) book.getItemMeta();

                BaseComponent[] page = new ComponentBuilder("Click me")
                        .event(new ClickEvent(ClickEvent.Action.OPEN_URL, "http://spigotmc.org"))
                        .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Go to the spigot website!").create()))
                        .create();

                //add the page to the meta
                bookMeta.spigot().addPage(page);

                //set the title and author of this book
                bookMeta.setTitle("Module editor");
                bookMeta.setAuthor("JoshC");

                book.setItemMeta(bookMeta);
    First part works fine except for the fact of bookMeta.spigot(). I got the error that this method cannot be resolved. So I went to the 1.15.2 snapshot api and in the docs I found out that it should exist. Could someone clarify what I'm doing wrong? Because this is the second time a method doesn't work which is described in the docs.

    Links:
    Bookmeta: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/meta/BookMeta.html
    Player: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Player.html

    Kind regards,
    JoshC

    edit:
    Another side note. I use IntelliJ IDE with maven and included the followping dependency (ofcourse with the corresponding repository):
    Code (Text):
    <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot-api</artifactId>
                <version>1.15.2-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
     
    #1 JoshC, Mar 11, 2020
    Last edited: Mar 11, 2020
  2. Not true, you just need to use the Spigot API instead of Bukkit.

    OP, try updating Maven indices first (Maven -> Reimport), it's possible IJ just hasn't updated. If that fails, make sure you don't have an outdated version of Bukkit, Spigot, or CB earlier in your dependencies (including those provided by other dependencies!) than spigot-api. Maven's dependency tree can be useful for tracking down issues like that.

    Edit: if you're having trouble still and need to check if it's maven or IJ bugging out, ignore the issue and run package or install and see if Maven can compile successfully. If yes, it's IJ, if no, it's an issue with your dependency ordering.
     
  3. Thank you for your post. I've reimported the libraries but that didn't work sadly. I also tried to package it but without succes... I currently use the
    1.15.2-R0.1-SNAPSHOT of the spigot maven. Is there any newer version to be found?
     
  4. Good, we know it's Maven and not IJ then. That should be the latest. Did you check your dependency tree? Please post your pom if you're still having issues.
     
  5. Hi, sorry for late reply. Here is my pom:
    Code (Java):
    <?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">
        <modelVersion>4.0.0</modelVersion>

        <groupId>com.joshuanc</groupId>
        <artifactId>mcplugin-dungeons</artifactId>
        <version>1.0-SNAPSHOT</version>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>7</source>
                        <target>7</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>

        <repositories>
            <repository>
                <id>enginehub-maven</id>
                <url>http://maven.enginehub.org/repo/</url>
            </repository>
            <repository>
                <id>spigotmc-repo</id>
                <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
            </repository>
        </repositories>

        <dependencies>
            <dependency>
                <groupId>com.sk89q.worldedit</groupId>
                <artifactId>worldedit-bukkit</artifactId>
                <version>7.0.0-SNAPSHOT</version>
            </dependency>

            <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot-api</artifactId>
                <version>1.15.2-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>


    </project>
     
  6. Move spigot-api above worldedit-bukkit in your dependency list. If that still doesn't solve the issue, please post the output of mvn
    -e -X dependency:tree
    so we can figure out exactly what exclusions you need to add to WE.
     
  7. Oh wow that solved the issue. How come that by moving it up solved such an issue? Is it related to the WE api?
     
  8. Yes. That's why I kept asking you to run maven's dependency tree - if you had, you'd see that WE depends on an earlier version of Bukkit, which is then provided by Maven when compiling, which is overriding the BookMeta provided by Spigot-API.

    Edit: accidentally a wrong class
     
  9. Ahh okay! Thanks a lot for your help! Very happy with this.