All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.alangomes.springspigot.context.DefaultSessionService Maven / Gradle / Ivy

There is a newer version: 0.20.7
Show newest version
package dev.alangomes.springspigot.context;

import dev.alangomes.springspigot.util.ServerUtil;
import lombok.val;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_SINGLETON;

@Component
@Scope(SCOPE_SINGLETON)
public class DefaultSessionService implements SessionService, Listener {

    @Autowired
    private Context context;

    @Autowired
    private ServerUtil serverUtil;

    private final Map> sessions = new ConcurrentHashMap<>();

    @Override
    public Map current() {
        return of(context.getSender());
    }

    @Override
    public Map of(CommandSender sender) {
        val senderId = serverUtil.getSenderId(sender);
        if (senderId == null) {
            return null;
        }
        return sessions.computeIfAbsent(senderId, k -> new ConcurrentHashMap<>());
    }

    @EventHandler
    private void onQuit(PlayerQuitEvent event) {
        val senderId = serverUtil.getSenderId(event.getPlayer());
        sessions.remove(senderId);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy