From 1ceda2a455225d2c9ab632b4ac3b50b8b7712ea7 Mon Sep 17 00:00:00 2001 From: AnimeGitB Date: Sat, 5 Nov 2022 17:04:15 +1030 Subject: [PATCH] Remove LinkedList usage https://twitter.com/joshbloch/status/583813919019573248 --- .../emu/grasscutter/command/CommandMap.java | 8 ++-- .../game/player/PlayerBuffManager.java | 39 ++++++++----------- .../emu/grasscutter/game/world/Scene.java | 13 ++----- .../emu/grasscutter/plugin/PluginManager.java | 12 ++---- .../grasscutter/plugin/api/ServerHook.java | 14 ++++++- .../send/PacketServerBuffChangeNotify.java | 28 ++++++------- .../java/emu/grasscutter/task/TaskMap.java | 2 +- 7 files changed, 51 insertions(+), 65 deletions(-) diff --git a/src/main/java/emu/grasscutter/command/CommandMap.java b/src/main/java/emu/grasscutter/command/CommandMap.java index 284667e73..5590e4f33 100644 --- a/src/main/java/emu/grasscutter/command/CommandMap.java +++ b/src/main/java/emu/grasscutter/command/CommandMap.java @@ -89,7 +89,7 @@ public final class CommandMap { } public List getAnnotationsAsList() { - return new LinkedList<>(this.annotations.values()); + return new ArrayList<>(this.annotations.values()); } public Map getAnnotations() { @@ -102,7 +102,7 @@ public final class CommandMap { * @return All command handlers as a list. */ public List getHandlersAsList() { - return new LinkedList<>(this.commands.values()); + return new ArrayList<>(this.commands.values()); } public Map getHandlers() { @@ -234,8 +234,8 @@ public final class CommandMap { // Parse message. String[] split = rawMessage.split(" "); - List args = new LinkedList<>(Arrays.asList(split)); - String label = args.remove(0).toLowerCase(); + String label = split[0].toLowerCase(); + List args = new ArrayList<>(Arrays.asList(split).subList(1, split.length)); String playerId = (player == null) ? consoleId : player.getAccount().getId(); // Check for special cases - currently only target command. diff --git a/src/main/java/emu/grasscutter/game/player/PlayerBuffManager.java b/src/main/java/emu/grasscutter/game/player/PlayerBuffManager.java index 00c4239f9..73ae4ab78 100644 --- a/src/main/java/emu/grasscutter/game/player/PlayerBuffManager.java +++ b/src/main/java/emu/grasscutter/game/player/PlayerBuffManager.java @@ -1,6 +1,6 @@ package emu.grasscutter.game.player; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -15,11 +15,8 @@ import emu.grasscutter.server.packet.send.PacketServerBuffChangeNotify; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; -import lombok.AccessLevel; import lombok.Getter; -import lombok.val; -@Getter(AccessLevel.PRIVATE) public class PlayerBuffManager extends BasePlayerManager { private int nextBuffUid; @@ -29,7 +26,7 @@ public class PlayerBuffManager extends BasePlayerManager { public PlayerBuffManager(Player player) { super(player); this.buffs = new Int2ObjectOpenHashMap<>(); - this.pendingBuffs = new LinkedList<>(); + this.pendingBuffs = new ArrayList<>(); } /** @@ -46,7 +43,7 @@ public class PlayerBuffManager extends BasePlayerManager { * @return True if a buff with this group id exists */ public synchronized boolean hasBuff(int groupId) { - return this.getBuffs().containsKey(groupId); + return this.buffs.containsKey(groupId); } /** @@ -55,11 +52,11 @@ public class PlayerBuffManager extends BasePlayerManager { public synchronized void clearBuffs() { // Remove from player getPlayer().sendPacket( - new PacketServerBuffChangeNotify(getPlayer(), ServerBuffChangeType.SERVER_BUFF_CHANGE_TYPE_DEL_SERVER_BUFF, getBuffs().values()) + new PacketServerBuffChangeNotify(getPlayer(), ServerBuffChangeType.SERVER_BUFF_CHANGE_TYPE_DEL_SERVER_BUFF, this.buffs.values()) ); // Clear - getBuffs().clear(); + this.buffs.clear(); } /** @@ -139,7 +136,7 @@ public class PlayerBuffManager extends BasePlayerManager { // Create and store buff PlayerBuff buff = new PlayerBuff(getNextBuffUid(), buffData, duration); - getBuffs().put(buff.getGroupId(), buff); + this.buffs.put(buff.getGroupId(), buff); // Packet getPlayer().sendPacket(new PacketServerBuffChangeNotify(getPlayer(), ServerBuffChangeType.SERVER_BUFF_CHANGE_TYPE_ADD_SERVER_BUFF, buff)); @@ -153,7 +150,7 @@ public class PlayerBuffManager extends BasePlayerManager { * @return True if a buff was remove */ public synchronized boolean removeBuff(int buffGroupId) { - PlayerBuff buff = this.getBuffs().get(buffGroupId); + PlayerBuff buff = this.buffs.get(buffGroupId); if (buff != null) { getPlayer().sendPacket( @@ -167,28 +164,24 @@ public class PlayerBuffManager extends BasePlayerManager { public synchronized void onTick() { // Skip if no buffs - if (getBuffs().size() == 0) return; + if (this.buffs.isEmpty()) return; long currentTime = System.currentTimeMillis(); // Add to pending buffs to remove if buff has expired - for (PlayerBuff buff : getBuffs().values()) { - if (currentTime > buff.getEndTime()) { - this.getPendingBuffs().add(buff); - } - } + this.buffs.values().removeIf(buff -> { + if (currentTime <= buff.getEndTime()) + return false; + this.pendingBuffs.add(buff); + return true; + }); - if (this.getPendingBuffs().size() > 0) { + if (this.pendingBuffs.size() > 0) { // Send packet getPlayer().sendPacket( new PacketServerBuffChangeNotify(getPlayer(), ServerBuffChangeType.SERVER_BUFF_CHANGE_TYPE_DEL_SERVER_BUFF, this.pendingBuffs) ); - - // Remove buff from player buff map - for (PlayerBuff buff : this.getPendingBuffs()) { - getBuffs().remove(buff.getGroupId()); - } - this.getPendingBuffs().clear(); + this.pendingBuffs.clear(); } } diff --git a/src/main/java/emu/grasscutter/game/world/Scene.java b/src/main/java/emu/grasscutter/game/world/Scene.java index 03da948e6..7ca42ce4c 100644 --- a/src/main/java/emu/grasscutter/game/world/Scene.java +++ b/src/main/java/emu/grasscutter/game/world/Scene.java @@ -274,15 +274,8 @@ public class Scene { } public void showOtherEntities(Player player) { - List entities = new LinkedList<>(); GameEntity currentEntity = player.getTeamManager().getCurrentAvatarEntity(); - - for (GameEntity entity : this.getEntities().values()) { - if (entity == currentEntity) { - continue; - } - entities.add(entity); - } + List entities = this.getEntities().values().stream().filter(entity -> entity != currentEntity).toList(); player.sendPacket(new PacketSceneEntityAppearNotify(entities, VisionType.VISION_TYPE_MEET)); } @@ -422,8 +415,8 @@ public class Scene { } // Todo - List toAdd = new LinkedList<>(); - List toRemove = new LinkedList<>(); + List toAdd = new ArrayList<>(); + List toRemove = new ArrayList<>(); var spawnedEntities = this.getSpawnedEntities(); for (SpawnDataEntry entry : visible) { // If spawn entry is in our view and hasnt been spawned/killed yet, we should spawn it diff --git a/src/main/java/emu/grasscutter/plugin/PluginManager.java b/src/main/java/emu/grasscutter/plugin/PluginManager.java index 478f8dc3f..3f78dfd67 100644 --- a/src/main/java/emu/grasscutter/plugin/PluginManager.java +++ b/src/main/java/emu/grasscutter/plugin/PluginManager.java @@ -181,7 +181,7 @@ public final class PluginManager { // Add the plugin to the list of loaded plugins. this.plugins.put(identifier.name, plugin); // Create a collection for the plugin's listeners. - this.listeners.put(plugin, new LinkedList<>()); + this.listeners.put(plugin, new ArrayList<>()); // Call the plugin's onLoad method. try { @@ -242,18 +242,14 @@ public final class PluginManager { * @param priority The priority to call for. */ private void checkAndFilter(Event event, HandlerPriority priority) { - // Create a collection of listeners. - List> listeners = new LinkedList<>(); - // Add all listeners from every plugin. - this.listeners.values().forEach(listeners::addAll); - - listeners.stream() + this.listeners.values().stream() + .flatMap(Collection::stream) // Filter the listeners by priority. .filter(handler -> handler.handles().isInstance(event)) .filter(handler -> handler.getPriority() == priority) // Invoke the event. - .toList().forEach(handler -> this.invokeHandler(event, handler)); + .forEach(handler -> this.invokeHandler(event, handler)); } /** diff --git a/src/main/java/emu/grasscutter/plugin/api/ServerHook.java b/src/main/java/emu/grasscutter/plugin/api/ServerHook.java index d43d646fb..deb98806a 100644 --- a/src/main/java/emu/grasscutter/plugin/api/ServerHook.java +++ b/src/main/java/emu/grasscutter/plugin/api/ServerHook.java @@ -11,8 +11,9 @@ import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.http.HttpServer; import emu.grasscutter.server.http.Router; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Stream; /** * Hooks into the {@link GameServer} class, adding convenient ways to do certain things. @@ -60,8 +61,17 @@ public final class ServerHook { * Gets all online players. * @return Players connected to the server. */ + @Deprecated(forRemoval = true) public List getOnlinePlayers() { - return new LinkedList<>(this.gameServer.getPlayers().values()); + return new ArrayList<>(this.gameServer.getPlayers().values()); + } + + /** + * Gets all online players. + * @return Players connected to the server. + */ + public Stream getOnlinePlayersStream() { + return this.gameServer.getPlayers().values().stream(); } /** diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketServerBuffChangeNotify.java b/src/main/java/emu/grasscutter/server/packet/send/PacketServerBuffChangeNotify.java index aebbafcd3..1bb04f907 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketServerBuffChangeNotify.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketServerBuffChangeNotify.java @@ -1,8 +1,8 @@ package emu.grasscutter.server.packet.send; import java.util.Collection; +import java.util.stream.Stream; -import emu.grasscutter.game.entity.EntityAvatar; import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.PlayerBuffManager.PlayerBuff; import emu.grasscutter.net.packet.BasePacket; @@ -13,31 +13,25 @@ import emu.grasscutter.net.proto.ServerBuffChangeNotifyOuterClass.ServerBuffChan public class PacketServerBuffChangeNotify extends BasePacket { public PacketServerBuffChangeNotify(Player player, ServerBuffChangeType changeType, PlayerBuff buff) { - super(PacketOpcodes.ServerBuffChangeNotify); - - var proto = ServerBuffChangeNotify.newBuilder(); - - for (EntityAvatar entity : player.getTeamManager().getActiveTeam()) { - proto.addAvatarGuidList(entity.getAvatar().getGuid()); - } - - proto.setServerBuffChangeType(changeType); - proto.addServerBuffList(buff.toProto()); - - this.setData(proto); + this(player, changeType, Stream.of(buff)); } public PacketServerBuffChangeNotify(Player player, ServerBuffChangeType changeType, Collection buffs) { + this(player, changeType, buffs.stream()); + } + + public PacketServerBuffChangeNotify(Player player, ServerBuffChangeType changeType, Stream buffs) { super(PacketOpcodes.ServerBuffChangeNotify); var proto = ServerBuffChangeNotify.newBuilder(); - for (EntityAvatar entity : player.getTeamManager().getActiveTeam()) { - proto.addAvatarGuidList(entity.getAvatar().getGuid()); - } + player.getTeamManager().getActiveTeam().stream() + .mapToLong(entity -> entity.getAvatar().getGuid()) + .forEach(proto::addAvatarGuidList); proto.setServerBuffChangeType(changeType); - proto.addAllServerBuffList(buffs.stream().map(PlayerBuff::toProto).toList()); + buffs.map(PlayerBuff::toProto) + .forEach(proto::addServerBuffList); this.setData(proto); } diff --git a/src/main/java/emu/grasscutter/task/TaskMap.java b/src/main/java/emu/grasscutter/task/TaskMap.java index a4b2ff02f..27e83c53f 100644 --- a/src/main/java/emu/grasscutter/task/TaskMap.java +++ b/src/main/java/emu/grasscutter/task/TaskMap.java @@ -133,7 +133,7 @@ public final class TaskMap { } public List getHandlersAsList() { - return new LinkedList<>(this.tasks.values()); + return new ArrayList<>(this.tasks.values()); } public HashMap getHandlers() {