Build AsminVoicechat / prepare-release (push) Successful in 14s
Build AsminVoicechat / build-and-test (17) (push) Canceled after 0s
Build AsminVoicechat / build-and-test (18) (push) Canceled after 0s
Build AsminVoicechat / build-and-test (19) (push) Canceled after 0s
Build AsminVoicechat / build-and-test (20) (push) Canceled after 0s
Build AsminVoicechat / build-and-test (21) (push) Canceled after 0s
Build AsminVoicechat / build-and-test (22) (push) Canceled after 0s
Build AsminVoicechat / build-and-test (23) (push) Canceled after 0s
Build AsminVoicechat / build-and-test (24) (push) Canceled after 0s
Build AsminVoicechat / build-and-test (25) (push) Canceled after 0s
Build AsminVoicechat / build-and-test (26) (push) Canceled after 0s
Build AsminVoicechat / build-and-test (27-ea) (push) Canceled after 0s
Build AsminVoicechat / build-and-test (16) (push) Canceled after 38s
186 lines
7.0 KiB
Java
186 lines
7.0 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* javax.annotation.Nullable
|
|
* org.bukkit.entity.Player
|
|
* org.bukkit.event.player.PlayerQuitEvent
|
|
*/
|
|
package de.maxhenkel.voicechat.voice.server;
|
|
|
|
import de.maxhenkel.voicechat.Voicechat;
|
|
import de.maxhenkel.voicechat.net.AddGroupPacket;
|
|
import de.maxhenkel.voicechat.net.CreateGroupPacket;
|
|
import de.maxhenkel.voicechat.net.JoinGroupPacket;
|
|
import de.maxhenkel.voicechat.net.JoinedGroupPacket;
|
|
import de.maxhenkel.voicechat.net.LeaveGroupPacket;
|
|
import de.maxhenkel.voicechat.net.NetManager;
|
|
import de.maxhenkel.voicechat.net.RemoveGroupPacket;
|
|
import de.maxhenkel.voicechat.permission.PermissionManager;
|
|
import de.maxhenkel.voicechat.plugins.PluginManager;
|
|
import de.maxhenkel.voicechat.voice.common.PlayerState;
|
|
import de.maxhenkel.voicechat.voice.server.Group;
|
|
import de.maxhenkel.voicechat.voice.server.PlayerStateManager;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.stream.Collectors;
|
|
import javax.annotation.Nullable;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.player.PlayerQuitEvent;
|
|
|
|
public class ServerGroupManager {
|
|
private final Map<UUID, Group> groups = new ConcurrentHashMap<UUID, Group>();
|
|
|
|
public void onJoinGroupPacket(Player player, JoinGroupPacket packet) {
|
|
if (!Voicechat.SERVER_CONFIG.groupsEnabled.get().booleanValue()) {
|
|
return;
|
|
}
|
|
if (!player.hasPermission(PermissionManager.GROUPS_PERMISSION)) {
|
|
NetManager.sendStatusMessage(player, "message.voicechat.no_group_permission", new String[0]);
|
|
return;
|
|
}
|
|
this.joinGroup(this.groups.get(packet.getGroup()), player, packet.getPassword());
|
|
}
|
|
|
|
public void onCreateGroupPacket(Player player, CreateGroupPacket packet) {
|
|
if (!Voicechat.SERVER_CONFIG.groupsEnabled.get().booleanValue()) {
|
|
return;
|
|
}
|
|
if (!player.hasPermission(PermissionManager.GROUPS_PERMISSION)) {
|
|
NetManager.sendStatusMessage(player, "message.voicechat.no_group_permission", new String[0]);
|
|
return;
|
|
}
|
|
if (!Voicechat.GROUP_REGEX.matcher(packet.getName()).matches()) {
|
|
Voicechat.LOGGER.warn("Player {} tried to create a group with an invalid name", player.getDisplayName());
|
|
return;
|
|
}
|
|
if (packet.getPassword() != null && !Voicechat.GROUP_REGEX.matcher(packet.getPassword()).matches()) {
|
|
Voicechat.LOGGER.warn("Player {} tried to create a group with an invalid password", player.getDisplayName());
|
|
return;
|
|
}
|
|
Group group = new Group(UUID.randomUUID(), packet.getName(), packet.getPassword(), false, false, packet.getType());
|
|
this.addGroup(group, player);
|
|
}
|
|
|
|
public void onLeaveGroupPacket(Player player, LeaveGroupPacket packet) {
|
|
this.leaveGroup(player);
|
|
}
|
|
|
|
public void onPlayerCompatibilityCheckSucceeded(Player player) {
|
|
Voicechat.LOGGER.debug("Synchronizing {} groups with {}", this.groups.size(), player.getDisplayName());
|
|
for (Group group : this.groups.values()) {
|
|
NetManager.sendToClient(player, new AddGroupPacket(group.toClientGroup()));
|
|
}
|
|
}
|
|
|
|
public void onPlayerQuit(PlayerQuitEvent event) {
|
|
this.cleanupGroups();
|
|
}
|
|
|
|
private PlayerStateManager getStates() {
|
|
return Voicechat.SERVER.getServer().getPlayerStateManager();
|
|
}
|
|
|
|
public void addGroup(Group group, @Nullable Player player) {
|
|
if (PluginManager.instance().onCreateGroup(player, group)) {
|
|
return;
|
|
}
|
|
this.groups.put(group.getId(), group);
|
|
this.broadcastAddGroup(group);
|
|
if (player == null) {
|
|
return;
|
|
}
|
|
PlayerStateManager manager = this.getStates();
|
|
manager.setGroup(player, group.getId());
|
|
NetManager.sendToClient(player, new JoinedGroupPacket(group.getId(), false));
|
|
}
|
|
|
|
public void joinGroup(@Nullable Group group, Player player, String password) {
|
|
if (group != null && group.getPassword() != null && !group.getPassword().equals(password)) {
|
|
NetManager.sendToClient(player, new JoinedGroupPacket(null, true));
|
|
return;
|
|
}
|
|
if (PluginManager.instance().onJoinGroup(player, group)) {
|
|
return;
|
|
}
|
|
if (group == null) {
|
|
NetManager.sendToClient(player, new JoinedGroupPacket(null, false));
|
|
return;
|
|
}
|
|
PlayerStateManager manager = this.getStates();
|
|
manager.setGroup(player, group.getId());
|
|
NetManager.sendToClient(player, new JoinedGroupPacket(group.getId(), false));
|
|
}
|
|
|
|
public void leaveGroup(Player player) {
|
|
if (PluginManager.instance().onLeaveGroup(player)) {
|
|
return;
|
|
}
|
|
PlayerStateManager manager = this.getStates();
|
|
manager.setGroup(player, null);
|
|
NetManager.sendToClient(player, new JoinedGroupPacket(null, false));
|
|
this.cleanupGroups();
|
|
}
|
|
|
|
public void cleanupGroups() {
|
|
PlayerStateManager manager = this.getStates();
|
|
List usedGroups = manager.getStates().stream().filter(PlayerState::hasGroup).map(PlayerState::getGroup).distinct().collect(Collectors.toList());
|
|
List groupsToRemove = this.groups.entrySet().stream().filter(entry -> !((Group)entry.getValue()).isPersistent()).map(Map.Entry::getKey).filter(uuid -> !usedGroups.contains(uuid)).collect(Collectors.toList());
|
|
for (UUID uuid2 : groupsToRemove) {
|
|
this.removeGroup(uuid2);
|
|
}
|
|
}
|
|
|
|
public boolean removeGroup(UUID groupId) {
|
|
Group group = this.groups.get(groupId);
|
|
if (group == null) {
|
|
return false;
|
|
}
|
|
PlayerStateManager manager = this.getStates();
|
|
if (manager.getStates().stream().anyMatch(state -> state.hasGroup() && state.getGroup().equals(groupId))) {
|
|
return false;
|
|
}
|
|
if (PluginManager.instance().onRemoveGroup(group)) {
|
|
return false;
|
|
}
|
|
this.groups.remove(groupId);
|
|
this.broadcastRemoveGroup(groupId);
|
|
return true;
|
|
}
|
|
|
|
@Nullable
|
|
public Group getGroup(UUID groupID) {
|
|
return this.groups.get(groupID);
|
|
}
|
|
|
|
private void broadcastAddGroup(Group group) {
|
|
AddGroupPacket packet = new AddGroupPacket(group.toClientGroup());
|
|
Voicechat.INSTANCE.getServer().getOnlinePlayers().forEach(p -> NetManager.sendToClient(p, packet));
|
|
}
|
|
|
|
private void broadcastRemoveGroup(UUID group) {
|
|
RemoveGroupPacket packet = new RemoveGroupPacket(group);
|
|
Voicechat.INSTANCE.getServer().getOnlinePlayers().forEach(p -> NetManager.sendToClient(p, packet));
|
|
}
|
|
|
|
@Nullable
|
|
public Group getPlayerGroup(Player player) {
|
|
PlayerState state = this.getStates().getState(player.getUniqueId());
|
|
if (state == null) {
|
|
return null;
|
|
}
|
|
UUID groupId = state.getGroup();
|
|
if (groupId == null) {
|
|
return null;
|
|
}
|
|
return this.getGroup(groupId);
|
|
}
|
|
|
|
public Map<UUID, Group> getGroups() {
|
|
return this.groups;
|
|
}
|
|
}
|
|
|