diff --git a/Plugins/Mineplex.Core/src/mineplex/core/game/region/GameRejoinData.java b/Plugins/Mineplex.Core/src/mineplex/core/game/region/GameRejoinData.java new file mode 100644 index 000000000..10971fad6 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/game/region/GameRejoinData.java @@ -0,0 +1,33 @@ +package mineplex.core.game.region; + +import mineplex.serverdata.data.Data; + +public class GameRejoinData implements Data +{ + + private final int _accountId, _gameId; + private final String _server; + + GameRejoinData(int accountId, int gameId, String server) + { + _accountId = accountId; + _gameId = gameId; + _server = server; + } + + public int getGameId() + { + return _gameId; + } + + public String getServer() + { + return _server; + } + + @Override + public String getDataId() + { + return String.valueOf(_accountId); + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/game/region/GameRejoinManager.java b/Plugins/Mineplex.Core/src/mineplex/core/game/region/GameRejoinManager.java new file mode 100644 index 000000000..0f10b3adb --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/game/region/GameRejoinManager.java @@ -0,0 +1,91 @@ +package mineplex.core.game.region; + +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; + +import mineplex.core.Managers; +import mineplex.core.MiniPlugin; +import mineplex.core.account.CoreClientManager; +import mineplex.core.common.util.C; +import mineplex.core.common.util.UtilServer; +import mineplex.core.game.GameDisplay; +import mineplex.serverdata.Region; +import mineplex.serverdata.redis.RedisDataRepository; + +public class GameRejoinManager implements Listener +{ + + private static final int KEY_TIMEOUT = 300; + + private final MiniPlugin _parent; + private final CoreClientManager _clientManager; + private final RedisDataRepository _repository; + + public GameRejoinManager(MiniPlugin parent) + { + _parent = parent; + _clientManager = Managers.require(CoreClientManager.class); + _repository = new RedisDataRepository<>(Region.ALL, GameRejoinData.class, "gameRejoin"); + } + + public void searchToRejoin() + { + UtilServer.RegisterEvents(this); + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void playerJoin(PlayerJoinEvent event) + { + Player player = event.getPlayer(); + String id = String.valueOf(_clientManager.getAccountId(player)); + + _parent.runAsync(() -> + { + GameRejoinData rejoinData = _repository.getElement(id); + + if (rejoinData == null) + { + return; + } + + GameDisplay game = GameDisplay.getById(rejoinData.getGameId()); + + if (game == null) + { + return; + } + + BaseComponent[] text = TextComponent.fromLegacyText("\n " + C.cYellowB + C.Line + "CLICK HERE" + C.cAqua + " to rejoin your last game of " + C.cYellowB + game.getLobbyName() + C.cAqua + "!\n"); + HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponent[] + { + new TextComponent(C.cGray + "Click to join " + C.cGold + rejoinData.getServer() + C.cGray + ".") + }); + ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/server " + rejoinData.getServer()); + + for (BaseComponent component : text) + { + component.setHoverEvent(hoverEvent); + component.setClickEvent(clickEvent); + } + + player.spigot().sendMessage(text); + + _repository.removeElement(id); + }); + } + + public void saveRejoinData(Player player, int gameId) + { + int accountId = _clientManager.getAccountId(player); + + _parent.runAsync(() -> _repository.addElement(new GameRejoinData(accountId, gameId, UtilServer.getServerName()), KEY_TIMEOUT)); + } +} diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/Game.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/Game.java index 45e84931d..57b22fcc4 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/Game.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/Game.java @@ -190,7 +190,7 @@ public abstract class Game extends ListenerComponent implements Lifetimed private NautHashMap _playerInTime = new NautHashMap<>(); // Player Location Store - private NautHashMap _playerLocationStore = new NautHashMap(); + private final Map _playerLocationStore = new HashMap<>(); // Scoreboard protected GameScoreboard Scoreboard; @@ -666,7 +666,7 @@ public abstract class Game extends ListenerComponent implements Lifetimed return _gemCount; } - public NautHashMap GetLocationStore() + public Map GetLocationStore() { return _playerLocationStore; } diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/TeamGame.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/TeamGame.java index 3fa641486..c6cc56ca5 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/TeamGame.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/TeamGame.java @@ -1,28 +1,18 @@ package nautilus.game.arcade.game; -import mineplex.core.common.util.C; -import mineplex.core.common.util.NautHashMap; -import mineplex.core.common.util.UtilTime; -import mineplex.core.common.util.UtilTime.TimeUnit; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; import nautilus.game.arcade.ArcadeManager; import nautilus.game.arcade.GameType; import nautilus.game.arcade.events.PlayerStateChangeEvent; import nautilus.game.arcade.game.GameTeam.PlayerState; -import nautilus.game.arcade.game.modules.RejoinModule; -import nautilus.game.arcade.game.modules.RejoinModule.RejoinPlayerData; import nautilus.game.arcade.kit.Kit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; -import org.bukkit.event.player.PlayerCommandPreprocessEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerQuitEvent; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; public abstract class TeamGame extends Game @@ -82,18 +72,6 @@ public abstract class TeamGame extends Game if (team.GetPlayers(true).size() > 0) teamsAlive.add(team); - if (!QuitOut) - { - //Offline Player Team - if (getModule(RejoinModule.class) != null) - { - for (RejoinPlayerData data : getModule(RejoinModule.class).getData()) - { - teamsAlive.add(data.getTeam()); - } - } - } - if (teamsAlive.size() <= 1) { //Announce diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/cakewars/CakeWars.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/cakewars/CakeWars.java index c94018e21..010a95a14 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/cakewars/CakeWars.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/cakewars/CakeWars.java @@ -91,6 +91,7 @@ import nautilus.game.arcade.game.modules.capturepoint.CapturePointModule; import nautilus.game.arcade.game.modules.chest.ChestLootModule; import nautilus.game.arcade.game.modules.chest.ChestLootPool; import nautilus.game.arcade.game.modules.compass.CompassModule; +import nautilus.game.arcade.game.modules.rejoin.RejoinModule; import nautilus.game.arcade.game.modules.winstreak.WinStreakModule; import nautilus.game.arcade.kit.Kit; import nautilus.game.arcade.managers.chat.ChatStatData; @@ -218,6 +219,9 @@ public class CakeWars extends TeamGame new WinStreakModule() .register(this); + new RejoinModule(manager) + .register(this); + _cakeTeamModule = new CakeTeamModule(this); _cakeTeamModule.register(); diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/cakewars/team/CakeTeamModule.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/cakewars/team/CakeTeamModule.java index 068b547b5..f1ee61a51 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/cakewars/team/CakeTeamModule.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/cakewars/team/CakeTeamModule.java @@ -60,6 +60,7 @@ import nautilus.game.arcade.game.games.cakewars.shop.CakeShopModule; import nautilus.game.arcade.game.games.cakewars.shop.trap.CakeTrapItem; import nautilus.game.arcade.game.games.cakewars.shop.trap.CakeTrapItem.TrapTrigger; import nautilus.game.arcade.game.modules.compass.CompassModule; +import nautilus.game.arcade.game.modules.rejoin.PlayerRejoinGameEvent; import nautilus.game.arcade.world.WorldData; public class CakeTeamModule extends CakeModule @@ -142,6 +143,18 @@ public class CakeTeamModule extends CakeModule ); } + @EventHandler + public void playerRejoin(PlayerRejoinGameEvent event) + { + Player player = event.getPlayer(); + CakeTeam cakeTeam = getCakeTeam(event.getPlayerGameInfo().getTeam()); + + if (cakeTeam == null || !cakeTeam.canRespawn()) + { + event.setCancelled(true); + } + } + @EventHandler public void updateHologramVisibility(UpdateEvent event) { @@ -285,7 +298,7 @@ public class CakeTeamModule extends CakeModule Location location = block.getLocation(); - block.getWorld().playEffect(location, Effect.STEP_SOUND, Material.CAKE); + block.getWorld().playEffect(location, Effect.STEP_SOUND, Material.CAKE_BLOCK); if (data < 6) { diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/uhc/UHC.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/uhc/UHC.java index 5b1cb0b5c..ca564ff81 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/uhc/UHC.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/uhc/UHC.java @@ -22,7 +22,6 @@ import net.minecraft.server.v1_8_R3.WorldServer; import org.bukkit.Bukkit; import org.bukkit.Chunk; -import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Sound; @@ -82,7 +81,6 @@ import mineplex.core.common.util.UtilAlg; import mineplex.core.common.util.UtilBlock; import mineplex.core.common.util.UtilInv; import mineplex.core.common.util.UtilMath; -import mineplex.core.common.util.UtilParser; import mineplex.core.common.util.UtilParticle; import mineplex.core.common.util.UtilParticle.ParticleType; import mineplex.core.common.util.UtilParticle.ViewDist; @@ -115,13 +113,6 @@ import nautilus.game.arcade.game.games.uhc.stat.LuckyMinerStat; import nautilus.game.arcade.game.modules.AbsorptionFix; import nautilus.game.arcade.game.modules.OreVeinEditorModule; import nautilus.game.arcade.game.modules.PlayerHeadModule; -import nautilus.game.arcade.game.modules.RejoinModule; -import nautilus.game.arcade.game.modules.RejoinModule.RejoinPlayerData; -import nautilus.game.arcade.game.modules.combatlog.CombatLogModule; -import nautilus.game.arcade.game.modules.combatlog.CombatLogNPC; -import nautilus.game.arcade.game.modules.combatlog.CombatLogNPCExpiredEvent; -import nautilus.game.arcade.game.modules.combatlog.CombatLogNPCKilledEvent; -import nautilus.game.arcade.game.modules.combatlog.CombatLogNPCPreSpawnEvent; import nautilus.game.arcade.game.modules.compass.CompassModule; import nautilus.game.arcade.kit.Kit; @@ -303,7 +294,6 @@ public abstract class UHC extends Game .setGiveCompass(false) .register(this); new OreVeinEditorModule().removeNonAirVeins().register(this); - new RejoinModule().register(this); new AbsorptionFix() .register(this); @@ -446,32 +436,6 @@ public abstract class UHC extends Game System.gc(); UtilPlayer.message(caller, F.main("Debug", "Cleaned up!")); }); - registerDebugCommand("setcombatlogtimeout", Perm.DEBUG_SETCOMBATLOGTIMEOUT_COMMAND, PermissionGroup.DEV, (caller, args) -> - { - CombatLogModule module = getModule(CombatLogModule.class); - if (module == null) - { - UtilPlayer.message(caller, F.main("Debug", "The combat log module has not been loaded yet")); - return; - } - - if (args.length == 0) - { - UtilPlayer.message(caller, F.main("Debug", "No timeout specified")); - return; - } - - try - { - int timeout = Integer.parseInt(args[0]); - module.setCombatLogTime(timeout); - getModule(RejoinModule.class).setRejoinTime(timeout); - UtilPlayer.message(caller, F.main("Debug", "Set the new timeout to " + timeout)); - } catch (NumberFormatException ex) - { - UtilPlayer.message(caller, F.main("Debug", "That's not a number!")); - } - }); registerDebugCommand("dm", Perm.DEBUG_DEATHMATCH_COMMAND, PermissionGroup.ADMIN, (caller, args) -> { SAFE_TIME = 0; @@ -812,38 +776,6 @@ public abstract class UHC extends Game } } } - - if (getModule(CombatLogModule.class) != null) - { - for (CombatLogNPC npc : getModule(CombatLogModule.class).getAllNPCs()) - { - LivingEntity ent = npc.getNPC(); - Location loc = ent.getLocation(); - - // Bump Players Back In - if (loc.getX() > border || loc.getX() < -border || loc.getZ() > border || loc.getZ() < -border || loc.getY() < _border.getYMin() || loc.getY() > _border.getYMax()) - { - // Can't use recharge on entities; blame bad design (mapping - // by name instead of uuid) - // if (Recharge.Instance.use(ent, "Hit by Border", 1000, - // false, false)) - { - Entity bottom = ent; - while (bottom.getVehicle() != null) - bottom = bottom.getVehicle(); - - UtilAction.velocity(bottom, UtilAlg.getTrajectory2d(loc, GetSpectatorLocation()), 1.2, true, 0.4, 0, 10, true); - - // If nothing else has damaged the NPC, then - // lastDamageCause will be null - Manager.GetDamage().NewDamageEvent(ent, null, null, DamageCause.VOID, WORLD_BORDER_DAMAGE, false, false, false, "Nether Field", "Vaporize"); - - ent.getWorld().playSound(loc, Sound.NOTE_BASS, 2f, 1f); - ent.getWorld().playSound(loc, Sound.NOTE_BASS, 2f, 1f); - } - } - } - } } @EventHandler @@ -923,11 +855,7 @@ public abstract class UHC extends Game DamagePvP = true; - RejoinModule rejoinModule = getModule(RejoinModule.class); - - rejoinModule.setRejoinTime((int) TimeUnit.MINUTES.toMillis(5)); getModule(CompassModule.class).setGiveCompass(true); - new CombatLogModule().setCombatLogTime((int) rejoinModule.getRejoinTime()).register(this); } public void startPreDeathmatch() @@ -946,9 +874,6 @@ public abstract class UHC extends Game // Toggle temporary game settings Damage = false; - // Disable rejoining - getModule(RejoinModule.class).stopAllPlayersFromRejoining(); - // Set time WorldTimeSet = 0; @@ -1041,66 +966,6 @@ public abstract class UHC extends Game } } - @EventHandler - public void preSpawnCombatLogNPC(CombatLogNPCPreSpawnEvent event) - { - if (event.getPlayer().getGameMode() == GameMode.CREATIVE) - { - event.setCancelled(true); - } - } - - @EventHandler - public void combatLogNPCDeathEvent(CombatLogNPCKilledEvent event) - { - CombatLogNPC npc = event.getNpc(); - - if (npc.getLastDamager() instanceof Player) - { - Player killer = (Player) npc.getLastDamager(); - Announce(npc.getPlayerInfo().getTeamColor() + C.Bold + npc.getPlayerInfo().getName() + C.cGray + C.Bold + " was killed by " + getArcadeManager().GetColor(killer) + C.Bold + npc - .getLastDamager().getName() + C.cGray + C.Bold + " while logged out."); - } - else - { - String cause = UtilParser.parseDamageCause(npc.getLastDamageCause()); - if (npc.getLastDamager() != null) - { - cause = npc.getLastDamager().getName(); - } - Announce(npc.getPlayerInfo().getTeamColor() + C.Bold + npc.getPlayerInfo().getName() + C.cGray + C.Bold + " was killed by " + cause + " while logged out."); - } - - ItemStack stack = PlayerHeadModule.getGoldenHead(); - - npc.getNPC().getWorld().dropItemNaturally(npc.getNPC().getLocation(), stack); - - Location location = npc.getNPC().getLocation(); - - placeItemsInChest(npc.getPlayerInfo().getItems(), location); - - getModule(RejoinModule.class).stopPlayerFromRejoining(npc.getPlayerInfo().getName()); - } - - @EventHandler - public void combatLogNpcExpireEvent(CombatLogNPCExpiredEvent event) - { - CombatLogNPC npc = event.getNpc(); - - ItemStack stack = PlayerHeadModule.getGoldenHead(); - - npc.getNPC().getWorld().dropItemNaturally(npc.getNPC().getLocation(), stack); - - Location location = npc.getNPC().getLocation(); - - for (ItemStack item : npc.getPlayerInfo().getItems()) - { - location.getWorld().dropItemNaturally(location, item); - } - - getModule(RejoinModule.class).stopPlayerFromRejoining(npc.getPlayerInfo().getName()); - } - @EventHandler public void EarlyGameUpdate(UpdateEvent event) { @@ -1668,15 +1533,6 @@ public abstract class UHC extends Game // Online Teams List teamsAlive = GetTeamList().stream().filter(team -> team.GetPlayers(true).size() > 0).collect(Collectors.toList()); - // Offline Player Team - if (teamsAlive.size() > 1) - { - for (RejoinPlayerData data : getModule(RejoinModule.class).getData()) - { - teamsAlive.add(data.getTeam()); - } - } - if (teamsAlive.size() <= 1) { if (teamsAlive.size() > 0) diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/RejoinModule.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/RejoinModule.java deleted file mode 100644 index ca3f26b6b..000000000 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/RejoinModule.java +++ /dev/null @@ -1,307 +0,0 @@ -package nautilus.game.arcade.game.modules; - -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -import org.bukkit.Location; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerLoginEvent; -import org.bukkit.event.player.PlayerQuitEvent; - -import mineplex.core.account.permissions.Permission; -import mineplex.core.account.permissions.PermissionGroup; -import mineplex.core.common.util.C; -import mineplex.core.common.util.F; -import mineplex.core.common.util.NautHashMap; -import mineplex.core.common.util.UtilAlg; -import mineplex.core.common.util.UtilTime; -import mineplex.core.updater.UpdateType; -import mineplex.core.updater.event.UpdateEvent; - -import nautilus.game.arcade.game.GameTeam; -import nautilus.game.arcade.kit.Kit; - -public class RejoinModule extends Module -{ - public enum Perm implements Permission - { - DEBUG_REJOIN_COMMAND, - } - - private Set _data = new HashSet<>(); - - private int _rejoinTime = (int) TimeUnit.MINUTES.toMillis(2); - - @Override - protected void setup() - { - getGame().registerDebugCommand("rejoin", Perm.DEBUG_REJOIN_COMMAND, PermissionGroup.ADMIN, (caller, args) -> - { - if (args.length < 1) - { - caller.sendMessage(F.main("Debug", "/rejoin ")); - return; - } - - String player = args[0]; - - RejoinPlayerData data = getRejoinPlayerData(player); - - // Player wasn't alive earlier or was too slow - if (data == null) - { - _data.add(new RejoinPlayerData(player, 20, null, null)); - } else - { - caller.sendMessage(F.main("Debug", "That player is already allowed to rejoin!")); - return; - } - - caller.sendMessage(F.main("Debug", C.cYellow + player + C.cGray + " is now allowed to rejoin. If they are online tell them to relog.")); - caller.sendMessage(F.main("Debug", "There are issues with this and it should not be used outside of the testing environment!")); - }); - } - - @EventHandler(priority = EventPriority.LOW) - public void playerQuit(PlayerQuitEvent event) - { - Player player = event.getPlayer(); - GameTeam team = getGame().GetTeam(player); - - if (team == null) - { - return; - } - - if (!team.IsAlive(player)) - { - return; - } - - if (player.isDead()) - { - return; - } - - if (player.getWorld().getName().equalsIgnoreCase("world")) - { - return; - } - - if (getGame().QuitOut) - { - return; - } - - _data.add(new RejoinPlayerData(player.getName(), player.getHealth(), getGame().GetKit(player), team)); - - NautHashMap location = getGame().GetLocationStore(); - - if (!location.containsKey(player.getName())) - { - location.put(player.getName(), player.getLocation()); - } - - team.RemovePlayer(player); - - // Announcement - getGame().Announce(team.GetColor() + C.Bold + player.getName() + " has disconnected! " + UtilTime.MakeStr(_rejoinTime) + " to rejoin.", false); - } - - @EventHandler(priority = EventPriority.LOWEST) - public void playerRejoinGame(PlayerLoginEvent event) - { - if (!getGame().InProgress() || getGame().QuitOut) - { - return; - } - - Player player = event.getPlayer(); - RejoinPlayerData data = getRejoinPlayerData(player.getName()); - - if (data == null) - { - return; - } - - _data.remove(data); - - GameTeam team = data.getTeam(); - - // Assume they have been revived - if (data.getTeam() == null) - { - team = UtilAlg.Random(getGame().GetTeamList()); - } - - team.AddPlayer(player, true); - getGame().Announce(team.GetColor() + C.Bold + event.getPlayer().getName() + " has reconnected!", false); - - if (data.getKit() != null) - { - getGame().SetKit(player, data.getKit(), false); - } - } - - // Do this on Join, not Login, otherwise player no get heal. - @EventHandler(priority = EventPriority.MONITOR) - public void playerRejoinGame(PlayerJoinEvent event) - { - if (!getGame().InProgress() || getGame().QuitOut) - { - return; - } - - Player player = event.getPlayer(); - RejoinPlayerData data = getRejoinPlayerData(player.getName()); - double health = data.getHealth(); - - if (!(health > 0)) - { - return; - } - - getGame().Manager.runSyncLater(() -> { - player.setHealth(health); - - NautHashMap location = getGame().GetLocationStore(); - - if (location.containsKey(player.getName())) - { - player.teleport(location.get(player.getName())); - } - - }, 1L); - } - - @EventHandler - public void rejoinExpire(UpdateEvent event) - { - if (event.getType() != UpdateType.SEC || getGame().QuitOut) - { - return; - } - - Iterator iterator = _data.iterator(); - - while (iterator.hasNext()) - { - RejoinPlayerData data = iterator.next(); - GameTeam team = data.getTeam(); - - if (!UtilTime.elapsed(data.getTime(), _rejoinTime)) - { - continue; - } - - if (data.getTeam() != null) - { - getGame().Announce(team.GetColor() + C.Bold + data.getName() + " did not reconnect in time!", false); - } - - iterator.remove(); - } - } - - public void setRejoinTime(int time) - { - _rejoinTime = time; - } - - public int getRejoinTime() - { - return _rejoinTime; - } - - public void stopPlayerFromRejoining(String name) - { - Iterator iterator = _data.iterator(); - - while (iterator.hasNext()) - { - RejoinPlayerData data = iterator.next(); - - if (data.getName().equals(name)) - { - iterator.remove(); - } - } - } - - public void stopAllPlayersFromRejoining() - { - getGame().QuitOut = true; - _data.clear(); - } - - public RejoinPlayerData getRejoinPlayerData(String name) - { - for (RejoinPlayerData data : _data) - { - if (data.getName().equals(name)) - { - return data; - } - } - - return null; - } - - public Set getData() - { - return _data; - } - - public class RejoinPlayerData - { - private String _name; - private double _health; - private Kit _kit; - private GameTeam _team; - private long _time; - - public RejoinPlayerData(String name, double health, Kit kit, GameTeam team) - { - _name = name; - _health = health; - _kit = kit; - _team = team; - _time = System.currentTimeMillis(); - } - - public void setHealth(double health) - { - _health = health; - } - - public String getName() - { - return _name; - } - - public double getHealth() - { - return _health; - } - - public Kit getKit() - { - return _kit; - } - - public GameTeam getTeam() - { - return _team; - } - - public long getTime() - { - return _time; - } - } -} \ No newline at end of file diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogModule.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogModule.java deleted file mode 100644 index b88de3562..000000000 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogModule.java +++ /dev/null @@ -1,374 +0,0 @@ -package nautilus.game.arcade.game.modules.combatlog; - -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.UUID; -import java.util.stream.Collectors; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Sound; -import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.event.entity.EntityDeathEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.event.world.ChunkUnloadEvent; - -import mineplex.core.common.util.C; -import mineplex.core.common.util.F; -import mineplex.core.common.util.UtilEvent; -import mineplex.core.common.util.UtilParser; -import mineplex.core.common.util.UtilPlayer; -import mineplex.core.common.util.UtilServer; -import mineplex.core.updater.UpdateType; -import mineplex.core.updater.event.UpdateEvent; - -import nautilus.game.arcade.game.GameTeam; -import nautilus.game.arcade.game.TeamGame; -import nautilus.game.arcade.game.modules.Module; -import nautilus.game.arcade.game.modules.RejoinModule; -import nautilus.game.arcade.game.modules.compass.CompassEntry; -import nautilus.game.arcade.game.modules.compass.CompassModule; - -/** - * This module will spawn combat log NPCs for players who disconnect - */ -public class CombatLogModule extends Module -{ - /** - * Map of UUIDs of players who are now offline, to their CombatLogNPCs - */ - private Map _logoutNpcs = new HashMap<>(); - - /** - * Map of UUIDs of players who are now offline, and who killed them - */ - private Map _killedBy = new HashMap<>(); - - /** - * How long combat log NPCs will stay spawned in for, measured in milliseconds - */ - private int _spawnTime = 60000; - - /** - * Whether to notify the combat logged player on join if they have been killed - */ - private boolean _notifyPlayer = true; - - /** - * Whether to integrate with {@link CompassModule} - */ - private boolean _integrateWithCompassModule = true; - - private int _locationTaskId = -1; - - protected void setup() - { - _locationTaskId = Bukkit.getScheduler().runTaskTimer(getGame().getArcadeManager().getPlugin(), () -> - { - for (CombatLogNPC npc : _logoutNpcs.values()) - { - getGame().GetLocationStore().put(npc.getPlayerInfo().getName(), npc.getNPC().getLocation()); - } - }, 0L, 1L).getTaskId(); - - if (this._integrateWithCompassModule) - { - CompassModule compassModule = getGame().getModule(CompassModule.class); - if (compassModule != null) - { - compassModule.addSupplier(() -> - getAllNPCs() - .stream() - .filter(ent -> - { - if (ent.getNPC() == null) - { - System.out.println("Null npc entity? " + ent.getPlayerInfo().getName() + " " + ent.getPlayerInfo().getUniqueId()); - return false; - } - return true; - }) - .map(npc -> new CompassEntry(npc.getNPC(), npc.getPlayerInfo().getName(), npc.getPlayerInfo().getName() + " (Disconnected)", npc.getPlayerInfo().getTeam(), npc.getPlayerInfo().getKit())) - .collect(Collectors.toList()) - ); - } - else - { - UtilServer.raiseError(new RuntimeException("CompassModule was null but integration was not disabled")); - } - } - } - - public CombatLogModule disableCompassModuleIntegration() - { - this._integrateWithCompassModule = false; - return this; - } - - public CombatLogModule setNotifyPlayer(boolean notifyPlayer) - { - this._notifyPlayer = notifyPlayer; - return this; - } - - public CombatLogModule setCombatLogTime(int time) - { - this._spawnTime = time; - return this; - } - - @EventHandler(priority = EventPriority.LOWEST) - public void on(PlayerQuitEvent event) - { - Player player = event.getPlayer(); - if (getGame().InProgress() && getGame().IsAlive(player)) - { - if (hasLogoutNpc(player)) - return; - - CombatLogNPCPreSpawnEvent preSpawnEvent = new CombatLogNPCPreSpawnEvent(player); - UtilServer.CallEvent(preSpawnEvent); - if (preSpawnEvent.isCancelled()) - return; - - CombatLogNPC npc = new CombatLogNPC(this, player); - _logoutNpcs.put(player.getUniqueId(), npc); - System.out.println(String.format("Spawned combat log NPC for %s!", player.getName())); - } - } - - @EventHandler - public void onJoin(PlayerJoinEvent event) - { - if (hasLogoutNpc(event.getPlayer())) - { - despawnLogoutNpc(event.getPlayer()); - } - - if (_killedBy.containsKey(event.getPlayer().getUniqueId())) - { - String name = _killedBy.remove(event.getPlayer().getUniqueId()); - if (_notifyPlayer && name != null) - { - UtilPlayer.message(event.getPlayer(), F.main("Combat Log", "While you were gone, you were killed by " + ChatColor.GREEN + name + C.mBody + ".")); - } - } - } - - @EventHandler - public void onEntityDeath(EntityDeathEvent event) - { - CombatLogNPC logoutNpc = getLogoutNpc(event.getEntity()); - - if (logoutNpc == null) - return; - - CombatLogNPCKilledEvent npcKilledEvent = new CombatLogNPCKilledEvent(logoutNpc); - UtilServer.CallEvent(npcKilledEvent); - - logoutNpc.despawn(); - _logoutNpcs.remove(logoutNpc.getPlayerInfo().getUniqueId()); - - event.getDrops().clear(); // Clear the entity's item drops. If drops are wanted they should be dropped in the event - - if (logoutNpc.getLastDamager() != null) - { - _killedBy.put(logoutNpc.getPlayerInfo().getUniqueId(), logoutNpc.getLastDamager().getName()); - } - else - { - _killedBy.put(logoutNpc.getPlayerInfo().getUniqueId(), UtilParser.parseDamageCause(logoutNpc.getLastDamageCause())); - } - } - - @EventHandler(priority = EventPriority.LOWEST) - public void onEntityDamaged(EntityDamageEvent event) - { - CombatLogNPC logoutNpc = getLogoutNpc(event.getEntity()); - - if (logoutNpc == null) - return; - - LivingEntity damager = UtilEvent.GetDamagerEntity(event, true); - - Player damagerPlayer = null; - if (damager instanceof Player) - { - damagerPlayer = (Player) damager; - } - - if (getGame() instanceof TeamGame && damagerPlayer != null) - { - GameTeam damagerTeam = getGame().GetTeam(damagerPlayer); - if (damagerTeam == logoutNpc.getPlayerInfo().getTeam()) - { - event.setCancelled(true); - } - } - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void recordDamage(EntityDamageEvent event) - { - CombatLogNPC logoutNpc = getLogoutNpc(event.getEntity()); - - if (logoutNpc == null) - return; - - logoutNpc.getNPC().getWorld().playSound(logoutNpc.getNPC().getLocation(), Sound.HURT_FLESH, 1, 1); - logoutNpc.recordDamage(event); - - getGame().getArcadeManager().runSync(() -> - { - CombatLogNPC npc = getLogoutNpc(event.getEntity()); - if (npc != null) - { - getGame().getModule(RejoinModule.class).getRejoinPlayerData(npc.getPlayerInfo().getName()).setHealth(npc.getNPC().getHealth()); - } - }); - } - - public boolean hasLogoutNpc(Player player) - { - return _logoutNpcs.containsKey(player.getUniqueId()); - } - - public CombatLogNPC getLogoutNpc(Player player) - { - return _logoutNpcs.get(player.getUniqueId()); - } - - public void despawnLogoutNpc(Player player) - { - CombatLogNPC npc = getLogoutNpc(player); - - if (npc != null) - { - npc.despawn(); - _logoutNpcs.remove(player.getUniqueId()); - System.out.println(String.format("Despawned combat log NPC for %s!", player.getName())); - } - } - - @Override - public void cleanup() - { - System.out.println("Killing combat log NPCs"); - - _logoutNpcs.values().forEach(CombatLogNPC::despawn); - - _logoutNpcs.clear(); - _killedBy.clear(); - - Bukkit.getScheduler().cancelTask(_locationTaskId); - } - - @EventHandler - public void onChunkUnload(ChunkUnloadEvent event) - { - for (CombatLogNPC npc : _logoutNpcs.values()) - { - if (npc.getNPC().getLocation().getChunk().equals(event.getChunk())) - { - event.setCancelled(true); - return; - } - } - } - - @EventHandler - public void onUpdate(UpdateEvent event) - { - if (event.getType() == UpdateType.FASTER) - { - _logoutNpcs.values().forEach(CombatLogNPC::update); - } - - if (event.getType() == UpdateType.SEC) - { - Iterator iterator = _logoutNpcs.values().iterator(); - - while (iterator.hasNext()) - { - CombatLogNPC npc = iterator.next(); - - if (npc.getNPC() == null) - { - try - { - new IllegalArgumentException("Strange, the NPC with data " + npc.getPlayerInfo().getName() + " " + npc.getPlayerInfo().getUniqueId() + " was null").printStackTrace(); - } - catch (Throwable t) - { - t.printStackTrace(); - } - finally - { - iterator.remove(); - } - } - else - { - if (Bukkit.getPlayerExact(npc.getPlayerInfo().getName()) != null) - { - // Should never happen - System.out.println("Removing NPC " + npc.getPlayerInfo().getName() + " for 1"); - npc.despawn(); - iterator.remove(); - } - else if (!npc.isAlive()) - { - // Should never happen - System.out.println("Removing NPC " + npc.getPlayerInfo().getName() + " for 2"); - npc.despawn(); - iterator.remove(); - } - else if (npc.getAliveDuation() > this._spawnTime) - { - System.out.println("Removing NPC " + npc.getPlayerInfo().getName() + " for 3"); - CombatLogNPCExpiredEvent expiredEvent = new CombatLogNPCExpiredEvent(npc); - UtilServer.CallEvent(expiredEvent); - npc.despawn(); - iterator.remove(); - } - } - } - } - } - - private CombatLogNPC getLogoutNpc(Entity entity) - { - return getLogoutNpc(entity.getEntityId()); - } - - private CombatLogNPC getLogoutNpc(int entityId) - { - for (CombatLogNPC npc : _logoutNpcs.values()) - { - if (npc.getNPC().getEntityId() == entityId) - { - return npc; - } - } - - return null; - } - - public int getSpawnTime() - { - return _spawnTime; - } - - public Collection getAllNPCs() - { - return _logoutNpcs.values(); - } -} diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogNPC.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogNPC.java deleted file mode 100644 index 974de2053..000000000 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogNPC.java +++ /dev/null @@ -1,177 +0,0 @@ -package nautilus.game.arcade.game.modules.combatlog; - -import net.minecraft.server.v1_8_R3.EntityCreeper; - -import mineplex.core.common.util.UtilEnt; -import mineplex.core.common.util.UtilTime; -import mineplex.core.disguise.DisguiseManager; -import mineplex.core.disguise.disguises.DisguiseBase; -import mineplex.core.disguise.disguises.DisguisePlayer; -import mineplex.core.hologram.Hologram; - -import nautilus.game.arcade.ArcadeManager; - -import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; -import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer; -import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Player; -import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.metadata.FixedMetadataValue; - -public class CombatLogNPC -{ - private PlayerInfo _playerInfo; - - private Hologram _hologram; - - private DisguiseManager _disguiseManager; - private long _spawnDate; - private final long _endingTime; - private double _spawnHealth; - private double _maxHealth; - - private LivingEntity _npc; - private DisguiseBase _disguise; - - private EntityDamageEvent.DamageCause _lastDamageCause; - private Entity _lastDamager; - - CombatLogNPC(CombatLogModule module, Player player) - { - ArcadeManager arcadeManager = module.getGame().getArcadeManager(); - - _playerInfo = new PlayerInfo(player, arcadeManager); - _disguiseManager = arcadeManager.GetDisguise(); - - _spawnDate = System.currentTimeMillis(); - _endingTime = System.currentTimeMillis() + module.getSpawnTime(); - - _hologram = new Hologram(arcadeManager.getHologramManager(), player.getEyeLocation().add(0, 1, 0), "Quitting in " + UtilTime.MakeStr(Math.max(_endingTime - System.currentTimeMillis(), 0))); - _spawnHealth = player.getHealth(); - _maxHealth = player.getMaxHealth(); - _hologram.start(); - _npc = spawnNpc(player); - } - - public void update() - { - _hologram.setText("Quitting in " + UtilTime.MakeStr(Math.max(_endingTime - System.currentTimeMillis(), 0))); - if (_npc != null) - { - _hologram.setLocation(_npc.getEyeLocation().add(0, 1, 0)); - } - } - - /** - * @return true, if the {@code _npc} associated with this CombatLogNPC is - * alive, false otherwise. - */ - public boolean isAlive() - { - return _npc != null && !_npc.isDead(); - } - - /** - * @return the amount of time (in milliseconds) that this npc has been alive - * an spawned in. - */ - public long getAliveDuation() - { - return System.currentTimeMillis() - _spawnDate; - } - - public void despawn() - { - if (_disguise != null) - { - try - { - _disguiseManager.undisguise(_disguise); - _disguise = null; - } - catch (Exception e) - { - e.printStackTrace(); - } - } - if (_npc != null) - { - _npc.remove(); - _npc = null; - } - if (_hologram != null) - { - _hologram.stop(); - _hologram = null; - } - } - - public PlayerInfo getPlayerInfo() - { - return _playerInfo; - } - - public LivingEntity getNPC() - { - return this._npc; - } - - public EntityDamageEvent.DamageCause getLastDamageCause() - { - return _lastDamageCause; - } - - public Entity getLastDamager() - { - return _lastDamager; - } - - private LivingEntity spawnNpc(Player player) - { - Location spawnLoc = player.getLocation(); - EntityCreeper entityCreeper = new EntityCreeper(((CraftWorld) spawnLoc.getWorld()).getHandle()); - LivingEntity skel = (LivingEntity) entityCreeper.getBukkitEntity(); - skel.teleport(spawnLoc); - skel.setRemoveWhenFarAway(false); - skel.setMetadata("CombatLogNPC", new FixedMetadataValue(_disguiseManager.getPlugin(), player.getUniqueId().toString())); - skel.teleport(spawnLoc); - skel.setMaxHealth(_maxHealth); - skel.setHealth(_spawnHealth); - skel.setFallDistance(player.getFallDistance()); - // fixme potion effects, mobs don't target, entity collision (setting to ghost disables arrows and fishing rods), logging while sleeping - // best solution to spawn EntityPlayer? - UtilEnt.vegetate(skel); - UtilEnt.silence(skel, true); - - skel.getEquipment().setHelmet(player.getInventory().getHelmet()); - skel.getEquipment().setChestplate(player.getInventory().getChestplate()); - skel.getEquipment().setLeggings(player.getInventory().getLeggings()); - skel.getEquipment().setBoots(player.getInventory().getBoots()); - skel.getEquipment().setItemInHand(player.getItemInHand()); - - // Disguise - DisguisePlayer disguise = new DisguisePlayer(skel, ((CraftPlayer) player).getHandle().getProfile()); - _disguiseManager.disguise(disguise); - - _disguise = disguise; - - return skel; - } - - void recordDamage(EntityDamageEvent event) - { - this._lastDamageCause = event.getCause(); - if (event instanceof EntityDamageByEntityEvent) - { - EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) event; - this._lastDamager = entityDamageByEntityEvent.getDamager(); - } - else - { - this._lastDamager = null; - } - } -} diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogNPCExpiredEvent.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogNPCExpiredEvent.java deleted file mode 100644 index e26443d32..000000000 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogNPCExpiredEvent.java +++ /dev/null @@ -1,32 +0,0 @@ -package nautilus.game.arcade.game.modules.combatlog; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class CombatLogNPCExpiredEvent extends Event -{ - private static final HandlerList HANDLERS = new HandlerList(); - - private final CombatLogNPC _npc; - - public CombatLogNPCExpiredEvent(CombatLogNPC npc) - { - _npc = npc; - } - - @Override - public HandlerList getHandlers() - { - return HANDLERS; - } - - public static HandlerList getHandlerList() - { - return HANDLERS; - } - - public CombatLogNPC getNpc() - { - return _npc; - } -} diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogNPCKilledEvent.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogNPCKilledEvent.java deleted file mode 100644 index 405b52e09..000000000 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogNPCKilledEvent.java +++ /dev/null @@ -1,32 +0,0 @@ -package nautilus.game.arcade.game.modules.combatlog; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class CombatLogNPCKilledEvent extends Event -{ - private static final HandlerList HANDLERS = new HandlerList(); - - private final CombatLogNPC _npc; - - public CombatLogNPCKilledEvent(CombatLogNPC npc) - { - _npc = npc; - } - - @Override - public HandlerList getHandlers() - { - return HANDLERS; - } - - public static HandlerList getHandlerList() - { - return HANDLERS; - } - - public CombatLogNPC getNpc() - { - return _npc; - } -} diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogNPCPreSpawnEvent.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogNPCPreSpawnEvent.java deleted file mode 100644 index d207dc1e8..000000000 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/CombatLogNPCPreSpawnEvent.java +++ /dev/null @@ -1,48 +0,0 @@ -package nautilus.game.arcade.game.modules.combatlog; - -import org.bukkit.entity.Player; -import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class CombatLogNPCPreSpawnEvent extends Event implements Cancellable -{ - private static final HandlerList HANDLERS = new HandlerList(); - - private final Player _player; - - private boolean _cancelled; - - public CombatLogNPCPreSpawnEvent(Player player) - { - _player = player; - } - - @Override - public HandlerList getHandlers() - { - return HANDLERS; - } - - public static HandlerList getHandlerList() - { - return HANDLERS; - } - - public Player getPlayer() - { - return _player; - } - - @Override - public boolean isCancelled() - { - return _cancelled; - } - - @Override - public void setCancelled(boolean b) - { - _cancelled = b; - } -} diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/PlayerInfo.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/PlayerInfo.java deleted file mode 100644 index f3b1b86b8..000000000 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/combatlog/PlayerInfo.java +++ /dev/null @@ -1,73 +0,0 @@ -package nautilus.game.arcade.game.modules.combatlog; - -import mineplex.core.common.util.UtilInv; -import nautilus.game.arcade.ArcadeManager; -import nautilus.game.arcade.game.GameTeam; -import nautilus.game.arcade.kit.Kit; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.World; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.PlayerInventory; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.UUID; - -public class PlayerInfo -{ - private String _playerName; - private UUID _playerUuid; - private List _items; - private ChatColor _teamColor = ChatColor.GRAY; - private GameTeam _team; - private Kit _kit; - - PlayerInfo(Player player, ArcadeManager arcadeManager) - { - _playerName = player.getName(); - _playerUuid = player.getUniqueId(); - _items = UtilInv.getItems(player); - _team = arcadeManager.GetGame().GetTeam(player); - if (_team != null) - { - _teamColor = _team.GetColor(); - } - _kit = arcadeManager.GetGame().GetKit(player); - } - - public String getName() - { - return _playerName; - } - - public UUID getUniqueId() - { - return _playerUuid; - } - - public ChatColor getTeamColor() - { - return _teamColor; - } - - public GameTeam getTeam() - { - return _team; - } - - public List getItems() - { - return this._items; - } - - public Kit getKit() - { - return this._kit; - } -} diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/rejoin/PlayerRejoinGameEvent.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/rejoin/PlayerRejoinGameEvent.java new file mode 100644 index 000000000..2c614fc22 --- /dev/null +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/rejoin/PlayerRejoinGameEvent.java @@ -0,0 +1,53 @@ +package nautilus.game.arcade.game.modules.rejoin; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; + +import nautilus.game.arcade.game.modules.rejoin.RejoinModule.PlayerGameInfo; + +public class PlayerRejoinGameEvent extends PlayerEvent implements Cancellable +{ + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + public static HandlerList getHandlerList() + { + return HANDLER_LIST; + } + + private final PlayerGameInfo _playerGameInfo; + + private boolean _cancelled; + + PlayerRejoinGameEvent(Player who, PlayerGameInfo playerGameInfo) + { + super(who); + + _playerGameInfo = playerGameInfo; + } + + public PlayerGameInfo getPlayerGameInfo() + { + return _playerGameInfo; + } + + @Override + public void setCancelled(boolean cancelled) + { + _cancelled = cancelled; + } + + @Override + public boolean isCancelled() + { + return _cancelled; + } + + @Override + public HandlerList getHandlers() + { + return HANDLER_LIST; + } +} diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/rejoin/RejoinModule.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/rejoin/RejoinModule.java new file mode 100644 index 000000000..920502f93 --- /dev/null +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/modules/rejoin/RejoinModule.java @@ -0,0 +1,168 @@ +package nautilus.game.arcade.game.modules.rejoin; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerLoginEvent.Result; +import org.bukkit.event.player.PlayerQuitEvent; + +import mineplex.core.common.util.UtilServer; +import mineplex.core.game.region.GameRejoinManager; + +import nautilus.game.arcade.ArcadeManager; +import nautilus.game.arcade.game.GameTeam; +import nautilus.game.arcade.game.modules.Module; +import nautilus.game.arcade.kit.Kit; + +public class RejoinModule extends Module +{ + + private final GameRejoinManager _manager; + private final Map _rejoinData; + + public RejoinModule(ArcadeManager manager) + { + _manager = new GameRejoinManager(manager); + _rejoinData = new HashMap<>(); + + _manager.searchToRejoin(); + } + + @Override + protected void setup() + { + getGame().QuitOut = false; + } + + @Override + public void cleanup() + { + _rejoinData.clear(); + UtilServer.Unregister(_manager); + } + + @EventHandler(priority = EventPriority.LOW) + public void playerQuit(PlayerQuitEvent event) + { + if (!isEnabled()) + { + return; + } + + Player player = event.getPlayer(); + GameTeam team = getGame().GetTeam(player); + + if (team == null || !team.IsAlive(player) || getGame().QuitOut || player.isDead() || !player.getWorld().equals(getGame().WorldData.World)) + { + return; + } + + _rejoinData.put(player.getUniqueId(), new PlayerGameInfo(player.getHealth(), getGame().GetKit(player), team)); + getGame().GetLocationStore().putIfAbsent(player.getName(), player.getLocation()); + + team.RemovePlayer(player); + + _manager.saveRejoinData(player, getGame().GetType().getGameId()); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void playerLogin(PlayerLoginEvent event) + { + if (!isEnabled() || event.getResult() != Result.ALLOWED) + { + return; + } + + Player player = event.getPlayer(); + PlayerGameInfo info = _rejoinData.get(player.getUniqueId()); + + if (info == null) + { + return; + } + + PlayerRejoinGameEvent rejoinEvent = new PlayerRejoinGameEvent(player, info); + UtilServer.CallEvent(rejoinEvent); + + if (rejoinEvent.isCancelled()) + { + info._cancelled = true; + return; + } + + if (info.getTeam() != null) + { + info.getTeam().AddPlayer(player, true); + } + } + + @EventHandler(priority = EventPriority.MONITOR) + public void playerJoin(PlayerJoinEvent event) + { + if (!isEnabled()) + { + return; + } + + Player player = event.getPlayer(); + PlayerGameInfo info = _rejoinData.remove(player.getUniqueId()); + + if (info == null || info.isCancelled()) + { + return; + } + + if (info.getKit() != null) + { + getGame().SetKit(player, info.getKit(), false); + } + + player.setHealth(Math.min(info.getHealth(), player.getHealth())); + } + + private boolean isEnabled() + { + return !getGame().getArcadeManager().GetGameHostManager().isPrivateServer() && getGame().IsLive(); + } + + public class PlayerGameInfo + { + private final double _health; + private final Kit _kit; + private final GameTeam _team; + private boolean _cancelled; + + PlayerGameInfo(double health, Kit kit, GameTeam team) + { + _health = health; + _kit = kit; + _team = team; + } + + public double getHealth() + { + return _health; + } + + public Kit getKit() + { + return _kit; + } + + public GameTeam getTeam() + { + return _team; + } + + public boolean isCancelled() + { + return _cancelled; + } + } +}