How to use WorldEdit API in a Sponge plugin ? Getting NoClassDefFoundError

Hello, I’m trying to work with WorldEdit regions in my sponge plugin. My goal is to create a remplacement for the //distr command that could handle custom blocks instead of showing “unknown”.

I put the world edit sponge API in maven’s dependencies

<dependency>
  <groupId>com.sk89q.worldedit</groupId>
  <artifactId>worldedit-sponge</artifactId>
  <version>7.2.0-SNAPSHOT</version>
</dependency>

And the code of my command is:

LocalSession session = WorldEdit.getInstance().getSessionManager().findByName(src.getName());
if (session != null) {
    try {
        HashMap<String, Integer> blocks = new HashMap<>();
        World world = EsperiaPlayer.getMcFrPlayer(src.getName()).getPlayer().getWorld();
        session.getRegionSelector(session.getSelectionWorld()).getRegion().getChunkCubes().forEach((point) -> {
            Location<World> location = new Location<>(world, point.getBlockX(), point.getBlockY(), point.getBlockZ());
            blocks.put(location.getBlockType().getName(), blocks.getOrDefault(location.getBlockType().getName(), 0) + 1);
        });
        final String[] message = {""};
        blocks.forEach((str, i) -> {
            message[0] += str + ": " + i;
        });
        src.sendMessage(Text.of(Color.YELLOW, message[0]));
    } catch (IncompleteRegionException e) {
        e.printStackTrace();
    }
}

I am able to use worldedit classes in my IDE (IntelliJ) and I can compile my plugin without any problem but when I’m trying to run the command in game I get the following error:

Java.lang.NoClassDefFoundError: com/sk89q/worldedit/math/BlockVector3

On the line:

session.getRegionSelector(session.getSelectionWorld()).getRegion().getChunkCubes().forEach((point) -> {

Is it a worldedit version problem ? I tried with both latest worldedit for sponge & forge 1.12 available with no luck. And i’m using spongeforge-1.12.2-2838-7.2.0.jar with forge-1.12.2-2838

First of, it will be the sponge version of the plugin as thats what you have requested. The forge version is not what you requested in your dependencies so it wont work (i dont believe worldedit uses a common layer yet).

Next up is the latest version of world edit on ore (im guessing where you got the plugin) is version 6.1.10-SNAPSHOT so I would match that version (or whatever version your supporting).

Lastly are you putting a dependency for worldedit in your @Plugin too? It could be that your plugin is loading before worldedit and then making calls to a plugin that isnt there. (Its also useful for when users attempt to load your plugin without worldedit and then report a bug where they say worldedit is there and im getting this error - if you have it set as a dependency the user will get a nice message saying that worldedit isnt present so hopefully you wont get any errors from users like that)

NoClassDefFoundError means it cannot find the class your attempting to use, meaning its not there. If that be its not there completely, hasnt loaded yet, etc.

@MoseMister
I’m not sure to understand how dependencies are working. I’m new in the java world.
If i’m using the forge version of worldedit, I have to use the worldedit-forge library ? I though worldedit-forge was meant to be used in a forge mod.

Anyway, I can’t see a forge 1.12 version of worldedit API on the maven repo. Same with sponge version, I can only get 6.1.7-SNAPSHOT/. Does that mean worldedit’s repo is badly managed and I can’t use worldedit api in a sponge 1.12 plugin ?

And I don’t know how the @Plugin annotation is working. Currently, my annotation is:
@Plugin(id = "esperials", name = "Esperials", version = "1.0")
I should set it to @Plugin(id = "esperials", name = "Esperials", version = "1.0", dependencies = {@Dependency(id = "wordedit") ?

Thank you for your help.

So the worldedit dependency is split up into sponge, bukkit and forge. I said you needed to use the sponge version of worldedit for your plugin to work as in your dependency you have shown your attempting to use the sponge edition.

<artifactId>worldedit-sponge</artifactId>

However if you have changed this to the forge edition then you will need the forge edition on your server.

As for maven, sometimes developers dont update there maven for new versions as the new version doesnt add anything new (API wise) which means just use the rule that one would with forge versions and SpongeForge, as in, use the highest number that is equal or less then your version.
Not sure how the developer updates there maven, could be that they arnt great at keeping that up to date.

If it is the later, then you can use the API directly from the Jar file (skipping maven completely) or use the worldedit github repo (you can add github urls as dependencies using Jitpack)

As for the @plugin, you are correct, assuming that the worldedit plugin/mod uses that as its id

Edit:
Looking at the maven myself, if you want to add the forge version, then use the 1.12.1 version as there wasnt much of a change between 1.12.1 to 1.12.2 so some mods would support both versions, worldedit was one of them.

https://maven.enginehub.org/repo/com/sk89q/worldedit/worldedit-forge-mc1.12.1/

I set the plugin annotation to
@Plugin(id = “esperials”, name = “Esperials”, version = “1.0”, dependencies = {@Dependency(id = “worldedit”)})

And the maven dependency to

<dependency>
    <groupId>com.sk89q.worldedit</groupId>
    <artifactId>worldedit-forge-mc1.12.1</artifactId>
    <version>7.0.0-SNAPSHOT</version>
</dependency>

But I still get the same NoClassDefFoundError. I think it’s really just a version mismatch because my code is partially working. I am able to get the WorldEdit instance et retrieve the player selection. I just can’t iterate on it. I have the feeling it could be easier to implement my own region selection and throw away worldedit dependency. Am I missing something ?

If some of it is working then yes its a versioning issue.

And tbh, yes … Yes it is much easier to make your own region space. Its one of the main reasons why i have never touched the worldedit code. Unless im needing to build shapes in a mc world then i just wrote my own code