From 7268c56deca2f9f7e70349e2044028f4167c4bd4 Mon Sep 17 00:00:00 2001 From: deirn Date: Sun, 26 May 2024 22:29:43 +0700 Subject: [PATCH] add api to remove objects from blacklist (#266) * add method to remove object from blacklist add priority system to blacklist modifiers closes #264 * implement blacklist as redirect --- .../java/mcp/mobius/waila/api/IRegistrar.java | 75 ++++++++++++++++++- .../mobius/waila/config/BlacklistConfig.java | 14 +++- .../mobius/waila/gui/hud/TooltipHandler.java | 6 -- .../mcp/mobius/waila/registry/Registrar.java | 51 ++++++++++--- .../waila/plugin/core/WailaPluginCore.java | 3 + .../plugin/core/provider/BlockProvider.java | 14 ++++ .../plugin/core/provider/EntityProvider.java | 9 +++ .../waila/plugin/test/WailaPluginTest.java | 36 +++++++++ .../plugin/vanilla/WailaPluginVanilla.java | 4 +- 9 files changed, 187 insertions(+), 25 deletions(-) diff --git a/src/api/java/mcp/mobius/waila/api/IRegistrar.java b/src/api/java/mcp/mobius/waila/api/IRegistrar.java index d54eb6847..59654e895 100644 --- a/src/api/java/mcp/mobius/waila/api/IRegistrar.java +++ b/src/api/java/mcp/mobius/waila/api/IRegistrar.java @@ -323,13 +323,59 @@ default void addEventListener(IEventListener listener) { /** * Adds the specified entity types to the default blacklist. + * + * @param priority the modifier priority, lower number will be called last + */ + void addBlacklist(int priority, Block... blocks); + + /** + * Adds the specified entity types to the default blacklist. + * + * @param priority the modifier priority, lower number will be called last */ - void addBlacklist(Block... blocks); + void addBlacklist(int priority, BlockEntityType... blockEntityTypes); + + /** + * Adds the specified entity types to the default blacklist. + */ + default void addBlacklist(Block... blocks) { + addBlacklist(DEFAULT_PRIORITY, blocks); + } /** * Adds the specified entity types to the default blacklist. */ - void addBlacklist(BlockEntityType... blockEntityTypes); + default void addBlacklist(BlockEntityType... blockEntityTypes) { + addBlacklist(DEFAULT_PRIORITY, blockEntityTypes); + } + + /** + * Removes the specified entity types to the default blacklist. + * + * @param priority the modifier priority, lower number will be called last + */ + void removeBlacklist(int priority, Block... blocks); + + /** + * Removes the specified entity types to the default blacklist. + * + * @param priority the modifier priority, lower number will be called last + */ + void removeBlacklist(int priority, BlockEntityType... blockEntityTypes); + + /** + * Removes the specified entity types to the default blacklist. + */ + default void removeBlacklist(Block... blocks) { + removeBlacklist(DEFAULT_PRIORITY, blocks); + } + + /** + * Removes the specified entity types to the default blacklist. + */ + default void removeBlacklist(BlockEntityType... blockEntityTypes) { + removeBlacklist(DEFAULT_PRIORITY, blockEntityTypes); + } /** * Registers an {@link IBlockComponentProvider} instance to allow redirecting the object being displayed. @@ -474,8 +520,31 @@ default void addBlockData(IDataProvider provider /** * Adds the specified entity types to the default blacklist. + * + * @param priority the modifier priority, lower number will be called last */ - void addBlacklist(EntityType... entityTypes); + void addBlacklist(int priority, EntityType... entityTypes); + + /** + * Adds the specified entity types to the default blacklist. + */ + default void addBlacklist(EntityType... entityTypes) { + addBlacklist(DEFAULT_PRIORITY, entityTypes); + } + + /** + * Removes the specified entity types to the default blacklist. + * + * @param priority the modifier priority, lower number will be called last + */ + void removeBlacklist(int priority, EntityType... entityTypes); + + /** + * Removes the specified entity types to the default blacklist. + */ + default void removeBlacklist(EntityType... entityTypes) { + removeBlacklist(DEFAULT_PRIORITY, entityTypes); + } /** * Registers an {@link IEntityComponentProvider} instance to allow redirecting the object being displayed. diff --git a/src/main/java/mcp/mobius/waila/config/BlacklistConfig.java b/src/main/java/mcp/mobius/waila/config/BlacklistConfig.java index 0c6a67ce9..9d52c6b6e 100644 --- a/src/main/java/mcp/mobius/waila/config/BlacklistConfig.java +++ b/src/main/java/mcp/mobius/waila/config/BlacklistConfig.java @@ -51,6 +51,12 @@ public void setConfigVersion(int configVersion) { this.configVersion = configVersion; } + public void addBlacklistTags() { + blocks.add(BLACKLIST_TAG); + blockEntityTypes.add(BLACKLIST_TAG); + entityTypes.add(BLACKLIST_TAG); + } + public View getView() { if (view == null) view = new View(); return view; @@ -127,6 +133,8 @@ public JsonElement serialize(BlacklistConfig src, Type typeOfSrc, JsonSerializat for (var line : comments) commentArray.add(line); object.add("_comment", commentArray); + src.addBlacklistTags(); + object.add("blocks", context.serialize(src.blocks)); object.add("blockEntityTypes", context.serialize(src.blockEntityTypes)); object.add("entityTypes", context.serialize(src.entityTypes)); @@ -142,14 +150,12 @@ public BlacklistConfig deserialize(JsonElement json, Type typeOfT, JsonDeseriali var object = json.getAsJsonObject(); var res = new BlacklistConfig(); - res.blocks.add(BLACKLIST_TAG); - res.blockEntityTypes.add(BLACKLIST_TAG); - res.entityTypes.add(BLACKLIST_TAG); - deserializeEntries(res.blocks, object.getAsJsonArray("blocks")); deserializeEntries(res.blockEntityTypes, object.getAsJsonArray("blockEntityTypes")); deserializeEntries(res.entityTypes, object.getAsJsonArray("entityTypes")); + res.addBlacklistTags(); + res.configVersion = object.get("configVersion").getAsInt(); res.pluginHash = context.deserialize(object.get("pluginHash"), int[].class); diff --git a/src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java b/src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java index 5f6c14317..a7fbf0d35 100644 --- a/src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java +++ b/src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java @@ -3,7 +3,6 @@ import mcp.mobius.waila.Waila; import mcp.mobius.waila.WailaClient; import mcp.mobius.waila.access.ClientAccessor; -import mcp.mobius.waila.api.IBlacklistConfig; import mcp.mobius.waila.api.IBlockComponentProvider; import mcp.mobius.waila.api.IEntityComponentProvider; import mcp.mobius.waila.api.ITargetRedirector; @@ -152,10 +151,6 @@ private static ProcessResult processTarget(HitResult target, Minecraft client, P return ProcessResult.CONTINUE; } - if (IBlacklistConfig.get().contains(block)) return ProcessResult.CONTINUE; - - if (blockEntity != null && IBlacklistConfig.get().contains(blockEntity)) return ProcessResult.CONTINUE; - var state = ComponentHandler.getOverrideBlock(target); if (state == IBlockComponentProvider.EMPTY_BLOCK_STATE) return ProcessResult.CONTINUE; @@ -198,7 +193,6 @@ private static ProcessResult processTarget(HitResult target, Minecraft client, P if (!PluginConfig.CLIENT.getBoolean(WailaConstants.CONFIG_SHOW_ENTITY)) return ProcessResult.CONTINUE; if (actualEntity == null) return ProcessResult.CONTINUE; - if (IBlacklistConfig.get().contains(actualEntity)) return ProcessResult.CONTINUE; var targetEnt = ComponentHandler.getOverrideEntity(target); if (targetEnt == IEntityComponentProvider.EMPTY_ENTITY) return ProcessResult.CONTINUE; diff --git a/src/main/java/mcp/mobius/waila/registry/Registrar.java b/src/main/java/mcp/mobius/waila/registry/Registrar.java index 3f7458717..6aa89d244 100644 --- a/src/main/java/mcp/mobius/waila/registry/Registrar.java +++ b/src/main/java/mcp/mobius/waila/registry/Registrar.java @@ -7,6 +7,9 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Function; import com.google.common.base.Preconditions; import com.google.common.collect.BiMap; @@ -77,6 +80,7 @@ public class Registrar implements IRegistrar { public final InstanceRegistry raycastVectorProviders = new InstanceRegistry<>(); public final BlacklistConfig blacklist = new BlacklistConfig(); + public final InstanceRegistry> blacklistModifiers = Util.make(new InstanceRegistry<>(), InstanceRegistry::reversed); public final Map intConfigFormats = new HashMap<>(); @@ -109,12 +113,14 @@ private void addConfig(ResourceLocation key, T defaultValue, T clientOnlyVal } @SafeVarargs - private void addBlacklist(Set set, Registry registry, T... values) { + private void modifyBlacklist(int priority, Function> getter, BiConsumer, String> modifier, Registry registry, T... values) { assertLock(); - for (var value : values) { - set.add(Objects.requireNonNull(registry.getKey(value)).toString()); - } + blacklistModifiers.add(Object.class, blacklist -> { + for (var value : values) { + modifier.accept(getter.apply(blacklist), Objects.requireNonNull(registry.getKey(value)).toString()); + } + }, priority); } @Override @@ -196,13 +202,23 @@ public void addEventListener(IEventListener listener, int priority) { } @Override - public void addBlacklist(Block... blocks) { - addBlacklist(blacklist.blocks, BuiltInRegistries.BLOCK, blocks); + public void addBlacklist(int priority, Block... blocks) { + modifyBlacklist(priority, it -> it.blocks, Set::add, BuiltInRegistries.BLOCK, blocks); + } + + @Override + public void addBlacklist(int priority, BlockEntityType... blockEntityTypes) { + modifyBlacklist(priority, it -> it.blockEntityTypes, Set::add, BuiltInRegistries.BLOCK_ENTITY_TYPE, blockEntityTypes); + } + + @Override + public void removeBlacklist(int priority, Block... blocks) { + modifyBlacklist(priority, it -> it.blocks, Set::remove, BuiltInRegistries.BLOCK, blocks); } @Override - public void addBlacklist(BlockEntityType... blockEntityTypes) { - addBlacklist(blacklist.blockEntityTypes, BuiltInRegistries.BLOCK_ENTITY_TYPE, blockEntityTypes); + public void removeBlacklist(int priority, BlockEntityType... blockEntityTypes) { + modifyBlacklist(priority, it -> it.blockEntityTypes, Set::remove, BuiltInRegistries.BLOCK_ENTITY_TYPE, blockEntityTypes); } @Override @@ -268,8 +284,13 @@ public void addBlockData(IDataProvider provider, } @Override - public void addBlacklist(EntityType... entityTypes) { - addBlacklist(blacklist.entityTypes, BuiltInRegistries.ENTITY_TYPE, entityTypes); + public void addBlacklist(int priority, EntityType... entityTypes) { + modifyBlacklist(priority, it -> it.entityTypes, Set::add, BuiltInRegistries.ENTITY_TYPE, entityTypes); + } + + @Override + public void removeBlacklist(int priority, EntityType... entityTypes) { + modifyBlacklist(priority, it -> it.entityTypes, Set::remove, BuiltInRegistries.ENTITY_TYPE, entityTypes); } @Override @@ -386,11 +407,15 @@ public void lock() { } } + blacklistModifiers.get(Object.class).forEach(it -> it.instance().accept(blacklist)); + blacklist.addBlacklistTags(); + var hash = new int[]{0, 0, 0}; hash[0] = blacklist.blocks.hashCode(); hash[1] = blacklist.blockEntityTypes.hashCode(); hash[2] = blacklist.entityTypes.hashCode(); + Waila.BLACKLIST_CONFIG.invalidate(); var userBlacklist = Waila.BLACKLIST_CONFIG.get(); if (!Arrays.equals(userBlacklist.pluginHash, hash)) { @@ -400,8 +425,14 @@ public void lock() { var newBlacklist = Waila.BLACKLIST_CONFIG.get(); newBlacklist.pluginHash = hash; + + newBlacklist.blocks.clear(); newBlacklist.blocks.addAll(blacklist.blocks); + + newBlacklist.blockEntityTypes.clear(); newBlacklist.blockEntityTypes.addAll(blacklist.blockEntityTypes); + + newBlacklist.entityTypes.clear(); newBlacklist.entityTypes.addAll(blacklist.entityTypes); } diff --git a/src/pluginCore/java/mcp/mobius/waila/plugin/core/WailaPluginCore.java b/src/pluginCore/java/mcp/mobius/waila/plugin/core/WailaPluginCore.java index 4073f1cbf..ab107b5bc 100644 --- a/src/pluginCore/java/mcp/mobius/waila/plugin/core/WailaPluginCore.java +++ b/src/pluginCore/java/mcp/mobius/waila/plugin/core/WailaPluginCore.java @@ -46,6 +46,9 @@ public void register(IRegistrar registrar) { registrar.addComponent(EntityProvider.INSTANCE, BODY, LivingEntity.class, PRIORITY); registrar.addComponent(EntityProvider.INSTANCE, TAIL, Entity.class, PRIORITY); + registrar.addRedirect(BlockProvider.INSTANCE, Block.class, 500); + registrar.addRedirect(EntityProvider.INSTANCE, Entity.class, 500); + registrar.addConfig(WailaConstants.CONFIG_SHOW_BLOCK, true); registrar.addConfig(WailaConstants.CONFIG_SHOW_FLUID, false); registrar.addConfig(WailaConstants.CONFIG_SHOW_ENTITY, true); diff --git a/src/pluginCore/java/mcp/mobius/waila/plugin/core/provider/BlockProvider.java b/src/pluginCore/java/mcp/mobius/waila/plugin/core/provider/BlockProvider.java index c1dd1887b..fae1fe3b2 100644 --- a/src/pluginCore/java/mcp/mobius/waila/plugin/core/provider/BlockProvider.java +++ b/src/pluginCore/java/mcp/mobius/waila/plugin/core/provider/BlockProvider.java @@ -1,5 +1,6 @@ package mcp.mobius.waila.plugin.core.provider; +import mcp.mobius.waila.api.IBlacklistConfig; import mcp.mobius.waila.api.IBlockAccessor; import mcp.mobius.waila.api.IBlockComponentProvider; import mcp.mobius.waila.api.IDataProvider; @@ -7,6 +8,7 @@ import mcp.mobius.waila.api.IModInfo; import mcp.mobius.waila.api.IPluginConfig; import mcp.mobius.waila.api.IServerAccessor; +import mcp.mobius.waila.api.ITargetRedirector; import mcp.mobius.waila.api.ITooltip; import mcp.mobius.waila.api.ITooltipComponent; import mcp.mobius.waila.api.IWailaConfig; @@ -16,11 +18,23 @@ import net.minecraft.world.Nameable; import net.minecraft.world.level.block.LiquidBlock; import net.minecraft.world.level.block.entity.BlockEntity; +import org.jetbrains.annotations.Nullable; public enum BlockProvider implements IBlockComponentProvider, IDataProvider { INSTANCE; + @Override + public @Nullable ITargetRedirector.Result redirect(ITargetRedirector redirect, IBlockAccessor accessor, IPluginConfig config) { + var blacklist = IBlacklistConfig.get(); + if (blacklist.contains(accessor.getBlock())) return redirect.toBehind(); + + var blockEntity = accessor.getBlockEntity(); + if (blockEntity != null && blacklist.contains(blockEntity)) return redirect.toBehind(); + + return null; + } + @Override public ITooltipComponent getIcon(IBlockAccessor accessor, IPluginConfig config) { return new ItemComponent(accessor.getBlock().getCloneItemStack(accessor.getWorld(), accessor.getPosition(), accessor.getBlockState())); diff --git a/src/pluginCore/java/mcp/mobius/waila/plugin/core/provider/EntityProvider.java b/src/pluginCore/java/mcp/mobius/waila/plugin/core/provider/EntityProvider.java index 304631d2b..0883b62cd 100644 --- a/src/pluginCore/java/mcp/mobius/waila/plugin/core/provider/EntityProvider.java +++ b/src/pluginCore/java/mcp/mobius/waila/plugin/core/provider/EntityProvider.java @@ -1,9 +1,11 @@ package mcp.mobius.waila.plugin.core.provider; +import mcp.mobius.waila.api.IBlacklistConfig; import mcp.mobius.waila.api.IEntityAccessor; import mcp.mobius.waila.api.IEntityComponentProvider; import mcp.mobius.waila.api.IModInfo; import mcp.mobius.waila.api.IPluginConfig; +import mcp.mobius.waila.api.ITargetRedirector; import mcp.mobius.waila.api.ITooltip; import mcp.mobius.waila.api.ITooltipComponent; import mcp.mobius.waila.api.IWailaConfig; @@ -18,6 +20,13 @@ public enum EntityProvider implements IEntityComponentProvider { INSTANCE; + @Override + public @Nullable ITargetRedirector.Result redirect(ITargetRedirector redirect, IEntityAccessor accessor, IPluginConfig config) { + if (IBlacklistConfig.get().contains(accessor.getEntity())) return redirect.toBehind(); + + return null; + } + @Nullable @Override public ITooltipComponent getIcon(IEntityAccessor accessor, IPluginConfig config) { diff --git a/src/pluginTest/java/mcp/mobius/waila/plugin/test/WailaPluginTest.java b/src/pluginTest/java/mcp/mobius/waila/plugin/test/WailaPluginTest.java index 70827ca91..8bec54eb9 100644 --- a/src/pluginTest/java/mcp/mobius/waila/plugin/test/WailaPluginTest.java +++ b/src/pluginTest/java/mcp/mobius/waila/plugin/test/WailaPluginTest.java @@ -8,10 +8,13 @@ import mcp.mobius.waila.api.__internal__.IApiService; import mcp.mobius.waila.api.data.EnergyData; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.EntityType; import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.ChestBlock; import net.minecraft.world.level.block.FurnaceBlock; import net.minecraft.world.level.block.entity.BarrelBlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityType; @SuppressWarnings("unused") public class WailaPluginTest implements IWailaPlugin { @@ -99,6 +102,39 @@ public void register(IRegistrar registrar) { registrar.addConfig(RedirectTest.TARGET, RedirectTest.Target.NONE); registrar.addRedirect(RedirectTest.INSTANCE, Block.class); + + registrar.addBlacklist(9000, + Blocks.GRASS_BLOCK, + Blocks.GRANITE); + + registrar.removeBlacklist(8900, + Blocks.GRASS_BLOCK); + + registrar.addBlacklist(9000, + BlockEntityType.BED, + BlockEntityType.BELL); + + registrar.removeBlacklist(8900, + BlockEntityType.BED); + + registrar.addBlacklist(9000, + EntityType.ARMADILLO, + EntityType.BLAZE); + + registrar.removeBlacklist(8900, + EntityType.BLAZE); + +// registrar.removeBlacklist( +// Blocks.BARRIER, +// Blocks.STRUCTURE_VOID); +// +// registrar.removeBlacklist( +// EntityType.AREA_EFFECT_CLOUD, +// EntityType.EXPERIENCE_ORB, +// EntityType.FIREBALL, +// EntityType.FIREWORK_ROCKET, +// EntityType.INTERACTION, +// EntityType.SNOWBALL); } } diff --git a/src/pluginVanilla/java/mcp/mobius/waila/plugin/vanilla/WailaPluginVanilla.java b/src/pluginVanilla/java/mcp/mobius/waila/plugin/vanilla/WailaPluginVanilla.java index e46a61d31..0b019c0b6 100644 --- a/src/pluginVanilla/java/mcp/mobius/waila/plugin/vanilla/WailaPluginVanilla.java +++ b/src/pluginVanilla/java/mcp/mobius/waila/plugin/vanilla/WailaPluginVanilla.java @@ -237,11 +237,11 @@ public void register(IRegistrar registrar) { registrar.addEntityData(ContainerEntityProvider.INSTANCE, Container.class, 1100); - registrar.addBlacklist( + registrar.addBlacklist(1100, Blocks.BARRIER, Blocks.STRUCTURE_VOID); - registrar.addBlacklist( + registrar.addBlacklist(1100, EntityType.AREA_EFFECT_CLOUD, EntityType.EXPERIENCE_ORB, EntityType.FIREBALL,