Commits
md_5 authored 931e7ed751b
1 1 | package org.bukkit.command; |
2 2 | |
3 3 | import java.util.ArrayList; |
4 - | import java.util.logging.Level; |
5 4 | |
6 5 | import org.bukkit.Bukkit; |
7 - | import org.bukkit.entity.Player; |
8 - | import org.bukkit.event.player.PlayerCommandPreprocessEvent; |
9 - | import org.bukkit.event.server.RemoteServerCommandEvent; |
10 - | import org.bukkit.event.server.ServerCommandEvent; |
11 6 | |
12 7 | public class FormattedCommandAlias extends Command { |
13 8 | private final String[] formatStrings; |
14 9 | |
15 10 | public FormattedCommandAlias(String alias, String[] formatStrings) { |
16 11 | super(alias); |
17 12 | this.formatStrings = formatStrings; |
18 13 | } |
19 14 | |
20 15 | |
35 30 | } |
36 31 | |
37 32 | for (String command : commands) { |
38 33 | result |= Bukkit.dispatchCommand(sender, command); |
39 34 | } |
40 35 | |
41 36 | return result; |
42 37 | } |
43 38 | |
44 39 | private String buildCommand(String formatString, String[] args) { |
45 - | int index = formatString.indexOf("$"); |
40 + | int index = formatString.indexOf('$'); |
46 41 | while (index != -1) { |
47 42 | int start = index; |
48 43 | |
49 44 | if (index > 0 && formatString.charAt(start - 1) == '\\') { |
50 45 | formatString = formatString.substring(0, start - 1) + formatString.substring(start); |
51 - | index = formatString.indexOf("$", index); |
46 + | index = formatString.indexOf('$', index); |
52 47 | continue; |
53 48 | } |
54 49 | |
55 50 | boolean required = false; |
56 51 | if (formatString.charAt(index + 1) == '$') { |
57 52 | required = true; |
58 53 | // Move index past the second $ |
59 54 | index++; |
60 55 | } |
61 56 | |
65 60 | while (index < formatString.length() && inRange(((int) formatString.charAt(index)) - 48, 0, 9)) { |
66 61 | // Move index past current digit |
67 62 | index++; |
68 63 | } |
69 64 | |
70 65 | // No numbers found |
71 66 | if (argStart == index) { |
72 67 | throw new IllegalArgumentException("Invalid replacement token"); |
73 68 | } |
74 69 | |
75 - | int position = Integer.valueOf(formatString.substring(argStart, index)); |
70 + | int position = Integer.parseInt(formatString.substring(argStart, index)); |
76 71 | |
77 72 | // Arguments are not 0 indexed |
78 73 | if (position == 0) { |
79 74 | throw new IllegalArgumentException("Invalid replacement token"); |
80 75 | } |
81 76 | |
82 77 | // Convert position to 0 index |
83 78 | position--; |
84 79 | |
85 80 | boolean rest = false; |
105 100 | } |
106 101 | } else if (position < args.length) { |
107 102 | replacement.append(args[position]); |
108 103 | } |
109 104 | |
110 105 | formatString = formatString.substring(0, start) + replacement.toString() + formatString.substring(end); |
111 106 | // Move index past the replaced data so we don't process it again |
112 107 | index = start + replacement.length(); |
113 108 | |
114 109 | // Move to the next replacement token |
115 - | index = formatString.indexOf("$", index); |
110 + | index = formatString.indexOf('$', index); |
116 111 | } |
117 112 | |
118 113 | return formatString; |
119 114 | } |
120 115 | |
121 116 | private static boolean inRange(int i, int j, int k) { |
122 117 | return i >= j && i <= k; |
123 118 | } |
124 119 | } |