feat: add GameTime

This commit is contained in:
Simon Giesel 2020-05-24 20:57:20 +02:00
parent 34dce3a3c8
commit e757ddb64e
5 changed files with 228 additions and 64 deletions

View file

@ -24,11 +24,13 @@ import de.cliffbreak.varo.listeners.PlayerInteractNPCListener;
import de.cliffbreak.varo.listeners.PlayerJoinQuitListener; import de.cliffbreak.varo.listeners.PlayerJoinQuitListener;
import de.cliffbreak.varo.listeners.PlayerLoginListener; import de.cliffbreak.varo.listeners.PlayerLoginListener;
import de.cliffbreak.varo.listeners.PlayerResourcePackStatusListener; import de.cliffbreak.varo.listeners.PlayerResourcePackStatusListener;
import de.cliffbreak.varo.managers.GameTimeManager;
import de.cliffbreak.varo.managers.NPCManager; import de.cliffbreak.varo.managers.NPCManager;
import de.cliffbreak.varo.managers.TeamManager; import de.cliffbreak.varo.managers.TeamManager;
import de.cliffbreak.varo.uitls.BanUtils; import de.cliffbreak.varo.uitls.BanUtils;
import de.cliffbreak.varo.uitls.MessageUtils; import de.cliffbreak.varo.uitls.MessageUtils;
import de.cliffbreak.varo.uitls.PlayerCache; import de.cliffbreak.varo.uitls.PlayerCache;
import de.cliffbreak.varo.uitls.PlayerUtils;
import de.cliffbreak.varo.uitls.VaroUtils; import de.cliffbreak.varo.uitls.VaroUtils;
public class Varo extends JavaPlugin { public class Varo extends JavaPlugin {
@ -43,6 +45,8 @@ public class Varo extends JavaPlugin {
private MessageUtils messageUtils; private MessageUtils messageUtils;
private DiscordBot discordBot; private DiscordBot discordBot;
private Database database; private Database database;
private GameTimeManager gameTimeManager;
private PlayerUtils playerUtils;
private boolean canCreatePortal = false; private boolean canCreatePortal = false;
@ -61,6 +65,8 @@ public class Varo extends JavaPlugin {
this.teamManager = new TeamManager(); this.teamManager = new TeamManager();
this.messageUtils = new MessageUtils(this); this.messageUtils = new MessageUtils(this);
this.database = new Database(); this.database = new Database();
this.gameTimeManager = new GameTimeManager(this);
this.playerUtils = new PlayerUtils(this);
this.config.addDefault("Varo.Start", 1590393600000f); this.config.addDefault("Varo.Start", 1590393600000f);
this.config.addDefault("Varo.Debug", false); this.config.addDefault("Varo.Debug", false);
@ -153,4 +159,12 @@ public class Varo extends JavaPlugin {
public Database getDatabase() { public Database getDatabase() {
return this.database; return this.database;
} }
public GameTimeManager getGameTimeManager() {
return this.gameTimeManager;
}
public PlayerUtils getPlayerUtils() {
return this.playerUtils;
}
} }

View file

@ -1,26 +1,15 @@
package de.cliffbreak.varo.listeners; 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 com.destroystokyo.paper.event.server.PaperServerListPingEvent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment; import org.bukkit.World.Environment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.server.ServerListPingEvent; import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import de.cliffbreak.varo.Varo; import de.cliffbreak.varo.Varo;
import de.cliffbreak.varo.uitls.PacketReader; import de.cliffbreak.varo.uitls.PacketReader;
@ -39,16 +28,19 @@ public class PlayerJoinQuitListener implements Listener {
if (!this.plugin.getTeamManager().hasTeam(e.getPlayer())) { if (!this.plugin.getTeamManager().hasTeam(e.getPlayer())) {
return; return;
} }
this.plugin.getTeamManager().setAFK(e.getPlayer().getName());
if (!this.plugin.getBanUtils().isBanned(e.getPlayer().getUniqueId())) { if (!this.plugin.getBanUtils().isBanned(e.getPlayer().getUniqueId())) {
this.plugin.getNPCManager().createClone(e.getPlayer());
Bukkit.broadcast(this.plugin.getMessageUtils().getServiceMessage(e.getPlayer().getName(), Bukkit.broadcast(this.plugin.getMessageUtils().getServiceMessage(e.getPlayer().getName(),
"hat den Server verlassen.")); "hat den Server verlassen."));
} }
this.plugin.getTeamManager().setAFK(e.getPlayer().getName());
if (!e.getPlayer().getLocation().getWorld().getEnvironment().equals(Environment.THE_END)) { if (!e.getPlayer().getLocation().getWorld().getEnvironment().equals(Environment.THE_END)) {
this.plugin.getPlayerCache().setPlayerLocationCache(e.getPlayer().getUniqueId().toString().replace("-", ""), this.plugin.getPlayerCache().setPlayerLocationCache(e.getPlayer().getUniqueId().toString().replace("-", ""),
e.getPlayer().getLocation()); 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 @EventHandler
@ -77,43 +69,8 @@ public class PlayerJoinQuitListener implements Listener {
} }
}.runTaskLater(this.plugin, 5); // wait 5 ticks }.runTaskLater(this.plugin, 5); // wait 5 ticks
// Login cooldown + Animation this.plugin.getPlayerUtils().showLoadingScreen(e.getPlayer(),
final Player p = e.getPlayer(); this.plugin.getGameTimeManager().getLoginTimeLeft());
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);
} }
@EventHandler @EventHandler
@ -127,19 +84,8 @@ public class PlayerJoinQuitListener implements Listener {
private void handlePaperServerListPing(final PaperServerListPingEvent e) { private void handlePaperServerListPing(final PaperServerListPingEvent e) {
e.setHidePlayers(true); e.setHidePlayers(true);
e.setMotd((this.plugin.getConfig().getBoolean("Varo.Debug") ? "§c§l[DEBUG] " : "") + "§9§l" + "VARO" + "§b§l "
try { + "Tag: " + this.plugin.getGameTimeManager().currentDay() + "\n§r§7" + "powered by " + "§4"
final Instant instant = Instant.ofEpochMilli(this.plugin.getConfig().getLong("Varo.Start")); + "Cliffbreak.de");
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");
}
} }
} }

View file

@ -19,6 +19,8 @@ public class PlayerLoginListener implements Listener {
public void onPlayerLogin(final PlayerLoginEvent e) { public void onPlayerLogin(final PlayerLoginEvent e) {
if (this.plugin.getBanUtils().isBanned(e.getPlayer().getUniqueId())) { 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."); 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");
} }
} }
} }

View file

@ -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);
}
}

View file

@ -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);
}
}