Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @QElement("EXAMPLE") // must be unique - defines the name of your condition used in command and storage
- public final class ExampleCondition extends Condition {
- /* Let's say this is condition that requires player's name to start or not start with certain string.*/
- private final String prefix;
- private final boolean inverted;
- public ExampleCondition(String prefix, boolean invert) {
- this.prefix - prefix;
- this.inverted = invert;
- }
- //This method is required when you want users to be able to have variables in custom comdition description
- @Override
- protected String parseDescription(String description) {
- //we are replacing every "%pref" in message with specified prefix
- return description.replaceAll("%pref", prefix);
- }
- //this method returns true if player meets the condition
- @Override
- public boolean isMet(Player player, Quester plugin) {
- return (player.getName().startsWith(prefix)) != inverted;
- }
- // this method returns string displayed in "/q show"
- @Override
- public String show() {
- String flag = inverted ? " not " : " ";
- return "Your name must" + flag + "start with " + prefix + ".";
- }
- // this method returns string that is appended to "EXAMPLE: " in "/q info"
- @Override
- public String info() {
- String flag = inverted ? " (-i)": "";
- return prefix + flag;
- // result is for example "EXAMPLE: prefix (-i)"
- }
- // element must contain this static fromCommand(QCommandContext context) method, that returns its supertype
- // it also hat to be annotated with @QCommand
- @QCommand(
- min = 1,
- max = 1,
- usage = "<prefix> (-i)")
- public static Condition fromCommand(QCommandContext context) throws QCommandException {
- String pre = context.getString(0);
- return new ExampleCondition(pre, context.hasFlag('i'));
- }
- // serialization to StorageKey
- @Override
- protected void save(StorageKey key) {
- key.setString("prefix", prefix);
- if(inverted) {
- key.setBoolean("inverted", inverted);
- }
- }
- // must contain static load(StorageKey key) method that returns its supertype, preferrably protected
- protected static Condition load(StorageKey key) {
- String pre = key.getString("amount", null);
- if(pre == null) { // invalid prefix
- return null;
- }
- return new ExampleCondition(pre, key.getBoolean("inverted", false));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment