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