From 2d927792e45a7c4aced5a859f066369bbe7386c8 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 4 Oct 2021 10:42:26 +0200 Subject: [PATCH] Add FightInfoPacketSender --- .../misslewars/FightInfoPacketSender.java | 75 +++++++++++++++++++ .../steamwar/misslewars/FightScoreboard.java | 6 +- src/de/steamwar/misslewars/MWTeam.java | 7 +- src/de/steamwar/misslewars/MissileWars.java | 6 +- .../misslewars/listener/JoinListener.java | 10 +-- 5 files changed, 84 insertions(+), 20 deletions(-) create mode 100644 src/de/steamwar/misslewars/FightInfoPacketSender.java diff --git a/src/de/steamwar/misslewars/FightInfoPacketSender.java b/src/de/steamwar/misslewars/FightInfoPacketSender.java new file mode 100644 index 0000000..fdd4a80 --- /dev/null +++ b/src/de/steamwar/misslewars/FightInfoPacketSender.java @@ -0,0 +1,75 @@ +/* + * + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 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.misslewars; + +import de.steamwar.comms.packets.FightInfoPacket; +import de.steamwar.sql.SteamwarUser; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.scheduler.BukkitRunnable; + +import java.util.List; +import java.util.stream.Collectors; + +public class FightInfoPacketSender extends BukkitRunnable { + + private final World world = Bukkit.getWorlds().get(0); + + private final String serverName = Bukkit.getServer().getName(); + private final String gameMode = "missilewars"; + private final String worldName = world.getName(); + private final String blueName = "§3blue"; + private final String redName = "§cred"; + private final int blueLeader = Config.BlueLeader != null ? SteamwarUser.get(Config.BlueLeader).getId() : 0; + private final int redLeader = Config.RedLeader != null ? SteamwarUser.get(Config.RedLeader).getId() : 0; + + @Override + public void run() { + if (Bukkit.getOnlinePlayers().isEmpty()) { + return; + } + if (Config.test()) { + return; + } + + int countdown; + if (FightScoreboard.getStartTime() == 0) { + countdown = 0; + } else { + countdown = (int) ((System.currentTimeMillis() - FightScoreboard.getStartTime()) / 1000); + } + + List bluePlayers = MissileWars.getBlueTeam().getPlayers().stream() + .map(player -> SteamwarUser.get(player.getUniqueId()).getId()) + .collect(Collectors.toList()); + List redPlayers = MissileWars.getRedTeam().getPlayers().stream() + .map(player -> SteamwarUser.get(player.getUniqueId()).getId()) + .collect(Collectors.toList()); + List spectatorPlayers = Bukkit.getOnlinePlayers().stream() + .map(player -> SteamwarUser.get(player.getUniqueId()).getId()) + .filter(integer -> !(bluePlayers.contains(integer) || redPlayers.contains(integer))) + .collect(Collectors.toList()); + + new FightInfoPacket(serverName, gameMode, worldName, blueName, redName, MissileWars.getFightState().name().toLowerCase(), countdown, blueLeader, redLeader, 0, 0, bluePlayers, redPlayers, spectatorPlayers) + .send(Bukkit.getOnlinePlayers().iterator().next()); + } +} diff --git a/src/de/steamwar/misslewars/FightScoreboard.java b/src/de/steamwar/misslewars/FightScoreboard.java index 1dbea54..8f86553 100644 --- a/src/de/steamwar/misslewars/FightScoreboard.java +++ b/src/de/steamwar/misslewars/FightScoreboard.java @@ -19,13 +19,13 @@ package de.steamwar.misslewars; +import jdk.nashorn.internal.objects.annotations.Getter; import org.bukkit.Bukkit; import org.bukkit.scoreboard.DisplaySlot; import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Scoreboard; import java.text.SimpleDateFormat; -import java.util.Date; import java.util.Objects; /** @@ -41,6 +41,10 @@ class FightScoreboard { private static long startTime = 0; + public static long getStartTime() { + return startTime; + } + static{ if(scoreboard.getObjective("AAA") == null) objective = scoreboard.registerNewObjective("AAA", "BBB", "MissleWars"); diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 83c3dbe..a540212 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -19,6 +19,7 @@ package de.steamwar.misslewars; +import de.steamwar.comms.packets.FightInfoPacket; import de.steamwar.comms.packets.TablistNamePacket; import de.steamwar.sql.SteamwarUser; import org.bukkit.*; @@ -139,9 +140,6 @@ public class MWTeam { p.setDisplayName(color + p.getName()); if (MissileWars.getFightState() == FightState.WAITING && !enemy().players.isEmpty()) MissileWars.startRound(); - - if (!Config.test()) - Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> new TablistNamePacket(SteamwarUser.get(p.getUniqueId()).getId(), color + p.getName()).send(p), 5); } public void leave(Player p) { @@ -161,9 +159,6 @@ public class MWTeam { sbteam.removePlayer(p); if (players.isEmpty() && MissileWars.getFightState() == FightState.FIGHTING) MissileWars.end(WinReasons.NO_ENEMY, enemy()); - - if (!Config.test()) - new TablistNamePacket(SteamwarUser.get(p.getUniqueId()).getId(), "§7§o" + p.getName()).send(p); } public void invitePlayer(Player p) { diff --git a/src/de/steamwar/misslewars/MissileWars.java b/src/de/steamwar/misslewars/MissileWars.java index 7fef0a0..adf509f 100644 --- a/src/de/steamwar/misslewars/MissileWars.java +++ b/src/de/steamwar/misslewars/MissileWars.java @@ -84,6 +84,8 @@ public class MissileWars extends JavaPlugin { CustomItem.init(); StateDependent.setupState(fightState); + + Bukkit.getScheduler().runTaskTimer(this, new FightInfoPacketSender(), 20, 20); } /** @@ -163,10 +165,6 @@ public class MissileWars extends JavaPlugin { } } - public static void spectateJoin(Player p) { - p.teleport(MissileWars.getRedTeam().getSpawn().toVector().midpoint(MissileWars.getBlueTeam().getSpawn().toVector()).toLocation(p.getWorld())); - } - public static void leave(Player p) { MissileWars.getBlueTeam().leave(p); MissileWars.getRedTeam().leave(p); diff --git a/src/de/steamwar/misslewars/listener/JoinListener.java b/src/de/steamwar/misslewars/listener/JoinListener.java index a5e9a33..0758ece 100644 --- a/src/de/steamwar/misslewars/listener/JoinListener.java +++ b/src/de/steamwar/misslewars/listener/JoinListener.java @@ -19,12 +19,9 @@ package de.steamwar.misslewars.listener; -import de.steamwar.comms.packets.TablistNamePacket; import de.steamwar.misslewars.Config; import de.steamwar.misslewars.FightState; import de.steamwar.misslewars.MissileWars; -import de.steamwar.sql.SteamwarUser; -import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerJoinEvent; @@ -46,12 +43,7 @@ public class JoinListener extends BasicListener { } else if (Config.BlueLeader.equals(e.getPlayer().getUniqueId())) { MissileWars.getBlueTeam().join(e.getPlayer()); } else { - if (!Config.test()) { - Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> { - new TablistNamePacket(SteamwarUser.get(e.getPlayer().getUniqueId()).getId(), "§7" + e.getPlayer().getName()).send(e.getPlayer()); - }, 5); - } - MissileWars.spectateJoin(e.getPlayer()); + e.getPlayer().teleport(MissileWars.getRedTeam().getSpawn().toVector().midpoint(MissileWars.getBlueTeam().getSpawn().toVector()).toLocation(e.getPlayer().getWorld())); } return; }