Permalink
Browse files

Replace Guava Iterables (#158)

Goes under #120

After this, only MultiMaps remain.
  • Loading branch information...
mikroskeem authored and aikar committed Aug 3, 2018
1 parent 8fda944 commit 65ff5a55002873d370f0bf21c338db94697285e2
@@ -23,7 +23,6 @@
package co.aikar.commands;
import com.google.common.collect.Iterables;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
@@ -281,7 +280,7 @@ public static Player findPlayerSmart(CommandIssuer issuer, String search) {
"{search}", name);
return null;
} else {
Player player = Iterables.getOnlyElement(confirmList);
Player player = ACFUtil.getFirstElement(confirmList);
issuer.sendInfo(MinecraftMessageKeys.PLAYER_IS_VANISHED_CONFIRM, "{vanished}", player.getName());
return null;
}
@@ -33,6 +33,7 @@
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
@@ -588,6 +589,16 @@ public static void sneaky(Throwable t) {
return list;
}
public static <T> T getFirstElement(Iterable<T> iterable) {
Iterator<T> iterator = iterable.iterator();
T first = iterator.next();
if (!iterator.hasNext()) {
return first;
}
throw new IllegalArgumentException("Expected one element in iterable");
}
private static class ApplyModifierToNumber {
private String num;
private boolean suffixes;
@@ -35,7 +35,6 @@
import co.aikar.commands.annotation.UnknownHandler;
import co.aikar.commands.apachecommonslang.ApacheCommonsLangUtil;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.SetMultimap;
import org.jetbrains.annotations.Nullable;
@@ -614,7 +613,7 @@ private CommandSearch findSubCommand(String[] args, boolean completion) {
if (!cmds.isEmpty()) {
RegisteredCommand cmd = null;
if (cmds.size() == 1) {
cmd = Iterables.getOnlyElement(cmds);
cmd = ACFUtil.getFirstElement(cmds);
} else {
Optional<RegisteredCommand> optCmd = cmds.stream().filter(c -> {
int required = c.requiredResolvers;
@@ -718,9 +717,9 @@ public boolean canExecute(CommandIssuer issuer, RegisteredCommand<?> cmd) {
if (search != null) {
cmds.addAll(completeCommand(issuer, search.cmd, Arrays.copyOfRange(args, search.argIndex, args.length), commandLabel, isAsync));
} else if (subCommands.get(CATCHUNKNOWN).size() == 1) {
cmds.addAll(completeCommand(issuer, Iterables.getOnlyElement(subCommands.get(CATCHUNKNOWN)), args, commandLabel, isAsync));
cmds.addAll(completeCommand(issuer, ACFUtil.getFirstElement(subCommands.get(CATCHUNKNOWN)), args, commandLabel, isAsync));
} else if (subCommands.get(DEFAULT).size() == 1) {
cmds.addAll(completeCommand(issuer, Iterables.getOnlyElement(subCommands.get(DEFAULT)), args, commandLabel, isAsync));
cmds.addAll(completeCommand(issuer, ACFUtil.getFirstElement(subCommands.get(DEFAULT)), args, commandLabel, isAsync));
}
return filterTabComplete(args[args.length - 1], cmds);
@@ -1,6 +1,5 @@
package co.aikar.commands;
import com.google.common.collect.Iterables;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;
@@ -45,7 +44,7 @@ public static Player findPlayerSmart(CommandIssuer issuer, String search) {
"{search}", name);
return null;
} else {
Player player = Iterables.getOnlyElement(confirmList);
Player player = ACFUtil.getFirstElement(confirmList);
issuer.sendInfo(MinecraftMessageKeys.PLAYER_IS_VANISHED_CONFIRM, "{vanished}", player.getName());
return null;
}

0 comments on commit 65ff5a5

Please sign in to comment.