Added player limiter

This commit is contained in:
Elias Müller 2023-11-18 15:27:07 +01:00
parent cb45047f71
commit 9d79e8dc07
6 changed files with 97 additions and 0 deletions

View File

@ -39,6 +39,7 @@ public final class Main extends JavaPlugin {
new Report(),
new Event(),
new Help(),
new PlayerLimit(),
new Debug()
);
Bukkit.getLogger().info("Loading appliances...");

View File

@ -0,0 +1,47 @@
package eu.mhsl.craftattack.spawn.appliances.playerlimit;
import eu.mhsl.craftattack.spawn.Main;
import eu.mhsl.craftattack.spawn.appliance.Appliance;
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
import eu.mhsl.craftattack.spawn.appliances.playerlimit.command.SetPlayerLimitCommand;
import eu.mhsl.craftattack.spawn.appliances.playerlimit.listener.PlayerJoinLimiterListener;
import eu.mhsl.craftattack.spawn.config.Configuration;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class PlayerLimit extends Appliance {
private static final String playerLimitKey = "maxPlayers";
private int limit;
public PlayerLimit() {
super("playerLimit");
this.limit = localConfig().getInt(playerLimitKey);
Bukkit.setMaxPlayers(Integer.MAX_VALUE);
}
public void setPlayerLimit(int limit) {
this.limit = limit;
localConfig().set(playerLimitKey, limit);
Configuration.saveChanges();
}
public int getLimit() {
return limit;
}
@Override
protected @NotNull List<Listener> eventHandlers() {
return List.of(
new PlayerJoinLimiterListener()
);
}
@Override
protected @NotNull List<ApplianceCommand<?>> commands() {
return List.of(
new SetPlayerLimitCommand()
);
}
}

View File

@ -0,0 +1,18 @@
package eu.mhsl.craftattack.spawn.appliances.playerlimit.command;
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
import eu.mhsl.craftattack.spawn.appliances.playerlimit.PlayerLimit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
public class SetPlayerLimitCommand extends ApplianceCommand<PlayerLimit> {
public SetPlayerLimitCommand() {
super("setPlayerLimit");
}
@Override
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
getAppliance().setPlayerLimit(Integer.parseInt(args[0]));
}
}

View File

@ -0,0 +1,18 @@
package eu.mhsl.craftattack.spawn.appliances.playerlimit.listener;
import eu.mhsl.craftattack.spawn.appliance.ApplianceListener;
import eu.mhsl.craftattack.spawn.appliances.playerlimit.PlayerLimit;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent;
public class PlayerJoinLimiterListener extends ApplianceListener<PlayerLimit> {
@EventHandler
public void onLogin(PlayerLoginEvent playerPreLoginEvent) {
playerPreLoginEvent.kickMessage(Component.text("Der Server ist derzeit voll! Versuche es bitte später erneut.", NamedTextColor.RED));
if(Bukkit.getOnlinePlayers().size() >= getAppliance().getLimit()) playerPreLoginEvent.setResult(PlayerLoginEvent.Result.KICK_FULL);
}
}

View File

@ -0,0 +1,12 @@
package eu.mhsl.craftattack.spawn.appliances.whitelist;
import eu.mhsl.craftattack.spawn.appliance.ApplianceListener;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
public class PlayerJoinListener extends ApplianceListener<Whitelist> {
@EventHandler
public void preLoginEvent(AsyncPlayerPreLoginEvent preLoginEvent) {
}
}

View File

@ -29,3 +29,4 @@ commands:
aliases: ["ts"]
discord:
aliases: ["dc"]
setPlayerLimit: