Merge branch 'master' into 1.16

This commit is contained in:
gnembon committed 2020-05-15 20:00:55 -03:00
commit aabfda3797
6 files changed
+256 -24

No files matched your search

+26
View File
@@ -4,6 +4,7 @@ import carpet.settings.ParsedRule;
import carpet.settings.Rule;
import carpet.settings.SettingsManager;
import carpet.settings.Validator;
import carpet.utils.Translations;
import carpet.utils.Messenger;
import carpet.utils.SpawnChunks;
import net.minecraft.server.MinecraftServer;
@@ -41,6 +42,31 @@ public class CarpetSettings
public static Vec3d fixedPosition = null;
public static int runPermissionLevel = 2;
private static class LanguageValidator extends Validator<String> {
@Override public String validate(ServerCommandSource source, ParsedRule<String> currentRule, String newValue, String string) {
if (currentRule.get().equals(newValue))
{
return newValue;
}
if (!Translations.isValidLanguage(newValue))
{
Messenger.m(source, "r "+newValue+" is not a valid language");
return null;
}
CarpetSettings.language = newValue;
Translations.updateLanguage(source);
return newValue;
}
}
@Rule(
desc = "sets the language for carpet",
category = FEATURE,
options = {"none", "zh_cn"},
strict = false,
validate = LanguageValidator.class
)
public static String language = "none";
@Rule(
desc = "Nether portals correctly place entities going through",
extra = "Entities shouldn't suffocate in obsidian",
@@ -1,6 +1,7 @@
package carpet.settings;
import carpet.CarpetServer;
import carpet.utils.Translations;
import carpet.utils.Messenger;
import com.google.common.collect.ImmutableList;
import net.minecraft.server.command.ServerCommandSource;
@@ -12,6 +13,8 @@ import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import static carpet.utils.Translations.tr;
public final class ParsedRule<T> implements Comparable<ParsedRule> {
public final Field field;
public final String name;
@@ -225,4 +228,32 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
{
return this.name + ": " + getAsString();
}
private String translationKey()
{
return String.format("rule.%s.name", name);
}
public String translatedName(){
String key = translationKey();
return Translations.hasTranslation(key) ? tr(key) + String.format(" (%s)", name): name;
}
public String translatedDescription()
{
return tr(String.format("rule.%s.desc", (name)), description);
}
public List<String> translatedExtras()
{
if (!Translations.hasTranslations()) return extraInfo;
String keyBase = String.format("rule.%s.extra.", name);
List<String> extras = new ArrayList<>();
int i = 0;
while (Translations.hasTranslation(keyBase+i))
{
extras.add(Translations.tr(keyBase+i));
}
return (extras.isEmpty()) ? extraInfo : extras;
}
}
@@ -2,6 +2,7 @@ package carpet.settings;
import carpet.CarpetServer;
import carpet.CarpetSettings;
import carpet.utils.Translations;
import carpet.utils.Messenger;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
@@ -40,6 +41,7 @@ import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import static carpet.utils.Translations.tr;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
import static net.minecraft.server.command.CommandSource.suggestMatching;
@@ -351,7 +353,7 @@ public class SettingsManager
String ruleName = StringArgumentType.getString(ctx, "rule");
ParsedRule<?> rule = getRule(ruleName);
if (rule == null)
throw new SimpleCommandExceptionType(Messenger.c("rb Unknown rule: "+ruleName)).create();
throw new SimpleCommandExceptionType(Messenger.c("rb "+ tr("ui.unknown_rule","Unknown rule")+": "+ruleName)).create();
return rule;
}
@@ -367,18 +369,18 @@ public class SettingsManager
LiteralArgumentBuilder<ServerCommandSource> literalargumentbuilder = literal(identifier).requires((player) ->
player.hasPermissionLevel(2) && !locked);
literalargumentbuilder.executes((context)->listAllSettings(context.getSource())).
literalargumentbuilder.executes((context)-> listAllSettings(context.getSource())).
then(literal("list").
executes( (c) -> listSettings(c.getSource(), "All "+fancyName+" Settings",
executes( (c) -> listSettings(c.getSource(), String.format(tr("ui.all_%(mod)s_settings","All %s Settings"), fancyName),
getRules())).
then(literal("defaults").
executes( (c)-> listSettings(c.getSource(),
"Current "+fancyName+" Startup Settings from "+identifier+".conf",
String.format(tr("ui.current_%(mod)s_startup_settings_from_%(conf)s","Current %s Startup Settings from %s"), fancyName, (identifier+".conf")),
findStartupOverrides()))).
then(argument("tag",StringArgumentType.word()).
suggests( (c, b)->suggestMatching(getCategories(), b)).
executes( (c) -> listSettings(c.getSource(),
String.format(fancyName+" Settings matching \"%s\"", StringArgumentType.getString(c, "tag")),
String.format(tr("ui.%(mod)s_settings_matching_'%(query)s'","%s Settings matching \"%s\""), fancyName, tr("category." + StringArgumentType.getString(c, "tag"),StringArgumentType.getString(c, "tag"))),
getRulesMatching(StringArgumentType.getString(c, "tag")))))).
then(literal("removeDefault").
requires(s -> !locked).
@@ -406,33 +408,35 @@ public class SettingsManager
private int displayRuleMenu(ServerCommandSource source, ParsedRule<?> rule)
{
PlayerEntity player;
String displayName = rule.translatedName();
try
{
player = source.getPlayer();
}
catch (CommandSyntaxException e)
{
Messenger.m(source, "w "+rule.name +" is set to: ","wb "+rule.getAsString());
Messenger.m(source, "w "+ displayName +" "+ tr( "ui.is_set_to","is set to")+": ","wb "+rule.getAsString());
return 1;
}
Messenger.m(player, "");
Messenger.m(player, "wb "+rule.name,"!/"+identifier+" "+rule.name,"^g refresh");
Messenger.m(player, "w "+rule.description);
Messenger.m(player, "wb "+ displayName ,"!/"+identifier+" "+rule.name,"^g refresh");
Messenger.m(player, "w "+ rule.translatedDescription());
rule.extraInfo.forEach(s -> Messenger.m(player, "g "+s));
rule.translatedExtras().forEach(s -> Messenger.m(player, "g "+s));
List<BaseText> tags = new ArrayList<>();
tags.add(Messenger.c("w Tags: "));
tags.add(Messenger.c("w "+ tr("ui.tags", "Tags")+": "));
for (String t: rule.categories)
{
tags.add(Messenger.c("c ["+t+"]", "^g list all "+t+" settings","!/"+identifier+" list "+t));
String translated = tr("category." + t, t);
tags.add(Messenger.c("c ["+ translated +"]", "^g "+ String.format(tr("list_all_%s_settings","list all %s settings"), translated),"!/"+identifier+" list "+t));
tags.add(Messenger.c("w , "));
}
tags.remove(tags.size()-1);
Messenger.m(player, tags.toArray(new Object[0]));
Messenger.m(player, "w Current value: ",String.format("%s %s (%s value)",rule.getBoolValue()?"lb":"nb", rule.getAsString(),rule.isDefault()?"default":"modified"));
Messenger.m(player, "w "+ tr("ui.current_value", "Current value")+": ",String.format("%s %s (%s value)",rule.getBoolValue()?"lb":"nb", rule.getAsString(),rule.isDefault()?"default":"modified"));
List<BaseText> options = new ArrayList<>();
options.add(Messenger.c("w Options: ", "y [ "));
for (String o: rule.options)
@@ -450,8 +454,8 @@ public class SettingsManager
private int setRule(ServerCommandSource source, ParsedRule<?> rule, String newValue)
{
if (rule.set(source, newValue) != null)
Messenger.m(source, "w "+rule.toString()+", ", "c [change permanently?]",
"^w Click to keep the settings in "+identifier+".conf to save across restarts",
Messenger.m(source, "w "+rule.toString()+", ", "c ["+ tr("ui.change_permanently","change permanently")+"?]",
"^w "+String.format(tr("ui.click_to_keep_the_settings_in_%(conf)s_to_save_across_restarts","Click to keep the settings in %s to save across restarts"), identifier+".conf"),
"?/"+identifier+" setDefault "+rule.name+" "+rule.getAsString());
return 1;
}
@@ -466,7 +470,7 @@ public class SettingsManager
writeSettingsToConf(conf.getLeft()); // this may feels weird, but if conf
// is locked, it will never reach this point.
rule.set(source,stringValue);
Messenger.m(source ,"gi rule "+ rule.name+" will now default to "+ stringValue);
Messenger.m(source ,"gi "+String.format(tr("ui.rule_%(rule)s_will_now_default_to_%(value)s","rule %s will now default to %s"), rule.translatedName(), stringValue));
return 1;
}
// removes overrides of the default values in the file
@@ -483,10 +487,11 @@ public class SettingsManager
private BaseText displayInteractiveSetting(ParsedRule<?> rule)
{
String displayName = rule.translatedName();
List<Object> args = new ArrayList<>();
args.add("w - "+rule.name+" ");
args.add("w - "+ displayName +" ");
args.add("!/"+identifier+" "+rule.name);
args.add("^y "+rule.description);
args.add("^y "+rule.translatedDescription());
for (String option: rule.options)
{
args.add(makeSetRuleButton(rule, option, true));
@@ -512,10 +517,10 @@ public class SettingsManager
}
String baseText = style + (brackets ? " [" : " ") + option + (brackets ? "]" : "");
if (locked)
return Messenger.c(baseText, "^g "+fancyName+" settings are locked");
return Messenger.c(baseText, "^g "+fancyName+" " + tr("ui.settings_are_locked","settings are locked"));
if (option.equalsIgnoreCase(rule.getAsString()))
return Messenger.c(baseText);
return Messenger.c(baseText, "^g Switch to " + option+(option.equalsIgnoreCase(rule.defaultAsString)?" (default)":""), "?/"+identifier+" " + rule.name + " " + option);
return Messenger.c(baseText, "^g "+ tr("ui.switch_to","Switch to") +" " + option+(option.equalsIgnoreCase(rule.defaultAsString)?" (default)":""), "?/"+identifier+" " + rule.name + " " + option);
}
private int listSettings(ServerCommandSource source, String title, Collection<ParsedRule<?>> settings_list)
@@ -536,19 +541,26 @@ public class SettingsManager
}
private int listAllSettings(ServerCommandSource source)
{
listSettings(source, "Current "+fancyName+" Settings", getNonDefault());
//listSettings(source, "Current "+fancyName+" Settings", getNonDefault());
listSettings(source, String.format(tr("ui.current_%(mod)s_settings","Current %s Settings"), fancyName), getNonDefault());
if (version != null)
Messenger.m(source, "g "+fancyName+" version: "+ version);
//Messenger.m(source, "g "+fancyName+" version: "+ version);
Messenger.m(source, "g "+fancyName+" "+ tr("ui.version", "version") + ": "+ version);
try
{
PlayerEntity player = source.getPlayer();
List<Object> tags = new ArrayList<>();
tags.add("w Browse Categories:\n");
//tags.add("w Browse Categories:\n");
tags.add("w " + tr("ui.browse_categories", "Browse Categories") + ":\n");
for (String t : getCategories())
{
tags.add("c [" + t+"]");
tags.add("^g list all " + t + " settings");
String catKey = "category." + t;
String translated = tr(catKey, t);
String translatedPlus = Translations.hasTranslation(catKey) ? String.format("%s (%s)",tr(catKey, t), t) : t;
tags.add("c [" + translated +"]");
//tags.add("^g list all " + translated + " settings");
tags.add("^g " + String.format(tr("ui.list_all_%(cat)s_settings","list all %s settings"), translatedPlus));
tags.add("!/"+identifier+" list " + t);
tags.add("w ");
}
@@ -0,0 +1,69 @@
package carpet.utils;
import carpet.CarpetSettings;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import net.minecraft.server.command.ServerCommandSource;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
public class Translations
{
private static Map<String, String> translationMap;
public static String tr(String key)
{
return translationMap == null ? key : translationMap.getOrDefault(key, key);
}
public static String tr(String key, String str)
{
return translationMap == null ? str : translationMap.getOrDefault(key, str);
}
public static boolean hasTranslations()
{
return translationMap != null;
}
public static boolean hasTranslation(String key)
{
return translationMap != null && translationMap.containsKey(key);
}
public static void updateLanguage(ServerCommandSource source)
{
if (CarpetSettings.language.equalsIgnoreCase("none"))
{
translationMap = null;
return;
}
String langJs;
try
{
langJs = IOUtils.toString(
Objects.requireNonNull(Translations.class.getClassLoader().getResourceAsStream(String.format("assets/carpet/lang/%s.json", CarpetSettings.language))),
StandardCharsets.UTF_8);
} catch (NullPointerException | IOException e) {
Messenger.m(source, "r Failed to update language");
return;
}
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
Type type = new TypeToken<Map<String, String>>() {}.getType();
translationMap = gson.fromJson(langJs, type);
translationMap.entrySet().removeIf(e -> e.getKey().startsWith("//"));
}
public static boolean isValidLanguage(String newValue)
{
// will put some validations for availble languages at some point
return true;
}
}
@@ -0,0 +1,53 @@
// This file isn't necessary at all, because the language module would use the strings coded in the source if language "none" is selected.
// So en_us is the default language and probably no change will be made after loading this file.
// But it could be a good example for writing the language file for other languages.
{
"// Rules:": "",
"rule.portalSuffocationFix.name": "",
"// the key is genetated as:" : "String.format('rule.%s.name', rule.name)",
"rule.portalSuffocationFix.desc": "",
"// the key is genetated as:" : "String.format('rule.%s.desc', rule.name)",
"rule.portalSuffocationFix.extra.0": "",
"rule.portalSuffocationFix.extra.1": "",
"// the key is genetated as:" : "String.format('rule.%s.extra.%i', rule.name, index)",
"// Categories:": "",
"category.bugfix": "bugfix",
"category.survival": "survival",
"category.creative": "creative",
"category.experimental": "experimental",
"category.optimization": "optimization",
"category.feature": "feature",
"category.command": "command",
"category.tnt": "TNT",
"category.dispenser": "dispenser",
"category.scarpet": "scarpet",
"category.client": "client",
"// Settings command UI elements": "",
"ui.browse_categories": "Browse categories",
"ui.list_all_%(cat)s_settings": "list all %s settings",
"ui.version": "Version",
"ui.current_%(mod)s_settings": "Current %s settings",
"ui.switch_to": "Switch to",
"ui.settings_are_locked": "settings are locked",
"ui.is_set_to": "is set to",
"ui.unknown_rule": "Unknown rule",
"ui.current_%(mod)s_startup_settings_from_%(conf)s": "Current %s Startup Settings from %s",
"ui.%(mod)s_settings_matching_'%(query)s'": "%s settings matching \"%s\" ",
"ui.all_%(mod)s_settings": "All %s settings",
"ui.tags": "Tags",
"ui.change_permanently": "Change permanently",
"ui.click_to_keep_the_settings_in_%(conf)s_to_save_across_restarts": "click to keep the settings in %s to save across restarts",
"ui.rule_%(rule)s_will_now_default_to_%(value)s": "rule %s will now default to %s",
"ui.current_value": "current value"
}
@@ -0,0 +1,41 @@
{
"rule.portalSuffocationFix.name": "传送门窒息修复",
"rule.portalSuffocationFix.desc": "使下界传送门正确地传送实体",
"rule.portalSuffocationFix.extra.0": "0000000",
"rule.portalSuffocationFix.extra.1": "1111111",
"category.bugfix": "BUG修复",
"category.survival": "生存",
"category.creative": "创造",
"category.experimental": "试验性",
"category.optimization": "优化",
"category.feature": "特性",
"category.command": "指令",
"category.tnt": "TNT",
"category.dispenser": "发射器",
"category.scarpet": "scarpet脚本语言",
"category.client": "客户端",
"ui.browse_categories": "浏览分类",
"ui.list_all_%(cat)s_settings": "列出所有 %s 选项",
"ui.version": "版本",
"ui.current_%(mod)s_settings": "目前启用的 %s 选项",
"ui.switch_to": "切换至",
"ui.settings_are_locked": "选项被锁定",
"ui.is_set_to": "被设置为",
"ui.unknown_rule": "未知的规则选项",
"ui.current_%(mod)s_startup_settings_from_%(conf)s": "目前启用的%s选项,从%s中读取得",
"ui.%(mod)s_settings_matching_'%(query)s'": "%s中符合\"%s\"的选项:",
"ui.all_%(mod)s_settings": "所有的%s选项",
"ui.tags": "分类",
"ui.change_permanently": "永久更改",
"ui.click_to_keep_the_settings_in_%(conf)s_to_save_across_restarts": "点击此按钮以使此选项保存于%s,重启后依旧生效",
"ui.rule_%(rule)s_will_now_default_to_%(value)s": "规则%s将会被默认设置为%s",
"ui.current_value": "当前的值"
}