Skip to content

Commit

Permalink
enhance: 调整BindableList相关设定
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Dec 8, 2023
1 parent 7812a8a commit ce3b083
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/main/java/xiamomc/morph/MorphManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/xiamomc/morph/MorphPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class OptionSubCommand extends MorphPluginObject implements ISubCommand
return "option";
}

@Resolved
@Resolved(shouldSolveImmediately = true)
private MorphConfigManager config;

public OptionSubCommand()
Expand Down Expand Up @@ -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 <T> ISubCommand getGeneric(String name, ConfigOption option, String perm,
Expand All @@ -84,6 +86,78 @@ private <T> 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 <T> ISubCommand getGeneric(String name, ConfigOption option, String perm,
@Nullable FormattableMessage displayName, Class<T> targetClass,
Function<String, T> func, FormattableMessage typeName)
Expand Down
55 changes: 45 additions & 10 deletions src/main/java/xiamomc/morph/config/MorphConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,37 @@ public Map<ConfigNode, Object> getAllNotDefault()
return map;
}

public <T> BindableList<T> getBindableList(ConfigOption option)
private Map<String, BindableList<?>> bindableLists;

public <T> BindableList<T> getBindableList(Class<T> clazz, ConfigOption option)
{
var originalBindable = getBindable(List.class, option.node, List.of());
var list = new BindableList<T>(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<T>) val;
}

List<?> originalList = backendConfig.getList(option.toString(), new ArrayList<T>());
originalList.removeIf(listVal -> !clazz.isInstance(listVal)); //Don't work for somehow

var list = new BindableList<T>();
list.addAll((List<T>)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;
}
Expand All @@ -108,12 +129,12 @@ public <T> void bind(Bindable<T> bindable, ConfigOption option)
throw new IllegalArgumentException("尝试将一个Bindable绑定在不兼容的配置(" + option + ")上");
}

public <T> void bind(BindableList<T> bindable, ConfigOption option)
public <T> void bind(Class<T> clazz, BindableList<T> bindable, ConfigOption option)
{
var bb = this.getBindableList(option);
var bb = this.getBindableList(clazz, option);

if (bindable.getClass().isInstance(bb))
bindable.bindTo((BindableList<T>) bb);
bindable.bindTo(bb);
else
throw new IllegalArgumentException("尝试将一个Bindable绑定在不兼容的配置(" + option + ")上");
}
Expand All @@ -123,11 +144,25 @@ public <T> Bindable<T> getBindable(Class<T> 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;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/xiamomc/morph/providers/DisguiseProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit ce3b083

Please sign in to comment.