-
Notifications
You must be signed in to change notification settings - Fork 81
Custom Openers (v5)
Note: This wiki page is for the CratesPlus v5.0 snapshots!
This page will give basic information on what and how to register a custom "Opener" for CratesPlus.
An "Opener" is what CratesPlus will call when someone opens a crate. Currently, CratesPlus features two openers, one for the Basic GUI and one without anything special and just returns the winning. Any plugin can create and add an opener to CratesPlus allowing endless possibilities for opening Crates.
Openers were added from CratesPlus v4.1, all documentation on this page is valid for version 5.0+. Currently, if your opener is added to a plugin with an older version of CratesPlus it will result in exceptions. I'll soon add some demo code for checking the CratesPlus version before attempting to register an Opener.
To get started you'll need to import CratesPlus into your plugin, you should be able to achieve this using something such as Maven or your IDE. Once you've imported the plugin you'll need to create a new class and extend the Opener
class from CratesPlus and override the doOpen
, doSetup
, doReopen
and doesSupport
methods.
public class CustomOpener extends Opener {
public CustomOpener(Plugin plugin, String name) {
super(plugin, name);
}
@Override
public void doSetup() {
// Handle any custom configuration loading etc
}
@Override
public void doReopen(Player player, Crate crate, Location blockLocation) {
// What to do if they interact with the crate again after it's already opened and not finished
}
@Override
public void doOpen(Player player, Crate crate, Location blockLocation) {
// Handle your code here
crate.handleWin(player); // Handle a win, you can also pass a "Winning" manually if required
finish(player);
}
@Override
public boolean doesSupport(Crate crate) {
return true; // Do any checks here to see what type of crate it is, such as KeyCrate or SupplyCrate etc
}
}
Using the doOpen method you can now handle the crate opening in any way you'd like. When a player opens a crate the key removing/handling is done for you and the enabled opener will be called running the doOpen method. To award a win to a player you can use getWinning which will return a random win and then execute the runWin(Player player) method for that. You'll need to make sure to call the finish method once you're done. Otherwise, CratesPlus won't know you've finished.
Once you've created your Opener class you now need to register it for CratesPlus to be aware it exists. You can do this using the OpenHandler
in CratesPlus. See the example code below.
CratesPlus.getOpenHandler().registerOpener(new CustomOpener(this, "Custom"));
The second argument of an Opener is the name and must be unique. This may change in the future.
With CratesPlus v5 a few changes have been made due to a lot of overall re-structure to the main plugin. You'll need to override the doesSupport
method to define what crate types your Opener supports. getWinning is also no longer valid and you should use crate#handleWin instead.
For a complete example of creating a custom Opener check out the CSGO Opener created for CratesPlus.