This repository was archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathCloudServer.java
More file actions
650 lines (552 loc) · 24.3 KB
/
Copy pathCloudServer.java
File metadata and controls
650 lines (552 loc) · 24.3 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
* Copyright (c) Tarek Hosni El Alaoui 2017
*/
package de.dytanic.cloudnet.bridge;
import com.google.gson.reflect.TypeToken;
import de.dytanic.cloudnet.api.CloudAPI;
import de.dytanic.cloudnet.api.ICloudService;
import de.dytanic.cloudnet.api.handlers.NetworkHandler;
import de.dytanic.cloudnet.api.network.packet.out.PacketOutUpdateServerInfo;
import de.dytanic.cloudnet.api.player.PlayerExecutorBridge;
import de.dytanic.cloudnet.bridge.event.bukkit.*;
import de.dytanic.cloudnet.bridge.internal.util.ReflectionUtil;
import de.dytanic.cloudnet.lib.CloudNetwork;
import de.dytanic.cloudnet.lib.NetworkUtils;
import de.dytanic.cloudnet.lib.player.CloudPlayer;
import de.dytanic.cloudnet.lib.player.OfflinePlayer;
import de.dytanic.cloudnet.lib.player.permission.PermissionGroup;
import de.dytanic.cloudnet.lib.server.ServerConfig;
import de.dytanic.cloudnet.lib.server.ServerProcessMeta;
import de.dytanic.cloudnet.lib.server.ServerState;
import de.dytanic.cloudnet.lib.server.SimpleServerGroup;
import de.dytanic.cloudnet.lib.server.info.ProxyInfo;
import de.dytanic.cloudnet.lib.server.info.ServerInfo;
import de.dytanic.cloudnet.lib.server.template.Template;
import de.dytanic.cloudnet.lib.utility.Acceptable;
import de.dytanic.cloudnet.lib.utility.CollectionWrapper;
import de.dytanic.cloudnet.lib.utility.document.Document;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.entity.Player;
import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.Team;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
/**
* Cloud-Server represents
*/
public class CloudServer implements ICloudService {
private static CloudServer instance;
private BukkitBootstrap bukkitBootstrap;
private Map<UUID, CloudPlayer> cloudPlayers = NetworkUtils.newConcurrentHashMap();
/*=================================================*/
private int maxPlayers;
private String motd;
private String hostAdress;
private int port;
private ServerState serverState;
private ServerConfig serverConfig;
private Template template;
private int memory;
private boolean allowAutoStart = true;
/*=================================================*/
public CloudServer(BukkitBootstrap bukkitBootstrap, CloudAPI cloudAPI) {
instance = this;
cloudAPI.setCloudService(this);
this.bukkitBootstrap = bukkitBootstrap;
ServerInfo serverInfo = cloudAPI.getConfig().getObject("serverInfo", new TypeToken<ServerInfo>() {}.getType());
cloudAPI.getNetworkHandlerProvider().registerHandler(new NetworkHandlerImpl());
this.allowAutoStart = !cloudAPI.getConfig().contains("cloudProcess");
this.maxPlayers = serverInfo.getMaxPlayers();
this.motd = serverInfo.getMotd();
this.hostAdress = serverInfo.getHost();
this.port = serverInfo.getPort();
this.serverConfig = serverInfo.getServerConfig();
this.memory = serverInfo.getMemory();
this.template = serverInfo.getTemplate();
this.serverState = ServerState.LOBBY;
}
/**
* Returns the instance from the CloudServer
*
* @return the cloud server instance
*/
public static CloudServer getInstance() {
return instance;
}
public void updateDisable() {
List<String> list = new CopyOnWriteArrayList<>();
for (Player all : Bukkit.getOnlinePlayers()) {
list.add(all.getName());
}
ServerInfo serverInfo = new ServerInfo(CloudAPI.getInstance().getServiceId(),
hostAdress,
port,
false,
list,
memory,
motd,
Bukkit.getOnlinePlayers().size(),
maxPlayers,
serverState,
serverConfig,
template);
CloudAPI.getInstance().getNetworkConnection().sendPacketSynchronized(new PacketOutUpdateServerInfo(serverInfo));
}
/**
* Updates the ServerInfo on a asynchronized BukkitScheduler Task
*/
public void updateAsync() {
bukkitBootstrap.getServer().getScheduler().runTaskAsynchronously(bukkitBootstrap, new Runnable() {
@Override
public void run() {
List<String> list = new CopyOnWriteArrayList<>();
for (Player all : Bukkit.getOnlinePlayers()) {
list.add(all.getName());
}
ServerInfo serverInfo = new ServerInfo(CloudAPI.getInstance().getServiceId(),
hostAdress,
port,
true,
list,
memory,
motd,
Bukkit.getOnlinePlayers().size(),
maxPlayers,
serverState,
serverConfig,
template);
CloudAPI.getInstance().update(serverInfo);
}
});
}
/**
* Changed the State to INGAME and Start a gameserver
*/
public void changeToIngame() {
setServerState(ServerState.INGAME);
if (isAllowAutoStart()) {
SimpleServerGroup simpleServerGroup = CloudAPI.getInstance().getServerGroupData(CloudAPI.getInstance().getGroup());
CloudAPI.getInstance().startGameServer(simpleServerGroup, template);
setAllowAutoStart(false);
Bukkit.getScheduler().runTaskLater(bukkitBootstrap, new Runnable() {
@Override
public void run() {
setAllowAutoStart(true);
}
}, 6000);
}
update();
}
/**
* Checks if this instance can starting game servers auto
*
* @return whether this server automatically starts servers
*/
public boolean isAllowAutoStart() {
return allowAutoStart;
}
/**
* Updates the ServerInfo
*/
public void update() {
List<String> list = new CopyOnWriteArrayList<>();
for (Player all : Bukkit.getOnlinePlayers()) {
list.add(all.getName());
}
ServerInfo serverInfo = new ServerInfo(CloudAPI.getInstance().getServiceId(),
hostAdress,
port,
true,
list,
memory,
motd,
Bukkit.getOnlinePlayers().size(),
maxPlayers,
serverState,
serverConfig,
template);
CloudAPI.getInstance().update(serverInfo);
}
/**
* You can disable the Autostart funtction from this server
*
* @param allowAutoStart whether this server automatically starts servers
*/
public void setAllowAutoStart(boolean allowAutoStart) {
this.allowAutoStart = allowAutoStart;
}
@Deprecated
public void getPlayerAndCache(UUID uniqueId) {
CloudPlayer cloudPlayer = CloudAPI.getInstance().getOnlinePlayer(uniqueId);
if (cloudPlayer != null) {
cloudPlayer.setPlayerExecutor(new PlayerExecutorBridge());
this.cloudPlayers.put(uniqueId, cloudPlayer);
}
}
public void setServerStateAndUpdate(ServerState serverStateAndUpdate) {
this.serverState = serverStateAndUpdate;
update();
}
public int getPort() {
return port;
}
public String getHostAdress() {
return hostAdress;
}
/**
* Returns the serverConfig from this instance
*
* @return the current server configuration
*/
public ServerConfig getServerConfig() {
return serverConfig;
}
/**
* Sets the serverConfig in a new default style
*
* @param serverConfig the new server configuration
*/
public void setServerConfig(ServerConfig serverConfig) {
this.serverConfig = serverConfig;
}
/**
* Returns the ServerState from this instance
*
* @return the current server state
*/
public ServerState getServerState() {
return serverState;
}
/**
* Set the serverState INGAME, LOBBY, OFFLINE for switching Signs or your API thinks
*
* @param serverState the new server state
*/
public void setServerState(ServerState serverState) {
this.serverState = serverState;
}
/**
* Returns the max players from the acceptings
*
* @return the maximum amount of players allowed
*/
public int getMaxPlayers() {
return maxPlayers;
}
/**
* Set the maxPlayers from this instance
*
* @param maxPlayers the maximum amount of players allowed
*/
public void setMaxPlayers(int maxPlayers) {
this.maxPlayers = maxPlayers;
}
public void setMaxPlayersAndUpdate(int maxPlayers) {
this.maxPlayers = maxPlayers;
update();
}
public void setMotdAndUpdate(String motd) {
this.motd = motd;
update();
}
/**
* Returns the motd from the server marks for the cloud
*
* @return the motd
*/
public String getMotd() {
return motd;
}
/**
* Sets the Motd for the ServerInfo
*
* @param motd the motd
*/
public void setMotd(String motd) {
this.motd = motd;
}
/**
* Returns the Template of the ServerInfo
*/
public Template getTemplate() {
return template;
}
/**
* Registerd one command
*
* @param command the command to register
*/
public void registerCommand(Command command) {
try {
Class<?> clazz = ReflectionUtil.reflectCraftClazz(".CraftServer");
CommandMap commandMap;
if (clazz != null) {
commandMap = (CommandMap) clazz.getMethod("getCommandMap").invoke(Bukkit.getServer());
} else {
commandMap = (CommandMap) Class.forName("net.glowstone.GlowServer").getMethod("getCommandMap").invoke(Bukkit.getServer());
}
commandMap.register("cloudnet", command);
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* Returns the SimpleServerGroup of the instance
*
* @return the group data of this servers group
*/
public SimpleServerGroup getGroupData() {
return CloudAPI.getInstance().getCloudNetwork().getServerGroups().get(CloudAPI.getInstance().getGroup());
}
public double getPercentOfPlayerNowOnline() {
return (((double) Bukkit.getOnlinePlayers().size()) / (double) maxPlayers) * 100;
}
/**
* Returns the Plugin instance from this CLoud-System
*
* @return this server's bootstrap plugin
*/
public JavaPlugin getPlugin() {
return bukkitBootstrap;
}
/**
* Returns the ServerProcessMeta for the bootstrap of the software
*
* @return this server's process metadata
*/
public ServerProcessMeta getServerProcessMeta() {
return CloudAPI.getInstance().getConfig().getObject("serverProcess", new TypeToken<ServerProcessMeta>() {}.getType());
}
/**
* @param player the player
*/
public void updateNameTags(Player player) {
this.updateNameTags(player, null);
}
public void updateNameTags(Player player, Function<Player, PermissionGroup> playerPermissionGroupFunction) {
this.updateNameTags(player, playerPermissionGroupFunction, null);
}
public void updateNameTags(Player player,
Function<Player, PermissionGroup> playerPermissionGroupFunction,
Function<Player, PermissionGroup> allOtherPlayerPermissionGroupFunction) {
if (CloudAPI.getInstance().getPermissionPool() == null || !CloudAPI.getInstance().getPermissionPool().isAvailable()) {
return;
}
PermissionGroup playerPermissionGroup = playerPermissionGroupFunction != null ? playerPermissionGroupFunction.apply(player) : getCloudPlayers()
.get(player.getUniqueId())
.getPermissionEntity()
.getHighestPermissionGroup(CloudAPI.getInstance().getPermissionPool());
initScoreboard(player);
for (Player all : player.getServer().getOnlinePlayers()) {
initScoreboard(all);
if (playerPermissionGroup != null) {
addTeamEntry(player, all, playerPermissionGroup);
}
PermissionGroup targetPermissionGroup = allOtherPlayerPermissionGroupFunction != null ? allOtherPlayerPermissionGroupFunction.apply(
all) : null;
if (targetPermissionGroup == null) {
targetPermissionGroup = getCachedPlayer(all.getUniqueId()).getPermissionEntity()
.getHighestPermissionGroup(CloudAPI.getInstance()
.getPermissionPool());
}
if (targetPermissionGroup != null) {
addTeamEntry(all, player, targetPermissionGroup);
}
}
}
/**
* Returns the cached CloudPlayer Objectives
*
* @return a map containing all cloud players
*/
public Map<UUID, CloudPlayer> getCloudPlayers() {
return cloudPlayers;
}
private void initScoreboard(Player all) {
if (all.getScoreboard() == null) {
all.setScoreboard(all.getServer().getScoreboardManager().getNewScoreboard());
}
}
private void addTeamEntry(Player target, Player all, PermissionGroup permissionGroup) {
String teamName = permissionGroup.getTagId() + permissionGroup.getName();
if (teamName.length() > 16) {
teamName = teamName.substring(0, 16);
CloudAPI.getInstance()
.dispatchConsoleMessage("In order to prevent issues, the name (+ tagID) of the group " + permissionGroup.getName() + " was temporarily shortened to 16 characters!");
CloudAPI.getInstance().dispatchConsoleMessage("Please fix this issue by changing the name of the group in your perms.yml");
Bukkit.broadcast("In order to prevent issues, the name (+ tagID) of the group " + permissionGroup.getName() + " was temporarily shortened to 16 characters!",
"cloudnet.notify");
Bukkit.broadcast("Please fix this issue by changing the name of the group in your perms.yml", "cloudnet.notify");
}
Team team = all.getScoreboard().getTeam(teamName);
if (team == null) {
team = all.getScoreboard().registerNewTeam(teamName);
}
if (permissionGroup.getPrefix().length() > 16) {
permissionGroup.setPrefix(permissionGroup.getPrefix().substring(0, 16));
CloudAPI.getInstance()
.dispatchConsoleMessage("In order to prevent issues, the prefix of the group " + permissionGroup.getName() + " was temporarily shortened to 16 characters!");
CloudAPI.getInstance().dispatchConsoleMessage("Please fix this issue by changing the prefix in your perms.yml");
Bukkit.broadcast("In order to prevent issues, the prefix of the group " + permissionGroup.getName() + " was temporarily shortened to 16 characters!",
"cloudnet.notify");
Bukkit.broadcast("Please fix this issue by changing the prefix in your perms.yml", "cloudnet.notify");
}
if (permissionGroup.getSuffix().length() > 16) {
permissionGroup.setSuffix(permissionGroup.getSuffix().substring(0, 16));
CloudAPI.getInstance()
.dispatchConsoleMessage("In order to prevent issues, the suffix of the group " + permissionGroup.getName() + " was temporarily shortened to 16 characters!");
CloudAPI.getInstance().dispatchConsoleMessage("Please fix this issue by changing the suffix in your perms.yml");
Bukkit.broadcast("In order to prevent issues, the suffix of the group " + permissionGroup.getName() + " was temporarily shortened to 16 characters!",
"cloudnet.notify");
Bukkit.broadcast("Please fix this issue by changing the suffix in your perms.yml", "cloudnet.notify");
}
try {
Method setColor = team.getClass().getDeclaredMethod("setColor", ChatColor.class);
setColor.setAccessible(true);
if (permissionGroup.getColor().length() != 0) {
setColor.invoke(team, ChatColor.getByChar(permissionGroup.getColor().replaceAll("&", "").replaceAll("§", "")));
} else {
setColor.invoke(team, ChatColor.getByChar(ChatColor.getLastColors(permissionGroup.getPrefix().replace('&', '§'))
.replaceAll("&", "")
.replaceAll("§", "")));
}
} catch (NoSuchMethodException ignored) {
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
team.setPrefix(ChatColor.translateAlternateColorCodes('&', permissionGroup.getPrefix()));
team.setSuffix(ChatColor.translateAlternateColorCodes('&', permissionGroup.getSuffix()));
team.addEntry(target.getName());
target.setDisplayName(ChatColor.translateAlternateColorCodes('&', permissionGroup.getDisplay() + target.getName()));
}
public CloudPlayer getCachedPlayer(UUID uniqueId) {
return cloudPlayers.get(uniqueId);
}
public CloudPlayer getCachedPlayer(String name) {
return CollectionWrapper.filter(this.cloudPlayers.values(), new Acceptable<CloudPlayer>() {
@Override
public boolean isAccepted(CloudPlayer cloudPlayer) {
return cloudPlayer.getName().equalsIgnoreCase(name);
}
});
}
@Override
public boolean isProxyInstance() {
return false;
}
@Override
public Map<String, ServerInfo> getServers() {
throw new UnsupportedOperationException();
}
public Map<UUID, CloudPlayer> getClonedCloudPlayers() {
return new HashMap<>(this.cloudPlayers);
}
//API Handler
/*================================================================================================================*/
private class NetworkHandlerImpl implements NetworkHandler {
@Override
public void onServerAdd(ServerInfo serverInfo) {
Bukkit.getPluginManager().callEvent(new BukkitServerAddEvent(serverInfo));
}
@Override
public void onServerInfoUpdate(ServerInfo serverInfo) {
Bukkit.getPluginManager().callEvent(new BukkitServerInfoUpdateEvent(serverInfo));
}
@Override
public void onServerRemove(ServerInfo serverInfo) {
Bukkit.getPluginManager().callEvent(new BukkitServerRemoveEvent(serverInfo));
}
@Override
public void onProxyAdd(ProxyInfo proxyInfo) {
Bukkit.getPluginManager().callEvent(new BukkitProxyAddEvent(proxyInfo));
}
@Override
public void onProxyInfoUpdate(ProxyInfo proxyInfo) {
Bukkit.getPluginManager().callEvent(new BukkitProxyInfoUpdateEvent(proxyInfo));
}
@Override
public void onProxyRemove(ProxyInfo proxyInfo) {
Bukkit.getPluginManager().callEvent(new BukkitProxyRemoveEvent(proxyInfo));
}
@Override
public void onCloudNetworkUpdate(CloudNetwork cloudNetwork) {
Bukkit.getPluginManager().callEvent(new BukkitCloudNetworkUpdateEvent(cloudNetwork));
}
@Override
public void onCustomChannelMessageReceive(String channel, String message, Document document) {
Bukkit.getPluginManager().callEvent(new BukkitCustomChannelMessageReceiveEvent(channel, message, document));
}
@Override
public void onCustomSubChannelMessageReceive(String channel, String message, Document document) {
Bukkit.getPluginManager().callEvent(new BukkitSubChannelMessageEvent(channel, message, document));
if (channel.equalsIgnoreCase("cloudnet_internal")) {
if (message.equalsIgnoreCase("install_plugin")) {
String url = document.getString("url");
try {
URLConnection urlConnection = new URL(url).openConnection();
urlConnection.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
urlConnection.connect();
Files.copy(urlConnection.getInputStream(), Paths.get("plugins/" + document.getString("name") + ".jar"));
File file = new File("plugins/" + document.getString("name") + ".jar");
Bukkit.getScheduler().runTask(CloudServer.this.getPlugin(), () -> {
try {
Plugin plugin = Bukkit.getPluginManager().loadPlugin(file);
Bukkit.getPluginManager().enablePlugin(plugin);
} catch (InvalidPluginException | InvalidDescriptionException e) {
e.printStackTrace();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public void onPlayerLoginNetwork(CloudPlayer cloudPlayer) {
//cloudPlayers.put(cloudPlayer.getUniqueId(), cloudPlayer);
Bukkit.getPluginManager().callEvent(new BukkitPlayerLoginNetworkEvent(cloudPlayer));
}
@Override
public void onPlayerDisconnectNetwork(CloudPlayer cloudPlayer) {
Bukkit.getPluginManager().callEvent(new BukkitPlayerDisconnectEvent(cloudPlayer));
}
@Override
public void onPlayerDisconnectNetwork(UUID uniqueId) {
}
@Override
public void onPlayerUpdate(CloudPlayer cloudPlayer) {
if (cloudPlayers.containsKey(cloudPlayer.getUniqueId())) {
cloudPlayers.put(cloudPlayer.getUniqueId(), cloudPlayer);
}
Bukkit.getPluginManager().callEvent(new BukkitPlayerUpdateEvent(cloudPlayer));
}
@Override
public void onOfflinePlayerUpdate(OfflinePlayer offlinePlayer) {
Bukkit.getPluginManager().callEvent(new BukkitOfflinePlayerUpdateEvent(offlinePlayer));
}
@Override
public void onUpdateOnlineCount(int onlineCount) {
Bukkit.getPluginManager().callEvent(new BukkitOnlineCountUpdateEvent(onlineCount));
}
}
}