103 lines
3.7 KiB
Java
103 lines
3.7 KiB
Java
/*
|
|
* Copyright (C) 2024 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.motd;
|
|
|
|
import com.comphenix.protocol.PacketType;
|
|
import com.comphenix.protocol.ProtocolLibrary;
|
|
import com.comphenix.protocol.events.*;
|
|
import com.comphenix.protocol.reflect.StructureModifier;
|
|
import com.google.gson.JsonElement;
|
|
import eu.m724.tweaks.TweaksConfig;
|
|
import eu.m724.tweaks.TweaksPlugin;
|
|
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 org.bukkit.plugin.Plugin;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.util.Arrays;
|
|
import java.util.Optional;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
public class MotdManager {
|
|
private final TweaksPlugin plugin;
|
|
|
|
private Component[] motds;
|
|
|
|
public MotdManager(TweaksPlugin plugin) {
|
|
this.plugin = plugin;
|
|
}
|
|
|
|
public void init() throws IOException {
|
|
// TODO adding more MOTD features would require checking whether to enable set
|
|
|
|
String motdSetName = TweaksConfig.getConfig().motdSet();
|
|
String motdSetPath = "motd sets/" + motdSetName + ".txt";
|
|
File motdSetsFile = new File(plugin.getDataFolder(), motdSetPath);
|
|
|
|
// create "motd sets" directory
|
|
motdSetsFile.getParentFile().mkdirs();
|
|
|
|
// if this is a builtin set
|
|
if (!motdSetsFile.exists() && plugin.hasResource(motdSetPath))
|
|
plugin.saveResource(motdSetPath, false);
|
|
|
|
if (!motdSetsFile.exists()) {
|
|
throw new RuntimeException("MOTD set \"%s\" doesn't exist".formatted(motdSetName));
|
|
}
|
|
|
|
String fileContent = Files.readString(motdSetsFile.toPath());
|
|
// MOTDs are split with an empty line
|
|
motds = Arrays.stream(fileContent.split("\n\n"))
|
|
.map(s -> {
|
|
JsonElement json = ComponentSerializer.toJson(TextComponent.fromLegacy(s.strip()));
|
|
return Component.Serializer.fromJson(json, RegistryAccess.EMPTY);
|
|
})
|
|
.toArray(Component[]::new);
|
|
|
|
registerListener(plugin);
|
|
}
|
|
|
|
private void registerListener(Plugin plugin) {
|
|
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(
|
|
plugin,
|
|
ListenerPriority.NORMAL,
|
|
PacketType.Status.Server.SERVER_INFO
|
|
) {
|
|
@Override
|
|
public void onPacketSending(PacketEvent 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)));
|
|
}
|
|
});
|
|
}
|
|
}
|