Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
VanillaCommand.java
1 package org.bukkit.command.defaults;
2 
3 import java.util.List;
4 
7 
8 public abstract class VanillaCommand extends Command {
9  static final int MAX_COORD = 30000000;
10  static final int MIN_COORD_MINUS_ONE = -30000001;
11  static final int MIN_COORD = -30000000;
12 
13  protected VanillaCommand(String name) {
14  super(name);
15  }
16 
17  protected VanillaCommand(String name, String description, String usageMessage, List<String> aliases) {
18  super(name, description, usageMessage, aliases);
19  }
20 
21  public boolean matches(String input) {
22  return input.equalsIgnoreCase(this.getName());
23  }
24 
25  protected int getInteger(CommandSender sender, String value, int min) {
26  return getInteger(sender, value, min, Integer.MAX_VALUE);
27  }
28 
29  int getInteger(CommandSender sender, String value, int min, int max) {
30  return getInteger(sender, value, min, max, false);
31  }
32 
33  int getInteger(CommandSender sender, String value, int min, int max, boolean Throws) {
34  int i = min;
35 
36  try {
37  i = Integer.valueOf(value);
38  } catch (NumberFormatException ex) {
39  if (Throws) {
40  throw new NumberFormatException(String.format("%s is not a valid number", value));
41  }
42  }
43 
44  if (i < min) {
45  i = min;
46  } else if (i > max) {
47  i = max;
48  }
49 
50  return i;
51  }
52 
53  Integer getInteger(String value) {
54  try {
55  return Integer.valueOf(value);
56  } catch (NumberFormatException ex) {
57  return null;
58  }
59  }
60 
61  public static double getRelativeDouble(double original, CommandSender sender, String input) {
62  if (input.startsWith("~")) {
63  double value = getDouble(sender, input.substring(1));
64  if (value == MIN_COORD_MINUS_ONE) {
65  return MIN_COORD_MINUS_ONE;
66  }
67  return original + value;
68  } else {
69  return getDouble(sender, input);
70  }
71  }
72 
73  public static double getDouble(CommandSender sender, String input) {
74  try {
75  return Double.parseDouble(input);
76  } catch (NumberFormatException ex) {
77  return MIN_COORD_MINUS_ONE;
78  }
79  }
80 
81  public static double getDouble(CommandSender sender, String input, double min, double max) {
82  double result = getDouble(sender, input);
83 
84  // TODO: This should throw an exception instead.
85  if (result < min) {
86  result = min;
87  } else if (result > max) {
88  result = max;
89  }
90 
91  return result;
92  }
93 
94  String createString(String[] args, int start) {
95  return createString(args, start, " ");
96  }
97 
98  String createString(String[] args, int start, String glue) {
99  StringBuilder string = new StringBuilder();
100 
101  for (int x = start; x < args.length; x++) {
102  string.append(args[x]);
103  if (x != args.length - 1) {
104  string.append(glue);
105  }
106  }
107 
108  return string.toString();
109  }
110 }