Skip to content

Commit

Permalink
plugin config alias api
Browse files Browse the repository at this point in the history
reorganize attribute configs
  • Loading branch information
deirn committed May 10, 2024
1 parent f4198c5 commit 89406bb
Show file tree
Hide file tree
Showing 31 changed files with 3,051 additions and 2,846 deletions.
7 changes: 7 additions & 0 deletions src/api/java/mcp/mobius/waila/api/IRegistrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ default void addSyncedConfig(ResourceLocation key, int defaultValue, int clientO
*/
<T extends Enum<T>> void addSyncedConfig(ResourceLocation key, T defaultValue, T clientOnlyValue);

/**
* Registers config key aliases that will be migrated gracefully to the actual key.
* <p>
* Also sync the value using aliased keys, so outdated client can still get the correct value.
*/
void addConfigAlias(ResourceLocation actual, ResourceLocation... aliases);

/**
* Adds an event listener
*/
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/mcp/mobius/waila/config/ConfigEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ public boolean isSynced() {
return serverRequired || merged;
}

public ConfigEntry<T> getActual() {
return this;
}

public Alias<T> createAlias(ResourceLocation id) {
return new Alias<>(id, getActual());
}

public boolean isAlias() {
return false;
}

private void assertInstance(T value) {
Preconditions.checkArgument(
value.getClass() == defaultValue.getClass(),
Expand All @@ -138,4 +150,45 @@ public ConfigEntry<T> create(IPluginInfo origin, ResourceLocation id, T defaultV

}

public static class Alias<T> extends ConfigEntry<T> {

public final ConfigEntry<T> delegate;

private Alias(ResourceLocation id, ConfigEntry<T> delegate) {
super(delegate.origin, id, delegate.defaultValue, delegate.clientOnlyValue, delegate.serverRequired, delegate.merged, delegate.type);
this.delegate = delegate;
}

@Override
public T getServerValue() {
return delegate.getServerValue();
}

@Override
public void setServerValue(@Nullable T serverValue) {
delegate.setServerValue(serverValue);
}

@Override
public T getLocalValue() {
return delegate.getLocalValue();
}

@Override
public void setLocalValue(T localValue) {
delegate.setLocalValue(localValue);
}

@Override
public ConfigEntry<T> getActual() {
return delegate.getActual();
}

@Override
public boolean isAlias() {
return true;
}

}

}
12 changes: 7 additions & 5 deletions src/main/java/mcp/mobius/waila/config/PluginConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public enum PluginConfig implements IPluginConfig {
}.getType(),
LinkedHashMap::new);

private static final Map<ResourceLocation, ConfigEntry<?>> CONFIGS = new LinkedHashMap<>();
private static final Map<ResourceLocation, ConfigEntry<Object>> CONFIGS = new LinkedHashMap<>();

public static <T> void addConfig(ConfigEntry<T> entry) {
CONFIGS.put(entry.getId(), entry);
CONFIGS.put(entry.getId(), (ConfigEntry<Object>) entry);
}

private static Stream<ResourceLocation> getKeyStream() {
Expand All @@ -59,7 +59,6 @@ public static Set<ResourceLocation> getAllKeys() {
public static Set<ConfigEntry<Object>> getSyncableConfigs() {
return CONFIGS.values().stream()
.filter(ConfigEntry::isSynced)
.map(t -> (ConfigEntry<Object>) t)
.collect(Collectors.toSet());
}

Expand Down Expand Up @@ -102,8 +101,9 @@ public static void save() {

private static void writeConfig() {
Map<String, Map<String, JsonPrimitive>> config = new LinkedHashMap<>();
for (var e : CONFIGS.values()) {
var entry = (ConfigEntry<Object>) e;
for (var entry : CONFIGS.values()) {
if (entry.isAlias()) continue;

var id = entry.getId();
config.computeIfAbsent(id.getNamespace(), k -> new LinkedHashMap<>())
.put(id.getPath(), entry.getType().serializer.apply(entry.getLocalValue()));
Expand All @@ -117,6 +117,8 @@ private <T> T getValue(ResourceLocation key, T defaultValue) {
if (entry != null && entry.getOrigin().isEnabled()) {
return (T) entry.getValue(this == SERVER);
}

if (Waila.DEV) LOG.error("Unknown plugin config key {}", key);
return defaultValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public ConfigListWidget getOptions() {

for (var key : keys) {
var entry = PluginConfig.getEntry(key);
if (entry.isAlias()) continue;

var path = key.getPath();
var category = NO_CATEGORY;

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/mcp/mobius/waila/registry/Registrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ public <T extends Enum<T>> void addSyncedConfig(ResourceLocation key, T defaultV
addConfig(key, defaultValue, clientOnlyValue, true, false, ConfigEntry.ENUM);
}

@Override
public void addConfigAlias(ResourceLocation actual, ResourceLocation... aliases) {
assertLock();

for (var alias : aliases) {
PluginConfig.addConfig(PluginConfig.getEntry(actual).createAlias(alias));
}
}

@Override
public void addEventListener(IEventListener listener, int priority) {
if (skip()) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,45 +93,50 @@ public class WailaPluginVanilla implements IWailaPlugin {

@Override
public void register(IRegistrar registrar) {
registrar.addFeatureConfig(Options.ITEM_ENTITY, true);
registrar.addConfig(Options.BLOCK_POSITION, false);
registrar.addConfig(Options.BLOCK_STATE, false);
registrar.addComponent(BlockAttributesProvider.INSTANCE, BODY, Block.class, 950);

registrar.addFeatureConfig(Options.ENTITY_ITEM_ENTITY, true);
registrar.addIcon(ItemEntityProvider.INSTANCE, ItemEntity.class);
registrar.addComponent(ItemEntityProvider.INSTANCE, HEAD, ItemEntity.class, 950);
registrar.addComponent(ItemEntityProvider.INSTANCE, BODY, ItemEntity.class, 950);
registrar.addComponent(ItemEntityProvider.INSTANCE, TAIL, ItemEntity.class, 950);
registrar.addOverride(ItemEntityProvider.INSTANCE, ItemEntity.class);

registrar.addConfig(Options.ENTITY_POSITION, false);
registrar.addFeatureConfig(Options.ENTITY_HEALTH, true);
registrar.addFeatureConfig(Options.ENTITY_ABSORPTION, false);
registrar.addFeatureConfig(Options.ENTITY_ARMOR, true);
registrar.addConfig(Options.ENTITY_COMPACT, false);
registrar.addConfig(Options.ENTITY_ICON_PER_LINE, 25);
registrar.addConfig(Options.ENTITY_LONG_HEALTH_MAX, 100);
registrar.addConfig(Options.ENTITY_LONG_ARMOR_MAX, 100);
registrar.addComponent(EntityAttributesProvider.INSTANCE, HEAD, Entity.class, 950);
registrar.addComponent(EntityAttributesProvider.INSTANCE, BODY, Entity.class, 950);
registrar.addEntityData(EntityAttributesProvider.INSTANCE, Entity.class);

registrar.addFeatureConfig(Options.PET_OWNER, false);
registrar.addConfig(Options.PET_HIDE_UNKNOWN_OWNER, false);
registrar.addComponent(PetOwnerProvider.INSTANCE, BODY, OwnableEntity.class);
registrar.addEntityData(PetOwnerProvider.INSTANCE, OwnableEntity.class);

registrar.addConfig(Options.ATTRIBUTE_BLOCK_POSITION, false);
registrar.addConfig(Options.ATTRIBUTE_BLOCK_STATE, false);
registrar.addConfig(Options.ATTRIBUTE_ENTITY_POSITION, false);
registrar.addFeatureConfig(Options.ATTRIBUTE_HEALTH, true);
registrar.addFeatureConfig(Options.ATTRIBUTE_ABSORPTION, false);
registrar.addFeatureConfig(Options.ATTRIBUTE_ARMOR, true);
registrar.addConfig(Options.ATTRIBUTE_COMPACT, false);
registrar.addConfig(Options.ATTRIBUTE_ICON_PER_LINE, 25);
registrar.addConfig(Options.ATTRIBUTE_LONG_HEALTH_MAX, 100);
registrar.addConfig(Options.ATTRIBUTE_LONG_ARMOR_MAX, 100);
registrar.addFeatureConfig(Options.ATTRIBUTE_HORSE_JUMP_HEIGHT, true);
registrar.addFeatureConfig(Options.ATTRIBUTE_HORSE_SPEED, true);
registrar.addFeatureConfig(Options.ATTRIBUTE_PANDA_GENES, true);
registrar.addFeatureConfig(Options.ATTRIBUTE_BEACON_EFFECTS, false);
registrar.addFeatureConfig(Options.ATTRIBUTE_MOB_EFFECTS, false);
registrar.addSyncedConfig(Options.ATTRIBUTE_HIDDEN_MOB_EFFECTS, false, false);
registrar.addComponent(BlockAttributesProvider.INSTANCE, BODY, Block.class, 950);
registrar.addComponent(EntityAttributesProvider.INSTANCE, HEAD, Entity.class, 950);
registrar.addComponent(EntityAttributesProvider.INSTANCE, BODY, Entity.class, 950);
registrar.addFeatureConfig(Options.HORSE_JUMP_HEIGHT, true);
registrar.addFeatureConfig(Options.HORSE_SPEED, true);
registrar.addComponent(HorseProvider.INSTANCE, BODY, AbstractHorse.class);

registrar.addFeatureConfig(Options.PANDA_GENES, true);
registrar.addComponent(PandaProvider.INSTANCE, BODY, Panda.class);
registrar.addComponent(BeaconProvider.INSTANCE, BODY, BeaconBlockEntity.class);
registrar.addComponent(MobEffectProvider.INSTANCE, BODY, LivingEntity.class);

registrar.addFeatureConfig(Options.EFFECT_BEACON, false);
registrar.addDataType(BeaconProvider.DATA, BeaconProvider.Data.class, BeaconProvider.Data::new);
registrar.addDataType(MobEffectProvider.DATA, MobEffectProvider.Data.class, MobEffectProvider.Data::new);
registrar.addComponent(BeaconProvider.INSTANCE, BODY, BeaconBlockEntity.class);
registrar.addBlockData(BeaconProvider.INSTANCE, BeaconBlockEntity.class);
registrar.addEntityData(EntityAttributesProvider.INSTANCE, Entity.class);

registrar.addFeatureConfig(Options.EFFECT_MOB, false);
registrar.addSyncedConfig(Options.EFFECT_HIDDEN_MOB, false, false);
registrar.addDataType(MobEffectProvider.DATA, MobEffectProvider.Data.class, MobEffectProvider.Data::new);
registrar.addComponent(MobEffectProvider.INSTANCE, BODY, LivingEntity.class);
registrar.addEntityData(MobEffectProvider.INSTANCE, LivingEntity.class);

registrar.addFeatureConfig(Options.JUKEBOX_RECORD, false);
Expand Down Expand Up @@ -162,9 +167,9 @@ public void register(IRegistrar registrar) {
registrar.addFeatureConfig(Options.SPAWNER_TYPE, true);
registrar.addComponent(SpawnerProvider.INSTANCE, HEAD, SpawnerBlockEntity.class, 950);

registrar.addFeatureConfig(Options.CROP_PROGRESS, true);
registrar.addFeatureConfig(Options.CROP_GROWABLE, true);
registrar.addFeatureConfig(Options.TREE_GROWABLE, true);
registrar.addFeatureConfig(Options.PLANT_CROP_PROGRESS, true);
registrar.addFeatureConfig(Options.PLANT_CROP_GROWABLE, true);
registrar.addFeatureConfig(Options.PLANT_TREE_GROWABLE, true);
registrar.addIcon(PlantProvider.INSTANCE, CropBlock.class);
registrar.addComponent(PlantProvider.INSTANCE, BODY, CropBlock.class);
registrar.addComponent(PlantProvider.INSTANCE, BODY, StemBlock.class);
Expand Down Expand Up @@ -243,6 +248,8 @@ public void register(IRegistrar registrar) {
EntityType.FIREWORK_ROCKET,
EntityType.INTERACTION,
EntityType.SNOWBALL);

Options.ALIASES.forEach(registrar::addConfigAlias);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package mcp.mobius.waila.plugin.vanilla.config;

import java.util.Map;

import com.google.common.collect.ImmutableMap;
import net.minecraft.resources.ResourceLocation;

public final class Options {
Expand All @@ -8,18 +11,17 @@ public final class Options {
public static final ResourceLocation BREAKING_PROGRESS = rl("breaking_progress.enabled");
public static final ResourceLocation BREAKING_PROGRESS_COLOR = rl("breaking_progress.color");
public static final ResourceLocation BREAKING_PROGRESS_BOTTOM_ONLY = rl("breaking_progress.bottom_only");
public static final ResourceLocation ITEM_ENTITY = rl("item_entity");
public static final ResourceLocation OVERRIDE_INFESTED = rl("override.infested");
public static final ResourceLocation OVERRIDE_TRAPPED_CHEST = rl("override.trapped_chest");
public static final ResourceLocation OVERRIDE_POWDER_SNOW = rl("override.powder_snow");
public static final ResourceLocation OVERRIDE_VEHICLE = rl("override.vehicle");
public static final ResourceLocation OVERRIDE_INVISIBLE_ENTITY = rl("override.invisible_entity");
public static final ResourceLocation PET_OWNER = rl("pet.owner");
public static final ResourceLocation PET_HIDE_UNKNOWN_OWNER = rl("pet.hide_unknown_owner");
public static final ResourceLocation SPAWNER_TYPE = rl("spawner_type");
public static final ResourceLocation CROP_PROGRESS = rl("crop_progress");
public static final ResourceLocation CROP_GROWABLE = rl("crop_growable");
public static final ResourceLocation TREE_GROWABLE = rl("tree_growable");
public static final ResourceLocation SPAWNER_TYPE = rl("spawner.type");
public static final ResourceLocation PLANT_CROP_PROGRESS = rl("plant.crop_progress");
public static final ResourceLocation PLANT_CROP_GROWABLE = rl("plant.crop_growable");
public static final ResourceLocation PLANT_TREE_GROWABLE = rl("plant.tree_growable");
public static final ResourceLocation REDSTONE_LEVER = rl("redstone.lever");
public static final ResourceLocation REDSTONE_REPEATER = rl("redstone.repeater");
public static final ResourceLocation REDSTONE_COMPARATOR = rl("redstone.comparator");
Expand All @@ -33,28 +35,55 @@ public final class Options {
public static final ResourceLocation NOTE_BLOCK_INT_VALUE = rl("note_block.int_value");
public static final ResourceLocation TIMER_GROW = rl("timer.grow");
public static final ResourceLocation TIMER_BREED = rl("timer.breed");
public static final ResourceLocation ATTRIBUTE_BLOCK_POSITION = rl("attribute.block_position");
public static final ResourceLocation ATTRIBUTE_BLOCK_STATE = rl("attribute.block_state");
public static final ResourceLocation ATTRIBUTE_ENTITY_POSITION = rl("attribute.entity_position");
public static final ResourceLocation ATTRIBUTE_HEALTH = rl("attribute.health");
public static final ResourceLocation ATTRIBUTE_ABSORPTION = rl("attribute.absorption");
public static final ResourceLocation ATTRIBUTE_ARMOR = rl("attribute.armor");
public static final ResourceLocation ATTRIBUTE_COMPACT = rl("attribute.compact");
public static final ResourceLocation ATTRIBUTE_ICON_PER_LINE = rl("attribute.icon_per_line");
public static final ResourceLocation ATTRIBUTE_LONG_HEALTH_MAX = rl("attribute.long_health_max");
public static final ResourceLocation ATTRIBUTE_LONG_ARMOR_MAX = rl("attribute.long_armor_max");
public static final ResourceLocation ATTRIBUTE_HORSE_JUMP_HEIGHT = rl("attribute.horse_jump_height");
public static final ResourceLocation ATTRIBUTE_HORSE_SPEED = rl("attribute.horse_speed");
public static final ResourceLocation ATTRIBUTE_PANDA_GENES = rl("attribute.panda_genes");
public static final ResourceLocation ATTRIBUTE_BEACON_EFFECTS = rl("attribute.beacon_effects");
public static final ResourceLocation ATTRIBUTE_MOB_EFFECTS = rl("attribute.mob_effects");
public static final ResourceLocation ATTRIBUTE_HIDDEN_MOB_EFFECTS = rl("attribute.hidden_mob_effects");
public static final ResourceLocation BLOCK_POSITION = rl("block.position");
public static final ResourceLocation BLOCK_STATE = rl("block.state");
public static final ResourceLocation ENTITY_ITEM_ENTITY = rl("entity.item_entity");
public static final ResourceLocation ENTITY_POSITION = rl("entity.position");
public static final ResourceLocation ENTITY_HEALTH = rl("entity.health");
public static final ResourceLocation ENTITY_ABSORPTION = rl("entity.absorption");
public static final ResourceLocation ENTITY_ARMOR = rl("entity.armor");
public static final ResourceLocation ENTITY_COMPACT = rl("entity.compact");
public static final ResourceLocation ENTITY_ICON_PER_LINE = rl("entity.icon_per_line");
public static final ResourceLocation ENTITY_LONG_HEALTH_MAX = rl("entity.long_health_max");
public static final ResourceLocation ENTITY_LONG_ARMOR_MAX = rl("entity.long_armor_max");
public static final ResourceLocation HORSE_JUMP_HEIGHT = rl("horse.jump_height");
public static final ResourceLocation HORSE_SPEED = rl("horse.speed");
public static final ResourceLocation PANDA_GENES = rl("panda.genes");
public static final ResourceLocation EFFECT_BEACON = rl("effect.beacon");
public static final ResourceLocation EFFECT_MOB = rl("effect.mob");
public static final ResourceLocation EFFECT_HIDDEN_MOB = rl("effect.hidden_mob");
public static final ResourceLocation BOOK_BOOKSHELF = rl("book.bookshelf");
public static final ResourceLocation BOOK_WRITTEN = rl("book.written");
public static final ResourceLocation BOOK_ENCHANTMENT_DISPLAY_MODE = rl("book.enchantment");
public static final ResourceLocation BOOK_ENCHANTMENT_CYCLE_TIMING = rl("book.enchantment_cycle_timing");
// @formatter:on

// @formatter:off
public static final Map<ResourceLocation, ResourceLocation> ALIASES = new ImmutableMap.Builder<ResourceLocation, ResourceLocation>()
.put(SPAWNER_TYPE , rl("spawner_type"))
.put(PLANT_CROP_PROGRESS , rl("crop_progress"))
.put(PLANT_CROP_GROWABLE , rl("crop_growable"))
.put(PLANT_TREE_GROWABLE , rl("tree_growable"))
.put(BLOCK_POSITION , rl("attribute.block_position"))
.put(BLOCK_STATE , rl("attribute.block_state"))
.put(ENTITY_ITEM_ENTITY , rl("item_entity"))
.put(ENTITY_POSITION , rl("attribute.entity_position"))
.put(ENTITY_HEALTH , rl("attribute.health"))
.put(ENTITY_ABSORPTION , rl("attribute.absorption"))
.put(ENTITY_ARMOR , rl("attribute.armor"))
.put(ENTITY_COMPACT , rl("attribute.compact"))
.put(ENTITY_ICON_PER_LINE , rl("attribute.icon_per_line"))
.put(ENTITY_LONG_HEALTH_MAX, rl("attribute.long_health_max"))
.put(ENTITY_LONG_ARMOR_MAX , rl("attribute.long_armor_max"))
.put(HORSE_JUMP_HEIGHT , rl("attribute.horse_jump_height"))
.put(HORSE_SPEED , rl("attribute.horse_speed"))
.put(PANDA_GENES , rl("attribute.panda_genes"))
.put(EFFECT_BEACON , rl("attribute.beacon_effects"))
.put(EFFECT_MOB , rl("attribute.mob_effects"))
.put(EFFECT_HIDDEN_MOB , rl("attribute.hidden_mob_effects"))
.build();
// @formatter:on

private static ResourceLocation rl(String rl) {
return new ResourceLocation(rl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private MutableComponent getText(MobEffect effect) {

@Override
public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig config) {
if (!config.getBoolean(Options.ATTRIBUTE_BEACON_EFFECTS)) return;
if (!config.getBoolean(Options.EFFECT_BEACON)) return;

var data = accessor.getData().get(Data.class);
if (data == null) return;
Expand All @@ -48,7 +48,7 @@ public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig

@Override
public void appendData(IDataWriter data, IServerAccessor<BeaconBlockEntity> accessor, IPluginConfig config) {
if (config.getBoolean(Options.ATTRIBUTE_BEACON_EFFECTS)) data.add(Data.class, res -> {
if (config.getBoolean(Options.EFFECT_BEACON)) data.add(Data.class, res -> {
var beacon = (BeaconBlockEntity & BeaconBlockEntityAccess) accessor.getTarget();
res.add(new Data(beacon.wthit_primaryPower(), beacon.wthit_levels() >= 4 ? beacon.wthit_secondaryPower() : null));
});
Expand Down
Loading

0 comments on commit 89406bb

Please sign in to comment.