All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.hendrixshen.magiclib.util.MessageUtil Maven / Gradle / Ivy

package top.hendrixshen.magiclib.util;

import net.minecraft.class_1937;
import net.minecraft.class_2168;
import net.minecraft.class_2554;
import net.minecraft.class_2561;
import net.minecraft.server.MinecraftServer;
import org.jetbrains.annotations.NotNull;
import top.hendrixshen.magiclib.MagicLibReference;
import top.hendrixshen.magiclib.compat.minecraft.api.network.chat.ComponentCompatApi;

import java.util.List;
import java.util.Optional;

public class MessageUtil {
    public static void sendMessage(class_2168 source, String message) {
        MessageUtil.sendMessage(source, ComponentCompatApi.literal(message));
    }

    public static void sendMessage(class_2168 source, class_2561 messages) {
        //#if MC > 11904
        //$$ Optional.ofNullable(source).ifPresent(sourceStack -> sourceStack.sendSuccess(() -> messages, source.getServer().getLevel(Level.OVERWORLD) != null));
        //#elseif MC > 11502
        Optional.ofNullable(source).ifPresent(sourceStack -> sourceStack.method_9226(messages, source.method_9211().method_3847(class_1937.field_25179) != null));
        //#else
        //$$ Optional.ofNullable(source).ifPresent(sourceStack -> sourceStack.sendSuccess(messages, source.getServer() != null && source.getServer().getLevel(DimensionType.OVERWORLD) != null));
        //#endif
    }

    public static void sendMessage(class_2168 source, List messages) {
        MessageUtil.sendMessage(source, MessageUtil.insertComponent(messages));
    }

    public static void sendServerMessage(MinecraftServer server, String message) {
        MessageUtil.sendServerMessage(server, ComponentCompatApi.literal(message));
    }

    public static void sendServerMessage(MinecraftServer server, class_2561 message) {
        Optional.of(server).ifPresent(s -> {
            MagicLibReference.getLogger().info(message.getString());
            s.method_3760().method_14571().forEach(p ->
                    //#if MC > 11802
                    //$$ p.sendSystemMessage(message));
                    //#elseif MC > 11502
                    p.method_9203(message, p.method_5667()));
                    //#else
                    //$$ p.sendMessage(message));
                    //#endif
        });
    }

    public static void sendServerMessage(MinecraftServer server, List messages) {
        MessageUtil.sendServerMessage(server, MessageUtil.insertComponent(messages));
    }

    //#if MC > 11802
    //$$ private static @NotNull MutableComponent insertComponent(@NotNull List messages) {
    //#else
    private static @NotNull class_2554 insertComponent(@NotNull List messages) {
    //#endif
        //#if MC > 11802
        //$$ MutableComponent components = ComponentCompatApi.literal("");
        //#else
        class_2554 components = ComponentCompatApi.literal("");
        //#endif
        messages.forEach(components::method_10852);
        return components;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy