Merge branch 'master' of https://github.com/gnembon/fabric-carpet
This commit is contained in:
commit
68093ed54a
6 files changed
+249
-23
No files matched your search
@@ -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("carpet.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("carpet.rule.%s.desc", (name)), description);
|
||||
}
|
||||
|
||||
public List<String> translatedExtras()
|
||||
{
|
||||
if (!Translations.hasTranslations()) return extraInfo;
|
||||
String keyBase = String.format("carpet.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;
|
||||
@@ -39,6 +40,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;
|
||||
@@ -350,7 +352,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("carpet.ui.statement.unknown_rule","Unknown rule")+": "+ruleName)).create();
|
||||
return rule;
|
||||
}
|
||||
|
||||
@@ -368,16 +370,16 @@ public class SettingsManager
|
||||
|
||||
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("carpet.ui.statement.all_%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("carpet.ui.statement.current_%s_startup_settings_from_%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("carpet.ui.statement.%s_settings_matching_'%s'","%s Settings matching \"%s\""), fancyName, tr("carpet.ui.category." + StringArgumentType.getString(c, "tag"),StringArgumentType.getString(c, "tag"))),
|
||||
getRulesMatching(StringArgumentType.getString(c, "tag")))))).
|
||||
then(literal("removeDefault").
|
||||
requires(s -> !locked).
|
||||
@@ -405,33 +407,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( "carpet.ui.statement.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("carpet.ui.statement.tags", "Tags")+": "));
|
||||
for (String t: rule.categories)
|
||||
{
|
||||
tags.add(Messenger.c("c ["+t+"]", "^g list all "+t+" settings","!/"+identifier+" list "+t));
|
||||
String translated = tr("carpet.ui.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("carpet.ui.statement.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)
|
||||
@@ -449,8 +453,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("carpet.ui.statement.change_permanently","change permanently")+"?]",
|
||||
"^w "+String.format(tr("carpet.ui.statement.click_to_keep_the_settings_in_%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;
|
||||
}
|
||||
@@ -465,7 +469,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("carpet.ui.statement.rule_%s_will_now_default_to_%s","rule %s will now default to %s"), rule.translatedName(), stringValue));
|
||||
return 1;
|
||||
}
|
||||
// removes overrides of the default values in the file
|
||||
@@ -482,10 +486,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));
|
||||
@@ -511,10 +516,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("carpet.ui.statement.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("carpet.ui.statement.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)
|
||||
@@ -535,19 +540,25 @@ 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("carpet.ui.statement.current_%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("carpet.ui.statement.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("carpet.ui.statement.browse_categories", "Browse Categories") + ":\n");
|
||||
for (String t : getCategories())
|
||||
{
|
||||
tags.add("c [" + t+"]");
|
||||
tags.add("^g list all " + t + " settings");
|
||||
String translated = tr("carpet.ui.category." + t, t);
|
||||
String translatedPlus = Translations.hasTranslation("carpet.ui.category." + t) ? tr("carpet.ui.category." + t, t) + String.format(" (%s)", t) : t;
|
||||
tags.add("c [" + translated +"]");
|
||||
//tags.add("^g list all " + translated + " settings");
|
||||
tags.add("^g " + String.format(tr("carpet.ui.statement.list_all_%s_settings","list all %s settings"), translatedPlus));
|
||||
tags.add("!/"+identifier+" list " + t);
|
||||
tags.add("w ");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
public static boolean isValidLanguage(String newValue)
|
||||
{
|
||||
// will put some validations for availble languages at some point
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// 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.
|
||||
|
||||
{
|
||||
"carpet.rule.portalSuffocationFix.name": "",
|
||||
"// the key is genetated as:" : "String.format('carpet.rule.%s.name', rule.name)",
|
||||
|
||||
|
||||
"carpet.rule.portalSuffocationFix.desc": "",
|
||||
"// the key is genetated as:" : "String.format('carpet.rule.%s.desc', rule.name)",
|
||||
|
||||
|
||||
"carpet.rule.portalSuffocationFix.extra.0": "",
|
||||
"carpet.rule.portalSuffocationFix.extra.1": "",
|
||||
"// the key is genetated as:" : "String.format('carpet.rule.%s.extra.%i', rule.name, index)",
|
||||
|
||||
|
||||
"carpet.ui.category.bugfix": "bugfix",
|
||||
"carpet.ui.category.survival": "survival",
|
||||
"carpet.ui.category.creative": "creative",
|
||||
"carpet.ui.category.experimental": "experimental",
|
||||
"carpet.ui.category.optimization": "optimization",
|
||||
"carpet.ui.category.feature": "feature",
|
||||
"carpet.ui.category.command": "command",
|
||||
"carpet.ui.category.tnt": "TNT",
|
||||
"carpet.ui.category.dispenser": "dispenser",
|
||||
"carpet.ui.category.scarpet": "scarpet",
|
||||
"carpet.ui.category.client": "client",
|
||||
|
||||
|
||||
"carpet.ui.statement.browse_categories": "Brouse categories",
|
||||
"carpet.ui.statement.list_all_%s_settings": "list all %s settings",
|
||||
"carpet.ui.statement.version": "Version",
|
||||
"carpet.ui.statement.current_%s_settings": "Current %s settings",
|
||||
"carpet.ui.statement.switch_to": "Switch to",
|
||||
"carpet.ui.statement.settings_are_locked": "settings are locked",
|
||||
"carpet.ui.statement.is_set_to": "is set to",
|
||||
"carpet.ui.statement.unknown_rule": "Unknown ruls",
|
||||
"carpet.ui.statement.current_%s_startup_settings_from_%s": "current %s startup settings from %s",
|
||||
"carpet.ui.statement.%s_settings_matching_'%s'": "%s settings matching \"%s\" ",
|
||||
"carpet.ui.statement.all_%s_settings": "all %s settings",
|
||||
"carpet.ui.statement.tags": "Tags",
|
||||
"carpet.ui.statement.change_permanently": "Change permanently",
|
||||
"carpet.ui.statement.click_to_keep_the_settings_in_%s_to_save_across_restarts": "click to keep the settings in %s to save across restarts",
|
||||
"carpet.ui.statement.rule_%s_will_now_default_to_%s": "rule %s will now default to %s",
|
||||
"carpet.ui.statement.current_value": "current value"
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"carpet.rule.portalSuffocationFix.name": "传送门窒息修复",
|
||||
|
||||
|
||||
"carpet.rule.portalSuffocationFix.desc": "使下界传送门正确地传送实体",
|
||||
|
||||
|
||||
"carpet.rule.portalSuffocationFix.extra.0": "0000000",
|
||||
"carpet.rule.portalSuffocationFix.extra.1": "1111111",
|
||||
|
||||
|
||||
"carpet.ui.category.bugfix": "BUG修复",
|
||||
"carpet.ui.category.survival": "生存",
|
||||
"carpet.ui.category.creative": "创造",
|
||||
"carpet.ui.category.experimental": "试验性",
|
||||
"carpet.ui.category.optimization": "优化",
|
||||
"carpet.ui.category.feature": "特性",
|
||||
"carpet.ui.category.command": "指令",
|
||||
"carpet.ui.category.tnt": "TNT",
|
||||
"carpet.ui.category.dispenser": "发射器",
|
||||
"carpet.ui.category.scarpet": "SCarpet脚本语言",
|
||||
"carpet.ui.category.client": "客户端",
|
||||
|
||||
"carpet.ui.statement.browse_categories": "浏览分类",
|
||||
"carpet.ui.statement.list_all_%s_settings": "列出所有 %s 选项",
|
||||
"carpet.ui.statement.version": "版本",
|
||||
"carpet.ui.statement.current_%s_settings": "目前启用的 %s 选项",
|
||||
"carpet.ui.statement.switch_to": "切换至",
|
||||
"carpet.ui.statement.settings_are_locked": "选项被锁定",
|
||||
"carpet.ui.statement.is_set_to": "被设置为",
|
||||
"carpet.ui.statement.unknown_rule": "未知的规则选项",
|
||||
"carpet.ui.statement.current_%s_startup_settings_from_%s": "目前启用的%s选项,从%s中读取得",
|
||||
"carpet.ui.statement.%s_settings_matching_'%s'": "%s中符合\"%s\"的选项:",
|
||||
"carpet.ui.statement.all_%s_settings": "所有的%s选项",
|
||||
"carpet.ui.statement.tags": "分类",
|
||||
"carpet.ui.statement.change_permanently": "永久更改",
|
||||
"carpet.ui.statement.click_to_keep_the_settings_in_%s_to_save_across_restarts": "点击此按钮以使此选项保存于%s,重启后依旧生效",
|
||||
"carpet.ui.statement.rule_%s_will_now_default_to_%s": "规则%s将会被默认设置为%s",
|
||||
"carpet.ui.statement.current_value": "当前的值"
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user