Skip to content

Commit

Permalink
REI & JEI entry filtering: 1.20.6 edition
Browse files Browse the repository at this point in the history
  • Loading branch information
imreallybadatnames committed Jun 17, 2024
1 parent 4c17d66 commit 4f7c4be
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 29 deletions.
12 changes: 11 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ group = project.maven_group

repositories {
mavenCentral()
maven { url "https://maven.shedaniel.me/" }
maven { url "https://maven.terraformersmc.com" }
maven { url "https://api.modrinth.com/maven/" }
maven { url "https://maven.blamejared.com/" }
maven { url "https://maven.shedaniel.me/" }
maven { url "https://maven2.bai.lol" }
}

Expand All @@ -32,6 +33,15 @@ dependencies {

modCompileOnly "maven.modrinth:jade:${project.jade_version}"
modCompileOnly "mcp.mobius.waila:wthit-api:fabric-${project.wthit_version}"

modCompileOnly "me.shedaniel:RoughlyEnoughItems-api-fabric:${project.rei_version}"

// modCompileOnlyApi "mezz.jei:jei-${project.minecraft_version}-common-api:${project.jei_version}"
// modCompileOnlyApi "mezz.jei:jei-${project.minecraft_version}-fabric-api:${project.jei_version}"

// required due to stupid mapping error
// revert when this issue is resolved: https://github.com/mezz/JustEnoughItems/issues/3451
modCompileOnly files("libs/jei-1.20.6-fabric-18.0.0.62.jar")
}

processResources {
Expand Down
17 changes: 10 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
org.gradle.jvmargs = -Xmx2G

# Fabric Properties
minecraft_version=1.20.5
yarn_mappings=1.20.5+build.1
loader_version=0.15.10
minecraft_version=1.20.6
yarn_mappings=1.20.6+build.3
loader_version=0.15.11

# Mod Properties
mod_version = 1.3.7+1.20.5
mod_version = 1.3.8+1.20.6
maven_group = de.dafuqs.revelationary
archives_base_name = revelationary

# https://fabricmc.net/develop/
fabric_version=0.97.8+1.20.5
fabric_version=0.100.0+1.20.6

jade_version=lwgbRGaY
wthit_version=11.1.2
jade_version=H9wg0p1c
wthit_version=11.3.1

rei_version=16.0.729
jei_version=18.0.0.62
Binary file added libs/jei-1.20.6-fabric-18.0.0.62.jar
Binary file not shown.
60 changes: 40 additions & 20 deletions src/main/java/de/dafuqs/revelationary/ClientRevelationHolder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.dafuqs.revelationary;

import com.google.common.collect.Sets;
import de.dafuqs.revelationary.api.revelations.CloakSetChanged;
import de.dafuqs.revelationary.api.revelations.RevealingCallback;
import de.dafuqs.revelationary.api.revelations.RevelationAware;
import de.dafuqs.revelationary.api.revelations.WorldRendererAccessor;
Expand All @@ -24,7 +26,20 @@ public class ClientRevelationHolder {
public static List<RevealingCallback> callbacks = new ArrayList<>();

private static final Set<BlockState> activeBlockStateSwaps = new HashSet<>();
// used for creating diffs for CloakSetChanged event
private static Set<Item> previousActiveItemSwaps = new HashSet<>();
private static final Set<Item> activeItemSwaps = new HashSet<>();

private static void onItemSwap(boolean cloak) {
var diff = cloak ? Sets.difference(activeItemSwaps, previousActiveItemSwaps) : Sets.difference(previousActiveItemSwaps, activeItemSwaps);
var copy = Set.copyOf(activeItemSwaps);
var emptySet = Set.<Item>of();
// that is a legal expression, apparently
if (cloak)
CloakSetChanged.EVENT.invoker().onChange(diff, emptySet, copy);
else CloakSetChanged.EVENT.invoker().onChange(emptySet, diff, copy);
previousActiveItemSwaps = copy;
}

public static void processNewAdvancements(Set<Identifier> doneAdvancements, boolean isJoinPacket) {
if (!doneAdvancements.isEmpty()) {
Expand All @@ -39,24 +54,22 @@ public static void processNewAdvancements(Set<Identifier> doneAdvancements, bool
revealedBlocks.add(block);
}
}

// Handle edge case of block states revealed but not blocks;
// Checking for revealed blocks isn't necessary as they're derived from revealed block states.
final boolean blocksRevealed = activeBlockStateSwaps.removeAll(revealedBlockStates);

if (!revealedBlockStates.isEmpty()) {
if (!revealedBlocks.isEmpty()) {
// uncloak the blocks
for (BlockState revealedBlockState : revealedBlockStates) {
activeBlockStateSwaps.remove(revealedBlockState);
Item blockItem = revealedBlockState.getBlock().asItem();
if (blockItem != null) {
activeItemSwaps.remove(blockItem);
}
}
rebuildAllChunks();
}

for (Block revealedBlock : revealedBlocks) {
if (revealedBlock instanceof RevelationAware revelationAware) {
revelationAware.onUncloak();
for (Block revealedBlock: revealedBlocks) {
Item blockItem = revealedBlock.asItem();
if (blockItem != null) activeItemSwaps.remove(blockItem);
if (revealedBlock instanceof RevelationAware revelationAware) revelationAware.onUncloak();
}
}

if (blocksRevealed) rebuildAllChunks();

for (Item revealedItem : revealedItems) {
activeItemSwaps.remove(revealedItem);
if (revealedItem instanceof RevelationAware revelationAware) {
Expand All @@ -68,6 +81,7 @@ public static void processNewAdvancements(Set<Identifier> doneAdvancements, bool
for (RevealingCallback callback : callbacks) {
callback.trigger(doneAdvancements, revealedBlocks, revealedItems, isJoinPacket);
}
onItemSwap(false);
}
}
}
Expand All @@ -87,16 +101,20 @@ public static void processRemovedAdvancements(@NotNull Set<Identifier> removedAd
}
}
}

if (!concealedBlockStates.isEmpty()) {

// Handle edge case of block states concealed but not blocks;
// Checking for concealed blocks isn't necessary as they're derived from concealed block states.
final boolean blocksConcealed = activeBlockStateSwaps.addAll(concealedBlockStates);

if (!concealedBlocks.isEmpty()) {
// uncloak the blocks
for (BlockState concealedBlockState : concealedBlockStates) {
activeBlockStateSwaps.add(concealedBlockState);
Item blockItem = concealedBlockState.getBlock().asItem();
for (Block concealedBlock : concealedBlocks) {
Item blockItem = concealedBlock.asItem();
if (blockItem != null) activeItemSwaps.add(blockItem);
}
rebuildAllChunks();
}

if (blocksConcealed) rebuildAllChunks();

activeItemSwaps.addAll(concealedItems);

Expand All @@ -110,6 +128,7 @@ public static void processRemovedAdvancements(@NotNull Set<Identifier> removedAd
revelationAware.onCloak();
}
}
if (!concealedBlocks.isEmpty() || !concealedItems.isEmpty()) onItemSwap(true);
}
}

Expand Down Expand Up @@ -177,6 +196,7 @@ public static void cloakAll() {
cloak(registeredRevelation);
}
}
onItemSwap(true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.dafuqs.revelationary.api.revelations;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.item.Item;

import java.util.Set;

@FunctionalInterface
public interface CloakSetChanged {
Event<CloakSetChanged> EVENT = EventFactory.createArrayBacked(CloakSetChanged.class,
(listeners) -> (addedCloaks, removedCloaks, newCloaks) -> {
for (CloakSetChanged listener : listeners) listener.onChange(addedCloaks, removedCloaks, newCloaks);
});
// the diffs matter for JEI, the new cloaks set matters for REI
void onChange(Set<Item> addedCloaks, Set<Item> removedCloaks, Set<Item> newCloaks);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package de.dafuqs.revelationary.compat.jei;

import de.dafuqs.revelationary.Revelationary;
import de.dafuqs.revelationary.api.revelations.CloakSetChanged;
import de.dafuqs.revelationary.config.RevelationaryConfig;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.constants.VanillaTypes;
import mezz.jei.api.runtime.IJeiRuntime;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;

import java.util.Set;
import java.util.stream.Collectors;

public class RevelationaryJEIPlugin implements IModPlugin {
private IJeiRuntime runtime;
private Set<Item> stacksCache;

public RevelationaryJEIPlugin() {
if (!RevelationaryConfig.get().HideCloakedEntriesFromRecipeViewers) return;
CloakSetChanged.EVENT.register((added, removed, newStacks) -> {
stacksCache = newStacks;
if (runtime != null) {
var manager = runtime.getIngredientManager();
manager.removeIngredientsAtRuntime(VanillaTypes.ITEM_STACK,
added.stream().map(ItemStack::new).collect(Collectors.toList()));
manager.addIngredientsAtRuntime(VanillaTypes.ITEM_STACK,
removed.stream().map(ItemStack::new).collect(Collectors.toList()));
}
});
}

@Override
public @NotNull Identifier getPluginUid() {
return new Identifier(Revelationary.MOD_ID, "jei_plugin");
}

@Override
public void onRuntimeAvailable(IJeiRuntime jeiRuntime) {
runtime = jeiRuntime;
if (!RevelationaryConfig.get().HideCloakedEntriesFromRecipeViewers) return;
if (stacksCache != null) runtime.getIngredientManager()
.removeIngredientsAtRuntime(VanillaTypes.ITEM_STACK,
stacksCache.stream().map(ItemStack::new).collect(Collectors.toList()));
}

@Override
public void onRuntimeUnavailable() {
runtime = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package de.dafuqs.revelationary.compat.rei;

import de.dafuqs.revelationary.api.revelations.CloakSetChanged;
import de.dafuqs.revelationary.config.RevelationaryConfig;
import me.shedaniel.rei.api.client.entry.filtering.base.BasicFilteringRule;
import me.shedaniel.rei.api.client.plugins.REIClientPlugin;
import me.shedaniel.rei.api.common.util.EntryStacks;
import net.minecraft.item.Item;

import java.util.Set;
import java.util.stream.Collectors;


public class RevelationaryREIPlugin implements REIClientPlugin {
@SuppressWarnings("UnstableApiUsage")
private BasicFilteringRule.MarkDirty filteringRule;
private static Set<Item> hiddenStacks = Set.of();

public RevelationaryREIPlugin() {
if (!RevelationaryConfig.get().HideCloakedEntriesFromRecipeViewers) return;
CloakSetChanged.EVENT.register((added, removed, newStacks) -> {
hiddenStacks = newStacks;
//noinspection UnstableApiUsage
filteringRule.markDirty();
});
}

@Override
public void registerBasicEntryFiltering(@SuppressWarnings("UnstableApiUsage") BasicFilteringRule<?> rule) {
// not using .show to not interfere with other filtering rules
if (!RevelationaryConfig.get().HideCloakedEntriesFromRecipeViewers) return;
//noinspection UnstableApiUsage
filteringRule = rule.hide(() ->
hiddenStacks.stream()
.map(EntryStacks::of)
.collect(Collectors.toList())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static Config get() {
public static class Config {
public boolean PreventMiningOfUnrevealedBlocks = false;
public boolean UseTargetBlockOrItemNameInsteadOfScatter = false;
public boolean HideCloakedEntriesFromRecipeViewers = true;
public String NameForUnrevealedBlocks = "";
public String NameForUnrevealedItems = "";

Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
],
"jade": [
"de.dafuqs.revelationary.compat.jade.RevelationaryJadePlugin"
],
"jei_mod_plugin": [
"de.dafuqs.revelationary.compat.jei.RevelationaryJEIPlugin"
],
"rei_client": [
"de.dafuqs.revelationary.compat.rei.RevelationaryREIPlugin"
]
},
"mixins": [
Expand All @@ -32,7 +38,7 @@
"depends": {
"fabricloader": "*",
"fabric": "*",
"minecraft": ">=1.20.5",
"minecraft": ">=1.20.6 <1.21",
"java": ">=21"
},
"suggests": {
Expand Down

0 comments on commit 4f7c4be

Please sign in to comment.