Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
48d73daaf7 | ||
|
|
5aaa3c2ace | ||
|
|
41fcc82953 | ||
|
|
8057fa1112 | ||
|
|
8f989ee135 |
@@ -1,5 +1,9 @@
|
||||
package kr.tkrmagid.chatanswer.core;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
@@ -29,36 +33,95 @@ public final class ChatAnswerCore {
|
||||
private static final String SCOREBOARD_HOLDER = "init";
|
||||
private static final int ACCEPTING_ANSWER_STATE = 5;
|
||||
|
||||
/** 음악퀴즈 데이터팩이 선언한 "모드 존재 확인" 점수 이름.
|
||||
* 본 모드는 서버 측에서 채팅을 가로채는 server-only 모드 — 클라이언트는
|
||||
* 설치할 필요가 없고 server 한 곳에 있으면 모든 플레이어에게 적용된다.
|
||||
* 따라서 per-player 검증은 무의미하고, fake player {@link #PRESENCE_HOLDER}
|
||||
* 점수만 1 로 set 한다. 데이터팩의 start 가드는
|
||||
* `score <PRESENCE_HOLDER> <OBJECTIVE> matches 1` 로 검사.
|
||||
*
|
||||
* presence pulse 는 여러 이벤트에서 중복 호출한다 — banner/mohist 같은
|
||||
* fabric-bukkit 하이브리드 호스트에서 일부 Fabric 이벤트(특히
|
||||
* ServerTickEvents.END_SERVER_TICK) 가 안 들어오는 케이스가 보고됨.
|
||||
* SERVER_STARTED / PlayerJoin / TickEnd 셋 중 하나라도 firing 되면
|
||||
* 데이터팩 가드가 통과하도록 모든 진입점에서 markModPresence 호출. */
|
||||
private static final String MOD_PRESENCE_OBJECTIVE = "mq_chat_mod";
|
||||
private static final String PRESENCE_HOLDER = "#server";
|
||||
|
||||
/** JOIN 이벤트 시점엔 클라이언트가 chat HUD 를 받을 준비가 안 됐을 수 있어
|
||||
* tellraw 패킷이 사라지는 경우가 있다. 그래서 N 틱 늦춰서 호출한다. */
|
||||
private static final int NOTICE_DELAY_TICKS = 20;
|
||||
private static final Map<UUID, Integer> PENDING_NOTICES = new ConcurrentHashMap<>();
|
||||
|
||||
private ChatAnswerCore() {}
|
||||
|
||||
/**
|
||||
* 플레이어 로그인 직후 호출. 음악퀴즈 데이터팩의
|
||||
* 플레이어 로그인 시점에 호출. 음악퀴즈 데이터팩의
|
||||
* mq:players/mod_active_notice
|
||||
* 함수를 해당 플레이어 컨텍스트로 직접 호출한다. 데이터팩이 메세지를 정의하고,
|
||||
* 모드는 "지금 막 들어온 이 플레이어에게 보여라" 만 트리거한다.
|
||||
*
|
||||
* 이전엔 storage chat_answer:status active=1b 플래그를 썼는데, 통합 서버에서
|
||||
* mq:load 의 0b 초기화가 player join 이후에 도는 케이스 때문에 race 가 발생했다.
|
||||
* 함수를 모드가 직접 호출하면 race 가 사라지고, 데이터팩이 없을 땐 함수가
|
||||
* 존재하지 않아 커맨드가 (suppressed source 라 채팅엔 안 뜨고 log warn 으로)
|
||||
* 무시된다.
|
||||
* 함수를 해당 플레이어 컨텍스트로 호출한다. 단, JOIN 이벤트가 너무 일러서
|
||||
* 즉시 호출 시 tellraw 가 클라이언트에 도달하지 못하는 race 가 있어
|
||||
* {@link #NOTICE_DELAY_TICKS} 만큼 늦춘다 ({@link #onServerTick} 가 처리).
|
||||
*/
|
||||
public static void onPlayerJoin(ServerPlayer player) {
|
||||
String name = player.getName().getString();
|
||||
LOG.info("[{}] onPlayerJoin fired for {}", MOD_ID, name);
|
||||
LOG.info("[{}] onPlayerJoin fired for {}, scheduling notice in {} ticks",
|
||||
MOD_ID, name, NOTICE_DELAY_TICKS);
|
||||
PENDING_NOTICES.put(player.getUUID(), NOTICE_DELAY_TICKS);
|
||||
// tick 이벤트가 안 들어오는 호스트 대비 — join 시점에도 presence 한 번 찍는다.
|
||||
MinecraftServer server = player.level().getServer();
|
||||
if (server == null) {
|
||||
LOG.warn("[{}] onPlayerJoin: server is null, skipping notice for {}", MOD_ID, name);
|
||||
return;
|
||||
if (server != null) markModPresence(server);
|
||||
}
|
||||
|
||||
/** 각 로더 entrypoint 가 서버 부팅 완료 시점에 호출. tick 이벤트가
|
||||
* 발화되지 않는 환경(banner/mohist) 에서 최소 한 번은 presence 가 찍히도록.
|
||||
* 데이터팩 load 가 SERVER_STARTED 보다 먼저 끝나므로 objective 도 이미 존재. */
|
||||
public static void onServerStarted(MinecraftServer server) {
|
||||
LOG.info("[{}] onServerStarted fired, marking presence", MOD_ID);
|
||||
markModPresence(server);
|
||||
}
|
||||
|
||||
/** 각 로더 entrypoint 가 매 server tick 마다 호출해야 한다. */
|
||||
public static void onServerTick(MinecraftServer server) {
|
||||
markModPresence(server);
|
||||
if (PENDING_NOTICES.isEmpty()) return;
|
||||
Iterator<Map.Entry<UUID, Integer>> it = PENDING_NOTICES.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<UUID, Integer> e = it.next();
|
||||
int remaining = e.getValue() - 1;
|
||||
if (remaining > 0) {
|
||||
e.setValue(remaining);
|
||||
continue;
|
||||
}
|
||||
UUID uuid = e.getKey();
|
||||
it.remove();
|
||||
ServerPlayer player = server.getPlayerList().getPlayer(uuid);
|
||||
if (player == null) continue;
|
||||
deliverNotice(server, player);
|
||||
}
|
||||
CommandSourceStack source = server.createCommandSourceStack().withSuppressedOutput();
|
||||
String command = "execute as " + player.getStringUUID()
|
||||
+ " at @s run function mq:players/mod_active_notice";
|
||||
}
|
||||
|
||||
/**
|
||||
* 데이터팩의 mq_chat_mod 점수(fake player #server 키) 를 1 로 set.
|
||||
* 데이터팩이 아직 load 되지 않아 objective 가 없으면 조용히 skip.
|
||||
* 점수 값이 이미 1 이면 Minecraft 가 packet 전송을 생략하므로
|
||||
* 매 tick 호출해도 트래픽은 늘지 않는다.
|
||||
*/
|
||||
private static void markModPresence(MinecraftServer server) {
|
||||
Scoreboard scoreboard = server.getScoreboard();
|
||||
Objective objective = scoreboard.getObjective(MOD_PRESENCE_OBJECTIVE);
|
||||
if (objective == null) return;
|
||||
scoreboard.getOrCreatePlayerScore(ScoreHolder.forNameOnly(PRESENCE_HOLDER), objective).set(1);
|
||||
}
|
||||
|
||||
private static void deliverNotice(MinecraftServer server, ServerPlayer player) {
|
||||
String name = player.getName().getString();
|
||||
// 플레이어 자체를 source 로 써서 함수 안의 @s 가 그대로 player.
|
||||
CommandSourceStack source = player.createCommandSourceStack().withSuppressedOutput();
|
||||
try {
|
||||
server.getCommands().performPrefixedCommand(source, command);
|
||||
LOG.info("[{}] mod_active_notice invoked for {}", MOD_ID, name);
|
||||
server.getCommands().performPrefixedCommand(source, "function mq:players/mod_active_notice");
|
||||
LOG.info("[{}] mod_active_notice delivered for {}", MOD_ID, name);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("[{}] failed to invoke mod_active_notice for {}: {}", MOD_ID, name, e.toString(), e);
|
||||
LOG.warn("[{}] failed to deliver mod_active_notice for {}: {}", MOD_ID, name, e.toString(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ package kr.tkrmagid.chatanswer.fabric;
|
||||
|
||||
import kr.tkrmagid.chatanswer.core.ChatAnswerCore;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
||||
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||
import org.slf4j.Logger;
|
||||
@@ -17,10 +19,12 @@ public final class ChatAnswerFabric implements ModInitializer {
|
||||
ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((message, sender, params) ->
|
||||
ChatAnswerCore.handleChat(sender, message.signedContent())
|
||||
);
|
||||
ServerLifecycleEvents.SERVER_STARTED.register(ChatAnswerCore::onServerStarted);
|
||||
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
|
||||
ChatAnswerCore.onPlayerJoin(handler.player)
|
||||
);
|
||||
LOG.info("[{}] Fabric entrypoint registered: ALLOW_CHAT_MESSAGE + JOIN", ChatAnswerCore.MOD_ID);
|
||||
ServerTickEvents.END_SERVER_TICK.register(ChatAnswerCore::onServerTick);
|
||||
LOG.info("[{}] Fabric entrypoint registered: ALLOW_CHAT_MESSAGE + SERVER_STARTED + JOIN + TICK", ChatAnswerCore.MOD_ID);
|
||||
} catch (Throwable t) {
|
||||
LOG.error("[{}] Fabric entrypoint event registration failed", ChatAnswerCore.MOD_ID, t);
|
||||
throw t;
|
||||
|
||||
@@ -2,6 +2,8 @@ package kr.tkrmagid.chatanswer.fabric;
|
||||
|
||||
import kr.tkrmagid.chatanswer.core.ChatAnswerCore;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
||||
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||
import org.slf4j.Logger;
|
||||
@@ -17,10 +19,12 @@ public final class ChatAnswerFabric implements ModInitializer {
|
||||
ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((message, sender, params) ->
|
||||
ChatAnswerCore.handleChat(sender, message.signedContent())
|
||||
);
|
||||
ServerLifecycleEvents.SERVER_STARTED.register(ChatAnswerCore::onServerStarted);
|
||||
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
|
||||
ChatAnswerCore.onPlayerJoin(handler.player)
|
||||
);
|
||||
LOG.info("[{}] Fabric entrypoint registered: ALLOW_CHAT_MESSAGE + JOIN", ChatAnswerCore.MOD_ID);
|
||||
ServerTickEvents.END_SERVER_TICK.register(ChatAnswerCore::onServerTick);
|
||||
LOG.info("[{}] Fabric entrypoint registered: ALLOW_CHAT_MESSAGE + SERVER_STARTED + JOIN + TICK", ChatAnswerCore.MOD_ID);
|
||||
} catch (Throwable t) {
|
||||
LOG.error("[{}] Fabric entrypoint event registration failed", ChatAnswerCore.MOD_ID, t);
|
||||
throw t;
|
||||
|
||||
@@ -3,7 +3,7 @@ org.gradle.parallel=true
|
||||
|
||||
# ───── mod metadata ─────────────────────────────────────────────────────────
|
||||
mod_id=chat_answer
|
||||
mod_version=1.3.3
|
||||
mod_version=1.3.6
|
||||
mod_group=kr.tkrmagid.chatanswer
|
||||
mod_name=채팅정답
|
||||
|
||||
|
||||
@@ -8,12 +8,16 @@ import net.neoforged.fml.common.Mod;
|
||||
import net.neoforged.neoforge.common.NeoForge;
|
||||
import net.neoforged.neoforge.event.ServerChatEvent;
|
||||
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
||||
import net.neoforged.neoforge.event.server.ServerStartedEvent;
|
||||
import net.neoforged.neoforge.event.tick.ServerTickEvent;
|
||||
|
||||
@Mod(ChatAnswerCore.MOD_ID)
|
||||
public final class ChatAnswerNeoForge {
|
||||
public ChatAnswerNeoForge(IEventBus modBus) {
|
||||
NeoForge.EVENT_BUS.addListener(ChatAnswerNeoForge::onServerChat);
|
||||
NeoForge.EVENT_BUS.addListener(ChatAnswerNeoForge::onServerStarted);
|
||||
NeoForge.EVENT_BUS.addListener(ChatAnswerNeoForge::onPlayerLogin);
|
||||
NeoForge.EVENT_BUS.addListener(ChatAnswerNeoForge::onServerTick);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
@@ -24,10 +28,20 @@ public final class ChatAnswerNeoForge {
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onServerStarted(ServerStartedEvent event) {
|
||||
ChatAnswerCore.onServerStarted(event.getServer());
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
|
||||
if (event.getEntity() instanceof ServerPlayer player) {
|
||||
ChatAnswerCore.onPlayerJoin(player);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onServerTick(ServerTickEvent.Post event) {
|
||||
ChatAnswerCore.onServerTick(event.getServer());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user