Mineplex2018-withcommit/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/ClansTransferManager.java

283 lines
7.6 KiB
Java
Raw Normal View History

2016-07-25 16:55:18 +02:00
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.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;
2016-07-27 04:32:46 +02:00
/**
* Server selection controller for clans
*/
2016-07-25 16:55:18 +02:00
public class ClansTransferManager extends MiniDbClientPlugin<SimpleClanToken>
{
private static final long SERVER_RELOAD_INTERVAL = 5000;
private PartyManager _party;
private Portal _portal;
private HubManager _hub;
private Region _region;
private final HashMap<MinecraftServer, ServerInfo> _servers = new HashMap<MinecraftServer, ServerInfo>();
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);
}
2016-07-27 04:32:46 +02:00
/**
* Gets the stored party manager
* @return The stored party manager
*/
2016-07-25 16:55:18 +02:00
public PartyManager getPartyManager()
{
return _party;
}
2016-07-27 04:32:46 +02:00
/**
* Gets a list of all loaded servers
* @return A list of all loaded servers
*/
2016-07-25 16:55:18 +02:00
public List<ServerInfo> getServers()
{
List<ServerInfo> servers = Lists.newArrayList();
servers.addAll(_servers.values());
return servers;
}
2016-07-27 04:32:46 +02:00
/**
* 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
*/
2016-07-25 16:55:18 +02:00
public ServerInfo getServer(String name)
{
for (ServerInfo server : _servers.values())
{
if (server.Name.equalsIgnoreCase(name))
{
return server;
}
}
return null;
}
2016-07-27 04:32:46 +02:00
/**
* Pulls all the clans servers from redis and loads them. SHOULD BE RUN ASYNC
*/
2016-07-25 16:55:18 +02:00
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);
}
}
2016-07-27 04:32:46 +02:00
/**
* Selects a server to send a player to
* @param player The player to send
* @param serverInfo The server to send the player to
*/
2016-07-25 16:55:18 +02:00
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 (!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();
}
}