diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopper.java b/BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopperListener.java similarity index 95% rename from BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopper.java rename to BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopperListener.java index 54e3858a..e0994afe 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopper.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/world/AFKStopperListener.java @@ -29,13 +29,13 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerMoveEvent; @Linked(LinkageType.LISTENER) -public class AFKStopper implements Listener { +public class AFKStopperListener implements Listener { private static final String afkWarning = BauSystem.PREFIX + "§cDieser Server wird bei weiterer Inaktivität in einer Minute gestoppt"; private int minutesAfk; - public AFKStopper() { + public AFKStopperListener() { minutesAfk = 0; Bukkit.getPluginManager().registerEvents(this, BauSystem.getInstance()); Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/world/AutoShutdownListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/world/AutoShutdownListener.java new file mode 100644 index 00000000..8e9a2602 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/world/AutoShutdownListener.java @@ -0,0 +1,90 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.world; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.tpslimit.TPSLimitUtils; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.core.Core; +import de.steamwar.scoreboard.SWScoreboard; +import org.bukkit.Bukkit; +import org.bukkit.GameRule; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.scheduler.BukkitTask; + +import java.util.logging.Level; + +@Linked(LinkageType.LISTENER) +public class AutoShutdownListener implements Listener { + + private BukkitTask autoShutdown; + + @EventHandler + public void onJoin(PlayerLoginEvent e) { + if (autoShutdown != null) { + autoShutdown.cancel(); + autoShutdown = null; + } + + Player p = e.getPlayer(); + p.setOp(true); + + if (Core.getVersion() == 15) { + Bukkit.getWorlds().get(0).setGameRule(GameRule.REDUCED_DEBUG_INFO, false); + } + } + + @EventHandler + public void onLeave(PlayerQuitEvent e) { + Player p = e.getPlayer(); + SWScoreboard.removeScoreboard(p); + if (Bukkit.getOnlinePlayers().isEmpty() || (Bukkit.getOnlinePlayers().size() == 1 && Bukkit.getOnlinePlayers().contains(p))) { + if (autoShutdown != null) { + autoShutdown.cancel(); + } + TPSLimitUtils.setTPS(20.0); + autoShutdown = Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), new Runnable() { + int count = 0; + + @Override + public void run() { + if (count >= 300) { + Bukkit.shutdown(); + return; + } + count++; + try { + if (RamUsage.getUsage() > 0.8) { + Bukkit.shutdown(); + } + } catch (Throwable throwable) { + Bukkit.getLogger().log(Level.WARNING, throwable.getMessage(), throwable); + Bukkit.shutdown(); + } + } + }, 20, 20); + } + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/utils/RamUsage.java b/BauSystem_Main/src/de/steamwar/bausystem/features/world/RamUsage.java similarity index 97% rename from BauSystem_Main/src/de/steamwar/bausystem/utils/RamUsage.java rename to BauSystem_Main/src/de/steamwar/bausystem/features/world/RamUsage.java index 9f82cc7c..35f8c81a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/utils/RamUsage.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/world/RamUsage.java @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -package de.steamwar.bausystem.utils; +package de.steamwar.bausystem.features.world; import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/utils/.gitkeep b/BauSystem_Main/src/de/steamwar/bausystem/utils/.gitkeep new file mode 100644 index 00000000..e69de29b