Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8f989ee135 | ||
|
|
a67ec47f89 | ||
|
|
d1c6504973 |
@@ -1,5 +1,9 @@
|
|||||||
package kr.tkrmagid.chatanswer.core;
|
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.commands.CommandSourceStack;
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.server.level.ServerPlayer;
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
@@ -29,25 +33,55 @@ public final class ChatAnswerCore {
|
|||||||
private static final String SCOREBOARD_HOLDER = "init";
|
private static final String SCOREBOARD_HOLDER = "init";
|
||||||
private static final int ACCEPTING_ANSWER_STATE = 5;
|
private static final int ACCEPTING_ANSWER_STATE = 5;
|
||||||
|
|
||||||
|
/** 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() {}
|
private ChatAnswerCore() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 플레이어 로그인 직후 호출. 데이터팩이 "모드 살아있음" 신호로 쓸 수 있게
|
* 플레이어 로그인 시점에 호출. 음악퀴즈 데이터팩의
|
||||||
* storage chat_answer:status 에 active=1b 를 세팅한다. 데이터팩의 mq:load 가
|
* mq:players/mod_active_notice
|
||||||
* 매 /reload 와 서버 시작 시 이 값을 0b 로 clear 하므로, 모드가 없으면 이
|
* 함수를 해당 플레이어 컨텍스트로 호출한다. 단, JOIN 이벤트가 너무 일러서
|
||||||
* 호출이 일어나지 않아 0b 로 유지되고, 모드가 있으면 첫 로그인 직후 1b 로 갱신.
|
* 즉시 호출 시 tellraw 가 클라이언트에 도달하지 못하는 race 가 있어
|
||||||
|
* {@link #NOTICE_DELAY_TICKS} 만큼 늦춘다 ({@link #onServerTick} 가 처리).
|
||||||
*/
|
*/
|
||||||
public static void onPlayerJoin(ServerPlayer player) {
|
public static void onPlayerJoin(ServerPlayer player) {
|
||||||
MinecraftServer server = player.level().getServer();
|
String name = player.getName().getString();
|
||||||
if (server == null) return;
|
LOG.info("[{}] onPlayerJoin fired for {}, scheduling notice in {} ticks",
|
||||||
CommandSourceStack source = server.createCommandSourceStack().withSuppressedOutput();
|
MOD_ID, name, NOTICE_DELAY_TICKS);
|
||||||
|
PENDING_NOTICES.put(player.getUUID(), NOTICE_DELAY_TICKS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 각 로더 entrypoint 가 매 server tick 마다 호출해야 한다. */
|
||||||
|
public static void onServerTick(MinecraftServer 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void deliverNotice(MinecraftServer server, ServerPlayer player) {
|
||||||
|
String name = player.getName().getString();
|
||||||
|
// 플레이어 자체를 source 로 써서 함수 안의 @s 가 그대로 player.
|
||||||
|
CommandSourceStack source = player.createCommandSourceStack().withSuppressedOutput();
|
||||||
try {
|
try {
|
||||||
server.getCommands().performPrefixedCommand(
|
server.getCommands().performPrefixedCommand(source, "function mq:players/mod_active_notice");
|
||||||
source,
|
LOG.info("[{}] mod_active_notice delivered for {}", MOD_ID, name);
|
||||||
"data modify storage chat_answer:status active set value 1b"
|
|
||||||
);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.debug("[{}] failed to set active flag: {}", MOD_ID, e.toString());
|
LOG.warn("[{}] failed to deliver mod_active_notice for {}: {}", MOD_ID, name, e.toString(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,17 +2,30 @@ package kr.tkrmagid.chatanswer.fabric;
|
|||||||
|
|
||||||
import kr.tkrmagid.chatanswer.core.ChatAnswerCore;
|
import kr.tkrmagid.chatanswer.core.ChatAnswerCore;
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
||||||
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
|
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
|
||||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public final class ChatAnswerFabric implements ModInitializer {
|
public final class ChatAnswerFabric implements ModInitializer {
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(ChatAnswerCore.MOD_ID);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
|
LOG.info("[{}] Fabric entrypoint onInitialize starting", ChatAnswerCore.MOD_ID);
|
||||||
|
try {
|
||||||
ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((message, sender, params) ->
|
ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((message, sender, params) ->
|
||||||
ChatAnswerCore.handleChat(sender, message.signedContent())
|
ChatAnswerCore.handleChat(sender, message.signedContent())
|
||||||
);
|
);
|
||||||
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
|
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
|
||||||
ChatAnswerCore.onPlayerJoin(handler.player)
|
ChatAnswerCore.onPlayerJoin(handler.player)
|
||||||
);
|
);
|
||||||
|
ServerTickEvents.END_SERVER_TICK.register(ChatAnswerCore::onServerTick);
|
||||||
|
LOG.info("[{}] Fabric entrypoint registered: ALLOW_CHAT_MESSAGE + JOIN + TICK", ChatAnswerCore.MOD_ID);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
LOG.error("[{}] Fabric entrypoint event registration failed", ChatAnswerCore.MOD_ID, t);
|
||||||
|
throw t;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,17 +2,30 @@ package kr.tkrmagid.chatanswer.fabric;
|
|||||||
|
|
||||||
import kr.tkrmagid.chatanswer.core.ChatAnswerCore;
|
import kr.tkrmagid.chatanswer.core.ChatAnswerCore;
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
||||||
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
|
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
|
||||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public final class ChatAnswerFabric implements ModInitializer {
|
public final class ChatAnswerFabric implements ModInitializer {
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(ChatAnswerCore.MOD_ID);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
|
LOG.info("[{}] Fabric entrypoint onInitialize starting", ChatAnswerCore.MOD_ID);
|
||||||
|
try {
|
||||||
ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((message, sender, params) ->
|
ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((message, sender, params) ->
|
||||||
ChatAnswerCore.handleChat(sender, message.signedContent())
|
ChatAnswerCore.handleChat(sender, message.signedContent())
|
||||||
);
|
);
|
||||||
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
|
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
|
||||||
ChatAnswerCore.onPlayerJoin(handler.player)
|
ChatAnswerCore.onPlayerJoin(handler.player)
|
||||||
);
|
);
|
||||||
|
ServerTickEvents.END_SERVER_TICK.register(ChatAnswerCore::onServerTick);
|
||||||
|
LOG.info("[{}] Fabric entrypoint registered: ALLOW_CHAT_MESSAGE + 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 metadata ─────────────────────────────────────────────────────────
|
||||||
mod_id=chat_answer
|
mod_id=chat_answer
|
||||||
mod_version=1.3.1
|
mod_version=1.3.4
|
||||||
mod_group=kr.tkrmagid.chatanswer
|
mod_group=kr.tkrmagid.chatanswer
|
||||||
mod_name=채팅정답
|
mod_name=채팅정답
|
||||||
|
|
||||||
|
|||||||
@@ -8,12 +8,14 @@ import net.neoforged.fml.common.Mod;
|
|||||||
import net.neoforged.neoforge.common.NeoForge;
|
import net.neoforged.neoforge.common.NeoForge;
|
||||||
import net.neoforged.neoforge.event.ServerChatEvent;
|
import net.neoforged.neoforge.event.ServerChatEvent;
|
||||||
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
||||||
|
import net.neoforged.neoforge.event.tick.ServerTickEvent;
|
||||||
|
|
||||||
@Mod(ChatAnswerCore.MOD_ID)
|
@Mod(ChatAnswerCore.MOD_ID)
|
||||||
public final class ChatAnswerNeoForge {
|
public final class ChatAnswerNeoForge {
|
||||||
public ChatAnswerNeoForge(IEventBus modBus) {
|
public ChatAnswerNeoForge(IEventBus modBus) {
|
||||||
NeoForge.EVENT_BUS.addListener(ChatAnswerNeoForge::onServerChat);
|
NeoForge.EVENT_BUS.addListener(ChatAnswerNeoForge::onServerChat);
|
||||||
NeoForge.EVENT_BUS.addListener(ChatAnswerNeoForge::onPlayerLogin);
|
NeoForge.EVENT_BUS.addListener(ChatAnswerNeoForge::onPlayerLogin);
|
||||||
|
NeoForge.EVENT_BUS.addListener(ChatAnswerNeoForge::onServerTick);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
@@ -30,4 +32,9 @@ public final class ChatAnswerNeoForge {
|
|||||||
ChatAnswerCore.onPlayerJoin(player);
|
ChatAnswerCore.onPlayerJoin(player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
public static void onServerTick(ServerTickEvent.Post event) {
|
||||||
|
ChatAnswerCore.onServerTick(event.getServer());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user