2021-11-12 14:02:14 +00:00
/ *
* Copyright ( c ) 2019 - 2021 GeyserMC . http : //geysermc.org
*
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the " Software " ) , to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software .
*
* THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE .
*
* @author GeyserMC
* @link https : //github.com/GeyserMC/Geyser
* /
2021-11-20 23:29:46 +00:00
package org.geysermc.geyser.session ;
2021-11-12 14:02:14 +00:00
import com.google.common.collect.ImmutableList ;
import lombok.AccessLevel ;
import lombok.Getter ;
2021-11-20 23:29:46 +00:00
import org.geysermc.geyser.text.GeyserLocale ;
2021-11-12 14:02:14 +00:00
import java.util.* ;
import java.util.concurrent.ConcurrentHashMap ;
public final class SessionManager {
/ * *
* A list of all players who don ' t currently have a permanent UUID attached yet .
* /
@Getter ( AccessLevel . PACKAGE )
2021-11-21 18:36:42 +00:00
private final Set < GeyserSessionImpl > pendingSessions = ConcurrentHashMap . newKeySet ( ) ;
2021-11-12 14:02:14 +00:00
/ * *
* A list of all players who are currently in - game .
* /
@Getter
2021-11-21 18:36:42 +00:00
private final Map < UUID , GeyserSessionImpl > sessions = new ConcurrentHashMap < > ( ) ;
2021-11-12 14:02:14 +00:00
/ * *
* Called once the player has successfully authenticated to the Geyser server .
* /
2021-11-21 18:36:42 +00:00
public void addPendingSession ( GeyserSessionImpl session ) {
2021-11-12 14:02:14 +00:00
pendingSessions . add ( session ) ;
}
/ * *
* Called once a player has successfully logged into their Java server .
* /
2021-11-21 18:36:42 +00:00
public void addSession ( UUID uuid , GeyserSessionImpl session ) {
2021-11-12 14:02:14 +00:00
pendingSessions . remove ( session ) ;
sessions . put ( uuid , session ) ;
}
2021-11-21 18:36:42 +00:00
public void removeSession ( GeyserSessionImpl session ) {
2021-11-12 14:02:14 +00:00
if ( sessions . remove ( session . getPlayerEntity ( ) . getUuid ( ) ) = = null ) {
// Session was likely pending
pendingSessions . remove ( session ) ;
}
}
/ * *
* Creates a new , immutable list containing all pending and active sessions .
* /
2021-11-21 18:36:42 +00:00
public List < GeyserSessionImpl > getAllSessions ( ) {
return ImmutableList . < GeyserSessionImpl > builder ( ) // builderWithExpectedSize is probably not a good idea yet as older Spigot builds probably won't have it.
2021-11-12 14:02:14 +00:00
. addAll ( pendingSessions )
. addAll ( sessions . values ( ) )
. build ( ) ;
}
public void disconnectAll ( String message ) {
2021-11-21 18:36:42 +00:00
Collection < GeyserSessionImpl > sessions = getAllSessions ( ) ;
for ( GeyserSessionImpl session : sessions ) {
2021-11-20 23:29:46 +00:00
session . disconnect ( GeyserLocale . getPlayerLocaleString ( message , session . getLocale ( ) ) ) ;
2021-11-12 14:02:14 +00:00
}
}
/ * *
* @return the total amount of sessions , including those pending .
* /
public int size ( ) {
return pendingSessions . size ( ) + sessions . size ( ) ;
}
}