Skip to content

Commit

Permalink
Merge pull request #16 from imreallybadatnames/1.20.1
Browse files Browse the repository at this point in the history
REI & JEI entry filtering
  • Loading branch information
DaFuqs authored May 6, 2024
2 parents 5e1cac6 + 33630da commit 521101c
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 25 deletions.
14 changes: 12 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.0-SNAPSHOT'
id 'fabric-loom' version '1.5-SNAPSHOT'
id 'maven-publish'
}

Expand All @@ -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.1-fabric-15.3.0.4.jar")
}

processResources {
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ archives_base_name = revelationary
fabric_version=0.90.0+1.20.1

jade_version=QhvPNPdp
wthit_version=8.5.0
wthit_version=8.5.0
rei_version=12.1.725
jei_version=15.3.0.4
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Binary file added libs/jei-1.20.1-fabric-15.3.0.4.jar
Binary file not shown.
52 changes: 31 additions & 21 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,8 +26,21 @@ 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()) {
Set<Item> revealedItems = new HashSet<>();
Expand All @@ -39,15 +54,13 @@ public static void processNewAdvancements(Set<Identifier> doneAdvancements, bool
revealedBlocks.add(block);
}
}

if (revealedBlockStates.size() > 0) {
activeBlockStateSwaps.removeAll(revealedBlockStates);

if (!revealedBlocks.isEmpty()) {
// uncloak the blocks
for (BlockState revealedBlockState : revealedBlockStates) {
activeBlockStateSwaps.remove(revealedBlockState);
Item blockItem = revealedBlockState.getBlock().asItem();
if (blockItem != null) {
activeItemSwaps.remove(blockItem);
}
for (Block revealedBlock: revealedBlocks) {
Item blockItem = revealedBlock.asItem();
if (blockItem != null) activeItemSwaps.remove(blockItem);
}
rebuildAllChunks();
}
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,19 +101,13 @@ public static void processRemovedAdvancements(@NotNull Set<Identifier> removedAd
}
}
}

if (concealedBlockStates.size() > 0) {

activeBlockStateSwaps.addAll(concealedBlockStates);
if (!concealedBlocks.isEmpty()) {
// uncloak the blocks
for (BlockState concealedBlockState : concealedBlockStates) {
if (!activeBlockStateSwaps.contains(concealedBlockState)) {
activeBlockStateSwaps.add(concealedBlockState);
}
Item blockItem = concealedBlockState.getBlock().asItem();
if (blockItem != null) {
if (!activeItemSwaps.contains(blockItem)) {
activeItemSwaps.add(blockItem);
}
}
for (Block concealedBlock : concealedBlocks) {
Item blockItem = concealedBlock.asItem();
if (blockItem != null) activeItemSwaps.add(blockItem);
}
rebuildAllChunks();
}
Expand All @@ -116,6 +124,7 @@ public static void processRemovedAdvancements(@NotNull Set<Identifier> removedAd
revelationAware.onCloak();
}
}
if (!concealedBlocks.isEmpty() || !concealedItems.isEmpty()) onItemSwap(true);
}
}

Expand Down Expand Up @@ -183,6 +192,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
//noinspection UnstableApiUsage
if (!RevelationaryConfig.get().HideCloakedEntriesFromRecipeViewers) return;
filteringRule = rule.hide(() ->
hiddenStacks.stream()
.map(EntryStacks::of)
.collect(Collectors.toList())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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
6 changes: 6 additions & 0 deletions 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 Down

0 comments on commit 521101c

Please sign in to comment.