package mineplex.clanshub; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import mineplex.core.MiniDbClientPlugin; import mineplex.core.account.CoreClientManager; import mineplex.core.common.util.F; import mineplex.core.common.util.UtilAction; import mineplex.core.common.util.UtilAlg; import mineplex.core.common.util.UtilPlayer; import mineplex.core.common.util.UtilTime; import mineplex.core.donation.DonationManager; import mineplex.core.npc.event.NpcDamageByEntityEvent; import mineplex.core.npc.event.NpcInteractEntityEvent; import mineplex.core.party.Lang; import mineplex.core.party.Party; import mineplex.core.party.PartyManager; import mineplex.core.portal.Portal; import mineplex.core.recharge.Recharge; import mineplex.core.shop.page.ShopPageBase; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; import mineplex.game.clans.core.repository.tokens.SimpleClanToken; import mineplex.serverdata.Region; import mineplex.serverdata.data.MinecraftServer; import mineplex.serverdata.servers.ServerManager; import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.EntityPortalEnterEvent; import org.bukkit.plugin.java.JavaPlugin; import com.google.common.collect.Lists; /** * Server selection controller for clans */ public class ClansTransferManager extends MiniDbClientPlugin { private static final long SERVER_RELOAD_INTERVAL = 5000; private PartyManager _party; private Portal _portal; private HubManager _hub; private Region _region; private final HashMap _servers = new HashMap(); private boolean _loading = false; private long _lastLoaded; private ClansServerShop _serverShop; public ClansTransferManager(JavaPlugin plugin, CoreClientManager client, DonationManager donation, PartyManager party, Portal portal, HubManager hub) { super("Server Transfer", plugin, client); _party = party; _portal = portal; _hub = hub; _region = plugin.getConfig().getBoolean("serverstatus.us") ? Region.US : Region.EU; _serverShop = new ClansServerShop(this, client, donation); } /** * Gets the stored party manager * @return The stored party manager */ public PartyManager getPartyManager() { return _party; } /** * Gets a list of all loaded servers * @return A list of all loaded servers */ public List getServers() { List servers = Lists.newArrayList(); servers.addAll(_servers.values()); return servers; } /** * Gets the loaded ServerInfo with the given name * @param name The name to check * @return The loaded ServerInfo, or null if it is not stored */ public ServerInfo getServer(String name) { for (ServerInfo server : _servers.values()) { if (server.Name.equalsIgnoreCase(name)) { return server; } } return null; } /** * Pulls all the clans servers from redis and loads them. SHOULD BE RUN ASYNC */ public void reload() { _servers.clear(); for (MinecraftServer server : ServerManager.getServerRepository(_region).getServerStatusesByPrefix("Clans-")) { ServerInfo info = new ServerInfo(); info.Name = server.getName(); info.CurrentPlayers = server.getPlayerCount(); info.MaxPlayers = server.getMaxPlayerCount(); _servers.put(server, info); } } /** * Selects a server to send a player to * @param player The player to send * @param serverInfo The server to send the player to */ public void selectServer(Player player, ServerInfo serverInfo) { Party party = _party.getParty(player); if (party != null) { if(!party.getOwner().equalsIgnoreCase(player.getName())) { Lang.NOT_OWNER_SERVER.send(player); return; } _party.getJoinManager().requestServerJoin(serverInfo.Name, party); return; } player.leaveVehicle(); player.eject(); _portal.sendPlayerToServer(player, serverInfo.Name); } @EventHandler public void reloadServers(UpdateEvent event) { if (event.getType() != UpdateType.SEC || _loading || !UtilTime.elapsed(_lastLoaded, SERVER_RELOAD_INTERVAL)) { return; } _loading = true; final Runnable after = new Runnable() { public void run() { _lastLoaded = System.currentTimeMillis(); _loading = false; } }; runAsync(() -> { reload(); runSync(after); }); } @SuppressWarnings("rawtypes") @EventHandler public void refreshPages(UpdateEvent event) { if (event.getType() != UpdateType.SEC) return; for (ShopPageBase page : _serverShop.getPageMap().values()) { if (page instanceof ClansServerPage) { ((ClansServerPage)page).update(); } } } @EventHandler public void onEnterPortal(EntityPortalEnterEvent event) { if (!(event.getEntity() instanceof Player)) { UtilAction.velocity(event.getEntity(), UtilAlg.getTrajectory(event.getEntity().getLocation(), _hub.GetSpawn()), 1, true, 0.5, 0, 1.0, true); return; } Player player = (Player) event.getEntity(); if (!_hub.CanPortal(player)) { UtilAction.velocity(player, UtilAlg.getTrajectory(player.getLocation(), _hub.GetSpawn()), 1, true, 0.5, 0, 1.0, true); return; } if (_party.getParty(player) != null) { player.playSound(player.getLocation(), Sound.ITEM_BREAK, 1, .6f); player.sendMessage(F.main("Party", "You cannot join Clans while in a party.")); UtilAction.velocity(player, UtilAlg.getTrajectory(player.getLocation(), _hub.GetSpawn()), 1, true, 0.5, 0, 1.0, true); return; } if (!Recharge.Instance.use(player, "Transfer Portal", 5000, false, false)) { return; } if (Get(player) == null || Get(player).getHomeServer() == null || Get(player).getHomeServer().isEmpty()) { player.teleport(_hub.GetSpawn()); runSyncLater(() -> { _serverShop.attemptShopOpen(player); }, 20L); return; } if (getServer(Get(player).getHomeServer()) != null) { UtilPlayer.message(player, F.main(getName(), "You are being transferred to your Clan's home server!")); selectServer(player, getServer(Get(player).getHomeServer())); } else { UtilPlayer.message(player, F.main(getName(), "Your Clan's home server is not online!")); } } @EventHandler public void onUseNPC(NpcInteractEntityEvent event) { if (event.getNpc().getName().contains("Clans")) { _serverShop.attemptShopOpen(event.getPlayer()); } if (event.getNpc().getName().contains("Return")) { _portal.sendToHub(event.getPlayer(), "Returning to Mineplex!"); } } @EventHandler public void onUseNPC(NpcDamageByEntityEvent event) { if (!(event.getDamager() instanceof Player)) { return; } Player player = (Player) event.getDamager(); if (event.getNpc().getName().contains("Clans") && Recharge.Instance.use(player, "Go to Clans", 1000, false, false)) { _serverShop.attemptShopOpen(player); } if (event.getNpc().getName().contains("Return") && Recharge.Instance.use(player, "Return to Mineplex", 1000, false, false)) { _portal.sendToHub(player, "Returning to Mineplex!"); } } @Override public String getQuery(int accountId, String uuid, String name) { return "SELECT clans.name, accountClan.clanRole, clanServer.serverName, clans.id FROM accountClan INNER JOIN clans ON clans.id = accountClan.clanId INNER JOIN clanServer ON clans.serverId = clanServer.id WHERE accountClan.accountId = " + accountId + ";"; } @Override public void processLoginResultSet(String playerName, int accountId, ResultSet resultSet) throws SQLException { SimpleClanToken clanToken = new SimpleClanToken(); while (resultSet.next()) { String clanName = resultSet.getString(1); String clanRole = resultSet.getString(2); String homeServer = resultSet.getString(3); int clanId = resultSet.getInt(4); clanToken = new SimpleClanToken(clanName, clanRole, homeServer, clanId); } Set(playerName, clanToken); } @Override protected SimpleClanToken addPlayer(String player) { return new SimpleClanToken(); } }