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

top.hendrixshen.magiclib.mixin.minecraft.i18n.ServerPlayerMixin Maven / Gradle / Ivy

There is a newer version: 0.6.59
Show newest version
/*
 * This file is part of the Carpet TIS Addition project, licensed under the
 * GNU Lesser General Public License v3.0
 *
 * Copyright (C) 2023  Fallen_Breath and contributors
 *
 * Carpet TIS Addition is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Carpet TIS Addition is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Carpet TIS Addition.  If not, see .
 */

package top.hendrixshen.magiclib.mixin.minecraft.i18n;

import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import top.hendrixshen.magiclib.api.compat.minecraft.network.chat.MutableComponentCompat;
import top.hendrixshen.magiclib.api.fake.i18n.ServerPlayerLanguage;
import top.hendrixshen.magiclib.impl.i18n.minecraft.translation.MagicTranslation;
import top.hendrixshen.magiclib.util.MiscUtil;

import java.util.Locale;
import net.minecraft.class_2561;
import net.minecraft.class_2803;
import net.minecraft.class_3222;
import net.minecraft.class_5250;

//#if MC > 11502 && MC < 11800
//$$ import top.hendrixshen.magiclib.mixin.minecraft.accessor.ServerboundClientInformationPacketAccessor;
//#endif

/**
 * Reference to Carpet-TIS-Addition
 */
@Mixin(class_3222.class)
public class ServerPlayerMixin implements ServerPlayerLanguage {
    @Unique
    private String magiclib$language = "en_us";

    @Inject(
            method = "updateOptions",
            at = @At(
                    value = "HEAD"
            )
    )
    private void preUpdateOptions(
            //#if MC > 12001
            //$$ @NotNull ClientInformation clientInformation,
            //#else
            @NotNull class_2803 serverboundClientInformationPacket,
            //#endif
            CallbackInfo ci
    ) {
        //#if MC > 12001
        //$$ this.magiclib$language = clientInformation.language().toLowerCase(Locale.ROOT);
        //#elseif MC > 11701
        this.magiclib$language = serverboundClientInformationPacket.comp_266().toLowerCase(Locale.ROOT);
        //#elseif MC > 11502
        //$$ this.magiclib$language = ((ServerboundClientInformationPacketAccessor) serverboundClientInformationPacket)
        //$$         .magiclib$getLanguage().toLowerCase(Locale.ROOT);
        //#else
        //$$ this.magiclib$language = serverboundClientInformationPacket.getLanguage().toLowerCase(Locale.ROOT);
        //#endif
    }

    @Override
    public String magicLib$getLanguage() {
        return this.magiclib$language;
    }

    /**
     * This handle all Magic translation on chat messages
     */
    @ModifyVariable(
            method = {
                    //#if MC > 11900
                    "sendSystemMessage(Lnet/minecraft/network/chat/Component;Z)V",
                    //#elseif MC > 11502
                    //$$ "sendMessage(Lnet/minecraft/network/chat/Component;Ljava/util/UUID;)V",
                    //$$ "sendMessage(Lnet/minecraft/network/chat/Component;Lnet/minecraft/network/chat/ChatType;Ljava/util/UUID;)V",
                    //#else
                    //$$ "displayClientMessage",
                    //$$ "sendMessage(Lnet/minecraft/network/chat/Component;Lnet/minecraft/network/chat/ChatType;)V",
                    //#endif
            },
            at = @At(
                    "HEAD"
            ),
            argsOnly = true
    )
    private class_2561 applyMagicTranslationToChatMessage(class_2561 message) {
        if (message instanceof
                //#if MC > 11502
                class_5250
            //#else
            //$$ BaseComponent
            //#endif
        ) {
            message = MagicTranslation.translate(MutableComponentCompat.of(MiscUtil.cast(message)),
                    (class_3222) (Object) this).get();
        }

        return message;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy