11package org .embeddedt .modernfix .searchtree ;
22
3+ import com .google .common .base .Predicates ;
34import me .shedaniel .rei .api .client .registry .entry .EntryRegistry ;
45import me .shedaniel .rei .api .common .entry .EntryStack ;
56import me .shedaniel .rei .api .common .entry .type .VanillaEntryTypes ;
67import me .shedaniel .rei .impl .client .search .AsyncSearchManager ;
8+ import me .shedaniel .rei .impl .common .entry .type .EntryRegistryImpl ;
9+ import me .shedaniel .rei .impl .common .util .HashedEntryStackWrapper ;
710import net .minecraft .client .searchtree .ReloadableIdSearchTree ;
811import net .minecraft .world .item .ItemStack ;
912import org .embeddedt .modernfix .ModernFix ;
1013import org .embeddedt .modernfix .platform .ModernFixPlatformHooks ;
1114
15+ import java .lang .invoke .MethodHandle ;
16+ import java .lang .invoke .MethodHandles ;
17+ import java .lang .reflect .Method ;
1218import java .util .ArrayList ;
1319import java .util .Collections ;
1420import java .util .List ;
21+ import java .util .function .Supplier ;
22+ import java .util .function .UnaryOperator ;
1523
1624public class REIBackedSearchTree extends DummySearchTree <ItemStack > {
17- private final AsyncSearchManager searchManager = new AsyncSearchManager (EntryRegistry .getInstance ()::getPreFilteredList , () -> {
18- return stack -> true ;
19- }, EntryStack ::normalize );
25+ private final AsyncSearchManager searchManager = createSearchManager ();
2026
2127 private final boolean filteringByTag ;
2228 private String lastSearchText = "" ;
@@ -39,14 +45,23 @@ private List<ItemStack> searchREI(String pSearchText) {
3945 if (!pSearchText .equals (lastSearchText )) {
4046 listCache .clear ();
4147 this .searchManager .updateFilter (pSearchText );
42- List < EntryStack <?>> stacks ;
48+ List stacks ;
4349 try {
4450 stacks = this .searchManager .getNow ();
4551 } catch (RuntimeException e ) {
4652 ModernFix .LOGGER .error ("Couldn't search for '" + pSearchText + "'" , e );
4753 stacks = Collections .emptyList ();
4854 }
49- for (EntryStack <?> stack : stacks ) {
55+ for (Object o : stacks ) {
56+ EntryStack <?> stack ;
57+ if (o instanceof EntryStack <?>)
58+ stack = (EntryStack <?>)o ;
59+ else if (o instanceof HashedEntryStackWrapper ) {
60+ stack = ((HashedEntryStackWrapper )o ).unwrap ();
61+ } else {
62+ ModernFix .LOGGER .error ("Don't know how to handle {}" , o .getClass ().getName ());
63+ continue ;
64+ }
5065 if (stack .getType () == VanillaEntryTypes .ITEM ) {
5166 listCache .add (stack .cheatsAs ().getValue ());
5267 }
@@ -56,6 +71,50 @@ private List<ItemStack> searchREI(String pSearchText) {
5671 return listCache ;
5772 }
5873
74+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
75+ private static AsyncSearchManager createSearchManager () {
76+ Method m , normalizeMethod ;
77+ try {
78+ try {
79+ m = EntryRegistryImpl .class .getDeclaredMethod ("getPreFilteredComplexList" );
80+ m .setAccessible (true );
81+ normalizeMethod = HashedEntryStackWrapper .class .getDeclaredMethod ("normalize" );
82+ normalizeMethod .setAccessible (true );
83+ } catch (NoSuchMethodException e ) {
84+ m = EntryRegistryImpl .class .getDeclaredMethod ("getPreFilteredList" );
85+ m .setAccessible (true );
86+ normalizeMethod = EntryStack .class .getDeclaredMethod ("normalize" );
87+ normalizeMethod .setAccessible (true );
88+ }
89+ final MethodHandle getListMethod = MethodHandles .publicLookup ().unreflect (m );
90+ final MethodHandle normalize = MethodHandles .publicLookup ().unreflect (normalizeMethod );
91+ final EntryRegistryImpl registry = (EntryRegistryImpl )EntryRegistry .getInstance ();
92+ Supplier stackListSupplier = () -> {
93+ try {
94+ return (List )getListMethod .invokeExact (registry );
95+ } catch (Throwable e ) {
96+ if (e instanceof RuntimeException )
97+ throw (RuntimeException )e ;
98+ throw new RuntimeException (e );
99+ }
100+ };
101+ UnaryOperator normalizeOperator = o -> {
102+ try {
103+ return normalize .invoke (o );
104+ } catch (Throwable e ) {
105+ if (e instanceof RuntimeException )
106+ throw (RuntimeException )e ;
107+ throw new RuntimeException (e );
108+ }
109+ };
110+ return new AsyncSearchManager (stackListSupplier , () -> {
111+ return Predicates .alwaysTrue ();
112+ }, normalizeOperator );
113+ } catch (ReflectiveOperationException e ) {
114+ throw new RuntimeException (e );
115+ }
116+ }
117+
59118 public static final SearchTreeProviderRegistry .Provider PROVIDER = new SearchTreeProviderRegistry .Provider () {
60119 @ Override
61120 public ReloadableIdSearchTree <ItemStack > getSearchTree (boolean tag ) {
0 commit comments