forked from EnigmaticaModpacks/Enigmatica2Expert
-
Notifications
You must be signed in to change notification settings - Fork 50
/
restart_server.zs
107 lines (90 loc) · 3.38 KB
/
restart_server.zs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import crafttweaker.player.IPlayer;
import crafttweaker.world.IWorld;
import mods.zenutils.command.CommandUtils;
import mods.zenutils.command.IGetTabCompletion;
import mods.zenutils.command.ZenCommand;
import mods.zenutils.I18n;
import mods.zenutils.StringList;
static playerPending as StringList = StringList.empty();
static inProcess as string[string] = {};
val cmd as ZenCommand = ZenCommand.create("restart_server");
cmd.getCommandUsage = function(sender) {return "commands.restart_server.usage";};
function sendSingle(player as IPlayer, key as string, substr as string = null) as void {
val langKey = game.localize("commands.restart_server."~key);
if (isNull(substr)) return player.sendRichTextMessage(crafttweaker.text.ITextComponent.fromTranslation(langKey));
player.sendRichTextMessage(crafttweaker.text.ITextComponent.fromTranslation(langKey, substr));
}
function send(key as string, mode as string = 'everyone', substr as string = null) as void {
for player in server.players {
if (mode=='pending' && !playerPending.contains(player.uuid)) continue;
if (mode=='unpending' && playerPending.contains(player.uuid)) continue;
sendSingle(player, key, substr);
}
}
function stopWithDelay(world as IWorld) as void {
inProcess['restart'] = 'true';
send('delay');
world.catenation().sleep(60).then(function(world) {
// send('stopping');
server.commandManager.executeCommand(server, '/say §8[§6ø§8]§r §4Server stopping by vote...');
server.commandManager.executeCommand(server, '/stop');
}).start();
}
function pendingCount() as int {
var k = 0;
for s in playerPending {
k += 1;
}
return k;
}
function getPlayersList(isVoted as bool = false) as string {
var list = '';
for p in server.players {
if(playerPending.contains(p.uuid) == isVoted)
list ~= (list!=''?', ':'') ~ '§5'~ p.name ~'§r';
}
return list;
}
function clearPending() as void {
for s in playerPending { playerPending.remove(s); }
}
function cancelVoting() as void {
if(!isNull(inProcess.restart)) return;
send('failed', 'pending', getPlayersList());
send('cancelled', 'unpending');
clearPending();
}
function checkComplete() as bool {
for p in server.players {
if (!playerPending.contains(p.uuid)) return false;
}
return true;
}
cmd.requiredPermissionLevel = 0; // require no permission, everyone can execute the command.
cmd.execute = function(command, server, sender, args) {
val player = CommandUtils.getCommandSenderAsPlayer(sender);
// Server already restarting
if(!isNull(inProcess.restart)) return sendSingle(player, 'in_process');
// Only one player - stop right now
if (server.playerCount == 1) return stopWithDelay(player.world);
if (pendingCount() == 0) {
// We are first player who activated
playerPending.add(player.uuid);
send('query', 'unpending', player.name);
player.world.catenation().sleep(300).then(function(world) { cancelVoting(); }).start();
} else if (playerPending.contains(player.uuid)) {
// We are already waiting for vote
sendSingle(player, 'already_voted', getPlayersList());
} else {
playerPending.add(player.uuid);
if (checkComplete()) {
// Everyone voted
stopWithDelay(player.world);
} else {
// New player added in vote list
send('awaiting', 'pending', getPlayersList(false));
send('query', 'unpending', getPlayersList(true));
}
}
};
cmd.register();