Sort servers

This commit is contained in:
Sam 2018-07-27 23:11:19 +01:00 committed by Alexander Meech
parent 064bc2c412
commit 27be29145b
2 changed files with 144 additions and 27 deletions

View File

@ -18,6 +18,7 @@ import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityPortalEnterEvent;
import org.bukkit.event.entity.EntityPortalEvent;
import org.bukkit.event.player.PlayerInteractEvent;
@ -313,6 +314,11 @@ public class ServerManager extends MiniPlugin
@EventHandler
public void playerInteract(PlayerInteractEvent event)
{
if (event.getAction() == Action.PHYSICAL)
{
return;
}
Player player = event.getPlayer();
ItemStack itemStack = player.getItemInHand();
@ -437,7 +443,7 @@ public class ServerManager extends MiniPlugin
gameServer.setInfo(gameInfo);
String prefix = gameServer.getPrefix();
String prefix = serverGroup.getPrefix();
// Special case for MPS. Normally they are all separate ServerGroups however here we want to group them together
if (serverGroup.getHost() != null && !serverGroup.getHost().isEmpty())

View File

@ -7,6 +7,8 @@ import java.util.concurrent.TimeUnit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import mineplex.core.account.CoreClientManager;
@ -20,23 +22,62 @@ import mineplex.core.game.status.GameInfo.GameDisplayStatus;
import mineplex.core.itemstack.ItemBuilder;
import mineplex.core.shop.item.IButton;
import mineplex.core.shop.page.ShopPageBase;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.hub.server.GameServer;
import mineplex.hub.server.ServerManager;
import mineplex.hub.server.ServerManager.Perm;
import mineplex.serverdata.data.MinecraftServer;
import mineplex.serverdata.data.ServerGroup;
public class ServerSelectionPage extends ShopPageBase<ServerManager, ServerSelectionShop>
{
private static final int STARTING_SLOT = 19;
private static final int STARTING_JOIN_SLOT = 19;
private static final int MAX_SHOWN_SERVERS = 7;
private static final int STARTING_PROGRESS_SLOT = 9;
private static final long MAX_VIEWING_TIME = TimeUnit.SECONDS.toMillis(45);
private static final Comparator<GameServer> GAME_SERVER_SORTER = (o1, o2) ->
{
return 0;
// Dev Server
if (o1.isDevServer())
{
return -1;
}
else if (o2.isDevServer())
{
return 1;
}
GameInfo info1 = o1.getInfo(), info2 = o2.getInfo();
// Join-ability
if (info1.getJoinable() != info2.getJoinable())
{
return info1.getJoinable().compareTo(info2.getJoinable());
}
// Status
if (info1.getStatus() != info2.getStatus())
{
return info1.getStatus().compareTo(info2.getStatus());
}
MinecraftServer server1 = o1.getServer(), server2 = o2.getServer();
// Players
if (server1.getPlayerCount() != server2.getPlayerCount())
{
return Integer.compare(server1.getPlayerCount(), server2.getPlayerCount());
}
// Server Number
return Integer.compare(o1.getNumber(), o2.getNumber());
};
private final ServerGroup _serverGroup;
private final long _openedAt;
private final boolean _ownsUltra;
private boolean _showInProgress;
@ -46,33 +87,31 @@ public class ServerSelectionPage extends ShopPageBase<ServerManager, ServerSelec
_serverGroup = serverGroup;
_openedAt = System.currentTimeMillis();
_ownsUltra = clientManager.Get(player).hasPermission(Perm.JOIN_FULL) || donationManager.Get(player).ownsUnknownSalesPackage(serverGroup.getServerType() + " ULTRA");
buildPage();
}
@Override
protected void buildPage()
{
if (UtilTime.elapsed(_openedAt, MAX_VIEWING_TIME))
{
getPlayer().closeInventory();
return;
}
List<GameServer> servers = new ArrayList<>(getPlugin().getServers(_serverGroup.getPrefix()));
servers.sort(GAME_SERVER_SORTER);
if (_showInProgress)
{
// TODO MAKE
buildInProgressPage(servers);
}
else
{
buildMainPage(servers);
buildJoinablePage(servers);
}
}
private void buildMainPage(List<GameServer> servers)
private void buildJoinablePage(List<GameServer> servers)
{
int inProgress = servers.size();
// Filter servers
servers.removeIf(server ->
{
@ -80,6 +119,9 @@ public class ServerSelectionPage extends ShopPageBase<ServerManager, ServerSelec
return status == null || status == GameDisplayStatus.IN_PROGRESS || status == GameDisplayStatus.CLOSING;
});
// Subtracting the new size of the servers list gets us the amount that was removed.
inProgress -= servers.size();
// Instant join
addRow(0, (byte) 3, new ItemBuilder(Material.DIAMOND_BLOCK)
.setTitle(C.cGreenB + "Click To Join Instantly!")
@ -90,19 +132,19 @@ public class ServerSelectionPage extends ShopPageBase<ServerManager, ServerSelec
});
// Public Servers
int slot = STARTING_SLOT;
int slot = STARTING_JOIN_SLOT, devSlot = STARTING_JOIN_SLOT + 9;
for (GameServer server : servers)
{
if (server.isDevServer())
{
addServer(slot++ + 9, server);
addServer(devSlot++, server);
continue;
}
addServer(slot++, server);
if (slot >= STARTING_SLOT + MAX_SHOWN_SERVERS)
if (slot >= STARTING_JOIN_SLOT + MAX_SHOWN_SERVERS)
{
break;
}
@ -110,19 +152,45 @@ public class ServerSelectionPage extends ShopPageBase<ServerManager, ServerSelec
// In Progress Servers
addRow(5, (byte) 4, new ItemBuilder(Material.GOLD_BLOCK)
.setTitle(C.cYellowB + "X Games In Progress")
.setTitle(C.cYellowB + inProgress + " Games In Progress")
.setLore(UtilText.splitLinesToArray(new String[]
{
"",
C.cWhite + C.Line + "Click to Spectate",
"",
C.cGray + "List all servers in the middle of playing. Find a random one to learn how to play better, or find one that your friend might be in."
}, LineFormat.LORE)),
(player, clickType) ->
}, LineFormat.LORE)), new SwapViewButton());
}
private void buildInProgressPage(List<GameServer> servers)
{
_showInProgress = !_showInProgress;
refresh();
// Filter servers
servers.removeIf(server ->
{
GameDisplayStatus status = server.getInfo().getStatus();
return status != GameDisplayStatus.IN_PROGRESS && status != GameDisplayStatus.CLOSING;
});
// Go back
addRow(0, (byte) 7, new ItemBuilder(Material.BED)
.setTitle(C.cGreenB + "Go Back")
.setLore(UtilText.splitLinesToArray(new String[]
{
"",
C.cWhite + C.Line + "Click to Go Back",
}, LineFormat.LORE)), new SwapViewButton());
int slot = STARTING_PROGRESS_SLOT;
for (GameServer server : servers)
{
addServer(slot++, server);
if (slot >= getSize() - 1)
{
break;
}
}
}
private void addRow(int row, byte glassData, ItemBuilder builder, IButton button)
@ -141,6 +209,8 @@ public class ServerSelectionPage extends ShopPageBase<ServerManager, ServerSelec
private void addServer(int slot, GameServer server)
{
GameInfo info = server.getInfo();
// Subtract 2 from the timer because of the delay of the MOTD updating and retrieval
int timerAdjust = info.getTimer() - 2;
Material material;
byte data = 0;
String votingColour = null, votingStatus = null, votingOn = null;
@ -162,13 +232,13 @@ public class ServerSelectionPage extends ShopPageBase<ServerManager, ServerSelec
data = (byte) 5;
votingColour = C.cYellow;
votingStatus = "Voting Is In Progress";
motd = "Voting Ends in " + timeString(info.getTimer());
motd = "Voting Ends in " + timeString(timerAdjust);
break;
case STARTING:
material = Material.EMERALD_BLOCK;
votingColour = C.cGreen;
votingStatus = "Voting Already Finished!";
motd = "Starting in " + timeString(info.getTimer());
motd = "Starting in " + timeString(timerAdjust);
break;
case IN_PROGRESS:
material = Material.GOLD_BLOCK;
@ -189,7 +259,7 @@ public class ServerSelectionPage extends ShopPageBase<ServerManager, ServerSelec
votingOn = "Map";
}
ItemBuilder builder = new ItemBuilder(material, data)
ItemBuilder builder = new ItemBuilder(material, Math.max(1, Math.min(64, server.getServer().getPlayerCount())), data)
.setTitle(C.cGreenB + _serverGroup.getServerNpcName() + " Server " + server.getNumber())
.addLore("");
@ -255,23 +325,64 @@ public class ServerSelectionPage extends ShopPageBase<ServerManager, ServerSelec
footer = "Click to Join!";
break;
case RANKS_ONLY:
footer = "ULTRA+ can join";
if (_ownsUltra)
{
footer = "You have ULTRA! Click to Join!";
}
else
{
footer = "ULTRA+ can join. Visit mineplex.com/shop";
}
break;
default:
footer = "Closed";
builder.setType(Material.REDSTONE_BLOCK);
footer = "Game is full!";
break;
}
builder.addLore(C.cWhite + C.Line + footer);
if (server.isDevServer())
{
builder.setType(Material.COMMAND);
builder.addLore(
C.cRed + "This is a server run by a",
C.cRed + "developer. It may contain unreleased content."
);
}
addButton(slot, builder.build(), (player, clickType) ->
{
// TODO select server
});
}
private class SwapViewButton implements IButton
{
@Override
public void onClick(Player player, ClickType clickType)
{
_showInProgress = !_showInProgress;
refresh();
playAcceptSound(player);
}
}
@EventHandler
public void closeIfIdle(UpdateEvent event)
{
if (event.getType() != UpdateType.SLOW || !UtilTime.elapsed(_openedAt, MAX_VIEWING_TIME))
{
return;
}
getPlayer().closeInventory();
}
private String timeString(int time)
{
time = Math.max(0, time);
return time + " Second" + (time == 1 ? "" : "s") + "...";
}
}