|
| 1 | +package io.github.miniplaceholders.expansion.proxyconnection.fabric; |
| 2 | + |
| 3 | +import com.google.common.io.ByteArrayDataInput; |
| 4 | +import com.google.common.io.ByteArrayDataOutput; |
| 5 | +import com.google.common.io.ByteStreams; |
| 6 | +import com.pokeskies.fabricpluginmessaging.PluginMessageEvent; |
| 7 | +import com.pokeskies.fabricpluginmessaging.PluginMessagePacket; |
| 8 | +import io.github.miniplaceholders.api.Expansion; |
| 9 | +import io.github.miniplaceholders.expansion.proxyconnection.common.BungeeMessageTypes; |
| 10 | +import io.github.miniplaceholders.expansion.proxyconnection.common.PlatformProvider; |
| 11 | +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; |
| 12 | +import net.kyori.adventure.text.minimessage.tag.Tag; |
| 13 | +import net.minecraft.server.MinecraftServer; |
| 14 | +import net.minecraft.server.level.ServerPlayer; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | + |
| 19 | +public final class FabricProvider extends PlatformProvider<MinecraftServer, Object> { |
| 20 | + public FabricProvider(Object platformInstance, Object miniInstance) { |
| 21 | + super((MinecraftServer) platformInstance, miniInstance); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public Expansion.Builder provideBuilder() { |
| 26 | + executor.schedule(() -> { |
| 27 | + final var players = platformInstance.getPlayerList().getPlayers().iterator(); |
| 28 | + if (!players.hasNext()) { |
| 29 | + return; |
| 30 | + } |
| 31 | + final ServerPlayer player = players.next(); |
| 32 | + for (String server : dataCache.getServers()) { |
| 33 | + final ByteArrayDataOutput output = ByteStreams.newDataOutput(); |
| 34 | + output.writeUTF(BungeeMessageTypes.PLAYER_COUNT.rawType()); |
| 35 | + output.writeUTF(server); |
| 36 | + ServerPlayNetworking.send(player, new PluginMessagePacket(output.toByteArray())); |
| 37 | + } |
| 38 | + }, 5, TimeUnit.SECONDS); |
| 39 | + |
| 40 | + executor.schedule(() -> { |
| 41 | + final var players = platformInstance.getPlayerList().getPlayers().iterator(); |
| 42 | + if (!players.hasNext()) { |
| 43 | + return; |
| 44 | + } |
| 45 | + final ServerPlayer player = players.next(); |
| 46 | + final ByteArrayDataOutput output = ByteStreams.newDataOutput(); |
| 47 | + output.writeUTF(BungeeMessageTypes.GET_SERVERS.rawType()); |
| 48 | + ServerPlayNetworking.send(player, new PluginMessagePacket(output.toByteArray())); |
| 49 | + }, 10, TimeUnit.MINUTES); |
| 50 | + |
| 51 | + PluginMessageEvent.EVENT.register((payload, context) -> { |
| 52 | + final ByteArrayDataInput data = ByteStreams.newDataInput(payload.getData()); |
| 53 | + final String subchannel = data.readUTF(); |
| 54 | + if (BungeeMessageTypes.PLAYER_COUNT.compareProvided(subchannel)) { |
| 55 | + final String server = data.readUTF(); |
| 56 | + final int playerCount = data.readInt(); |
| 57 | + dataCache.setPlayerCount(server, playerCount); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (BungeeMessageTypes.GET_SERVERS.compareProvided(subchannel)) { |
| 62 | + final String serversString = data.readUTF(); |
| 63 | + if (serversString.isEmpty()) { |
| 64 | + return; |
| 65 | + } |
| 66 | + String[] servers = SPLIT_PATTERN.split(serversString); |
| 67 | + dataCache.updateServers(List.of(servers)); |
| 68 | + } |
| 69 | + }); |
| 70 | + return Expansion.builder("proxyconnection") |
| 71 | + .globalPlaceholder("player_count", (queue, context) -> { |
| 72 | + if (queue.hasNext()) { |
| 73 | + final String server = queue.pop().value(); |
| 74 | + final int serverPlayerCount = dataCache.getPlayerCount(server); |
| 75 | + return Tag.preProcessParsed(Integer.toString(serverPlayerCount)); |
| 76 | + } |
| 77 | + final int proxyPlayerCount = dataCache.getProxyPlayerCount(); |
| 78 | + return Tag.preProcessParsed(Integer.toString(proxyPlayerCount)); |
| 79 | + }); |
| 80 | + } |
| 81 | +} |
0 commit comments