Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
FormattedCommandAlias.java
1 package org.bukkit.command;
2 
3 import java.util.ArrayList;
4 import java.util.logging.Level;
5 
6 import org.bukkit.Bukkit;
7 import org.bukkit.entity.Player;
11 
12 public class FormattedCommandAlias extends Command {
13  private final String[] formatStrings;
14 
15  public FormattedCommandAlias(String alias, String[] formatStrings) {
16  super(alias);
17  this.formatStrings = formatStrings;
18  }
19 
20  @Override
21  public boolean execute(CommandSender sender, String commandLabel, String[] args) {
22  boolean result = false;
23  ArrayList<String> commands = new ArrayList<String>();
24  for (String formatString : formatStrings) {
25  try {
26  commands.add(buildCommand(formatString, args));
27  } catch (Throwable throwable) {
28  if (throwable instanceof IllegalArgumentException) {
29  sender.sendMessage(throwable.getMessage());
30  } else {
31  sender.sendMessage(org.bukkit.ChatColor.RED + "An internal error occurred while attempting to perform this command");
32  }
33  return false;
34  }
35  }
36 
37  for (String command : commands) {
38  result |= Bukkit.dispatchCommand(sender, command);
39  }
40 
41  return result;
42  }
43 
44  private String buildCommand(String formatString, String[] args) {
45  int index = formatString.indexOf("$");
46  while (index != -1) {
47  int start = index;
48 
49  if (index > 0 && formatString.charAt(start - 1) == '\\') {
50  formatString = formatString.substring(0, start - 1) + formatString.substring(start);
51  index = formatString.indexOf("$", index);
52  continue;
53  }
54 
55  boolean required = false;
56  if (formatString.charAt(index + 1) == '$') {
57  required = true;
58  // Move index past the second $
59  index++;
60  }
61 
62  // Move index past the $
63  index++;
64  int argStart = index;
65  while (index < formatString.length() && inRange(((int) formatString.charAt(index)) - 48, 0, 9)) {
66  // Move index past current digit
67  index++;
68  }
69 
70  // No numbers found
71  if (argStart == index) {
72  throw new IllegalArgumentException("Invalid replacement token");
73  }
74 
75  int position = Integer.valueOf(formatString.substring(argStart, index));
76 
77  // Arguments are not 0 indexed
78  if (position == 0) {
79  throw new IllegalArgumentException("Invalid replacement token");
80  }
81 
82  // Convert position to 0 index
83  position--;
84 
85  boolean rest = false;
86  if (index < formatString.length() && formatString.charAt(index) == '-') {
87  rest = true;
88  // Move index past the -
89  index++;
90  }
91 
92  int end = index;
93 
94  if (required && position >= args.length) {
95  throw new IllegalArgumentException("Missing required argument " + (position + 1));
96  }
97 
98  StringBuilder replacement = new StringBuilder();
99  if (rest && position < args.length) {
100  for (int i = position; i < args.length; i++) {
101  if (i != position) {
102  replacement.append(' ');
103  }
104  replacement.append(args[i]);
105  }
106  } else if (position < args.length) {
107  replacement.append(args[position]);
108  }
109 
110  formatString = formatString.substring(0, start) + replacement.toString() + formatString.substring(end);
111  // Move index past the replaced data so we don't process it again
112  index = start + replacement.length();
113 
114  // Move to the next replacement token
115  index = formatString.indexOf("$", index);
116  }
117 
118  return formatString;
119  }
120 
121  private static boolean inRange(int i, int j, int k) {
122  return i >= j && i <= k;
123  }
124 }
void sendMessage(String message)
static boolean dispatchCommand(CommandSender sender, String commandLine)
Definition: Bukkit.java:315