Skip to content

Commit

Permalink
Added non-found methods from Commons
Browse files Browse the repository at this point in the history
  • Loading branch information
Despical committed Jul 14, 2023
1 parent 0d9fa57 commit 7691e0d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/main/java/me/despical/commandframework/CommandArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package me.despical.commandframework;

import me.despical.commandframework.utils.NumberUtils;
import me.despical.commandframework.utils.Utils;
import org.bukkit.command.CommandSender;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -100,7 +100,7 @@ public String getArgument(int i) {
*/
@NotNull
public Integer getArgumentAsInt(int i) {
return NumberUtils.getInt(this.getArgument(i));
return Utils.getInt(this.getArgument(i));
}

/**
Expand All @@ -110,7 +110,7 @@ public Integer getArgumentAsInt(int i) {
*/
@NotNull
public Double getArgumentAsDouble(int i) {
return NumberUtils.getDouble(this.getArgument(i));
return Utils.getDouble(this.getArgument(i));
}

/**
Expand All @@ -120,7 +120,7 @@ public Double getArgumentAsDouble(int i) {
*/
@NotNull
public Float getArgumentAsFloat(int i) {
return NumberUtils.getFloat(this.getArgument(i));
return Utils.getFloat(this.getArgument(i));
}

/**
Expand All @@ -130,7 +130,7 @@ public Float getArgumentAsFloat(int i) {
*/
@NotNull
public Long getArgumentAsLong(int i) {
return NumberUtils.getLong(this.getArgument(i));
return Utils.getLong(this.getArgument(i));
}

/**
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/me/despical/commandframework/CommandFramework.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package me.despical.commandframework;

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

if (completer.name().contains(".")) {
subCommandCompletions.put(completer, me.despical.commons.util.Collections.mapEntry(method, instance));
subCommandCompletions.put(completer, Utils.mapEntry(method, instance));
} else {
commandCompletions.put(completer, me.despical.commons.util.Collections.mapEntry(method, instance));
commandCompletions.put(completer, Utils.mapEntry(method, instance));
}
}
}
Expand All @@ -161,9 +162,9 @@ private void registerCommand(Command command, Method method, Object instance) {
final String cmdName = command.name();

if (cmdName.contains(".")) {
subCommands.put(command, me.despical.commons.util.Collections.mapEntry(method, instance));
subCommands.put(command, Utils.mapEntry(method, instance));
} else {
commands.put(command, me.despical.commons.util.Collections.mapEntry(method, instance));
commands.put(command, Utils.mapEntry(method, instance));

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

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

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

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

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

if (completer != null) {
return me.despical.commons.util.Collections.mapEntry(completer, subCommandCompletions.get(completer));
return Utils.mapEntry(completer, subCommandCompletions.get(completer));
}

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

if (completer != null) {
return me.despical.commons.util.Collections.mapEntry(completer, commandCompletions.get(completer));
return Utils.mapEntry(completer, commandCompletions.get(completer));
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@

package me.despical.commandframework.utils;

import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Despical
* <p>
* Created at 30.05.2020
*
* This class is a part of Despical's Commons library.
*/
public class NumberUtils {
public class Utils {

private NumberUtils() {
private Utils() {
}

/**
Expand Down Expand Up @@ -145,4 +150,43 @@ public static float getFloat(String string, float def) {
return def;
}
}

/**
* Returns a {@link Map.Entry} containing the given key and value.
*
* @param a new key to be stored in this entry.
* @param b new value to be stored in this entry.
* @param <K> new key type to be stored in this entry.
* @param <V> new value type to be stored in this entry.
* @return new {@link Map.Entry} containing the given key and value.
*/
public static <K, V> Map.Entry<K, V> mapEntry(K a, V b) {
return new AbstractMap.SimpleEntry<>(a, b);
}

/**
* Returns an mutable map containing one element.
*
* @param a key to be stored in this map.
* @param b value to be stored in this map.
* @param <K> key type to be stored in this map.
* @param <V> value type to be stored in this map.
* @return Returns an mutable map containing one element.
*/
public static <K, V> Map<K, V> mapOf(K a, V b) {
return mapOf(mapEntry(a, b));
}

/**
* Returns a mutable map containing an arbitrary number of elements.
*
* @param a Array of given entries to be stored in this map.
* @param <K> key type to be stored in this map.
* @param <V> value type to be stored in this map.
* @return Returns a mutable map containing an arbitrary number of elements.
*/
@SafeVarargs
public static <K, V> Map<K, V> mapOf(Map.Entry<K, V>... a) {
return Arrays.stream(a).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (b, c) -> c));
}
}

0 comments on commit 7691e0d

Please sign in to comment.