From e757ddb64e95445fea23fe5f73279a567fdcda5f Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 24 May 2020 20:57:20 +0200 Subject: [PATCH] feat: add GameTime --- .../main/java/de/cliffbreak/varo/Varo.java | 14 +++ .../listeners/PlayerJoinQuitListener.java | 74 ++--------- .../varo/listeners/PlayerLoginListener.java | 2 + .../varo/managers/GameTimeManager.java | 119 ++++++++++++++++++ .../de/cliffbreak/varo/uitls/PlayerUtils.java | 83 ++++++++++++ 5 files changed, 228 insertions(+), 64 deletions(-) create mode 100644 varo/src/main/java/de/cliffbreak/varo/managers/GameTimeManager.java create mode 100644 varo/src/main/java/de/cliffbreak/varo/uitls/PlayerUtils.java diff --git a/varo/src/main/java/de/cliffbreak/varo/Varo.java b/varo/src/main/java/de/cliffbreak/varo/Varo.java index ab70823..8664a80 100644 --- a/varo/src/main/java/de/cliffbreak/varo/Varo.java +++ b/varo/src/main/java/de/cliffbreak/varo/Varo.java @@ -24,11 +24,13 @@ import de.cliffbreak.varo.listeners.PlayerInteractNPCListener; import de.cliffbreak.varo.listeners.PlayerJoinQuitListener; import de.cliffbreak.varo.listeners.PlayerLoginListener; import de.cliffbreak.varo.listeners.PlayerResourcePackStatusListener; +import de.cliffbreak.varo.managers.GameTimeManager; import de.cliffbreak.varo.managers.NPCManager; import de.cliffbreak.varo.managers.TeamManager; import de.cliffbreak.varo.uitls.BanUtils; import de.cliffbreak.varo.uitls.MessageUtils; import de.cliffbreak.varo.uitls.PlayerCache; +import de.cliffbreak.varo.uitls.PlayerUtils; import de.cliffbreak.varo.uitls.VaroUtils; public class Varo extends JavaPlugin { @@ -43,6 +45,8 @@ public class Varo extends JavaPlugin { private MessageUtils messageUtils; private DiscordBot discordBot; private Database database; + private GameTimeManager gameTimeManager; + private PlayerUtils playerUtils; private boolean canCreatePortal = false; @@ -61,6 +65,8 @@ public class Varo extends JavaPlugin { this.teamManager = new TeamManager(); this.messageUtils = new MessageUtils(this); this.database = new Database(); + this.gameTimeManager = new GameTimeManager(this); + this.playerUtils = new PlayerUtils(this); this.config.addDefault("Varo.Start", 1590393600000f); this.config.addDefault("Varo.Debug", false); @@ -153,4 +159,12 @@ public class Varo extends JavaPlugin { public Database getDatabase() { return this.database; } + + public GameTimeManager getGameTimeManager() { + return this.gameTimeManager; + } + + public PlayerUtils getPlayerUtils() { + return this.playerUtils; + } } diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerJoinQuitListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerJoinQuitListener.java index 07b74f3..507d09a 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerJoinQuitListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerJoinQuitListener.java @@ -1,26 +1,15 @@ package de.cliffbreak.varo.listeners; -import java.time.Duration; -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneId; - -import com.destroystokyo.paper.Title; import com.destroystokyo.paper.event.server.PaperServerListPingEvent; import org.bukkit.Bukkit; -import org.bukkit.GameMode; -import org.bukkit.Location; -import org.bukkit.World; import org.bukkit.World.Environment; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.server.ServerListPingEvent; import org.bukkit.scheduler.BukkitRunnable; -import org.bukkit.scheduler.BukkitTask; import de.cliffbreak.varo.Varo; import de.cliffbreak.varo.uitls.PacketReader; @@ -39,16 +28,19 @@ public class PlayerJoinQuitListener implements Listener { if (!this.plugin.getTeamManager().hasTeam(e.getPlayer())) { return; } + this.plugin.getTeamManager().setAFK(e.getPlayer().getName()); if (!this.plugin.getBanUtils().isBanned(e.getPlayer().getUniqueId())) { - this.plugin.getNPCManager().createClone(e.getPlayer()); Bukkit.broadcast(this.plugin.getMessageUtils().getServiceMessage(e.getPlayer().getName(), "hat den Server verlassen.")); } - this.plugin.getTeamManager().setAFK(e.getPlayer().getName()); if (!e.getPlayer().getLocation().getWorld().getEnvironment().equals(Environment.THE_END)) { this.plugin.getPlayerCache().setPlayerLocationCache(e.getPlayer().getUniqueId().toString().replace("-", ""), e.getPlayer().getLocation()); } + if (!e.getPlayer().getLocation().getWorld().getEnvironment().equals(Environment.THE_END) + && !this.plugin.getBanUtils().isBanned(e.getPlayer().getUniqueId())) { + this.plugin.getNPCManager().createClone(e.getPlayer()); + } } @EventHandler @@ -77,43 +69,8 @@ public class PlayerJoinQuitListener implements Listener { } }.runTaskLater(this.plugin, 5); // wait 5 ticks - // Login cooldown + Animation - final Player p = e.getPlayer(); - World w = null; - for (final World world : Bukkit.getWorlds()) { - if (world.getEnvironment().equals(Environment.THE_END)) { - w = world; - } - } - - p.teleport(new Location(w, 0, 0, 100)); - p.setGameMode(GameMode.SPECTATOR); - p.setFlying(true); - p.setFlySpeed(0.0f); - final Title title = new Title("Varo 2020", "Bitte warte, bis das Spiel geladen hat...", 20, 8 * 20, 20); - p.sendTitle(title); - - final BukkitTask hidePlayersTask = new BukkitRunnable() { - @Override - public void run() { - for (final Player player : Bukkit.getOnlinePlayers()) - p.hidePlayer(plugin, player); - } - }.runTaskTimer(this.plugin, 0l, 1l); - - new BukkitRunnable() { - @Override - public void run() { - plugin.getNPCManager().removeClone(e.getPlayer()); - p.setGameMode(GameMode.SURVIVAL); - p.setFlySpeed(0.1f); - p.setFlying(false); - p.teleport(plugin.getPlayerCache().getPlayerLocationCache(p.getUniqueId().toString().replace("-", ""))); - hidePlayersTask.cancel(); - for (final Player player : Bukkit.getOnlinePlayers()) - p.showPlayer(plugin, player); - } - }.runTaskLater(this.plugin, 10 * 20); + this.plugin.getPlayerUtils().showLoadingScreen(e.getPlayer(), + this.plugin.getGameTimeManager().getLoginTimeLeft()); } @EventHandler @@ -127,19 +84,8 @@ public class PlayerJoinQuitListener implements Listener { private void handlePaperServerListPing(final PaperServerListPingEvent e) { e.setHidePlayers(true); - - try { - final Instant instant = Instant.ofEpochMilli(this.plugin.getConfig().getLong("Varo.Start")); - final LocalDateTime start = instant.atZone(ZoneId.systemDefault()).toLocalDateTime(); - final LocalDateTime current = LocalDateTime.now(); - - e.setMotd((this.plugin.getConfig().getBoolean("Varo.Debug") ? "§c§l[DEBUG] " : "") + "§9§l" + "VARO" - + "§b§l " + "Tag: " + (Duration.between(start, current).toDays() + 1) + "\n§r§7" + "powered by " - + "§4" + "Cliffbreak.de"); - } catch (final Exception ex) { - ex.printStackTrace(); - e.setMotd((this.plugin.getConfig().getBoolean("Varo.Debug") ? "§c§l[DEBUG] " : "") + "§9§l" + "VARO" - + "§b§l " + "Tag: -1" + "\n§r§7" + "powered by " + "§4" + "Cliffbreak.de"); - } + e.setMotd((this.plugin.getConfig().getBoolean("Varo.Debug") ? "§c§l[DEBUG] " : "") + "§9§l" + "VARO" + "§b§l " + + "Tag: " + this.plugin.getGameTimeManager().currentDay() + "\n§r§7" + "powered by " + "§4" + + "Cliffbreak.de"); } } \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerLoginListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerLoginListener.java index 62b2110..a98d363 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerLoginListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerLoginListener.java @@ -19,6 +19,8 @@ public class PlayerLoginListener implements Listener { public void onPlayerLogin(final PlayerLoginEvent e) { if (this.plugin.getBanUtils().isBanned(e.getPlayer().getUniqueId())) { e.disallow(Result.KICK_BANNED, "§4Du bist gestorben.\n \n§cDamit bist du aus §eVaro§c ausgeschieden."); + } else if (!this.plugin.getGameTimeManager().canLogin()) { + e.disallow(Result.KICK_OTHER, "Außerhalb der Playtime"); } } } \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/managers/GameTimeManager.java b/varo/src/main/java/de/cliffbreak/varo/managers/GameTimeManager.java new file mode 100644 index 0000000..1f73e99 --- /dev/null +++ b/varo/src/main/java/de/cliffbreak/varo/managers/GameTimeManager.java @@ -0,0 +1,119 @@ +package de.cliffbreak.varo.managers; + +import java.time.Duration; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.temporal.ChronoUnit; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + +import de.cliffbreak.varo.Varo; + +public class GameTimeManager { + + private final Varo plugin; + private static final int firstPlayTimeMinutes = 50; + private static final int firstPlayTimeLoginMinutes = 10; + private static final LocalTime firstPlayStart = LocalTime.of(19, 10, 00); + private static final int playTimeMinutes = 30; + private static final int playTimeLoginMinutes = 5; + private static final LocalTime playStart = LocalTime.of(19, 30, 00); + + public GameTimeManager(final Varo plugin) { + this.plugin = plugin; + startScheduler(); + } + + public int currentDay() { + final Instant instant = Instant.ofEpochMilli(this.plugin.getConfig().getLong("Varo.Start")); + final LocalDateTime start = instant.atZone(ZoneId.systemDefault()).toLocalDateTime(); + final LocalDateTime current = LocalDateTime.now(); + return (int) Duration.between(start, current).toDays() + 1; + } + + public boolean canLogin() { + final LocalTime currentTime = LocalTime.now(); + if (this.currentDay() == 1) { + if (!currentTime.isBefore(firstPlayStart.minusMinutes(firstPlayTimeLoginMinutes)) + && !currentTime.isAfter(firstPlayStart.plusMinutes(firstPlayTimeMinutes))) { + return true; + } else { + return false; + } + } else { + if (!currentTime.isBefore(playStart.minusMinutes(playTimeLoginMinutes)) + && !currentTime.isAfter(playStart.plusMinutes(playTimeMinutes))) { + return true; + } else { + return false; + } + } + } + + public boolean isLoginTimeOver() { + final LocalTime currentTime = LocalTime.now(); + if (this.currentDay() == 1) { + if (currentTime.isAfter(firstPlayStart)) { + return true; + } else { + return false; + } + } else { + if (currentTime.isAfter(playStart)) { + return true; + } else { + return false; + } + } + } + + public int getLoginTimeLeft() { + final LocalTime currentTime = LocalTime.now(); + return (int) currentTime.until(playStart, ChronoUnit.SECONDS); + } + + private boolean isPlayTimeOver() { + final LocalTime currentTime = LocalTime.now(); + if (this.currentDay() == 1) { + if (currentTime.isAfter(firstPlayStart.plusMinutes(firstPlayTimeMinutes))) { + return true; + } else { + return false; + } + } else { + if (currentTime.isAfter(playStart.plusMinutes(playTimeMinutes))) { + return true; + } else { + return false; + } + } + } + + private void startScheduler() { + new BukkitRunnable() { + boolean playersTeleported = false; + + @Override + public void run() { + if (isLoginTimeOver()) { + if (!this.playersTeleported) { + this.playersTeleported = true; + System.out.println("Teleport Players!"); + // plugin.getTeamManager(). + } + } + if (isPlayTimeOver()) { + for (Player p : Bukkit.getOnlinePlayers()) { + p.kickPlayer("Spielzeit abgelaufen"); + } + this.cancel(); + } + } + }.runTaskTimer(this.plugin, 20l, 20l); + } + +} \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/uitls/PlayerUtils.java b/varo/src/main/java/de/cliffbreak/varo/uitls/PlayerUtils.java new file mode 100644 index 0000000..1864f9e --- /dev/null +++ b/varo/src/main/java/de/cliffbreak/varo/uitls/PlayerUtils.java @@ -0,0 +1,83 @@ +package de.cliffbreak.varo.uitls; + +import com.destroystokyo.paper.Title; + +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.World.Environment; +import org.bukkit.entity.Player; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.scheduler.BukkitTask; + +import de.cliffbreak.varo.Varo; + +public class PlayerUtils { + private final Varo plugin; + + public PlayerUtils(final Varo plugin) { + this.plugin = plugin; + } + + public void showLoadingScreen(final Player p, int seconds) { + if (seconds < 10) { + seconds = 10; // default to 10 seconds for a smooth transition + } + World w = null; + for (final World world : Bukkit.getWorlds()) { + if (world.getEnvironment().equals(Environment.THE_END)) { + w = world; + } + } + + p.teleport(new Location(w, 0, 0, 100)); + p.setGameMode(GameMode.SPECTATOR); + p.setFlying(true); + p.setFlySpeed(0.0f); + final Title title = new Title("Varo 2020", "Bitte warte, bis das Spiel geladen hat...", 20, (seconds - 2) * 20, + 20); + p.sendTitle(title); + + p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, seconds * 20, 1, false, false)); + + final BukkitTask hidePlayersTask = new BukkitRunnable() { + @Override + public void run() { + for (final Player player : Bukkit.getOnlinePlayers()) + p.hidePlayer(plugin, player); + } + }.runTaskTimer(this.plugin, 0l, 1l); + + new BukkitRunnable() { + @Override + public void run() { + final int seconds = plugin.getGameTimeManager().getLoginTimeLeft(); + if (seconds <= 0) { + this.cancel(); + p.sendActionBar(""); + } else { + p.sendActionBar( + String.format("§r§l Noch: %02d:%02d", (int) Math.floor(seconds / 60), seconds % 60)); + } + } + }.runTaskTimer(this.plugin, 0, 20); + + new BukkitRunnable() { + @Override + public void run() { + plugin.getNPCManager().removeClone(p); + p.setGameMode(GameMode.SURVIVAL); + p.setFlySpeed(0.1f); + p.setFlying(false); + p.teleport(plugin.getPlayerCache().getPlayerLocationCache(p.getUniqueId().toString().replace("-", ""))); + hidePlayersTask.cancel(); + for (final Player player : Bukkit.getOnlinePlayers()) + p.showPlayer(plugin, player); + } + }.runTaskLater(this.plugin, seconds * 20); + } + +} \ No newline at end of file