2013-09-28 18:21:16 +02:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
|
|
|
import com.earth2me.essentials.ChargeException;
|
2013-10-16 21:59:39 +02:00
|
|
|
import com.earth2me.essentials.CommandSource;
|
2013-09-28 18:21:16 +02:00
|
|
|
import com.earth2me.essentials.User;
|
2014-06-27 02:46:37 +02:00
|
|
|
import com.earth2me.essentials.utils.FormatUtil;
|
2019-12-23 14:06:15 +01:00
|
|
|
import com.earth2me.essentials.utils.StringUtil;
|
2014-02-02 17:07:32 +01:00
|
|
|
import net.ess3.api.MaxMoneyException;
|
2013-09-28 18:21:16 +02:00
|
|
|
import org.bukkit.Server;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
2013-09-28 18:21:16 +02:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
public abstract class EssentialsLoopCommand extends EssentialsCommand {
|
|
|
|
public EssentialsLoopCommand(String command) {
|
|
|
|
super(command);
|
|
|
|
}
|
2013-09-28 18:21:16 +02:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
protected void loopOfflinePlayers(final Server server, final CommandSource sender, final boolean multipleStringMatches, boolean matchWildcards, final String searchTerm, final String[] commandArgs) throws PlayerNotFoundException, NotEnoughArgumentsException, PlayerExemptException, ChargeException, MaxMoneyException {
|
2020-08-11 20:09:22 +02:00
|
|
|
loopOfflinePlayersConsumer(server, sender, multipleStringMatches, matchWildcards, searchTerm, user -> updatePlayer(server, sender, user, commandArgs));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void loopOfflinePlayersConsumer(final Server server, final CommandSource sender, final boolean multipleStringMatches, boolean matchWildcards, final String searchTerm, UserConsumer userConsumer) throws PlayerNotFoundException, NotEnoughArgumentsException, PlayerExemptException, ChargeException, MaxMoneyException {
|
2015-04-15 06:06:16 +02:00
|
|
|
if (searchTerm.isEmpty()) {
|
|
|
|
throw new PlayerNotFoundException();
|
|
|
|
}
|
2013-09-28 18:21:16 +02:00
|
|
|
|
2019-12-23 14:06:15 +01:00
|
|
|
final UUID uuid = StringUtil.toUUID(searchTerm);
|
|
|
|
if (uuid != null) {
|
|
|
|
final User matchedUser = ess.getUser(uuid);
|
2020-08-11 20:09:22 +02:00
|
|
|
userConsumer.accept(matchedUser);
|
2019-12-23 14:06:15 +01:00
|
|
|
} else if (matchWildcards && searchTerm.contentEquals("**")) {
|
2015-04-15 06:06:16 +02:00
|
|
|
for (UUID sUser : ess.getUserMap().getAllUniqueUsers()) {
|
|
|
|
final User matchedUser = ess.getUser(sUser);
|
2020-08-11 20:09:22 +02:00
|
|
|
userConsumer.accept(matchedUser);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
} else if (matchWildcards && searchTerm.contentEquals("*")) {
|
|
|
|
boolean skipHidden = sender.isPlayer() && !ess.getUser(sender.getPlayer()).canInteractVanished();
|
|
|
|
for (User onlineUser : ess.getOnlineUsers()) {
|
|
|
|
if (skipHidden && onlineUser.isHidden(sender.getPlayer()) && !sender.getPlayer().canSee(onlineUser.getBase())) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-11 20:09:22 +02:00
|
|
|
userConsumer.accept(onlineUser);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
} else if (multipleStringMatches) {
|
|
|
|
if (searchTerm.trim().length() < 3) {
|
|
|
|
throw new PlayerNotFoundException();
|
|
|
|
}
|
|
|
|
final List<Player> matchedPlayers = server.matchPlayer(searchTerm);
|
|
|
|
if (matchedPlayers.isEmpty()) {
|
|
|
|
final User matchedUser = getPlayer(server, searchTerm, true, true);
|
2020-08-11 20:09:22 +02:00
|
|
|
userConsumer.accept(matchedUser);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
for (Player matchPlayer : matchedPlayers) {
|
|
|
|
final User matchedUser = ess.getUser(matchPlayer);
|
2020-08-11 20:09:22 +02:00
|
|
|
userConsumer.accept(matchedUser);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
final User user = getPlayer(server, searchTerm, true, true);
|
2020-08-11 20:09:22 +02:00
|
|
|
userConsumer.accept(user);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
2013-09-28 18:21:16 +02:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
protected void loopOnlinePlayers(final Server server, final CommandSource sender, final boolean multipleStringMatches, boolean matchWildcards, final String searchTerm, final String[] commandArgs) throws PlayerNotFoundException, NotEnoughArgumentsException, PlayerExemptException, ChargeException, MaxMoneyException {
|
2020-08-11 20:09:22 +02:00
|
|
|
loopOnlinePlayersConsumer(server, sender, multipleStringMatches, matchWildcards, searchTerm, user -> updatePlayer(server, sender, user, commandArgs));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void loopOnlinePlayersConsumer(final Server server, final CommandSource sender, final boolean multipleStringMatches, boolean matchWildcards, final String searchTerm, UserConsumer userConsumer) throws PlayerNotFoundException, NotEnoughArgumentsException, PlayerExemptException, ChargeException, MaxMoneyException {
|
2015-04-15 06:06:16 +02:00
|
|
|
if (searchTerm.isEmpty()) {
|
|
|
|
throw new PlayerNotFoundException();
|
|
|
|
}
|
2013-09-28 18:21:16 +02:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
boolean skipHidden = sender.isPlayer() && !ess.getUser(sender.getPlayer()).canInteractVanished();
|
2013-09-28 18:21:16 +02:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
if (matchWildcards && (searchTerm.contentEquals("**") || searchTerm.contentEquals("*"))) {
|
|
|
|
for (User onlineUser : ess.getOnlineUsers()) {
|
|
|
|
if (skipHidden && onlineUser.isHidden(sender.getPlayer()) && !sender.getPlayer().canSee(onlineUser.getBase())) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-11 20:09:22 +02:00
|
|
|
userConsumer.accept(onlineUser);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
} else if (multipleStringMatches) {
|
|
|
|
if (searchTerm.trim().length() < 2) {
|
|
|
|
throw new PlayerNotFoundException();
|
|
|
|
}
|
|
|
|
boolean foundUser = false;
|
|
|
|
final List<Player> matchedPlayers = server.matchPlayer(searchTerm);
|
2014-06-27 02:46:37 +02:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
if (matchedPlayers.isEmpty()) {
|
|
|
|
final String matchText = searchTerm.toLowerCase(Locale.ENGLISH);
|
|
|
|
for (User player : ess.getOnlineUsers()) {
|
|
|
|
if (skipHidden && player.isHidden(sender.getPlayer()) && !sender.getPlayer().canSee(player.getBase())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final String displayName = FormatUtil.stripFormat(player.getDisplayName()).toLowerCase(Locale.ENGLISH);
|
|
|
|
if (displayName.contains(matchText)) {
|
|
|
|
foundUser = true;
|
2020-08-11 20:09:22 +02:00
|
|
|
userConsumer.accept(player);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (Player matchPlayer : matchedPlayers) {
|
|
|
|
final User player = ess.getUser(matchPlayer);
|
|
|
|
if (skipHidden && player.isHidden(sender.getPlayer()) && !sender.getPlayer().canSee(matchPlayer)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foundUser = true;
|
2020-08-11 20:09:22 +02:00
|
|
|
userConsumer.accept(player);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!foundUser) {
|
|
|
|
throw new PlayerNotFoundException();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
final User player = getPlayer(server, sender, searchTerm);
|
2020-08-11 20:09:22 +02:00
|
|
|
userConsumer.accept(player);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
}
|
2013-09-28 18:21:16 +02:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
protected abstract void updatePlayer(Server server, CommandSource sender, User user, String[] args) throws NotEnoughArgumentsException, PlayerExemptException, ChargeException, MaxMoneyException;
|
2017-04-14 03:46:43 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<String> getPlayers(final Server server, final CommandSource interactor) {
|
|
|
|
List<String> players = super.getPlayers(server, interactor);
|
|
|
|
players.add("**");
|
|
|
|
players.add("*");
|
|
|
|
return players;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<String> getPlayers(final Server server, final User interactor) {
|
|
|
|
List<String> players = super.getPlayers(server, interactor);
|
|
|
|
players.add("**");
|
|
|
|
players.add("*");
|
|
|
|
return players;
|
|
|
|
}
|
2020-08-11 20:09:22 +02:00
|
|
|
|
|
|
|
public interface UserConsumer {
|
|
|
|
void accept(User user) throws PlayerNotFoundException, NotEnoughArgumentsException, PlayerExemptException, ChargeException, MaxMoneyException;
|
|
|
|
}
|
2013-09-28 18:21:16 +02:00
|
|
|
}
|