Skip to content

Commit

Permalink
Fix #2916 Fix #3714 Item hiding mode is broken
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Aug 28, 2024
1 parent e2c9e0c commit 9d17753
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 54 deletions.
10 changes: 10 additions & 0 deletions Common/src/main/java/mezz/jei/common/config/ClientToggleState.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package mezz.jei.common.config;

import mezz.jei.core.util.WeakList;

public class ClientToggleState implements IClientToggleState {
private final WeakList<IEditModeListener> editModeListeners = new WeakList<>();

private boolean overlayEnabled = true;
private boolean cheatItemsEnabled = false;
private boolean editModeEnabled = false;
Expand Down Expand Up @@ -56,5 +60,11 @@ public boolean isEditModeEnabled() {
@Override
public void toggleEditModeEnabled() {
this.editModeEnabled = !this.editModeEnabled;
editModeListeners.forEach(IEditModeListener::onEditModeChanged);
}

@Override
public void addEditModeToggleListener(IEditModeListener listener) {
editModeListeners.add(listener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public interface IClientToggleState {
void toggleBookmarkEnabled();

void setBookmarkEnabled(boolean value);

void addEditModeToggleListener(IEditModeListener listener);

interface IEditModeListener {
void onEditModeChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ default Object getUid(V ingredient, UidContext context) {
return getUniqueId(ingredient, context);
}

/**
* Unique ID for use in grouping ingredients together.
* This is used for hiding groups of ingredients together at once.
*
* @since 19.13.0
*/
default Object getGroupingUid(V ingredient) {
return getWildcardId(ingredient);
}

/**
* Return true if the given ingredient can have subtypes.
* For example in the vanilla game an enchanted book may have subtypes, but an apple does not.
Expand All @@ -71,7 +81,10 @@ default boolean hasSubtypes(V ingredient) {
* Wildcard ID for use in comparing, blacklisting, and looking up ingredients.
* For an example, ItemStack's wildcardId does not include NBT.
* For ingredients which do not have a wildcardId, just return the uniqueId here.
*
* @deprecated use {@link #getGroupingUid} instead
*/
@Deprecated(since = "19.13.0", forRemoval = true)
default String getWildcardId(V ingredient) {
return getUniqueId(ingredient, UidContext.Ingredient);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ enum HideMode {
SINGLE,
/**
* Hides or shows a single item based on its Wildcard UID.
* See {@link IIngredientHelper#getWildcardId(Object)}.
* See {@link IIngredientHelper#getGroupingUid}.
*
* @since 11.5.0
*/
Expand Down
19 changes: 17 additions & 2 deletions Gui/src/main/java/mezz/jei/gui/ingredients/IngredientFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import mezz.jei.api.runtime.IIngredientVisibility;
import mezz.jei.common.config.DebugConfig;
import mezz.jei.common.config.IClientConfig;
import mezz.jei.common.config.IClientToggleState;
import mezz.jei.common.config.IIngredientFilterConfig;
import mezz.jei.gui.filter.IFilterTextSource;
import mezz.jei.gui.overlay.IIngredientGridSource;
Expand Down Expand Up @@ -37,7 +38,12 @@
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class IngredientFilter implements IIngredientGridSource, IIngredientManager.IIngredientListener {
public class IngredientFilter implements
IIngredientGridSource,
IIngredientManager.IIngredientListener,
IIngredientVisibility.IListener,
IClientToggleState.IEditModeListener
{
private static final Logger LOGGER = LogManager.getLogger();
private static final Pattern QUOTE_PATTERN = Pattern.compile("\"");
private static final Pattern FILTER_SPLIT_PATTERN = Pattern.compile("(-?\".*?(?:\"|$)|\\S+)");
Expand Down Expand Up @@ -65,7 +71,8 @@ public IngredientFilter(
List<IListElementInfo<?>> ingredients,
IModIdHelper modIdHelper,
IIngredientVisibility ingredientVisibility,
IColorHelper colorHelper
IColorHelper colorHelper,
IClientToggleState clientToggleState
) {
this.filterTextSource = filterTextSource;
this.clientConfig = clientConfig;
Expand All @@ -90,6 +97,8 @@ public IngredientFilter(
ingredientListCached = null;
notifyListenersOfChange();
});

clientToggleState.addEditModeToggleListener(this);
}

private static IElementSearch createElementSearch(IClientConfig clientConfig, ElementPrefixParser elementPrefixParser) {
Expand Down Expand Up @@ -139,6 +148,11 @@ public <V> Optional<IListElement<V>> searchForMatchingElement(
.findFirst();
}

@Override
public void onEditModeChanged() {
updateHidden();
}

public void updateHidden() {
boolean changed = false;
for (IListElement<?> element : this.elementSearch.getAllIngredients()) {
Expand All @@ -160,6 +174,7 @@ public <V> boolean updateHiddenState(IListElement<V> element) {
return false;
}

@Override
public <V> void onIngredientVisibilityChanged(ITypedIngredient<V> ingredient, boolean visible) {
IIngredientType<V> ingredientType = ingredient.getType();
IIngredientHelper<V> ingredientHelper = ingredientManager.getIngredientHelper(ingredientType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.client.gui.screens.Screen;

import java.util.Optional;
import java.util.Set;

public class EditInputHandler implements IUserInputHandler {
private final CombinedRecipeFocusSource focusSource;
Expand Down Expand Up @@ -53,7 +54,8 @@ private Optional<IUserInputHandler> handle(UserInput input, IInternalKeyMappings

private <V> void execute(IClickableIngredientInternal<V> clicked, IEditModeConfig.HideMode hideMode) {
ITypedIngredient<?> typedIngredient = clicked.getTypedIngredient();
if (editModeConfig.isIngredientHiddenUsingConfigFile(typedIngredient)) {
Set<IEditModeConfig.HideMode> hideModes = editModeConfig.getIngredientHiddenUsingConfigFile(typedIngredient);
if (hideModes.contains(hideMode)) {
editModeConfig.showIngredientUsingConfigFile(typedIngredient, hideMode);
} else {
editModeConfig.hideIngredientUsingConfigFile(typedIngredient, hideMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.RenderType;

import java.util.Set;

public class ElementRenderer<T> implements IElementRenderer<T> {
private static final int BLACKLIST_COLOR = 0xFFFF0000;
private static final int BLACKLIST_COLOR = 0xDDFF0000;
private static final int WILDCARD_BLACKLIST_COLOR = 0xDDFFA500;

private final IIngredientRenderer<T> ingredientRenderer;

Expand All @@ -41,15 +44,28 @@ public void render(GuiGraphics guiGraphics, IElement<T> element, ImmutableRect2i

private static <T> void renderEditMode(GuiGraphics guiGraphics, ImmutableRect2i area, int padding, ITypedIngredient<T> typedIngredient) {
IEditModeConfig editModeConfig = Internal.getJeiRuntime().getEditModeConfig();
if (editModeConfig.isIngredientHiddenUsingConfigFile(typedIngredient)) {
guiGraphics.fill(
RenderType.guiOverlay(),
area.getX() + padding,
area.getY() + padding,
area.getX() + 16 + padding,
area.getY() + 16 + padding,
BLACKLIST_COLOR
);
Set<IEditModeConfig.HideMode> hideModes = editModeConfig.getIngredientHiddenUsingConfigFile(typedIngredient);
if (!hideModes.isEmpty()) {
if (hideModes.contains(IEditModeConfig.HideMode.WILDCARD)) {
guiGraphics.fill(
RenderType.guiOverlay(),
area.getX() + padding,
area.getY() + padding,
area.getX() + 16 + padding,
area.getY() + 16 + padding,
WILDCARD_BLACKLIST_COLOR
);
}
if (hideModes.contains(IEditModeConfig.HideMode.SINGLE)) {
guiGraphics.fill(
RenderType.guiOverlay(),
area.getX() + padding,
area.getY() + padding,
area.getX() + 16 + padding,
area.getY() + 16 + padding,
BLACKLIST_COLOR
);
}
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
}
}
Expand Down
5 changes: 3 additions & 2 deletions Gui/src/main/java/mezz/jei/gui/startup/JeiGuiStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ public static JeiEventHandlers start(IRuntimeRegistration registration) {
ingredientList,
modIdHelper,
ingredientVisibility,
colorHelper
colorHelper,
toggleState
);
ingredientManager.registerIngredientListener(ingredientFilter);
ingredientVisibility.registerListener(ingredientFilter::onIngredientVisibilityChanged);
ingredientVisibility.registerListener(ingredientFilter);
timer.stop();

IIngredientFilter ingredientFilterApi = new IngredientFilterApi(ingredientFilter, filterTextSource);
Expand Down
60 changes: 40 additions & 20 deletions Library/src/main/java/mezz/jei/library/config/EditModeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import mezz.jei.api.helpers.ICodecHelper;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientType;
Expand All @@ -16,29 +17,30 @@
import mezz.jei.api.runtime.IEditModeConfig;
import mezz.jei.api.runtime.IIngredientManager;
import mezz.jei.common.codecs.EnumCodec;
import mezz.jei.core.util.WeakList;
import mezz.jei.library.ingredients.IngredientVisibility;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.RegistryOps;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class EditModeConfig implements IEditModeConfig {
private static final Logger LOGGER = LogManager.getLogger();

private final Map<Object, Pair<HideMode, ITypedIngredient<?>>> blacklist = new LinkedHashMap<>();
private final ISerializer serializer;
private final IIngredientManager ingredientManager;
private final WeakList<IListener> listeners = new WeakList<>();
private WeakReference<IngredientVisibility> ingredientVisibilityRef = new WeakReference<>(null);

public EditModeConfig(ISerializer serializer, IIngredientManager ingredientManager) {
this.ingredientManager = ingredientManager;
Expand Down Expand Up @@ -107,9 +109,23 @@ public <V> boolean isIngredientOnConfigBlacklist(ITypedIngredient<V> typedIngred
}

private <V> Set<HideMode> getIngredientOnConfigBlacklist(ITypedIngredient<V> ingredient, IIngredientHelper<V> ingredientHelper) {
return Arrays.stream(HideMode.values())
.filter(hideMode -> isIngredientOnConfigBlacklist(ingredient, hideMode, ingredientHelper))
.collect(Collectors.toUnmodifiableSet());
final Object singleUid = getIngredientUid(ingredient, HideMode.SINGLE, ingredientHelper);
final Object wildcardUid = getIngredientUid(ingredient, HideMode.WILDCARD, ingredientHelper);
if (singleUid.equals(wildcardUid)) {
if (blacklist.containsKey(singleUid)) {
return Set.of(HideMode.SINGLE);
}
return Set.of();
}

Set<HideMode> set = new HashSet<>();
if (blacklist.containsKey(singleUid)) {
set.add(HideMode.SINGLE);
}
if (blacklist.containsKey(wildcardUid)) {
set.add(HideMode.WILDCARD);
}
return Collections.unmodifiableSet(set);
}

public <V> boolean isIngredientOnConfigBlacklist(ITypedIngredient<V> typedIngredient, HideMode blacklistType, IIngredientHelper<V> ingredientHelper) {
Expand All @@ -121,7 +137,7 @@ private static <V> Object getIngredientUid(ITypedIngredient<V> typedIngredient,
final V ingredient = typedIngredient.getIngredient();
return switch (blacklistType) {
case SINGLE -> ingredientHelper.getUid(ingredient, UidContext.Ingredient);
case WILDCARD -> ingredientHelper.getWildcardId(ingredient);
case WILDCARD -> ingredientHelper.getGroupingUid(ingredient);
};
}

Expand Down Expand Up @@ -153,8 +169,8 @@ public <V> void showIngredientUsingConfigFile(ITypedIngredient<V> ingredient, Hi
removeIngredientFromConfigBlacklist(ingredient, hideMode, ingredientHelper);
}

public void registerListener(IListener listener) {
this.listeners.add(listener);
public void registerListener(IngredientVisibility ingredientVisibility) {
this.ingredientVisibilityRef = new WeakReference<>(ingredientVisibility);
}

public interface ISerializer {
Expand All @@ -170,12 +186,17 @@ public static class FileSerializer implements ISerializer {

public FileSerializer(Path path, RegistryAccess registryAccess, ICodecHelper codecHelper) {
this.path = path;
this.listCodec = Codec.list(
Codec.pair(
EnumCodec.create(HideMode.class, HideMode::valueOf),
Codec<Pair<HideMode, ITypedIngredient<?>>> elementCodec = RecordCodecBuilder.create(builder -> {
return builder.group(
EnumCodec.create(HideMode.class, HideMode::valueOf)
.fieldOf("hide_mode")
.forGetter(Pair::getFirst),
codecHelper.getTypedIngredientCodec().codec()
)
);
.fieldOf("ingredient")
.forGetter(Pair::getSecond)
).apply(builder, Pair::new);
});
this.listCodec = Codec.list(elementCodec);
this.registryOps = registryAccess.createSerializationContext(JsonOps.COMPRESSED);
}

Expand Down Expand Up @@ -233,11 +254,10 @@ public void load(EditModeConfig config) {
}
}

public interface IListener {
<V> void onIngredientVisibilityChanged(ITypedIngredient<V> ingredient, boolean visible);
}

private <T> void notifyListenersOfVisibilityChange(ITypedIngredient<T> ingredient, boolean visible) {
listeners.forEach(listener -> listener.onIngredientVisibilityChanged(ingredient, visible));
IngredientVisibility ingredientVisibility = this.ingredientVisibilityRef.get();
if (ingredientVisibility != null) {
ingredientVisibility.notifyListeners(ingredient, visible);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@
import mezz.jei.api.ingredients.ITypedIngredient;
import mezz.jei.api.ingredients.subtypes.UidContext;
import mezz.jei.api.runtime.IIngredientManager;
import mezz.jei.core.util.WeakList;

import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class IngredientBlacklistInternal implements IIngredientManager.IIngredientListener {
public interface IListener {
<V> void onIngredientVisibilityChanged(ITypedIngredient<V> ingredient, boolean visible);
}

private final Set<Object> uidBlacklist = new HashSet<>();
private final WeakList<IListener> listeners = new WeakList<>();
private WeakReference<IngredientVisibility> ingredientVisibilityRef = new WeakReference<>(null);

public <V> void addIngredientToBlacklist(ITypedIngredient<V> typedIngredient, IIngredientHelper<V> ingredientHelper) {
V ingredient = typedIngredient.getIngredient();
Expand All @@ -37,16 +33,16 @@ public <V> void removeIngredientFromBlacklist(ITypedIngredient<V> typedIngredien
public <V> boolean isIngredientBlacklistedByApi(ITypedIngredient<V> typedIngredient, IIngredientHelper<V> ingredientHelper) {
V ingredient = typedIngredient.getIngredient();
Object uid = ingredientHelper.getUid(ingredient, UidContext.Ingredient);
Object uidWild = ingredientHelper.getWildcardId(ingredient);
Object uidWild = ingredientHelper.getGroupingUid(ingredient);

if (uid.equals(uidWild)) {
return uidBlacklist.contains(uid);
}
return uidBlacklist.contains(uid) || uidBlacklist.contains(uidWild);
}

public void registerListener(IListener listener) {
this.listeners.add(listener);
public void registerListener(IngredientVisibility ingredientVisibility) {
this.ingredientVisibilityRef = new WeakReference<>(ingredientVisibility);
}

@Override
Expand All @@ -64,6 +60,9 @@ public <V> void onIngredientsRemoved(IIngredientHelper<V> ingredientHelper, Coll
}

private <T> void notifyListenersOfVisibilityChange(ITypedIngredient<T> ingredient, boolean visible) {
listeners.forEach(listener -> listener.onIngredientVisibilityChanged(ingredient, visible));
IngredientVisibility ingredientVisibility = ingredientVisibilityRef.get();
if (ingredientVisibility != null) {
ingredientVisibility.notifyListeners(ingredient, visible);
}
}
}
Loading

0 comments on commit 9d17753

Please sign in to comment.