Here’s a quick guide to using the selection API for WorldEdit from a Bukkit plugin.
A large part of WorldEdit uses an abstract internal API for easy portability For your convenience, we provide a Bukkit-native selection API that makes it easier to work with that part of WorldEdit. It is possible to “reach through” to access to the internal features of WorldEdit, but if you are interested in only getting selections, that shouldn’t be needed.
Getting a selection
To get a selection, you need to:
- Get a reference to WorldEditPlugin from Bukkit’s plugin manager.
- Cast the reference from JavaPlugin to a more specific WorldEditPlugin.
- Get the selection by calling getSelection().
See the snippet below.
Remember that the user may not have a selection yet, in which case, you need to detect it and notify the user.
What kind of shape?
Selections may be cuboids — that is, they may be boxes aligned to the world. However, they may also be 2D polygons extruded vertically. Depending on your use case, you may not care (you can just get the min/max points), but if you do, the selection object returned is actually one of a more specific kind. By using “instanceof,” you can detect which kind of shape it is and act accordingly.
Be aware that new shapes may be added in the future and you need to be aware of changes.
If you are using WorldEdit with your plugin, you may need to require that WorldEdit be loaded before your plugin. That way, you can guarantee that it’s available when your plugin is loaded. There are two kind of dependencies supported by Bukkit: a hard dependency, that requires WorldEdit to be installed for your plugin to load, or a soft one, where the loader will try its best to load your plugin at the end, but will continue if the soft dependencies are not found.
The following would go into your plugin.yml for a hard dependency.
In the case of soft dependencies, you can specify it as so:
Be aware that the call to getPlugin() in the first snippet may return null if you are using a soft dependency.
We don’t expect to change any of this any time soon, which means you won’t have to figure out why your plugin broke because of WorldEdit! It won’t break when MC versions change either.