Created
July 27, 2020 11:37
-
-
Save SizableShrimp/a4ab26e8fd1e943b14cce63c12020c6b to your computer and use it in GitHub Desktop.
LangManager for Forge mod 1.12.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.sizableshrimp.compactcommands.lang; | |
import me.sizableshrimp.compactcommands.CompactCommands; | |
import net.minecraft.command.ICommandSender; | |
import net.minecraft.entity.player.EntityPlayerMP; | |
import net.minecraft.network.NetworkManager; | |
import net.minecraft.util.text.ITextComponent; | |
import net.minecraft.util.text.TextComponentString; | |
import net.minecraft.util.text.TextComponentTranslation; | |
import net.minecraft.util.text.translation.I18n; | |
import net.minecraftforge.fml.common.network.handshake.NetworkDispatcher; | |
import net.minecraftforge.server.command.TextComponentHelper; | |
import java.util.Map; | |
public class LangManager { | |
public static final LangManager INSTANCE = new LangManager(CompactCommands.MOD_ID, CompactCommands.VERSION); | |
private final String modid; | |
private final String version; | |
public LangManager(String modid, String version) { | |
this.modid = modid; | |
this.version = version; | |
} | |
public ITextComponent localizeForPlayer(ICommandSender sender, String translationKey, Object... args) { | |
if (!(sender instanceof EntityPlayerMP)) | |
return new TextComponentTranslation(translationKey, args); | |
EntityPlayerMP player = (EntityPlayerMP) sender; | |
ITextComponent message = TextComponentHelper.createComponentTranslation(player, translationKey, args); | |
if (message instanceof TextComponentString) { | |
return message; | |
} | |
if (hasLatestVersion(player)) | |
return message; | |
// Localize for client if they don't have the mod or don't have the latest version | |
return new TextComponentString(localizeFormatted(translationKey, args)); | |
} | |
public boolean hasLatestVersion(EntityPlayerMP player) { | |
NetworkManager manager = player.connection.getNetworkManager(); | |
Map<String, String> modList = NetworkDispatcher.get(manager).getModList(); | |
if (modList != null) { | |
String ver = modList.get(modid); | |
return version.equals(ver); | |
} | |
return false; | |
} | |
public String getLocalized(ICommandSender sender, String translationKey) { | |
if (sender instanceof EntityPlayerMP) { | |
EntityPlayerMP player = (EntityPlayerMP) sender; | |
if (hasLatestVersion(player)) | |
return translationKey; | |
return localize(translationKey); | |
} | |
return translationKey; | |
} | |
private String localize(String translationKey) { | |
return I18n.translateToLocal(translationKey); | |
} | |
private String localizeFormatted(String translationKey, Object... args) { | |
return I18n.translateToLocalFormatted(translationKey, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment