Guest User

Untitled

a guest
Jul 4th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. @QElement("EXAMPLE") // must be unique - defines the name of your condition used in command and storage
  2. public final class ExampleCondition extends Condition {
  3.  
  4.     /* Let's say this is condition that requires player's name to start or not start with certain string.*/
  5.  
  6.     private final String prefix;
  7.     private final boolean inverted;
  8.  
  9.     public ExampleCondition(String prefix, boolean invert) {
  10.         this.prefix - prefix;
  11.         this.inverted = invert;
  12.     }
  13.  
  14.     //This method is required when you want users to be able to have variables in custom comdition description
  15.     @Override
  16.     protected String parseDescription(String description) {
  17.         //we are replacing every "%pref" in message with specified prefix
  18.         return description.replaceAll("%pref", prefix);
  19.     }
  20.  
  21.     //this method returns true if player meets the condition
  22.     @Override
  23.     public boolean isMet(Player player, Quester plugin) {
  24.         return (player.getName().startsWith(prefix)) != inverted;
  25.     }
  26.  
  27.     // this method returns string displayed in "/q show"
  28.     @Override
  29.     public String show() {
  30.         String flag = inverted ? " not " : " ";
  31.         return "Your name must" + flag + "start with " + prefix + ".";
  32.     }
  33.  
  34.     // this method returns string that is appended to "EXAMPLE: " in "/q info"
  35.     @Override
  36.     public String info() {
  37.         String flag = inverted ? " (-i)": "";
  38.         return prefix + flag;
  39.         // result is for example "EXAMPLE: prefix (-i)"
  40.     }
  41.    
  42.     // element must contain this static fromCommand(QCommandContext context) method, that returns its supertype
  43.     // it also hat to be annotated with @QCommand
  44.     @QCommand(
  45.             min = 1,
  46.             max = 1,
  47.             usage = "<prefix> (-i)")
  48.     public static Condition fromCommand(QCommandContext context) throws QCommandException {
  49.         String pre = context.getString(0);
  50.         return new ExampleCondition(pre, context.hasFlag('i'));
  51.     }
  52.  
  53.     // serialization to StorageKey
  54.     @Override
  55.     protected void save(StorageKey key) {
  56.         key.setString("prefix", prefix);
  57.         if(inverted) {
  58.             key.setBoolean("inverted", inverted);
  59.         }
  60.     }
  61.  
  62.     // must contain static load(StorageKey key) method that returns its supertype, preferrably protected
  63.     protected static Condition load(StorageKey key) {
  64.         String pre = key.getString("amount", null);
  65.         if(pre == null) { // invalid prefix
  66.             return null;
  67.         }
  68.  
  69.         return new ExampleCondition(pre, key.getBoolean("inverted", false));
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment