修复退出/登录消息提醒
This commit is contained in:
parent
2e5e1195b5
commit
20330df2a9
|
|
@ -20,7 +20,7 @@ public class CrossServerModMixinPlugin implements IMixinConfigPlugin {
|
|||
|
||||
@Override
|
||||
public boolean shouldApplyMixin(String s, String s1) {
|
||||
return !FMLEnvironment.dist.isDedicatedServer();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package com.leisuretimedock.crossmod.mixin;
|
|||
import com.leisuretimedock.crossmod.config.CrossServerConfigManager;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.contents.TranslatableContents;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.players.PlayerList;
|
||||
import org.slf4j.Logger;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
|
|
@ -12,31 +13,56 @@ import org.spongepowered.asm.mixin.injection.At;
|
|||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
@Mixin(PlayerList.class)
|
||||
public class MixinPlayerList {
|
||||
@Shadow
|
||||
@Final
|
||||
private static Logger LOGGER;
|
||||
|
||||
/**
|
||||
* 拦截双参数版本
|
||||
*/
|
||||
@Inject(
|
||||
method = "broadcastSystemMessage(Lnet/minecraft/network/chat/Component;Z)V",
|
||||
at = @At("HEAD"), cancellable = true
|
||||
at = @At("HEAD"),
|
||||
cancellable = true
|
||||
)
|
||||
private void send(Component message, boolean bypassHiddenChat, CallbackInfo ci) {
|
||||
try {
|
||||
if (CrossServerConfigManager.INSTANCE.isDisabledJoinQuitMessage()) {
|
||||
// 更好的方式:检查消息的翻译键
|
||||
private void onBroadcastSystemMessage(Component message, boolean bypassHiddenChat, CallbackInfo ci) {
|
||||
if (shouldCancel(message)) {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截三参数版本
|
||||
*/
|
||||
@Inject(
|
||||
method = "broadcastSystemMessage(Lnet/minecraft/network/chat/Component;Ljava/util/function/Function;Z)V",
|
||||
at = @At("HEAD"),
|
||||
cancellable = true
|
||||
)
|
||||
private void onBroadcastSystemMessage(Component serverMessage, Function<ServerPlayer, Component> playerMessageFactory, boolean bypassHiddenChat, CallbackInfo ci) {
|
||||
if (shouldCancel(serverMessage)) {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean shouldCancel(Component message) {
|
||||
if (!CrossServerConfigManager.INSTANCE.isDisabledJoinQuitMessage()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查 TranslatableContents 的翻译键
|
||||
if (message.getContents() instanceof TranslatableContents translatable) {
|
||||
String key = translatable.getKey();
|
||||
if ("multiplayer.player.joined".equals(key) ||
|
||||
"multiplayer.player.joined.renamed".equals(key) ||
|
||||
"multiplayer.player.left".equals(key)) {
|
||||
ci.cancel();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.warn("Exception while sending system message to client", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user