-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-MinecraftServer.js
120 lines (103 loc) · 3.81 KB
/
MMM-MinecraftServer.js
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
Module.register("MMM-MinecraftServer", {
defaults: {
ip: "127.0.0.1", // Minecraft server IP
title: "Minecraft Server",
bedrock: false, // Whether the server is running on Bedrock edition
hidePlayers: false, // Hide player list
maxPlayers: 10, // Max players to show in list
hideInfo: false, // Hide bottom info (ip, version)
updateInterval: 5, // Default update interval (5 minutes)
},
players: [],
info: [],
start() {
// Log module start
Log.info(`Starting module: ${this.name}`);
this.getPlayers();
setInterval(() => {
this.getPlayers();
}, this.config.updateInterval * 60000);
},
getStyles() {
return ["MMM-MinecraftServer.css"];
},
getPlayers() {
fetch(`https://api.mcsrvstat.us${this.config.bedrock ? "/bedrock" : ""}/3/${this.config.ip}`)
.then((response) => response.json())
.then((data) => {
if (data.players && data.players.list) {
this.players = data.players.list;
}
this.info.online = data.online;
console.log(this.info.online);
if (this.info.online) {
this.info.motd = data.motd.html[0];
this.info.hostname = data.hostname;
this.info.port = data.port;
this.info.version = data.version;
}
this.updateDom();
})
.catch((error) => {
console.error("Error fetching Minecraft server data:", error);
});
},
getDom() {
// Wrapper
const wrapper = document.createElement("div");
wrapper.className = "minecraft-card";
// Title
const title = document.createElement("h1");
title.className = "title";
title.innerHTML = this.config.title;
wrapper.appendChild(title);
// Server not available
if (!this.info.online) {
const info = document.createElement("span");
info.className = "motd";
info.innerHTML = `Server offline`;
wrapper.appendChild(info);
} else {
// MOTD
const motd = document.createElement("span");
motd.className = "motd";
motd.innerHTML = this.info.motd || "";
wrapper.appendChild(motd);
}
// List players
if (!this.config.hidePlayers) {
const playerTable = document.createElement("table");
playerTable.className = "players";
this.players.slice(0, this.config.maxPlayers).forEach((player) => {
const playerRow = document.createElement("tr");
const avatarCell = document.createElement("td");
const avatar = document.createElement("img");
avatar.className = "player-avatar";
avatar.src = `https://mc-heads.net/avatar/${player.uuid}/64`;
avatarCell.appendChild(avatar);
playerRow.appendChild(avatarCell);
const nameCell = document.createElement("td");
const playerName = document.createElement("span");
playerName.className = "player-name";
playerName.innerHTML = (player.name.length < 16) ? player.name : player.name.substring(0, 14) + "...";
nameCell.appendChild(playerName);
playerRow.appendChild(nameCell);
const connectionCell = document.createElement("td");
const connectionBars = document.createElement("div");
connectionBars.className = "connection-bars";
connectionCell.appendChild(connectionBars);
playerRow.appendChild(connectionCell);
playerTable.appendChild(playerRow);
});
wrapper.appendChild(playerTable);
}
// Info
if (!this.config.hideInfo) {
const info = document.createElement("span");
info.className = "info";
info.innerHTML = `${this.config.ip}${this.config.bedrock ? "<br><i>Bedrock<i>" : ""}${this.info.version ? " - " + (this.info.version.length > 25 ? this.info.version.substring(0, 23) + '...' : `<i>${this.info.version}`) : ""}`;
wrapper.appendChild(info);
}
return wrapper;
},
});