Skip to content

Commit 8b83a3b

Browse files
committed
Added global method for kick message for maintenance
1 parent 1548883 commit 8b83a3b

File tree

2 files changed

+57
-127
lines changed

2 files changed

+57
-127
lines changed

src/main/java/dev/lars/utilsmanager/features/maintenance/MaintenanceListener.java

Lines changed: 2 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -17,135 +17,11 @@ public class MaintenanceListener implements Listener {
1717
@EventHandler
1818
public void onLogin(PlayerLoginEvent event) {
1919
Player player = event.getPlayer();
20-
if (player.hasPermission("utilsmanager.maintenance")) return;
2120
if (player.isOp()) return;
2221
if (RankAPI.getApi().getRankId(player) >= 8) return;
2322

2423
if (ServerSettingsAPI.getApi().isMaintenanceEnabled()) {
25-
26-
Instant maintenanceEnd = ServerSettingsAPI.getApi().getMaintenanceEstimatedEnd();
27-
28-
if (maintenanceEnd == null) {
29-
Component noEnd = Component.text("There is no ending available");
30-
}
31-
long now = System.currentTimeMillis();
32-
33-
long maintenanceTime = (maintenanceEnd.compareTo(Instant.now())) / 1000;
34-
35-
if (maintenanceTime < 0) maintenanceTime *= -1;
36-
37-
int seconds = (int) (maintenanceTime % 60);
38-
int minutes = (int) ((maintenanceTime / 60) % 60);
39-
int hours = (int) ((maintenanceTime / 3600) % 24);
40-
int days = (int) (maintenanceTime / 86400);
41-
42-
Component formattedTime = Component.text(
43-
String.format("%02dd %02dh %02dm %02ds", days, hours, minutes, seconds),
44-
NamedTextColor.GOLD
45-
);
46-
Component formattedTimeNoDay = Component.text(
47-
String.format("%02dh %02dm %02ds", hours, minutes, seconds),
48-
NamedTextColor.GOLD
49-
);
50-
Component formattedTimeNoHour = Component.text(
51-
String.format("%02dm %02ds", minutes, seconds),
52-
NamedTextColor.GOLD
53-
);
54-
55-
if (maintenanceTime > 0) {
56-
if (LanguageAPI.getApi().getLanguage(player) == 2) {
57-
if ((maintenanceTime / 86400) != 0) {
58-
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Component.text("Wir entschuldigen uns aber der Server ist derzeit in Wartung! \n", NamedTextColor.RED)
59-
.append(Component.text("Grund: ", NamedTextColor.WHITE))
60-
.append(Component.text(ServerSettingsAPI.getApi().getMaintenanceReason() + "\n", NamedTextColor.BLUE))
61-
.append(Component.text("Voraussichtlich Verbliebende Zeit: ", NamedTextColor.WHITE))
62-
.append(formattedTime));
63-
} else {
64-
if ((maintenanceTime / 3600) == 0) {
65-
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Component.text("Wir entschuldigen uns aber der Server ist derzeit in Wartung! \n", NamedTextColor.RED)
66-
.append(Component.text("Grund: ", NamedTextColor.WHITE))
67-
.append(Component.text(ServerSettingsAPI.getApi().getMaintenanceReason() + "\n", NamedTextColor.BLUE))
68-
.append(Component.text("Voraussichtlich Verbliebende Zeit: ", NamedTextColor.WHITE))
69-
.append(formattedTimeNoHour));
70-
} else {
71-
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Component.text("Wir entschuldigen uns aber der Server ist derzeit in Wartung! \n", NamedTextColor.RED)
72-
.append(Component.text("Grund: ", NamedTextColor.WHITE))
73-
.append(Component.text(ServerSettingsAPI.getApi().getMaintenanceReason() + "\n", NamedTextColor.BLUE))
74-
.append(Component.text("Voraussichtlich Verbliebende Zeit: ", NamedTextColor.WHITE))
75-
.append(formattedTimeNoDay));
76-
}
77-
}
78-
} else {
79-
if ((maintenanceTime / 86400) != 0) {
80-
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Component.text("We apologize but the server is currently under maintenance! \n", NamedTextColor.RED)
81-
.append(Component.text("Reason: ", NamedTextColor.WHITE))
82-
.append(Component.text(ServerSettingsAPI.getApi().getMaintenanceReason() + "\n", NamedTextColor.BLUE))
83-
.append(Component.text("Estimated Remaining Time: ", NamedTextColor.WHITE))
84-
.append(formattedTime));
85-
} else {
86-
if ((maintenanceTime / 3600) == 0) {
87-
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Component.text("We apologize but the server is currently under maintenance! \n", NamedTextColor.RED)
88-
.append(Component.text("Reason: ", NamedTextColor.WHITE))
89-
.append(Component.text(ServerSettingsAPI.getApi().getMaintenanceReason() + "\n", NamedTextColor.BLUE))
90-
.append(Component.text("Estimated Remaining Time: ", NamedTextColor.WHITE))
91-
.append(formattedTimeNoHour));
92-
} else {
93-
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Component.text("We apologize but the server is currently under maintenance! \n", NamedTextColor.RED)
94-
.append(Component.text("Reason: ", NamedTextColor.WHITE))
95-
.append(Component.text(ServerSettingsAPI.getApi().getMaintenanceReason() + "\n", NamedTextColor.BLUE))
96-
.append(Component.text("Estimated Remaining Time: ", NamedTextColor.WHITE))
97-
.append(formattedTimeNoDay));
98-
}
99-
}
100-
}
101-
} else {
102-
if (LanguageAPI.getApi().getLanguage(player) == 2) {
103-
if ((maintenanceTime / 86400) != 0) {
104-
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Component.text("Wir entschuldigen uns aber der Server ist derzeit in Wartung! \n", NamedTextColor.RED)
105-
.append(Component.text("Grund: ", NamedTextColor.WHITE))
106-
.append(Component.text(ServerSettingsAPI.getApi().getMaintenanceReason() + "\n", NamedTextColor.BLUE))
107-
.append(Component.text("Überschrittene Voraussichtliche Zeit: ", NamedTextColor.WHITE))
108-
.append(formattedTime));
109-
} else {
110-
if ((maintenanceTime / 3600) == 0) {
111-
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Component.text("Wir entschuldigen uns aber der Server ist derzeit in Wartung! \n", NamedTextColor.RED)
112-
.append(Component.text("Grund: ", NamedTextColor.WHITE))
113-
.append(Component.text(ServerSettingsAPI.getApi().getMaintenanceReason() + "\n", NamedTextColor.BLUE))
114-
.append(Component.text("Überschrittene Voraussichtliche Zeit: ", NamedTextColor.WHITE))
115-
.append(formattedTimeNoHour));
116-
} else {
117-
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Component.text("Wir entschuldigen uns aber der Server ist derzeit in Wartung! \n", NamedTextColor.RED)
118-
.append(Component.text("Grund: ", NamedTextColor.WHITE))
119-
.append(Component.text(ServerSettingsAPI.getApi().getMaintenanceReason() + "\n", NamedTextColor.BLUE))
120-
.append(Component.text("Überschrittene Voraussichtliche Zeit: ", NamedTextColor.WHITE))
121-
.append(formattedTimeNoDay));
122-
}
123-
}
124-
} else {
125-
if ((maintenanceTime / 86400) != 0) {
126-
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Component.text("We apologize but the server is currently under maintenance! \n", NamedTextColor.RED)
127-
.append(Component.text("Reason: ", NamedTextColor.WHITE))
128-
.append(Component.text(ServerSettingsAPI.getApi().getMaintenanceReason() + "\n", NamedTextColor.BLUE))
129-
.append(Component.text("Exceeded Estimated Time: ", NamedTextColor.WHITE))
130-
.append(formattedTime));
131-
} else {
132-
if ((maintenanceTime / 3600) == 0) {
133-
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Component.text("We apologize but the server is currently under maintenance! \n", NamedTextColor.RED)
134-
.append(Component.text("Reason: ", NamedTextColor.WHITE))
135-
.append(Component.text(ServerSettingsAPI.getApi().getMaintenanceReason() + "\n", NamedTextColor.BLUE))
136-
.append(Component.text("Exceeded Estimated Time: ", NamedTextColor.WHITE))
137-
.append(formattedTimeNoHour));
138-
} else {
139-
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, Component.text("We apologize but the server is currently under maintenance! \n", NamedTextColor.RED)
140-
.append(Component.text("Reason: ", NamedTextColor.WHITE))
141-
.append(Component.text(ServerSettingsAPI.getApi().getMaintenanceReason() + "\n", NamedTextColor.BLUE))
142-
.append(Component.text("Exceeded Estimated Time: ", NamedTextColor.WHITE))
143-
.append(formattedTimeNoDay));
144-
}
145-
}
146-
}
147-
}
24+
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, MaintenanceManager.kickMessage(player));
14825
}
14926
}
150-
151-
}
27+
}

src/main/java/dev/lars/utilsmanager/features/maintenance/MaintenanceManager.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,58 @@ public MaintenanceManager() {
207207
}
208208
}, 20, 20);
209209
}
210-
}
210+
211+
public static Component kickMessage(Player player) {
212+
boolean german = LanguageAPI.getApi().getLanguage(player) == 2;
213+
214+
Component message = Component.text(
215+
german
216+
? "Wir entschuldigen uns, aber der Server ist derzeit in Wartung!\n"
217+
: "We apologize but the server is currently under maintenance!\n",
218+
NamedTextColor.RED
219+
);
220+
221+
String reason = ServerSettingsAPI.getApi().getMaintenanceReason();
222+
if (reason != null && !reason.isBlank()) {
223+
message = message
224+
.append(Component.text(
225+
german ? "Grund: " : "Reason: ",
226+
NamedTextColor.WHITE
227+
))
228+
.append(Component.text(reason + "\n", NamedTextColor.BLUE));
229+
}
230+
231+
Instant now = Instant.now();
232+
Instant estimatedEndTime = ServerSettingsAPI.getApi().getMaintenanceEstimatedEnd();
233+
Instant deadline = ServerSettingsAPI.getApi().getMaintenanceDeadline();
234+
Component timeComponent = null;
235+
236+
if (estimatedEndTime != null && estimatedEndTime.isAfter(now)) {
237+
long seconds = Duration.between(now, estimatedEndTime).getSeconds();
238+
timeComponent = FormatNumbers.formatDuration(seconds);
239+
240+
message = message.append(Component.text(
241+
german
242+
? "Voraussichtlich verbleibende Zeit: "
243+
: "Estimated remaining time: ",
244+
NamedTextColor.WHITE
245+
));
246+
} else if (deadline != null && deadline.isAfter(now)) {
247+
long seconds = Duration.between(now, deadline).getSeconds();
248+
timeComponent = FormatNumbers.formatDuration(seconds);
249+
250+
message = message.append(Component.text(
251+
german
252+
? "Endet in: "
253+
: "Ends in: ",
254+
NamedTextColor.WHITE
255+
));
256+
}
257+
258+
if (timeComponent != null) {
259+
message = message.append(timeComponent);
260+
}
261+
262+
return message;
263+
}
264+
}

0 commit comments

Comments
 (0)