Skip to content

Commit

Permalink
add api to remove objects from blacklist (#266)
Browse files Browse the repository at this point in the history
* add method to remove object from blacklist
add priority system to blacklist modifiers
closes #264

* implement blacklist as redirect
  • Loading branch information
deirn authored May 26, 2024
1 parent a56174f commit 7268c56
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 25 deletions.
75 changes: 72 additions & 3 deletions src/api/java/mcp/mobius/waila/api/IRegistrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -474,8 +520,31 @@ default <T, BE extends BlockEntity> void addBlockData(IDataProvider<BE> 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.
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/mcp/mobius/waila/config/BlacklistConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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);

Expand Down
6 changes: 0 additions & 6 deletions src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
51 changes: 41 additions & 10 deletions src/main/java/mcp/mobius/waila/registry/Registrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,6 +80,7 @@ public class Registrar implements IRegistrar {
public final InstanceRegistry<IRayCastVectorProvider> raycastVectorProviders = new InstanceRegistry<>();

public final BlacklistConfig blacklist = new BlacklistConfig();
public final InstanceRegistry<Consumer<BlacklistConfig>> blacklistModifiers = Util.make(new InstanceRegistry<>(), InstanceRegistry::reversed);

public final Map<ResourceLocation, IntFormat> intConfigFormats = new HashMap<>();

Expand Down Expand Up @@ -109,12 +113,14 @@ private <T> void addConfig(ResourceLocation key, T defaultValue, T clientOnlyVal
}

@SafeVarargs
private <T> void addBlacklist(Set<String> set, Registry<T> registry, T... values) {
private <T> void modifyBlacklist(int priority, Function<BlacklistConfig, Set<String>> getter, BiConsumer<Set<String>, String> modifier, Registry<T> 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -268,8 +284,13 @@ public <T, BE extends BlockEntity> void addBlockData(IDataProvider<BE> 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
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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;
import mcp.mobius.waila.api.IDataWriter;
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;
Expand All @@ -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<BlockEntity> {

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()));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 7268c56

Please sign in to comment.