Use java 17 toList()

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-11-23 20:27:25 +01:00
parent ad75e942aa
commit 0f2ad33e52
9 changed files with 23 additions and 48 deletions

View File

@ -8,7 +8,6 @@ import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors;
/** /**
* Utility class to access Adventure audiences. * Utility class to access Adventure audiences.
@ -61,7 +60,7 @@ public class Audiences {
* @return all players matching the predicate * @return all players matching the predicate
*/ */
public static @NotNull Audience players(@NotNull Predicate<Player> filter) { public static @NotNull Audience players(@NotNull Predicate<Player> filter) {
return PacketGroupingAudience.of(MinecraftServer.getConnectionManager().getOnlinePlayers().stream().filter(filter).collect(Collectors.toList())); return PacketGroupingAudience.of(MinecraftServer.getConnectionManager().getOnlinePlayers().stream().filter(filter).toList());
} }
/** /**

View File

@ -13,7 +13,6 @@ import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
/** /**
@ -39,7 +38,7 @@ class IterableAudienceProvider implements AudienceProvider<Iterable<? extends Au
@Override @Override
public @NotNull Iterable<? extends Audience> players(@NotNull Predicate<Player> filter) { public @NotNull Iterable<? extends Audience> players(@NotNull Predicate<Player> filter) {
return MinecraftServer.getConnectionManager().getOnlinePlayers().stream().filter(filter).collect(Collectors.toList()); return MinecraftServer.getConnectionManager().getOnlinePlayers().stream().filter(filter).toList();
} }
@Override @Override
@ -64,7 +63,7 @@ class IterableAudienceProvider implements AudienceProvider<Iterable<? extends Au
@Override @Override
public @NotNull Iterable<? extends Audience> custom(@NotNull Key key, Predicate<Audience> filter) { public @NotNull Iterable<? extends Audience> custom(@NotNull Key key, Predicate<Audience> filter) {
return StreamSupport.stream(this.registry.of(key).spliterator(), false).filter(filter).collect(Collectors.toList()); return StreamSupport.stream(this.registry.of(key).spliterator(), false).filter(filter).toList();
} }
@Override @Override
@ -74,7 +73,7 @@ class IterableAudienceProvider implements AudienceProvider<Iterable<? extends Au
@Override @Override
public @NotNull Iterable<? extends Audience> all(@NotNull Predicate<Audience> filter) { public @NotNull Iterable<? extends Audience> all(@NotNull Predicate<Audience> filter) {
return StreamSupport.stream(this.all().spliterator(), false).filter(filter).collect(Collectors.toList()); return StreamSupport.stream(this.all().spliterator(), false).filter(filter).toList();
} }
@Override @Override

View File

@ -7,7 +7,6 @@ import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors;
/** /**
* A provider of audiences. For complex returns, this instance is backed by * A provider of audiences. For complex returns, this instance is backed by
@ -43,7 +42,7 @@ class SingleAudienceProvider implements AudienceProvider<Audience> {
@Override @Override
public @NotNull Audience players(@NotNull Predicate<Player> filter) { public @NotNull Audience players(@NotNull Predicate<Player> filter) {
return PacketGroupingAudience.of(MinecraftServer.getConnectionManager().getOnlinePlayers().stream().filter(filter).collect(Collectors.toList())); return PacketGroupingAudience.of(MinecraftServer.getConnectionManager().getOnlinePlayers().stream().filter(filter).toList());
} }
@Override @Override

View File

@ -9,7 +9,6 @@ import org.jetbrains.annotations.NotNull;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/** /**
* Manages all boss bars known to this Minestom instance. Although this class can be used * Manages all boss bars known to this Minestom instance. Although this class can be used
@ -76,8 +75,7 @@ public class BossBarManager {
*/ */
public void addBossBar(@NotNull Collection<Player> players, @NotNull BossBar bar) { public void addBossBar(@NotNull Collection<Player> players, @NotNull BossBar bar) {
BossBarHolder holder = this.getOrCreateHandler(bar); BossBarHolder holder = this.getOrCreateHandler(bar);
Collection<Player> addedPlayers = players.stream().filter(holder::addViewer).collect(Collectors.toList()); Collection<Player> addedPlayers = players.stream().filter(holder::addViewer).toList();
if (!addedPlayers.isEmpty()) { if (!addedPlayers.isEmpty()) {
PacketUtils.sendGroupedPacket(addedPlayers, holder.createAddPacket()); PacketUtils.sendGroupedPacket(addedPlayers, holder.createAddPacket());
} }
@ -91,10 +89,8 @@ public class BossBarManager {
*/ */
public void removeBossBar(@NotNull Collection<Player> players, @NotNull BossBar bar) { public void removeBossBar(@NotNull Collection<Player> players, @NotNull BossBar bar) {
BossBarHolder holder = this.bars.get(bar); BossBarHolder holder = this.bars.get(bar);
if (holder != null) { if (holder != null) {
Collection<Player> removedPlayers = players.stream().filter(holder::removeViewer).collect(Collectors.toList()); Collection<Player> removedPlayers = players.stream().filter(holder::removeViewer).toList();
if (!removedPlayers.isEmpty()) { if (!removedPlayers.isEmpty()) {
PacketUtils.sendGroupedPacket(removedPlayers, holder.createRemovePacket()); PacketUtils.sendGroupedPacket(removedPlayers, holder.createRemovePacket());
} }
@ -108,10 +104,8 @@ public class BossBarManager {
*/ */
public void destroyBossBar(@NotNull BossBar bossBar) { public void destroyBossBar(@NotNull BossBar bossBar) {
BossBarHolder holder = this.bars.remove(bossBar); BossBarHolder holder = this.bars.remove(bossBar);
if (holder != null) { if (holder != null) {
PacketUtils.sendGroupedPacket(holder.players, holder.createRemovePacket()); PacketUtils.sendGroupedPacket(holder.players, holder.createRemovePacket());
for (Player player : holder.players) { for (Player player : holder.players) {
this.removePlayer(player, holder); this.removePlayer(player, holder);
} }
@ -127,7 +121,6 @@ public class BossBarManager {
*/ */
public void removeAllBossBars(@NotNull Player player) { public void removeAllBossBars(@NotNull Player player) {
Set<BossBarHolder> holders = this.playerBars.remove(player.getUuid()); Set<BossBarHolder> holders = this.playerBars.remove(player.getUuid());
if (holders != null) { if (holders != null) {
for (BossBarHolder holder : holders) { for (BossBarHolder holder : holders) {
holder.removeViewer(player); holder.removeViewer(player);
@ -143,12 +136,8 @@ public class BossBarManager {
*/ */
public @NotNull Collection<BossBar> getPlayerBossBars(@NotNull Player player) { public @NotNull Collection<BossBar> getPlayerBossBars(@NotNull Player player) {
Collection<BossBarHolder> holders = this.playerBars.get(player.getUuid()); Collection<BossBarHolder> holders = this.playerBars.get(player.getUuid());
return holders != null ?
if (holders == null) { holders.stream().map(holder -> holder.bar).toList() : Collections.emptyList();
return Collections.emptyList();
} else {
return holders.stream().map(holder -> holder.bar).toList();
}
} }
/** /**
@ -159,12 +148,8 @@ public class BossBarManager {
*/ */
public @NotNull Collection<Player> getBossBarViewers(@NotNull BossBar bossBar) { public @NotNull Collection<Player> getBossBarViewers(@NotNull BossBar bossBar) {
BossBarHolder holder = this.bars.get(bossBar); BossBarHolder holder = this.bars.get(bossBar);
return holder != null ?
if (holder == null) { Collections.unmodifiableCollection(holder.players) : Collections.emptyList();
return Collections.emptyList();
} else {
return Collections.unmodifiableCollection(holder.players);
}
} }
/** /**
@ -183,10 +168,8 @@ public class BossBarManager {
private void removePlayer(Player player, BossBarHolder holder) { private void removePlayer(Player player, BossBarHolder holder) {
Set<BossBarHolder> holders = this.playerBars.get(player.getUuid()); Set<BossBarHolder> holders = this.playerBars.get(player.getUuid());
if (holders != null) { if (holders != null) {
holders.remove(holder); holders.remove(holder);
if (holders.isEmpty()) { if (holders.isEmpty()) {
this.playerBars.remove(player.getUuid()); this.playerBars.remove(player.getUuid());
} }

View File

@ -21,7 +21,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* Manager used to register {@link Command commands}. * Manager used to register {@link Command commands}.
@ -315,7 +314,7 @@ public final class CommandManager {
Map<IndexedArgument, List<DeclareCommandsPacket.Node[]>> storedArgumentsNodes = new HashMap<>(); Map<IndexedArgument, List<DeclareCommandsPacket.Node[]>> storedArgumentsNodes = new HashMap<>();
// Sort syntaxes by argument count. Brigadier requires it. // Sort syntaxes by argument count. Brigadier requires it.
syntaxes = syntaxes.stream().sorted(Comparator.comparingInt(o -> -o.getArguments().length)).collect(Collectors.toList()); syntaxes = syntaxes.stream().sorted(Comparator.comparingInt(o -> -o.getArguments().length)).toList();
for (CommandSyntax syntax : syntaxes) { for (CommandSyntax syntax : syntaxes) {
final CommandCondition commandCondition = syntax.getCommandCondition(); final CommandCondition commandCondition = syntax.getCommandCondition();
if (commandCondition != null && !commandCondition.canUse(sender, null)) { if (commandCondition != null && !commandCondition.canUse(sender, null)) {

View File

@ -9,7 +9,6 @@ import net.minestom.server.utils.binary.Writeable;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* A full query response containing a dynamic set of responses. * A full query response containing a dynamic set of responses.
@ -36,7 +35,7 @@ public class FullQueryResponse implements Writeable {
this.players = MinecraftServer.getConnectionManager().getOnlinePlayers() this.players = MinecraftServer.getConnectionManager().getOnlinePlayers()
.stream() .stream()
.map(player -> PLAIN.serialize(player.getName())) .map(player -> PLAIN.serialize(player.getName()))
.collect(Collectors.toList()); .toList();
} }
/** /**

View File

@ -19,7 +19,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.*;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
// TODO // TODO
@ -150,7 +149,7 @@ public class EntityFinder {
final int maxDistance = distance.getMaximum(); final int maxDistance = distance.getMaximum();
result = result.stream() result = result.stream()
.filter(entity -> MathUtils.isBetween(entity.getDistance(pos), minDistance, maxDistance)) .filter(entity -> MathUtils.isBetween(entity.getDistance(pos), minDistance, maxDistance))
.collect(Collectors.toList()); .toList();
} }
// Diff X/Y/Z // Diff X/Y/Z
@ -173,14 +172,14 @@ public class EntityFinder {
return false; return false;
return true; return true;
}).collect(Collectors.toList()); }).toList();
} }
// Entity type // Entity type
if (!entityTypes.isEmpty()) { if (!entityTypes.isEmpty()) {
result = result.stream() result = result.stream()
.filter(entity -> filterToggleableMap(entity.getEntityType(), entityTypes)) .filter(entity -> filterToggleableMap(entity.getEntityType(), entityTypes))
.collect(Collectors.toList()); .toList();
} }
// GameMode // GameMode
@ -188,7 +187,7 @@ public class EntityFinder {
result = result.stream() result = result.stream()
.filter(Player.class::isInstance) .filter(Player.class::isInstance)
.filter(entity -> filterToggleableMap(((Player) entity).getGameMode(), gameModes)) .filter(entity -> filterToggleableMap(((Player) entity).getGameMode(), gameModes))
.collect(Collectors.toList()); .toList();
} }
// Level // Level
@ -198,7 +197,7 @@ public class EntityFinder {
result = result.stream() result = result.stream()
.filter(Player.class::isInstance) .filter(Player.class::isInstance)
.filter(entity -> MathUtils.isBetween(((Player) entity).getLevel(), minLevel, maxLevel)) .filter(entity -> MathUtils.isBetween(((Player) entity).getLevel(), minLevel, maxLevel))
.collect(Collectors.toList()); .toList();
} }
// Name // Name
@ -206,14 +205,14 @@ public class EntityFinder {
result = result.stream() result = result.stream()
.filter(Player.class::isInstance) .filter(Player.class::isInstance)
.filter(entity -> filterToggleableMap(((Player) entity).getUsername(), names)) .filter(entity -> filterToggleableMap(((Player) entity).getUsername(), names))
.collect(Collectors.toList()); .toList();
} }
// UUID // UUID
if (!uuids.isEmpty()) { if (!uuids.isEmpty()) {
result = result.stream() result = result.stream()
.filter(entity -> filterToggleableMap(entity.getUuid(), uuids)) .filter(entity -> filterToggleableMap(entity.getUuid(), uuids))
.collect(Collectors.toList()); .toList();
} }
@ -232,7 +231,7 @@ public class EntityFinder {
1 : 0; 1 : 0;
}) })
.limit(limit != null ? limit : Integer.MAX_VALUE) .limit(limit != null ? limit : Integer.MAX_VALUE)
.collect(Collectors.toList()); .toList();
if (entitySort == EntitySort.RANDOM) { if (entitySort == EntitySort.RANDOM) {
Collections.shuffle(result); Collections.shuffle(result);

View File

@ -7,7 +7,6 @@ import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
public class DimensionCommand extends Command { public class DimensionCommand extends Command {
@ -18,7 +17,7 @@ public class DimensionCommand extends Command {
addSyntax((sender, context) -> { addSyntax((sender, context) -> {
final Player player = sender.asPlayer(); final Player player = sender.asPlayer();
final Instance instance = player.getInstance(); final Instance instance = player.getInstance();
final var instances = MinecraftServer.getInstanceManager().getInstances().stream().filter(instance1 -> !instance1.equals(instance)).collect(Collectors.toList()); final var instances = MinecraftServer.getInstanceManager().getInstances().stream().filter(instance1 -> !instance1.equals(instance)).toList();
if (instances.isEmpty()) { if (instances.isEmpty()) {
player.sendMessage("No instance available"); player.sendMessage("No instance available");
return; return;

View File

@ -8,7 +8,6 @@ import net.minestom.server.command.builder.CommandContext;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import java.util.Collection; import java.util.Collection;
import java.util.stream.Collectors;
public class PlayersCommand extends Command { public class PlayersCommand extends Command {
@ -27,7 +26,7 @@ public class PlayersCommand extends Command {
sender.sendMessage(Component.text(player.getUsername())); sender.sendMessage(Component.text(player.getUsername()));
} }
} else { } else {
for (final Player player : players.stream().limit(limit).collect(Collectors.toList())) { for (final Player player : players.stream().limit(limit).toList()) {
sender.sendMessage(Component.text(player.getUsername())); sender.sendMessage(Component.text(player.getUsername()));
} }
sender.sendMessage(Component.text("...")); sender.sendMessage(Component.text("..."));