Added player limiter
This commit is contained in:
		| @@ -39,6 +39,7 @@ public final class Main extends JavaPlugin { | |||||||
|                 new Report(), |                 new Report(), | ||||||
|                 new Event(), |                 new Event(), | ||||||
|                 new Help(), |                 new Help(), | ||||||
|  |                 new PlayerLimit(), | ||||||
|                 new Debug() |                 new Debug() | ||||||
|         ); |         ); | ||||||
|         Bukkit.getLogger().info("Loading appliances..."); |         Bukkit.getLogger().info("Loading appliances..."); | ||||||
|   | |||||||
| @@ -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() | ||||||
|  |         ); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -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])); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -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); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -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) { | ||||||
|  |  | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -29,3 +29,4 @@ commands: | |||||||
|     aliases: ["ts"] |     aliases: ["ts"] | ||||||
|   discord: |   discord: | ||||||
|     aliases: ["dc"] |     aliases: ["dc"] | ||||||
|  |   setPlayerLimit: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user