1919
2020package de .mschae23 .grindenchantments .config ;
2121
22+ import java .util .List ;
23+ import java .util .Optional ;
24+ import java .util .stream .Stream ;
2225import net .minecraft .component .type .ItemEnchantmentsComponent ;
2326import net .minecraft .enchantment .Enchantment ;
2427import net .minecraft .item .Item ;
25- import net .minecraft .registry .RegistryCodecs ;
28+ import net .minecraft .registry .RegistryKey ;
2629import net .minecraft .registry .RegistryKeys ;
30+ import net .minecraft .registry .RegistryWrapper ;
2731import net .minecraft .registry .entry .RegistryEntry ;
28- import net .minecraft .registry .entry .RegistryEntryList ;
2932import net .minecraft .registry .tag .EnchantmentTags ;
33+ import net .minecraft .util .Identifier ;
34+ import net .minecraft .util .dynamic .Codecs ;
35+ import com .mojang .datafixers .util .Pair ;
3036import com .mojang .serialization .Codec ;
3137import com .mojang .serialization .codecs .RecordCodecBuilder ;
38+ import de .mschae23 .grindenchantments .GrindEnchantmentsMod ;
39+ import org .apache .logging .log4j .Level ;
3240
3341public record FilterConfig (boolean enabled , ItemConfig item , EnchantmentConfig enchantment , FilterAction curses ) {
3442 public static final Codec <FilterConfig > CODEC = RecordCodecBuilder .create (instance -> instance .group (
@@ -40,32 +48,41 @@ public record FilterConfig(boolean enabled, ItemConfig item, EnchantmentConfig e
4048
4149 public static final FilterConfig DEFAULT = new FilterConfig (true , ItemConfig .DEFAULT , EnchantmentConfig .DEFAULT , FilterAction .IGNORE );
4250
43- public ItemEnchantmentsComponent filter (ItemEnchantmentsComponent enchantments ) {
44- if (!this .enabled ) {
45- return enchantments ;
46- }
47-
48- ItemEnchantmentsComponent .Builder builder = new ItemEnchantmentsComponent .Builder (enchantments );
49-
51+ private boolean shouldDeny (ItemEnchantmentsComponent .Builder builder ) {
5052 if (this .curses == FilterAction .DENY ) {
5153 for (RegistryEntry <Enchantment > entry : builder .getEnchantments ()) {
5254 if (entry .isIn (EnchantmentTags .CURSE )) {
53- return ItemEnchantmentsComponent . DEFAULT ;
55+ return true ;
5456 }
5557 }
5658 }
5759
5860 if (this .enchantment .action == FilterAction .DENY ) {
5961 for (RegistryEntry <Enchantment > entry : builder .getEnchantments ()) {
60- if (this .enchantment .enchantments .contains (entry )) {
61- return ItemEnchantmentsComponent . DEFAULT ;
62+ if (entry . getKey (). map ( key -> this .enchantment .enchantments .contains (key . getValue ())). orElse ( false )) {
63+ return true ;
6264 }
6365 }
6466 }
6567
68+ return false ;
69+ }
70+
71+ public ItemEnchantmentsComponent filter (ItemEnchantmentsComponent enchantments ) {
72+ if (!this .enabled ) {
73+ return enchantments ;
74+ }
75+
76+ ItemEnchantmentsComponent .Builder builder = new ItemEnchantmentsComponent .Builder (enchantments );
77+
78+ if (this .shouldDeny (builder )) {
79+ return ItemEnchantmentsComponent .DEFAULT ;
80+ }
81+
6682 builder .remove (enchantment ->
6783 ((this .curses == FilterAction .IGNORE ) && enchantment .isIn (EnchantmentTags .CURSE ))
68- || ((this .enchantment .action == FilterAction .IGNORE ) == this .enchantment .enchantments .contains (enchantment )));
84+ || ((this .enchantment .action == FilterAction .IGNORE ) == enchantment .getKey ().map (key ->
85+ this .enchantment .enchantments .contains (key .getValue ())).orElse (false )));
6986
7087 return builder .build ();
7188 }
@@ -77,44 +94,72 @@ public ItemEnchantmentsComponent filterReversed(ItemEnchantmentsComponent enchan
7794
7895 ItemEnchantmentsComponent .Builder builder = new ItemEnchantmentsComponent .Builder (enchantments );
7996
80- if (this .curses == FilterAction .DENY ) {
81- for (RegistryEntry <Enchantment > entry : builder .getEnchantments ()) {
82- if (entry .isIn (EnchantmentTags .CURSE )) {
83- return ItemEnchantmentsComponent .DEFAULT ;
84- }
85- }
86- }
87-
88- if (this .enchantment .action == FilterAction .DENY ) {
89- for (RegistryEntry <Enchantment > entry : builder .getEnchantments ()) {
90- if (this .enchantment .enchantments .contains (entry )) {
91- return ItemEnchantmentsComponent .DEFAULT ;
92- }
93- }
97+ if (this .shouldDeny (builder )) {
98+ return ItemEnchantmentsComponent .DEFAULT ;
9499 }
95100
96101 builder .remove (enchantment ->
97102 ((this .curses == FilterAction .ALLOW ) || !enchantment .isIn (EnchantmentTags .CURSE ))
98- && ((this .enchantment .action == FilterAction .ALLOW ) == this .enchantment .enchantments .contains (enchantment )));
103+ && ((this .enchantment .action == FilterAction .ALLOW ) == enchantment .getKey ().map (key ->
104+ this .enchantment .enchantments .contains (key .getValue ())).orElse (false )));
99105
100106 return builder .build ();
101107 }
102108
103- public record ItemConfig (RegistryEntryList <Item > items , FilterAction action ) {
109+ public void validateRegistryEntries (RegistryWrapper .WrapperLookup wrapperLookup ) {
110+ this .item .validateRegistryEntries (wrapperLookup );
111+ this .enchantment .validateRegistryEntries (wrapperLookup );
112+ }
113+
114+ public record ItemConfig (List <Identifier > items , FilterAction action ) {
104115 public static final Codec <ItemConfig > CODEC = RecordCodecBuilder .create (instance -> instance .group (
105- RegistryCodecs . entryList ( RegistryKeys . ITEM ).fieldOf ("enchantments" ).forGetter (ItemConfig ::items ),
116+ Codecs . listOrSingle ( Identifier . CODEC ).fieldOf ("enchantments" ).forGetter (ItemConfig ::items ),
106117 FilterAction .NON_IGNORE_CODEC .fieldOf ("action" ).forGetter (ItemConfig ::action )
107118 ).apply (instance , instance .stable (ItemConfig ::new )));
108119
109- public static final ItemConfig DEFAULT = new ItemConfig (RegistryEntryList .empty (), FilterAction .DENY );
120+ public static final ItemConfig DEFAULT = new ItemConfig (List .of (), FilterAction .DENY );
121+
122+ public void validateRegistryEntries (RegistryWrapper .WrapperLookup wrapperLookup ) {
123+ Optional <? extends RegistryWrapper .Impl <Item >> registryWrapperOpt = wrapperLookup .getOptional (RegistryKeys .ITEM );
124+
125+ if (registryWrapperOpt .isEmpty ()) {
126+ GrindEnchantmentsMod .log (Level .WARN , "Item registry is not present" );
127+ return ;
128+ }
129+
130+ RegistryWrapper .Impl <Item > registryWrapper = registryWrapperOpt .get ();
131+
132+ this .items .stream ()
133+ .map (item -> Pair .of (item , registryWrapper .getOptional (RegistryKey .of (RegistryKeys .ITEM , item ))))
134+ .flatMap (result -> result .getSecond ().isEmpty () ? Stream .of (result .getFirst ()) : Stream .empty ())
135+ .map (Identifier ::toString )
136+ .forEach (item -> GrindEnchantmentsMod .log (Level .WARN , "Filter config contains unknown item: " + item ));
137+ }
110138 }
111139
112- public record EnchantmentConfig (RegistryEntryList < Enchantment > enchantments , FilterAction action ) {
140+ public record EnchantmentConfig (List < Identifier > enchantments , FilterAction action ) {
113141 public static final Codec <EnchantmentConfig > CODEC = RecordCodecBuilder .create (instance -> instance .group (
114- RegistryCodecs . entryList ( RegistryKeys . ENCHANTMENT ).fieldOf ("enchantments" ).forGetter (EnchantmentConfig ::enchantments ),
142+ Codecs . listOrSingle ( Identifier . CODEC ).fieldOf ("enchantments" ).forGetter (EnchantmentConfig ::enchantments ),
115143 FilterAction .CODEC .fieldOf ("action" ).forGetter (EnchantmentConfig ::action )
116144 ).apply (instance , instance .stable (EnchantmentConfig ::new )));
117145
118- public static final EnchantmentConfig DEFAULT = new EnchantmentConfig (RegistryEntryList .empty (), FilterAction .IGNORE );
146+ public static final EnchantmentConfig DEFAULT = new EnchantmentConfig (List .of (), FilterAction .IGNORE );
147+
148+ public void validateRegistryEntries (RegistryWrapper .WrapperLookup wrapperLookup ) {
149+ Optional <? extends RegistryWrapper .Impl <Enchantment >> registryWrapperOpt = wrapperLookup .getOptional (RegistryKeys .ENCHANTMENT );
150+
151+ if (registryWrapperOpt .isEmpty ()) {
152+ GrindEnchantmentsMod .log (Level .WARN , "Enchantment registry is not present" );
153+ return ;
154+ }
155+
156+ RegistryWrapper .Impl <Enchantment > registryWrapper = registryWrapperOpt .get ();
157+
158+ this .enchantments .stream ()
159+ .map (enchantment -> Pair .of (enchantment , registryWrapper .getOptional (RegistryKey .of (RegistryKeys .ENCHANTMENT , enchantment ))))
160+ .flatMap (result -> result .getSecond ().isEmpty () ? Stream .of (result .getFirst ()) : Stream .empty ())
161+ .map (Identifier ::toString )
162+ .forEach (item -> GrindEnchantmentsMod .log (Level .WARN , "Filter config contains unknown enchantment: " + item ));
163+ }
119164 }
120165}
0 commit comments