
Added a catch block for FileAlreadyExistsException to prevent unnecessary exceptions when the "motd sets" directory already exists. This ensures smoother execution and improves error handling during initialization. Signed-off-by: Minecon724 <git@m724.eu>
108 lines
4.1 KiB
Java
108 lines
4.1 KiB
Java
/*
|
|
* Copyright (C) 2025 Minecon724
|
|
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
|
|
* in the project root for the full license text.
|
|
*/
|
|
|
|
package eu.m724.tweaks.module.motd;
|
|
|
|
import com.comphenix.protocol.PacketType;
|
|
import com.comphenix.protocol.events.InternalStructure;
|
|
import com.comphenix.protocol.events.PacketContainer;
|
|
import com.comphenix.protocol.reflect.StructureModifier;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonParseException;
|
|
import com.google.gson.JsonParser;
|
|
import eu.m724.tweaks.DebugLogger;
|
|
import eu.m724.tweaks.module.TweaksModule;
|
|
import net.md_5.bungee.api.chat.TextComponent;
|
|
import net.md_5.bungee.chat.ComponentSerializer;
|
|
import net.minecraft.SharedConstants;
|
|
import net.minecraft.core.RegistryAccess;
|
|
import net.minecraft.network.chat.Component;
|
|
import net.minecraft.network.protocol.status.ServerStatus;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.file.FileAlreadyExistsException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.Arrays;
|
|
import java.util.Optional;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
public class MotdModule extends TweaksModule {
|
|
@Override
|
|
protected void onInit() {
|
|
Path motdSetPath = getPlugin().getDataFolder().toPath().resolve("motd sets").resolve(getConfig().motdSet() + ".txt");
|
|
|
|
// create "motd sets" directory
|
|
try {
|
|
Files.createDirectories(motdSetPath);
|
|
} catch (FileAlreadyExistsException ignored) {
|
|
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
|
|
// if this is a builtin set
|
|
if (!Files.exists(motdSetPath) && getPlugin().hasResource("motd sets/" + motdSetPath.getFileName()))
|
|
getPlugin().saveResource("motd sets/" + motdSetPath.getFileName(), false);
|
|
|
|
if (!Files.exists(motdSetPath)) {
|
|
throw new RuntimeException("MOTD set \"%s\" doesn't exist".formatted(getConfig().motdSet()));
|
|
}
|
|
|
|
String fileContent;
|
|
try {
|
|
fileContent = Files.readString(motdSetPath);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException("Reading motd set", e);
|
|
}
|
|
|
|
// MOTDs are split with an empty line
|
|
Component[] motds = Arrays.stream(fileContent.split("\n\n"))
|
|
.map(entry -> {
|
|
entry = entry.strip();
|
|
JsonElement json = null;
|
|
|
|
try {
|
|
json = JsonParser.parseString(entry);
|
|
DebugLogger.finer("JSON line: %s...", entry.substring(0, Math.min(entry.length(), 10)));
|
|
} catch (JsonParseException e) {
|
|
DebugLogger.finer("JSON line: %s...", entry.substring(0, Math.min(entry.length(), 10)));
|
|
}
|
|
|
|
if (json == null) {
|
|
json = ComponentSerializer.toJson(TextComponent.fromLegacy(entry));
|
|
}
|
|
|
|
return Component.Serializer.fromJson(json, RegistryAccess.EMPTY);
|
|
})
|
|
.toArray(Component[]::new);
|
|
|
|
onPacketSend(PacketType.Status.Server.SERVER_INFO, (event) -> {
|
|
PacketContainer packet = event.getPacket();
|
|
|
|
Component motd = motds[ThreadLocalRandom.current().nextInt(motds.length)];
|
|
|
|
ServerStatus serverStatus = (ServerStatus) packet.getStructures().read(0).getHandle();
|
|
|
|
/* this:
|
|
* removes server mod prefix (Paper, Spigot, any brand)
|
|
* hides players
|
|
*/
|
|
ServerStatus newStatus = new ServerStatus(
|
|
motd,
|
|
Optional.empty(),
|
|
Optional.of(new ServerStatus.Version(
|
|
SharedConstants.getCurrentVersion().getName(),
|
|
SharedConstants.getProtocolVersion()
|
|
)),
|
|
serverStatus.favicon(),
|
|
false
|
|
);
|
|
|
|
packet.getStructures().write(0, new InternalStructure(newStatus, new StructureModifier<>(ServerStatus.class)));
|
|
});
|
|
}
|
|
}
|