Skip to content

Commit

Permalink
Optimize BukkitUtility
Browse files Browse the repository at this point in the history
Use for-loops instead of streams
  • Loading branch information
srnyx committed May 23, 2024
1 parent acb9615 commit ef15b8c
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/main/java/xyz/srnyx/annoyingapi/utility/BukkitUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachmentInfo;
Expand All @@ -16,7 +17,6 @@
import java.util.*;
import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static xyz.srnyx.annoyingapi.reflection.org.bukkit.entity.RefEntity.*;

Expand Down Expand Up @@ -50,9 +50,9 @@ public static String color(@Nullable Object object) {
@NotNull
public static List<String> colorCollection(@Nullable Collection<?> objects) {
if (objects == null) return new ArrayList<>();
return objects.stream()
.map(BukkitUtility::color)
.collect(Collectors.toList());
final List<String> list = new ArrayList<>();
for (final Object object : objects) list.add(color(object));
return list;
}

/**
Expand Down Expand Up @@ -137,16 +137,23 @@ public static Integer getPermissionValue(@NotNull Player player, @NotNull String

/**
* Gets an {@link OfflinePlayer} from the specified name
* <br>Returns a {@link Player} if they're online
*
* @param name the name of the player
*
* @return the {@link OfflinePlayer}, or null if not found
*/
@Nullable
public static OfflinePlayer getOfflinePlayer(@NotNull String name) {
// Check online players
final Player online = Bukkit.getPlayerExact(name);
if (online != null) return online;

// Check offline players
final String nameLower = name.toLowerCase();
for (final OfflinePlayer offline : Bukkit.getOfflinePlayers()) {
final String offlineName = offline.getName();
if (offlineName != null && offlineName.equalsIgnoreCase(name)) return offline;
if (offlineName != null && offlineName.toLowerCase().equals(nameLower)) return offline;
}
return null;
}
Expand All @@ -158,9 +165,9 @@ public static OfflinePlayer getOfflinePlayer(@NotNull String name) {
*/
@NotNull
public static Set<String> getOnlinePlayerNames() {
return Bukkit.getOnlinePlayers().stream()
.map(Player::getName)
.collect(Collectors.toSet());
final Set<String> set = new HashSet<>();
for (final Player player : Bukkit.getOnlinePlayers()) set.add(player.getName());
return set;
}

/**
Expand All @@ -170,10 +177,13 @@ public static Set<String> getOnlinePlayerNames() {
*/
@NotNull
public static Set<String> getOfflinePlayerNames() {
return Arrays.stream(Bukkit.getOfflinePlayers())
.filter(player -> !player.isOnline())
.map(OfflinePlayer::getName)
.collect(Collectors.toSet());
final Set<String> set = new HashSet<>();
for (final OfflinePlayer player : Bukkit.getOfflinePlayers()) {
if (player.isOnline()) continue;
final String name = player.getName();
if (name != null) set.add(name);
}
return set;
}

/**
Expand All @@ -183,9 +193,12 @@ public static Set<String> getOfflinePlayerNames() {
*/
@NotNull
public static Set<String> getAllPlayerNames() {
return Arrays.stream(Bukkit.getOfflinePlayers())
.map(OfflinePlayer::getName)
.collect(Collectors.toSet());
final Set<String> set = new HashSet<>();
for (final OfflinePlayer player : Bukkit.getOfflinePlayers()) {
final String name = player.getName();
if (name != null) set.add(name);
}
return set;
}

/**
Expand All @@ -195,9 +208,9 @@ public static Set<String> getAllPlayerNames() {
*/
@NotNull
public static Set<String> getWorldNames() {
return Bukkit.getWorlds().stream()
.map(org.bukkit.World::getName)
.collect(Collectors.toSet());
final Set<String> set = new HashSet<>();
for (final World world : Bukkit.getWorlds()) set.add(world.getName());
return set;
}

/**
Expand Down

0 comments on commit ef15b8c

Please sign in to comment.