Skip to content

Commit

Permalink
Set server max players to number of total members (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
zefir-git authored May 27, 2024
2 parents 9a47442 + 323d912 commit fee8caa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/pro/cloudnode/smp/smpcore/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,21 @@ public boolean delete() {
return members;
}

public static int count() {
try (
final @NotNull Connection conn = SMPCore.getInstance().db()
.getConnection(); final @NotNull PreparedStatement stmt = conn.prepareStatement("SELECT COUNT(*) as `n` FROM `members`")
) {
final @NotNull ResultSet rs = stmt.executeQuery();
rs.next();
return rs.getInt("n");
}
catch (final @NotNull SQLException e) {
SMPCore.getInstance().getLogger().log(Level.SEVERE, "could not count members", e);
return 0;
}
}

public static @NotNull Set<@NotNull String> getNames() {
return get().stream().map(m -> m.player().getName()).filter(Objects::nonNull).collect(Collectors.toSet());
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/pro/cloudnode/smp/smpcore/SMPCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pro.cloudnode.smp.smpcore.command.TimeCommand;
import pro.cloudnode.smp.smpcore.command.UnbanCommand;
import pro.cloudnode.smp.smpcore.listener.NationTeamUpdaterListener;
import pro.cloudnode.smp.smpcore.listener.PlayerSlotsListener;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -69,6 +70,7 @@ public void onEnable() {
initDatabase();

getServer().getPluginManager().registerEvents(new NationTeamUpdaterListener(), this);
getServer().getPluginManager().registerEvents(new PlayerSlotsListener(), this);

final @NotNull HashMap<@NotNull String, @NotNull Command> commands = new HashMap<>() {{
put("smpcore", new MainCommand());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pro.cloudnode.smp.smpcore.listener;

import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.server.ServerListPingEvent;
import org.jetbrains.annotations.NotNull;
import pro.cloudnode.smp.smpcore.Member;

import java.util.Optional;

public final class PlayerSlotsListener implements Listener {
/**
* Change the max players number in server list ping
*/
@EventHandler
public void onServerListPing(final @NotNull ServerListPingEvent event) {
event.setMaxPlayers(Member.count());
}

/**
* If the player is a member, but the server thinks it's full, allow them to join
*/
@EventHandler
public void onPlayerLogin(final @NotNull PlayerLoginEvent event) {
if (event.getResult() == PlayerLoginEvent.Result.KICK_FULL) {
final @NotNull Optional<@NotNull Member> member = Member.get(event.getPlayer());
if (member.isPresent()) event.allow();
}
}
}

0 comments on commit fee8caa

Please sign in to comment.