From ce3b0836264fc5cb2c3f589c95e8caea9ec5acc2 Mon Sep 17 00:00:00 2001 From: MATRIX-feather Date: Fri, 8 Dec 2023 16:35:34 +0800 Subject: [PATCH] =?UTF-8?q?enhance:=20=E8=B0=83=E6=95=B4BindableList?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E8=AE=BE=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- src/main/java/xiamomc/morph/MorphManager.java | 2 +- src/main/java/xiamomc/morph/MorphPlugin.java | 3 +- .../subcommands/plugin/OptionSubCommand.java | 76 ++++++++++++++++++- .../morph/config/MorphConfigManager.java | 55 +++++++++++--- .../morph/providers/DisguiseProvider.java | 4 +- 6 files changed, 126 insertions(+), 16 deletions(-) diff --git a/gradle.properties b/gradle.properties index 832349c9..ae012ded 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ minecraft_version=1.20.2-R0.1-SNAPSHOT # Dependencies ld_version=10.0.33 -pluginbase_version=17d180a +pluginbase_version=76b1510 gsit_version=1.4.1 papi_version=2.11.3 bstats_version=3.0.1 diff --git a/src/main/java/xiamomc/morph/MorphManager.java b/src/main/java/xiamomc/morph/MorphManager.java index 63e1f0da..9cfa5cfd 100644 --- a/src/main/java/xiamomc/morph/MorphManager.java +++ b/src/main/java/xiamomc/morph/MorphManager.java @@ -135,7 +135,7 @@ private void load() logger.info("Using backend: %s".formatted(currentBackend)); - bannedDisguises = config.getBindableList(ConfigOption.BANNED_DISGUISES); + bannedDisguises = config.getBindableList(String.class, ConfigOption.BANNED_DISGUISES); config.bind(allowHeadMorph, ConfigOption.ALLOW_HEAD_MORPH); config.bind(allowAcquireMorph, ConfigOption.ALLOW_ACQUIRE_MORPHS); config.bind(useClientRenderer, ConfigOption.USE_CLIENT_RENDERER); diff --git a/src/main/java/xiamomc/morph/MorphPlugin.java b/src/main/java/xiamomc/morph/MorphPlugin.java index 2414f29f..210d7551 100644 --- a/src/main/java/xiamomc/morph/MorphPlugin.java +++ b/src/main/java/xiamomc/morph/MorphPlugin.java @@ -105,7 +105,6 @@ public void onEnable() dependencyManager.cache(morphManager = new MorphManager()); dependencyManager.cache(skillHandler = new MorphSkillHandler()); dependencyManager.cache(abilityHandler = new AbilityHandler()); - dependencyManager.cache(cmdHelper = new MorphCommandManager()); dependencyManager.cache(new RevealingHandler()); dependencyManager.cache(new Transformer()); @@ -121,6 +120,8 @@ public void onEnable() dependencyManager.cacheAs(MorphConfigManager.class, new MorphConfigManager(this)); dependencyManager.cache(playerTracker); + dependencyManager.cache(cmdHelper = new MorphCommandManager()); + dependencyManager.cache(new SkillAbilityConfigurationStore()); dependencyManager.cache(new MessageUtils()); diff --git a/src/main/java/xiamomc/morph/commands/subcommands/plugin/OptionSubCommand.java b/src/main/java/xiamomc/morph/commands/subcommands/plugin/OptionSubCommand.java index 7298e390..c67202f8 100644 --- a/src/main/java/xiamomc/morph/commands/subcommands/plugin/OptionSubCommand.java +++ b/src/main/java/xiamomc/morph/commands/subcommands/plugin/OptionSubCommand.java @@ -25,7 +25,7 @@ public class OptionSubCommand extends MorphPluginObject implements ISubCommand return "option"; } - @Resolved + @Resolved(shouldSolveImmediately = true) private MorphConfigManager config; public OptionSubCommand() @@ -74,6 +74,8 @@ public OptionSubCommand() subCommands.add(getToggle("allow_flight", ConfigOption.ALLOW_FLIGHT, "allow_flight")); subCommands.add(getToggle("client_renderer", ConfigOption.USE_CLIENT_RENDERER, "client_renderer")); + + subCommands.add(getList("banned_disguises", ConfigOption.BANNED_DISGUISES, "banned_disguises", null)); } private ISubCommand getGeneric(String name, ConfigOption option, String perm, @@ -84,6 +86,78 @@ private ISubCommand getGeneric(String name, ConfigOption option, String perm displayName, targetClass, func, new FormattableMessage(plugin, typeName)); } + private ISubCommand getList(String optionName, ConfigOption option, String perm, + @Nullable FormattableMessage displayName) + { + var targetDisplay = displayName == null ? new FormattableMessage(plugin, optionName) : displayName; + + var bindableList = config.getBindableList(String.class, option); + + return SubCommandGenerator.command() + .setName(optionName) + .setPerm("xiamomc.morph.toggle." + perm) + .setExec((sender, args) -> + { + if (args.length < 1) + { + var displayValue = bindableList.toString(); + sender.sendMessage(MessageUtils.prefixes(sender, + CommandStrings.optionValueString() + .withLocale(MessageUtils.getLocale(sender)) + .resolve("what", targetDisplay, null) + .resolve("value", displayValue))); + + return true; + } + + if (args.length < 2) + { + sender.sendMessage(MessageUtils.prefixes(sender, "参数数量不对")); + return true; + } + + var operation = args[0]; + if (operation.equalsIgnoreCase("add")) + { + var value = args[1]; + try + { + bindableList.add(value); + + //bug: bindableList的add和remove方法永远返回true + if (bindableList.contains(value)) + sender.sendMessage(MessageUtils.prefixes(sender, "成功添加" + value)); + else + sender.sendMessage(MessageUtils.prefixes(sender, "未能添加" + value + ", 可能是类型不对")); + } + catch (Throwable t) + { + sender.sendMessage(MessageUtils.prefixes(sender, "未能添加" + value + ", 可能是类型不对")); + logger.error("Error adding option to bindable list: " + t.getMessage()); + } + + return true; + } + else if (operation.equalsIgnoreCase("remove")) + { + var value = args[1]; + bindableList.remove(value); + + if (!bindableList.contains(value)) + sender.sendMessage(MessageUtils.prefixes(sender, "成功移除" + value)); + else + sender.sendMessage(MessageUtils.prefixes(sender, "未能移除" + value + ", 可能是其不在列表中")); + + return true; + } + else + { + sender.sendMessage(MessageUtils.prefixes(sender, "未知操作: " + operation)); + return true; + } + }); + } + private ISubCommand getGeneric(String name, ConfigOption option, String perm, @Nullable FormattableMessage displayName, Class targetClass, Function func, FormattableMessage typeName) diff --git a/src/main/java/xiamomc/morph/config/MorphConfigManager.java b/src/main/java/xiamomc/morph/config/MorphConfigManager.java index 7d0cffec..74121f6a 100644 --- a/src/main/java/xiamomc/morph/config/MorphConfigManager.java +++ b/src/main/java/xiamomc/morph/config/MorphConfigManager.java @@ -76,16 +76,37 @@ public Map getAllNotDefault() return map; } - public BindableList getBindableList(ConfigOption option) + private Map> bindableLists; + + public BindableList getBindableList(Class clazz, ConfigOption option) { - var originalBindable = getBindable(List.class, option.node, List.of()); - var list = new BindableList(originalBindable.get()); + ensureBindableListNotNull(); + + //System.out.println("GET LIST " + option.toString()); - originalBindable.onValueChanged((o, n) -> + var val = bindableLists.getOrDefault(option.toString(), null); + if (val != null) { - list.clear(); - list.addAll(n); - }); + //System.out.println("FIND EXISTING LIST, RETURNING " + val); + return (BindableList) val; + } + + List originalList = backendConfig.getList(option.toString(), new ArrayList()); + originalList.removeIf(listVal -> !clazz.isInstance(listVal)); //Don't work for somehow + + var list = new BindableList(); + list.addAll((List)originalList); + + list.onListChanged((diffList, reason) -> + { + //System.out.println("LIST CHANGED: " + diffList + " WITH REASON " + reason); + backendConfig.set(option.toString(), list); + save(); + }, true); + + bindableLists.put(option.toString(), list); + + //System.out.println("RETURN " + list); return list; } @@ -108,12 +129,12 @@ public void bind(Bindable bindable, ConfigOption option) throw new IllegalArgumentException("尝试将一个Bindable绑定在不兼容的配置(" + option + ")上"); } - public void bind(BindableList bindable, ConfigOption option) + public void bind(Class clazz, BindableList bindable, ConfigOption option) { - var bb = this.getBindableList(option); + var bb = this.getBindableList(clazz, option); if (bindable.getClass().isInstance(bb)) - bindable.bindTo((BindableList) bb); + bindable.bindTo(bb); else throw new IllegalArgumentException("尝试将一个Bindable绑定在不兼容的配置(" + option + ")上"); } @@ -123,11 +144,25 @@ public Bindable getBindable(Class type, ConfigOption path, T defaultVa return super.getBindable(type, path.node, defaultValue); } + private void ensureBindableListNotNull() + { + if (bindableLists == null) + bindableLists = new Object2ObjectOpenHashMap<>(); + } + @Override public void reload() { super.reload(); + ensureBindableListNotNull(); + bindableLists.forEach((node, list) -> + { + var configList = backendConfig.getList(node); + list.clear(); + list.addAllInternal(configList); + }); + //更新配置 int targetVersion = 27; diff --git a/src/main/java/xiamomc/morph/providers/DisguiseProvider.java b/src/main/java/xiamomc/morph/providers/DisguiseProvider.java index e63799b4..24ef7af8 100644 --- a/src/main/java/xiamomc/morph/providers/DisguiseProvider.java +++ b/src/main/java/xiamomc/morph/providers/DisguiseProvider.java @@ -114,8 +114,8 @@ private void load(MorphConfigManager configManager) { if (!bindableInitialized) { - configManager.bind(blackListTags, ConfigOption.BLACKLIST_TAGS); - configManager.bind(blackListPatterns, ConfigOption.BLACKLIST_PATTERNS); + configManager.bind(String.class, blackListTags, ConfigOption.BLACKLIST_TAGS); + configManager.bind(String.class, blackListPatterns, ConfigOption.BLACKLIST_PATTERNS); bindableInitialized = true; }