fix all migrate 80%
This commit is contained in:
parent
d1dcf6ab57
commit
574f9fc4b5
21 changed files with 1054 additions and 1154 deletions
|
|
@ -42,14 +42,9 @@ public class Session {
|
|||
private Visibility visibility = Visibility.PUBLIC;
|
||||
|
||||
/**
|
||||
* List of muted users.
|
||||
* Manages users (players and spectators) in this session.
|
||||
*/
|
||||
private final List<ParkourUser> muted = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* List of users.
|
||||
*/
|
||||
private final Map<UUID, ParkourUser> users = new HashMap<>();
|
||||
private final SessionUserManager userManager = new SessionUserManager(this);
|
||||
|
||||
/**
|
||||
* Function that takes the current session and returns whether new players should be accepted.
|
||||
|
|
@ -121,13 +116,7 @@ public class Session {
|
|||
* @param toAdd The players to add.
|
||||
*/
|
||||
public void addPlayers(ParkourPlayer... toAdd) {
|
||||
for (ParkourPlayer player : toAdd) {
|
||||
for (ParkourPlayer to : getPlayers()) {
|
||||
to.send(Locales.getString(player.locale, "lobby.other_join").formatted(player.getName()));
|
||||
}
|
||||
|
||||
users.put(player.getUUID(), player);
|
||||
}
|
||||
userManager.addPlayers(toAdd);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -136,31 +125,14 @@ public class Session {
|
|||
* @param toRemove The players to remove.
|
||||
*/
|
||||
public void removePlayers(ParkourPlayer... toRemove) {
|
||||
for (ParkourPlayer player : toRemove) {
|
||||
users.remove(player.getUUID());
|
||||
}
|
||||
|
||||
List<ParkourPlayer> players = getPlayers();
|
||||
for (ParkourPlayer player : toRemove) {
|
||||
for (ParkourPlayer to : players) {
|
||||
to.send(Locales.getString(player.locale, "lobby.other_leave").formatted(player.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
if (toRemove.length > 0 && players.isEmpty()) {
|
||||
generator.reset(false);
|
||||
Divider.remove(this);
|
||||
}
|
||||
userManager.removePlayers(toRemove);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The players.
|
||||
*/
|
||||
public List<ParkourPlayer> getPlayers() {
|
||||
return users.values().stream()
|
||||
.filter(user -> user instanceof ParkourPlayer)
|
||||
.map(user -> (ParkourPlayer) user)
|
||||
.toList();
|
||||
return userManager.getPlayers();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -169,13 +141,7 @@ public class Session {
|
|||
* @param spectators The spectators to add.
|
||||
*/
|
||||
public void addSpectators(ParkourSpectator... spectators) {
|
||||
for (ParkourSpectator spectator : spectators) {
|
||||
for (ParkourPlayer player : getPlayers()) {
|
||||
player.sendTranslated("play.spectator.other_join", spectator.getName());
|
||||
}
|
||||
|
||||
users.put(spectator.getUUID(), spectator);
|
||||
}
|
||||
userManager.addSpectators(spectators);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -184,30 +150,21 @@ public class Session {
|
|||
* @param spectators The spectators to remove.
|
||||
*/
|
||||
public void removeSpectators(ParkourSpectator... spectators) {
|
||||
for (ParkourSpectator spectator : spectators) {
|
||||
for (ParkourPlayer player : getPlayers()) {
|
||||
player.sendTranslated("play.spectator.other_leave", spectator.getName());
|
||||
}
|
||||
|
||||
users.remove(spectator.getUUID());
|
||||
}
|
||||
userManager.removeSpectators(spectators);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The spectators.
|
||||
*/
|
||||
public List<ParkourSpectator> getSpectators() {
|
||||
return users.values().stream()
|
||||
.filter(user -> user instanceof ParkourSpectator)
|
||||
.map(user -> (ParkourSpectator) user)
|
||||
.toList();
|
||||
return userManager.getSpectators();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The users.
|
||||
*/
|
||||
public List<ParkourUser> getUsers() {
|
||||
return new ArrayList<>(users.values());
|
||||
return userManager.getUsers();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -224,9 +181,7 @@ public class Session {
|
|||
* @param user The user to (un)mute.
|
||||
*/
|
||||
public void toggleMute(@NotNull ParkourUser user) {
|
||||
if (!muted.remove(user)) {
|
||||
muted.add(user);
|
||||
}
|
||||
userManager.toggleMute(user);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -234,7 +189,15 @@ public class Session {
|
|||
* @return True when the user is muted, false if not.
|
||||
*/
|
||||
public boolean isMuted(@NotNull ParkourUser user) {
|
||||
return muted.contains(user);
|
||||
return userManager.isMuted(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when all players have left the session.
|
||||
*/
|
||||
void onAllPlayersLeft() {
|
||||
generator.reset(false);
|
||||
Divider.remove(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
112
src/main/java/dev/loki/loparkour/session/SessionUserManager.java
Normal file
112
src/main/java/dev/loki/loparkour/session/SessionUserManager.java
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
package dev.loki.loparkour.session;
|
||||
|
||||
import dev.loki.loparkour.config.Locales;
|
||||
import dev.loki.loparkour.player.ParkourPlayer;
|
||||
import dev.loki.loparkour.player.ParkourSpectator;
|
||||
import dev.loki.loparkour.player.ParkourUser;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Manages users (players and spectators) within a session.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*/
|
||||
class SessionUserManager {
|
||||
|
||||
private final Map<UUID, ParkourUser> users = new HashMap<>();
|
||||
private final List<ParkourUser> muted = new ArrayList<>();
|
||||
private final Session session;
|
||||
|
||||
public SessionUserManager(Session session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
public void addPlayers(ParkourPlayer... toAdd) {
|
||||
for (ParkourPlayer player : toAdd) {
|
||||
notifyJoin(player);
|
||||
users.put(player.getUUID(), player);
|
||||
}
|
||||
}
|
||||
|
||||
public void removePlayers(ParkourPlayer... toRemove) {
|
||||
for (ParkourPlayer player : toRemove) {
|
||||
users.remove(player.getUUID());
|
||||
}
|
||||
|
||||
List<ParkourPlayer> remainingPlayers = getPlayers();
|
||||
notifyLeave(toRemove, remainingPlayers);
|
||||
|
||||
if (toRemove.length > 0 && remainingPlayers.isEmpty()) {
|
||||
session.onAllPlayersLeft();
|
||||
}
|
||||
}
|
||||
|
||||
public void addSpectators(ParkourSpectator... spectators) {
|
||||
for (ParkourSpectator spectator : spectators) {
|
||||
notifySpectatorJoin(spectator);
|
||||
users.put(spectator.getUUID(), spectator);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeSpectators(ParkourSpectator... spectators) {
|
||||
for (ParkourSpectator spectator : spectators) {
|
||||
notifySpectatorLeave(spectator);
|
||||
users.remove(spectator.getUUID());
|
||||
}
|
||||
}
|
||||
|
||||
public List<ParkourPlayer> getPlayers() {
|
||||
return users.values().stream()
|
||||
.filter(user -> user instanceof ParkourPlayer)
|
||||
.map(user -> (ParkourPlayer) user)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<ParkourSpectator> getSpectators() {
|
||||
return users.values().stream()
|
||||
.filter(user -> user instanceof ParkourSpectator)
|
||||
.map(user -> (ParkourSpectator) user)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<ParkourUser> getUsers() {
|
||||
return new ArrayList<>(users.values());
|
||||
}
|
||||
|
||||
public void toggleMute(ParkourUser user) {
|
||||
if (!muted.remove(user)) {
|
||||
muted.add(user);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isMuted(ParkourUser user) {
|
||||
return muted.contains(user);
|
||||
}
|
||||
|
||||
private void notifyJoin(ParkourPlayer player) {
|
||||
for (ParkourPlayer to : getPlayers()) {
|
||||
to.send(Locales.getString(player.locale, "lobby.other_join").formatted(player.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyLeave(ParkourPlayer[] toRemove, List<ParkourPlayer> remainingPlayers) {
|
||||
for (ParkourPlayer player : toRemove) {
|
||||
for (ParkourPlayer to : remainingPlayers) {
|
||||
to.send(Locales.getString(player.locale, "lobby.other_leave").formatted(player.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void notifySpectatorJoin(ParkourSpectator spectator) {
|
||||
for (ParkourPlayer player : getPlayers()) {
|
||||
player.sendTranslated("play.spectator.other_join", spectator.getName());
|
||||
}
|
||||
}
|
||||
|
||||
private void notifySpectatorLeave(ParkourSpectator spectator) {
|
||||
for (ParkourPlayer player : getPlayers()) {
|
||||
player.sendTranslated("play.spectator.other_leave", spectator.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue