Skip to content

Commit 7691e0d

Browse files
committed
Added non-found methods from Commons
1 parent 0d9fa57 commit 7691e0d

File tree

3 files changed

+61
-16
lines changed

3 files changed

+61
-16
lines changed

src/main/java/me/despical/commandframework/CommandArguments.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package me.despical.commandframework;
1919

20-
import me.despical.commandframework.utils.NumberUtils;
20+
import me.despical.commandframework.utils.Utils;
2121
import org.bukkit.command.CommandSender;
2222
import org.bukkit.command.Command;
2323
import org.bukkit.entity.Player;
@@ -100,7 +100,7 @@ public String getArgument(int i) {
100100
*/
101101
@NotNull
102102
public Integer getArgumentAsInt(int i) {
103-
return NumberUtils.getInt(this.getArgument(i));
103+
return Utils.getInt(this.getArgument(i));
104104
}
105105

106106
/**
@@ -110,7 +110,7 @@ public Integer getArgumentAsInt(int i) {
110110
*/
111111
@NotNull
112112
public Double getArgumentAsDouble(int i) {
113-
return NumberUtils.getDouble(this.getArgument(i));
113+
return Utils.getDouble(this.getArgument(i));
114114
}
115115

116116
/**
@@ -120,7 +120,7 @@ public Double getArgumentAsDouble(int i) {
120120
*/
121121
@NotNull
122122
public Float getArgumentAsFloat(int i) {
123-
return NumberUtils.getFloat(this.getArgument(i));
123+
return Utils.getFloat(this.getArgument(i));
124124
}
125125

126126
/**
@@ -130,7 +130,7 @@ public Float getArgumentAsFloat(int i) {
130130
*/
131131
@NotNull
132132
public Long getArgumentAsLong(int i) {
133-
return NumberUtils.getLong(this.getArgument(i));
133+
return Utils.getLong(this.getArgument(i));
134134
}
135135

136136
/**

src/main/java/me/despical/commandframework/CommandFramework.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package me.despical.commandframework;
1919

20+
import me.despical.commandframework.utils.Utils;
2021
import org.bukkit.ChatColor;
2122
import org.bukkit.command.*;
2223
import org.bukkit.entity.Player;
@@ -142,9 +143,9 @@ public void registerCommands(@NotNull Object instance) {
142143
final Completer completer = method.getAnnotation(Completer.class);
143144

144145
if (completer.name().contains(".")) {
145-
subCommandCompletions.put(completer, me.despical.commons.util.Collections.mapEntry(method, instance));
146+
subCommandCompletions.put(completer, Utils.mapEntry(method, instance));
146147
} else {
147-
commandCompletions.put(completer, me.despical.commons.util.Collections.mapEntry(method, instance));
148+
commandCompletions.put(completer, Utils.mapEntry(method, instance));
148149
}
149150
}
150151
}
@@ -161,9 +162,9 @@ private void registerCommand(Command command, Method method, Object instance) {
161162
final String cmdName = command.name();
162163

163164
if (cmdName.contains(".")) {
164-
subCommands.put(command, me.despical.commons.util.Collections.mapEntry(method, instance));
165+
subCommands.put(command, Utils.mapEntry(method, instance));
165166
} else {
166-
commands.put(command, me.despical.commons.util.Collections.mapEntry(method, instance));
167+
commands.put(command, Utils.mapEntry(method, instance));
167168

168169
try {
169170
final Constructor<PluginCommand> constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
@@ -200,7 +201,7 @@ private Map.Entry<Command, Map.Entry<Method, Object>> getAssociatedCommand(@NotN
200201

201202
// If we found the sub command then return it, otherwise search the commands map
202203
if (command != null) {
203-
return me.despical.commons.util.Collections.mapEntry(command, subCommands.get(command));
204+
return Utils.mapEntry(command, subCommands.get(command));
204205
}
205206

206207
// If our command is not a sub command then search for a main command
@@ -217,7 +218,7 @@ private Map.Entry<Command, Map.Entry<Method, Object>> getAssociatedCommand(@NotN
217218
if (command != null) {
218219
// Quick fix to accept any match consumer if defined
219220
if (command.min() >= possibleArgs.length || command.allowInfiniteArgs()) {
220-
return me.despical.commons.util.Collections.mapEntry(command, commands.get(command));
221+
return Utils.mapEntry(command, commands.get(command));
221222
}
222223
}
223224

@@ -231,7 +232,7 @@ private boolean hasCooldown(final CommandSender sender, final Command command) {
231232
final Map<Command, Long> cooldownMap = cooldowns.get(sender);
232233

233234
if (cooldownMap == null) {
234-
cooldowns.put(sender, me.despical.commons.util.Collections.mapOf(command, System.currentTimeMillis()));
235+
cooldowns.put(sender, Utils.mapOf(command, System.currentTimeMillis()));
235236
return false;
236237
} else if (!cooldownMap.containsKey(command)) {
237238
cooldownMap.put(command, System.currentTimeMillis());
@@ -313,7 +314,7 @@ private Map.Entry<Completer, Map.Entry<Method, Object>> getAssociatedCompleter(@
313314
}
314315

315316
if (completer != null) {
316-
return me.despical.commons.util.Collections.mapEntry(completer, subCommandCompletions.get(completer));
317+
return Utils.mapEntry(completer, subCommandCompletions.get(completer));
317318
}
318319

319320
for (Completer comp : commandCompletions.keySet()) {
@@ -326,7 +327,7 @@ private Map.Entry<Completer, Map.Entry<Method, Object>> getAssociatedCompleter(@
326327
}
327328

328329
if (completer != null) {
329-
return me.despical.commons.util.Collections.mapEntry(completer, commandCompletions.get(completer));
330+
return Utils.mapEntry(completer, commandCompletions.get(completer));
330331
}
331332

332333
return null;

src/main/java/me/despical/commandframework/utils/NumberUtils.java renamed to src/main/java/me/despical/commandframework/utils/Utils.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@
1818

1919
package me.despical.commandframework.utils;
2020

21+
import java.util.AbstractMap;
22+
import java.util.Arrays;
23+
import java.util.Map;
24+
import java.util.stream.Collectors;
25+
2126
/**
2227
* @author Despical
2328
* <p>
2429
* Created at 30.05.2020
2530
*
2631
* This class is a part of Despical's Commons library.
2732
*/
28-
public class NumberUtils {
33+
public class Utils {
2934

30-
private NumberUtils() {
35+
private Utils() {
3136
}
3237

3338
/**
@@ -145,4 +150,43 @@ public static float getFloat(String string, float def) {
145150
return def;
146151
}
147152
}
153+
154+
/**
155+
* Returns a {@link Map.Entry} containing the given key and value.
156+
*
157+
* @param a new key to be stored in this entry.
158+
* @param b new value to be stored in this entry.
159+
* @param <K> new key type to be stored in this entry.
160+
* @param <V> new value type to be stored in this entry.
161+
* @return new {@link Map.Entry} containing the given key and value.
162+
*/
163+
public static <K, V> Map.Entry<K, V> mapEntry(K a, V b) {
164+
return new AbstractMap.SimpleEntry<>(a, b);
165+
}
166+
167+
/**
168+
* Returns an mutable map containing one element.
169+
*
170+
* @param a key to be stored in this map.
171+
* @param b value to be stored in this map.
172+
* @param <K> key type to be stored in this map.
173+
* @param <V> value type to be stored in this map.
174+
* @return Returns an mutable map containing one element.
175+
*/
176+
public static <K, V> Map<K, V> mapOf(K a, V b) {
177+
return mapOf(mapEntry(a, b));
178+
}
179+
180+
/**
181+
* Returns a mutable map containing an arbitrary number of elements.
182+
*
183+
* @param a Array of given entries to be stored in this map.
184+
* @param <K> key type to be stored in this map.
185+
* @param <V> value type to be stored in this map.
186+
* @return Returns a mutable map containing an arbitrary number of elements.
187+
*/
188+
@SafeVarargs
189+
public static <K, V> Map<K, V> mapOf(Map.Entry<K, V>... a) {
190+
return Arrays.stream(a).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (b, c) -> c));
191+
}
148192
}

0 commit comments

Comments
 (0)