Skip to content

Commit

Permalink
SERVER_MESSAGE 和 PLAYER_MESSAGE 直接传字符串
Browse files Browse the repository at this point in the history
  • Loading branch information
Xujiayao committed Feb 10, 2024
1 parent ed54cf6 commit a28a873
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@
public class MinecraftEventListener {

public static void init() {
MinecraftEvents.SERVER_MESSAGE.register((playerChatMessage, commandSourceStack) -> {
sendDiscordMessage(playerChatMessage.decoratedContent().getString(), commandSourceStack.getDisplayName().getString(), JDA.getSelfUser().getAvatarUrl());
MinecraftEvents.SERVER_MESSAGE.register((message, commandSourceStack) -> {
sendDiscordMessage(message, commandSourceStack.getDisplayName().getString(), JDA.getSelfUser().getAvatarUrl());
if (CONFIG.multiServer.enable) {
MULTI_SERVER.sendMessage(false, true, false, commandSourceStack.getDisplayName().getString(), playerChatMessage.decoratedContent().getString());
MULTI_SERVER.sendMessage(false, true, false, commandSourceStack.getDisplayName().getString(), message);
}
});

MinecraftEvents.PLAYER_MESSAGE.register((player, playerChatMessage) -> {
String contentToDiscord = playerChatMessage.decoratedContent().getString();
String contentToMinecraft = playerChatMessage.decoratedContent().getString();
MinecraftEvents.PLAYER_MESSAGE.register((player, message) -> {
String contentToDiscord = message;
String contentToMinecraft = message;

if (StringUtils.countMatches(contentToDiscord, ":") >= 2) {
String[] emojiNames = StringUtils.substringsBetween(contentToDiscord, ":", ":");
Expand Down Expand Up @@ -134,7 +134,7 @@ public static void init() {
if (CONFIG.generic.broadcastChatMessages) {
sendDiscordMessage(contentToDiscord, Objects.requireNonNull(player.getDisplayName()).getString(), CONFIG.generic.avatarApi.replace("%player%", (CONFIG.generic.useUuidInsteadOfName ? player.getUUID().toString() : player.getDisplayName().getString())));
if (CONFIG.multiServer.enable) {
MULTI_SERVER.sendMessage(false, true, false, Objects.requireNonNull(player.getDisplayName()).getString(), CONFIG.generic.formatChatMessages ? contentToMinecraft : playerChatMessage.decoratedContent().getString());
MULTI_SERVER.sendMessage(false, true, false, Objects.requireNonNull(player.getDisplayName()).getString(), CONFIG.generic.formatChatMessages ? contentToMinecraft : message);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import net.minecraft.advancements.AdvancementHolder;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.PlayerChatMessage;
import net.minecraft.network.protocol.game.ServerboundChatCommandPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.damagesource.DamageSource;

Expand All @@ -17,16 +15,16 @@
*/
public interface MinecraftEvents {

Event<ServerMessage> SERVER_MESSAGE = EventFactory.createArrayBacked(ServerMessage.class, callbacks -> (playerChatMessage, commandSourceStack) -> {
Event<ServerMessage> SERVER_MESSAGE = EventFactory.createArrayBacked(ServerMessage.class, callbacks -> (message, commandSourceStack) -> {
for (ServerMessage callback : callbacks) {
callback.message(playerChatMessage, commandSourceStack);
callback.message(message, commandSourceStack);
}
});

Event<PlayerMessage> PLAYER_MESSAGE = EventFactory.createArrayBacked(PlayerMessage.class, callbacks -> (player, playerChatMessage) -> {
Event<PlayerMessage> PLAYER_MESSAGE = EventFactory.createArrayBacked(PlayerMessage.class, callbacks -> (player, message) -> {
Optional<Component> result = Optional.empty();
for (PlayerMessage callback : callbacks) {
result = callback.message(player, playerChatMessage);
result = callback.message(player, message);
}
return result;
});
Expand Down Expand Up @@ -62,11 +60,11 @@ public interface MinecraftEvents {
});

interface ServerMessage {
void message(PlayerChatMessage playerChatMessage, CommandSourceStack commandSourceStack);
void message(String message, CommandSourceStack commandSourceStack);
}

interface PlayerMessage {
Optional<Component> message(ServerPlayer player, PlayerChatMessage playerChatMessage);
Optional<Component> message(ServerPlayer player, String message);
}

interface PlayerCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public class MixinPlayerList {
//#if MC > 11900
@Inject(method = "broadcastChatMessage(Lnet/minecraft/network/chat/PlayerChatMessage;Lnet/minecraft/commands/CommandSourceStack;Lnet/minecraft/network/chat/ChatType$Bound;)V", at = @At("HEAD"))
private void broadcastChatMessage(PlayerChatMessage playerChatMessage, CommandSourceStack commandSourceStack, ChatType.Bound bound, CallbackInfo ci) {
MinecraftEvents.SERVER_MESSAGE.invoker().message(playerChatMessage, commandSourceStack);
MinecraftEvents.SERVER_MESSAGE.invoker().message(playerChatMessage.decoratedContent().getString(), commandSourceStack);
}
//#else
//$$ @Inject(method = "broadcastChatMessage", at = @At("HEAD"))
//$$ private void broadcastChatMessage(FilteredText<PlayerChatMessage> filteredText, CommandSourceStack commandSourceStack, ResourceKey<ChatType> resourceKey, CallbackInfo ci) {
//$$ MinecraftEvents.SERVER_MESSAGE.invoker().message(filteredText.filtered(), commandSourceStack);
//$$ MinecraftEvents.SERVER_MESSAGE.invoker().message(filteredText.filtered().serverContent().getString(), commandSourceStack);
//$$ // TODO filtered() or raw() ?
//$$ }
//#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class MixinServerGamePacketListenerImpl {

@Inject(method = "broadcastChatMessage", at = @At("HEAD"), cancellable = true)
private void broadcastChatMessage(PlayerChatMessage playerChatMessage, CallbackInfo ci) {
Optional<Component> result = MinecraftEvents.PLAYER_MESSAGE.invoker().message(player, playerChatMessage);
Optional<Component> result = MinecraftEvents.PLAYER_MESSAGE.invoker().message(player, playerChatMessage.decoratedContent().getString());
if (result.isPresent()) {
//#if MC > 11900
SERVER.getPlayerList().broadcastChatMessage(playerChatMessage.withUnsignedContent(result.get()), this.player, ChatType.bind(ChatType.CHAT, player));
Expand Down

0 comments on commit a28a873

Please sign in to comment.