WesJD / AnvilGUI

Capture user input in Minecraft through an anvil GUI in under 20 lines of code
MIT License
481 stars 114 forks source link

AnvilGUI$StateSnapshot does not exist. #352

Closed MarkManFlame55 closed 2 months ago

MarkManFlame55 commented 2 months ago

Following tutorial on README and getting to try ot get a string from the menu. Getting this error on the console.

AnvilGUI.Builder gui = new AnvilGUI.Builder();
        gui.onClickAsync((slot, stateSnapshot) -> CompletableFuture.supplyAsync(() -> {
            if (slot == AnvilGUI.Slot.OUTPUT) {
                ItemStack itemStack = stateSnapshot.getOutputItem();
                if (itemStack != null) {
                    message = stateSnapshot.getOutputItem().getItemMeta().getDisplayName();
                }
            } else {
                return Arrays.asList(AnvilGUI.ResponseAction.replaceInputText(""));
            }
            return Collections.emptyList();
        }));

Also getting NoClassDefFoundError when trying to call the function that opens the gui

[12:47:23 ERROR]: Failed to register events for class net.espectralgames.pluginuhc_121.gui.MessagesMenu because net/wesjd/anvilgui/AnvilGUI$StateSnapshot does not exist.
0dinD commented 2 months ago

That means you have either not configured Maven/Gradle to include the AnvilGUI classes into your JAR file (using the Maven Shade plugin or Gradle Shadow plugin), or you have not deployed the shaded JAR with the AnvilGUI clases to your server (perhaps you deployed the unshaded version?).

Please double-check this and send your Maven/Gradle build file if you can't figure it out.

MarkManFlame55 commented 2 months ago

This is how I have my build.gradle

repositories {
    mavenCentral()
    maven {
        name = "papermc-repo"
        url = "https://repo.papermc.io/repository/maven-public/"
    }
    maven {
        name = "sonatype"
        url = "https://oss.sonatype.org/content/groups/public/"
    }
    maven {
        name = "codemc-snapshot"
        url = "https://repo.codemc.io/repository/maven-snapshots/"
    }
}
dependencies {
    compileOnly "io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT"
    compileOnly 'net.luckperms:api:5.4'
    compileOnly "net.wesjd:anvilgui:1.10.2-SNAPSHOT"
}

it is getting imported cause I can navigate through the library inside my IDE, maybe its tha unshaded version but I dont know how to verify that. Any help?

0dinD commented 2 months ago

Thanks, now I see why it's not working for you. Right now you are only compiling against AnvilGUI (which yes, does mean you can see it in your IDE), but you are not including the class files from AnvilGUI inside your plugin JAR file. This is normally done via the Maven Shade plugin, or (in your case) the Gradle Shadow plugin, which I linked above.

Here's an example of how you could modify your build.gradle script you provided, in order to include the AnvilGUI classes:

plugins {
    // Your other plugins

    // ...

    id "com.gradleup.shadow" version "8.3.0"
}

repositories {
    mavenCentral()
    maven {
        name = "papermc-repo"
        url = "https://repo.papermc.io/repository/maven-public/"
    }
    maven {
        name = "sonatype"
        url = "https://oss.sonatype.org/content/groups/public/"
    }
    maven {
        name = "codemc-snapshot"
        url = "https://repo.codemc.io/repository/maven-snapshots/"
    }
}
dependencies {
    compileOnly "io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT"
    compileOnly 'net.luckperms:api:5.4'
    implementation "net.wesjd:anvilgui:1.10.2-SNAPSHOT"
}

tasks.named("shadowJar") {
    archiveClassifier = ""
    relocate "net.wesjd.anvilgui", "[YOUR_PLUGIN_PACKAGE].anvilgui"
}

tasks.named("assemble") {
    dependsOn tasks.named("shadowJar")
}

Note especially the switch from compileOnly to implementation on the AnvilGUI dependency. Also, don't forget to replace the [YOUR_PLUGIN_PACKAGE] part with your plugin package name, such as com.example.myplugin. After modifying your buildscript to include all this, just run the Gradle build task like normal, and you should get a plugin JAR with AnvilGUI included, in your build/libs/ directory that Gradle creates.

Note, this stuff is already documented for Maven Shade plugin, but the documentation could be improved, and an example for Gradle like the one I just showed you is missing, so I'll add that when I get time.

Let me know if you have further questions. If you'd like to read more about Gradle Shadow plugin etc, here are some links:

MarkManFlame55 commented 2 months ago

Happy to say that it worked!! Tysm <3