dev.alangomes.springspigot.context.DefaultSessionService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spigot-spring-boot-starter Show documentation
Show all versions of spigot-spring-boot-starter Show documentation
Spring support for spigot plugins
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