9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-19 14:59:21 +00:00

refactor: make attributes allow-listed instead of deny-listed

This is a better default - as a number of attributes are primarily synced through other means (potions, items), or were applied from a context-sensitive action that does not warrant syncing across server contexts (sprinting, flying)
This commit is contained in:
William
2024-09-05 13:37:53 +01:00
parent daaf5147a7
commit 605d314a58
3 changed files with 30 additions and 11 deletions

View File

@@ -571,9 +571,9 @@ public abstract class BukkitData implements Data {
final AttributeSettings settings = plugin.getSettings().getSynchronization().getAttributes(); final AttributeSettings settings = plugin.getSettings().getSynchronization().getAttributes();
Registry.ATTRIBUTE.forEach(id -> { Registry.ATTRIBUTE.forEach(id -> {
final AttributeInstance instance = player.getAttribute(id); final AttributeInstance instance = player.getAttribute(id);
if (instance == null || Double.compare(instance.getValue(), instance.getDefaultValue()) == 0 if (!settings.isSyncedAttribute(id.getKey().toString()) || instance == null
|| settings.isIgnoredAttribute(id.getKey().toString())) { || Double.compare(instance.getValue(), instance.getDefaultValue()) == 0) {
return; // We don't sync unmodified or disabled attributes return; // We don't sync attributes not marked as to be synced
} }
attributes.add(adapt(instance, settings)); attributes.add(adapt(instance, settings));
}); });

View File

@@ -275,12 +275,19 @@ public class Settings {
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class AttributeSettings { public static class AttributeSettings {
@Comment({"Which attributes should not be saved when syncing users. Supports wildcard matching.", @Comment({"Which attribute types should be saved as part of attribute syncing. Supports wildcard matching.",
"(e.g. ['minecraft:generic.max_health', 'minecraft:generic.*'])"}) "(e.g. ['minecraft:generic.max_health', 'minecraft:generic.*'])"})
@Getter(AccessLevel.NONE) @Getter(AccessLevel.NONE)
private List<String> ignoredAttributes = new ArrayList<>(List.of("")); private List<String> syncedAttributes = new ArrayList<>(List.of(
"minecraft:generic.max_health", "minecraft:max_health",
"minecraft:generic.max_absorption", "minecraft:max_absorption",
"minecraft:generic.luck", "minecraft:luck",
"minecraft:generic.scale", "minecraft:scale",
"minecraft:generic.step_height", "minecraft:step_height",
"minecraft:generic.gravity", "minecraft:gravity"
));
@Comment({"Which modifiers should not be saved when syncing users. Supports wildcard matching.", @Comment({"Which attribute modifiers should be saved. Supports wildcard matching.",
"(e.g. ['minecraft:effect.speed', 'minecraft:effect.*'])"}) "(e.g. ['minecraft:effect.speed', 'minecraft:effect.*'])"})
@Getter(AccessLevel.NONE) @Getter(AccessLevel.NONE)
private List<String> ignoredModifiers = new ArrayList<>(List.of( private List<String> ignoredModifiers = new ArrayList<>(List.of(
@@ -297,8 +304,8 @@ public class Settings {
return pat.contains("*") ? value.matches(pat.replace("*", ".*")) : pat.equals(value); return pat.contains("*") ? value.matches(pat.replace("*", ".*")) : pat.equals(value);
} }
public boolean isIgnoredAttribute(@NotNull String attribute) { public boolean isSyncedAttribute(@NotNull String attribute) {
return ignoredAttributes.stream().anyMatch(wildcard -> matchesWildcard(wildcard, attribute)); return syncedAttributes.stream().anyMatch(wildcard -> matchesWildcard(wildcard, attribute));
} }
public boolean isIgnoredModifier(@NotNull String modifier) { public boolean isIgnoredModifier(@NotNull String modifier) {

View File

@@ -136,10 +136,22 @@ synchronization:
- '*' - '*'
# Configuration for how to sync attributes # Configuration for how to sync attributes
attributes: attributes:
# Which attributes should not be saved when syncing users. Supports wildcard matching. # Which attribute types should be saved as part of attribute syncing. Supports wildcard matching.
# (e.g. ['minecraft:generic.max_health', 'minecraft:generic.*']) # (e.g. ['minecraft:generic.max_health', 'minecraft:generic.*'])
ignored_attributes: [] synced_attributes:
# Which modifiers should not be saved when syncing users. Supports wildcard matching. - "minecraft:generic.max_health"
- "minecraft:max_health"
- "minecraft:generic.max_absorption"
- "minecraft:max_absorption"
- "minecraft:generic.luck"
- "minecraft:luck"
- "minecraft:generic.scale"
- "minecraft:scale"
- "minecraft:generic.step_height"
- "minecraft:step_height"
- "minecraft:generic.gravity"
- "minecraft:gravity"
# Which attribute modifiers should not be saved when syncing users. Supports wildcard matching.
# (e.g. ['minecraft:effect.speed', 'minecraft:effect.*']) # (e.g. ['minecraft:effect.speed', 'minecraft:effect.*'])
ignored_modifiers: ['minecraft:effect.*', 'minecraft:creative_mode_*'] ignored_modifiers: ['minecraft:effect.*', 'minecraft:creative_mode_*']
# Event priorities for listeners (HIGHEST, NORMAL, LOWEST). Change if you encounter plugin conflicts # Event priorities for listeners (HIGHEST, NORMAL, LOWEST). Change if you encounter plugin conflicts