impove motds

This commit is contained in:
Sen 2025-06-05 12:04:27 +02:00
parent 1d1a2027e0
commit bc997ea42d
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
3 changed files with 70 additions and 58 deletions

View file

@ -107,8 +107,7 @@ public class Proxy {
private int maxAttempts = 5;
private int maxPlayers = 64;
private ServerInfo status = getStatus(new File("icon.png"), Formatter.AQUA + "Server\n" + Formatter.GREEN + "Test!!", 90, 10, "Test1",
"Test2 ...", "Test #3 !!");
private ServerInfo status;
private static String getIcon(File file) {
if(file.isFile()) {
@ -130,16 +129,6 @@ public class Proxy {
return null;
}
public static ServerInfo getStatus(File icon, String motd, int max, int current, String... lines) {
ServerInfo info = new ServerInfo("1.8.9", 47);
info.setMotd(motd);
info.setCapacity(max);
info.setOnline(current);
info.setList(lines);
info.setIcon(getIcon(icon));
return info;
}
public static UUID getOfflineUUID(String name) {
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8));
}
@ -157,6 +146,16 @@ public class Proxy {
}
public void run() {
this.status = new ServerInfo("VLoginProxy 1.8.9", 47);
this.status.setCapacity(this.maxPlayers);
Collections.addAll(this.status.getMotds(), Formatter.DARK_RED + "Miau\n" + Formatter.YELLOW + "Test??", Formatter.AQUA + "Server\n" + Formatter.GREEN + "Test!!");
String icon = getIcon(new File("icon1.png"));
if(icon != null)
this.status.getIcons().add(icon);
icon = getIcon(new File("icon2.png"));
if(icon != null)
this.status.getIcons().add(icon);
Collections.addAll(this.status.getList(), Formatter.DARK_GREEN + "TESTTTT", "Test 2!", Formatter.BLUE + "Test numbah 3!!!!!");
Log.info("Starting login proxy on %s:%d", this.proxyHost.isEmpty() ? "0.0.0.0" : this.proxyHost, this.proxyPort);
try {
this.addLanEndpoint(this.proxyHost.isEmpty() ? null : InetAddress.getByName(IDN.toASCII(this.proxyHost)), this.proxyPort);
@ -381,6 +380,10 @@ public class Proxy {
public int getMaximumPlayers() {
return this.maxPlayers;
}
public int getOnlinePlayers() {
return this.players.size();
}
public User getUser(String user) {
return this.users.get(user.toLowerCase(Locale.US));
@ -394,6 +397,7 @@ public class Proxy {
user = user.toLowerCase(Locale.US);
this.users.remove(user);
this.players.remove(user);
this.status.setOnline(this.players.size());
}
public void setLoggedIn(String user, ProxyHandler handler) {
@ -403,6 +407,7 @@ public class Proxy {
handler.copyFrom(usr);
this.users.put(user, handler);
this.players.put(user, handler);
this.status.setOnline(this.players.size());
}
public void setLoggedOut(String user) {
@ -413,6 +418,7 @@ public class Proxy {
usr.copyFrom(handler);
this.users.put(user, usr);
}
this.status.setOnline(this.players.size());
}
public boolean isLoggedIn(String user) {

View file

@ -279,6 +279,9 @@ public class ProxyHandler extends User implements Handler {
if(!this.proxy.canRegister()) {
this.disconnect("You are not whitelisted on this server");
}
else if(this.proxy.getMaximumPlayers() > 0 && this.proxy.getOnlinePlayers() >= this.proxy.getMaximumPlayers()) {
this.disconnect("The server is full (" + this.proxy.getOnlinePlayers() + " / " + this.proxy.getMaximumPlayers() + " players), please try registering later");
}
else if(password.isEmpty() || password.length() < this.proxy.getMinimumPasswordLength()) {
this.setSign(Formatter.DARK_RED + "" + Formatter.UNDERLINE + "Too short" + Formatter.DARK_RED + ", use",
Formatter.DARK_RED + "" + this.proxy.getMinimumPasswordLength() + "-32 characters");
@ -309,6 +312,9 @@ public class ProxyHandler extends User implements Handler {
this.disconnect("You are already logged in");
Log.info("%s was already logged in from another client", this.username);
}
else if(!stored.isAdmin() && this.proxy.getMaximumPlayers() > 0 && this.proxy.getOnlinePlayers() >= this.proxy.getMaximumPlayers()) {
this.disconnect("The server is full (" + this.proxy.getOnlinePlayers() + " / " + this.proxy.getMaximumPlayers() + " players), please try again later");
}
else {
Log.info("%s logged in with password", this.username);
this.connectToServer();

View file

@ -1,5 +1,6 @@
package proxy.packet;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
@ -7,27 +8,37 @@ import com.google.gson.JsonObject;
import proxy.Proxy;
import java.util.UUID;
import java.util.List;
import java.util.Random;
public class ServerInfo {
private static final Gson GSON = new GsonBuilder().create();
private static final Random RANDOM = new Random();
private final String name;
private final int protocol;
private final List<String> motds = Lists.newArrayList();
private final List<String> list = Lists.newArrayList();
private final List<String> icons = Lists.newArrayList();
private String motd = "";
private int capacity;
private int online;
private Profile[] list;
private String icon;
private int capacity = 0;
private int online = 0;
public ServerInfo(String name, int protocol) {
this.name = name;
this.protocol = protocol;
}
public void setMotd(String motd) {
this.motd = motd;
public List<String> getMotds() {
return this.motds;
}
public List<String> getIcons() {
return this.icons;
}
public List<String> getList() {
return this.list;
}
public void setCapacity(int capacity) {
@ -38,46 +49,35 @@ public class ServerInfo {
this.online = online;
}
public void setList(String[] lines) {
if(lines == null) {
this.list = null;
}
else {
this.list = new Profile[Math.min(lines.length, 12)];
for(int z = 0; z < this.list.length; z++) {
this.list[z] = new Profile(Proxy.getOfflineUUID(lines[z]), lines[z]);
}
}
}
public void setIcon(String icon) {
this.icon = icon;
}
public String serialize() {
JsonObject obj = new JsonObject();
obj.addProperty("description", this.motd == null ? "" : this.motd);
JsonObject info = new JsonObject();
info.addProperty("max", this.capacity);
info.addProperty("online", this.online);
if(this.list != null && this.list.length > 0) {
JsonArray arr = new JsonArray();
for(int z = 0; z < this.list.length; z++) {
JsonObject profile = new JsonObject();
UUID id = this.list[z].getId();
profile.addProperty("id", id == null ? "" : id.toString());
profile.addProperty("name", this.list[z].getName());
arr.add(profile);
try {
JsonObject obj = new JsonObject();
obj.addProperty("description", this.motds.isEmpty() ? "" : this.motds.get(RANDOM.nextInt(this.motds.size())));
JsonObject info = new JsonObject();
info.addProperty("max", this.capacity);
info.addProperty("online", this.online);
if(!this.list.isEmpty()) {
JsonArray arr = new JsonArray();
for(int z = 0; z < this.list.size(); z++) {
String line = this.list.get(z);
JsonObject profile = new JsonObject();
profile.addProperty("id", Proxy.getOfflineUUID(line).toString());
profile.addProperty("name", line);
arr.add(profile);
}
info.add("sample", arr);
}
info.add("sample", arr);
obj.add("players", info);
JsonObject version = new JsonObject();
version.addProperty("name", this.name);
version.addProperty("protocol", this.protocol);
obj.add("version", version);
if(!this.icons.isEmpty())
obj.addProperty("favicon", this.icons.get(RANDOM.nextInt(this.icons.size())));
return GSON.toJson(obj);
}
catch(Throwable t) {
return "{}";
}
obj.add("players", info);
JsonObject version = new JsonObject();
version.addProperty("name", this.name);
version.addProperty("protocol", this.protocol);
obj.add("version", version);
if(this.icon != null)
obj.addProperty("favicon", this.icon);
return GSON.toJson(obj);
}
}