mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-01-24 15:42:53 +08:00
Remove LinkedList usage
https://twitter.com/joshbloch/status/583813919019573248
This commit is contained in:
parent
09f392bb5b
commit
1ceda2a455
@ -89,7 +89,7 @@ public final class CommandMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<Command> getAnnotationsAsList() {
|
public List<Command> getAnnotationsAsList() {
|
||||||
return new LinkedList<>(this.annotations.values());
|
return new ArrayList<>(this.annotations.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Command> getAnnotations() {
|
public Map<String, Command> getAnnotations() {
|
||||||
@ -102,7 +102,7 @@ public final class CommandMap {
|
|||||||
* @return All command handlers as a list.
|
* @return All command handlers as a list.
|
||||||
*/
|
*/
|
||||||
public List<CommandHandler> getHandlersAsList() {
|
public List<CommandHandler> getHandlersAsList() {
|
||||||
return new LinkedList<>(this.commands.values());
|
return new ArrayList<>(this.commands.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, CommandHandler> getHandlers() {
|
public Map<String, CommandHandler> getHandlers() {
|
||||||
@ -234,8 +234,8 @@ public final class CommandMap {
|
|||||||
|
|
||||||
// Parse message.
|
// Parse message.
|
||||||
String[] split = rawMessage.split(" ");
|
String[] split = rawMessage.split(" ");
|
||||||
List<String> args = new LinkedList<>(Arrays.asList(split));
|
String label = split[0].toLowerCase();
|
||||||
String label = args.remove(0).toLowerCase();
|
List<String> args = new ArrayList<>(Arrays.asList(split).subList(1, split.length));
|
||||||
String playerId = (player == null) ? consoleId : player.getAccount().getId();
|
String playerId = (player == null) ? consoleId : player.getAccount().getId();
|
||||||
|
|
||||||
// Check for special cases - currently only target command.
|
// Check for special cases - currently only target command.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package emu.grasscutter.game.player;
|
package emu.grasscutter.game.player;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
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.Int2ObjectMap;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||||
|
|
||||||
import lombok.AccessLevel;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.val;
|
|
||||||
|
|
||||||
@Getter(AccessLevel.PRIVATE)
|
|
||||||
public class PlayerBuffManager extends BasePlayerManager {
|
public class PlayerBuffManager extends BasePlayerManager {
|
||||||
private int nextBuffUid;
|
private int nextBuffUid;
|
||||||
|
|
||||||
@ -29,7 +26,7 @@ public class PlayerBuffManager extends BasePlayerManager {
|
|||||||
public PlayerBuffManager(Player player) {
|
public PlayerBuffManager(Player player) {
|
||||||
super(player);
|
super(player);
|
||||||
this.buffs = new Int2ObjectOpenHashMap<>();
|
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
|
* @return True if a buff with this group id exists
|
||||||
*/
|
*/
|
||||||
public synchronized boolean hasBuff(int groupId) {
|
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() {
|
public synchronized void clearBuffs() {
|
||||||
// Remove from player
|
// Remove from player
|
||||||
getPlayer().sendPacket(
|
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
|
// Clear
|
||||||
getBuffs().clear();
|
this.buffs.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -139,7 +136,7 @@ public class PlayerBuffManager extends BasePlayerManager {
|
|||||||
|
|
||||||
// Create and store buff
|
// Create and store buff
|
||||||
PlayerBuff buff = new PlayerBuff(getNextBuffUid(), buffData, duration);
|
PlayerBuff buff = new PlayerBuff(getNextBuffUid(), buffData, duration);
|
||||||
getBuffs().put(buff.getGroupId(), buff);
|
this.buffs.put(buff.getGroupId(), buff);
|
||||||
|
|
||||||
// Packet
|
// Packet
|
||||||
getPlayer().sendPacket(new PacketServerBuffChangeNotify(getPlayer(), ServerBuffChangeType.SERVER_BUFF_CHANGE_TYPE_ADD_SERVER_BUFF, buff));
|
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
|
* @return True if a buff was remove
|
||||||
*/
|
*/
|
||||||
public synchronized boolean removeBuff(int buffGroupId) {
|
public synchronized boolean removeBuff(int buffGroupId) {
|
||||||
PlayerBuff buff = this.getBuffs().get(buffGroupId);
|
PlayerBuff buff = this.buffs.get(buffGroupId);
|
||||||
|
|
||||||
if (buff != null) {
|
if (buff != null) {
|
||||||
getPlayer().sendPacket(
|
getPlayer().sendPacket(
|
||||||
@ -167,28 +164,24 @@ public class PlayerBuffManager extends BasePlayerManager {
|
|||||||
|
|
||||||
public synchronized void onTick() {
|
public synchronized void onTick() {
|
||||||
// Skip if no buffs
|
// Skip if no buffs
|
||||||
if (getBuffs().size() == 0) return;
|
if (this.buffs.isEmpty()) return;
|
||||||
|
|
||||||
long currentTime = System.currentTimeMillis();
|
long currentTime = System.currentTimeMillis();
|
||||||
|
|
||||||
// Add to pending buffs to remove if buff has expired
|
// Add to pending buffs to remove if buff has expired
|
||||||
for (PlayerBuff buff : getBuffs().values()) {
|
this.buffs.values().removeIf(buff -> {
|
||||||
if (currentTime > buff.getEndTime()) {
|
if (currentTime <= buff.getEndTime())
|
||||||
this.getPendingBuffs().add(buff);
|
return false;
|
||||||
}
|
this.pendingBuffs.add(buff);
|
||||||
}
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
if (this.getPendingBuffs().size() > 0) {
|
if (this.pendingBuffs.size() > 0) {
|
||||||
// Send packet
|
// Send packet
|
||||||
getPlayer().sendPacket(
|
getPlayer().sendPacket(
|
||||||
new PacketServerBuffChangeNotify(getPlayer(), ServerBuffChangeType.SERVER_BUFF_CHANGE_TYPE_DEL_SERVER_BUFF, this.pendingBuffs)
|
new PacketServerBuffChangeNotify(getPlayer(), ServerBuffChangeType.SERVER_BUFF_CHANGE_TYPE_DEL_SERVER_BUFF, this.pendingBuffs)
|
||||||
);
|
);
|
||||||
|
this.pendingBuffs.clear();
|
||||||
// Remove buff from player buff map
|
|
||||||
for (PlayerBuff buff : this.getPendingBuffs()) {
|
|
||||||
getBuffs().remove(buff.getGroupId());
|
|
||||||
}
|
|
||||||
this.getPendingBuffs().clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,15 +274,8 @@ public class Scene {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void showOtherEntities(Player player) {
|
public void showOtherEntities(Player player) {
|
||||||
List<GameEntity> entities = new LinkedList<>();
|
|
||||||
GameEntity currentEntity = player.getTeamManager().getCurrentAvatarEntity();
|
GameEntity currentEntity = player.getTeamManager().getCurrentAvatarEntity();
|
||||||
|
List<GameEntity> entities = this.getEntities().values().stream().filter(entity -> entity != currentEntity).toList();
|
||||||
for (GameEntity entity : this.getEntities().values()) {
|
|
||||||
if (entity == currentEntity) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
entities.add(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
player.sendPacket(new PacketSceneEntityAppearNotify(entities, VisionType.VISION_TYPE_MEET));
|
player.sendPacket(new PacketSceneEntityAppearNotify(entities, VisionType.VISION_TYPE_MEET));
|
||||||
}
|
}
|
||||||
@ -422,8 +415,8 @@ public class Scene {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Todo
|
// Todo
|
||||||
List<GameEntity> toAdd = new LinkedList<>();
|
List<GameEntity> toAdd = new ArrayList<>();
|
||||||
List<GameEntity> toRemove = new LinkedList<>();
|
List<GameEntity> toRemove = new ArrayList<>();
|
||||||
var spawnedEntities = this.getSpawnedEntities();
|
var spawnedEntities = this.getSpawnedEntities();
|
||||||
for (SpawnDataEntry entry : visible) {
|
for (SpawnDataEntry entry : visible) {
|
||||||
// If spawn entry is in our view and hasnt been spawned/killed yet, we should spawn it
|
// If spawn entry is in our view and hasnt been spawned/killed yet, we should spawn it
|
||||||
|
@ -181,7 +181,7 @@ public final class PluginManager {
|
|||||||
// Add the plugin to the list of loaded plugins.
|
// Add the plugin to the list of loaded plugins.
|
||||||
this.plugins.put(identifier.name, plugin);
|
this.plugins.put(identifier.name, plugin);
|
||||||
// Create a collection for the plugin's listeners.
|
// 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.
|
// Call the plugin's onLoad method.
|
||||||
try {
|
try {
|
||||||
@ -242,18 +242,14 @@ public final class PluginManager {
|
|||||||
* @param priority The priority to call for.
|
* @param priority The priority to call for.
|
||||||
*/
|
*/
|
||||||
private void checkAndFilter(Event event, HandlerPriority priority) {
|
private void checkAndFilter(Event event, HandlerPriority priority) {
|
||||||
// Create a collection of listeners.
|
|
||||||
List<EventHandler<? extends Event>> listeners = new LinkedList<>();
|
|
||||||
|
|
||||||
// Add all listeners from every plugin.
|
// Add all listeners from every plugin.
|
||||||
this.listeners.values().forEach(listeners::addAll);
|
this.listeners.values().stream()
|
||||||
|
.flatMap(Collection::stream)
|
||||||
listeners.stream()
|
|
||||||
// Filter the listeners by priority.
|
// Filter the listeners by priority.
|
||||||
.filter(handler -> handler.handles().isInstance(event))
|
.filter(handler -> handler.handles().isInstance(event))
|
||||||
.filter(handler -> handler.getPriority() == priority)
|
.filter(handler -> handler.getPriority() == priority)
|
||||||
// Invoke the event.
|
// Invoke the event.
|
||||||
.toList().forEach(handler -> this.invokeHandler(event, handler));
|
.forEach(handler -> this.invokeHandler(event, handler));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -11,8 +11,9 @@ import emu.grasscutter.server.game.GameServer;
|
|||||||
import emu.grasscutter.server.http.HttpServer;
|
import emu.grasscutter.server.http.HttpServer;
|
||||||
import emu.grasscutter.server.http.Router;
|
import emu.grasscutter.server.http.Router;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hooks into the {@link GameServer} class, adding convenient ways to do certain things.
|
* 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.
|
* Gets all online players.
|
||||||
* @return Players connected to the server.
|
* @return Players connected to the server.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated(forRemoval = true)
|
||||||
public List<Player> getOnlinePlayers() {
|
public List<Player> 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<Player> getOnlinePlayersStream() {
|
||||||
|
return this.gameServer.getPlayers().values().stream();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package emu.grasscutter.server.packet.send;
|
package emu.grasscutter.server.packet.send;
|
||||||
|
|
||||||
import java.util.Collection;
|
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.Player;
|
||||||
import emu.grasscutter.game.player.PlayerBuffManager.PlayerBuff;
|
import emu.grasscutter.game.player.PlayerBuffManager.PlayerBuff;
|
||||||
import emu.grasscutter.net.packet.BasePacket;
|
import emu.grasscutter.net.packet.BasePacket;
|
||||||
@ -13,31 +13,25 @@ import emu.grasscutter.net.proto.ServerBuffChangeNotifyOuterClass.ServerBuffChan
|
|||||||
public class PacketServerBuffChangeNotify extends BasePacket {
|
public class PacketServerBuffChangeNotify extends BasePacket {
|
||||||
|
|
||||||
public PacketServerBuffChangeNotify(Player player, ServerBuffChangeType changeType, PlayerBuff buff) {
|
public PacketServerBuffChangeNotify(Player player, ServerBuffChangeType changeType, PlayerBuff buff) {
|
||||||
super(PacketOpcodes.ServerBuffChangeNotify);
|
this(player, changeType, Stream.of(buff));
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public PacketServerBuffChangeNotify(Player player, ServerBuffChangeType changeType, Collection<PlayerBuff> buffs) {
|
public PacketServerBuffChangeNotify(Player player, ServerBuffChangeType changeType, Collection<PlayerBuff> buffs) {
|
||||||
|
this(player, changeType, buffs.stream());
|
||||||
|
}
|
||||||
|
|
||||||
|
public PacketServerBuffChangeNotify(Player player, ServerBuffChangeType changeType, Stream<PlayerBuff> buffs) {
|
||||||
super(PacketOpcodes.ServerBuffChangeNotify);
|
super(PacketOpcodes.ServerBuffChangeNotify);
|
||||||
|
|
||||||
var proto = ServerBuffChangeNotify.newBuilder();
|
var proto = ServerBuffChangeNotify.newBuilder();
|
||||||
|
|
||||||
for (EntityAvatar entity : player.getTeamManager().getActiveTeam()) {
|
player.getTeamManager().getActiveTeam().stream()
|
||||||
proto.addAvatarGuidList(entity.getAvatar().getGuid());
|
.mapToLong(entity -> entity.getAvatar().getGuid())
|
||||||
}
|
.forEach(proto::addAvatarGuidList);
|
||||||
|
|
||||||
proto.setServerBuffChangeType(changeType);
|
proto.setServerBuffChangeType(changeType);
|
||||||
proto.addAllServerBuffList(buffs.stream().map(PlayerBuff::toProto).toList());
|
buffs.map(PlayerBuff::toProto)
|
||||||
|
.forEach(proto::addServerBuffList);
|
||||||
|
|
||||||
this.setData(proto);
|
this.setData(proto);
|
||||||
}
|
}
|
||||||
|
@ -133,7 +133,7 @@ public final class TaskMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<TaskHandler> getHandlersAsList() {
|
public List<TaskHandler> getHandlersAsList() {
|
||||||
return new LinkedList<>(this.tasks.values());
|
return new ArrayList<>(this.tasks.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashMap<String, TaskHandler> getHandlers() {
|
public HashMap<String, TaskHandler> getHandlers() {
|
||||||
|
Loading…
Reference in New Issue
Block a user