2
2
3
3
import com .mojang .brigadier .StringReader ;
4
4
import com .mojang .brigadier .exceptions .CommandSyntaxException ;
5
- import com .mojang .brigadier .exceptions .SimpleCommandExceptionType ;
6
5
import com .mojang .datafixers .util .Either ;
7
- import com .mojang .serialization .DataResult ;
8
- import com .mojang .serialization .JsonOps ;
9
6
import io .github .cottonmc .cotton .gui .client .BackgroundPainter ;
10
7
import io .github .cottonmc .cotton .gui .client .CottonClientScreen ;
11
8
import io .github .cottonmc .cotton .gui .client .LightweightGuiDescription ;
16
13
import io .github .cottonmc .cotton .gui .widget .data .Axis ;
17
14
import io .github .cottonmc .cotton .gui .widget .data .HorizontalAlignment ;
18
15
import io .github .cottonmc .cotton .gui .widget .data .Insets ;
19
- import it .unimi .dsi .fastutil .objects .ReferenceArraySet ;
20
16
import net .fabricmc .fabric .api .client .networking .v1 .ClientPlayNetworking ;
21
17
import net .minecraft .client .MinecraftClient ;
22
18
import net .minecraft .command .argument .ItemStringReader ;
23
- import net .minecraft .component .ComponentChanges ;
24
19
import net .minecraft .component .ComponentMap ;
25
- import net .minecraft .component .ComponentType ;
26
20
import net .minecraft .item .Item ;
27
21
import net .minecraft .item .ItemStack ;
28
- import net .minecraft .nbt .NbtElement ;
29
- import net .minecraft .nbt .NbtOps ;
30
- import net .minecraft .nbt .StringNbtReader ;
31
22
import net .minecraft .registry .Registries ;
32
23
import net .minecraft .resource .Resource ;
33
24
import net .minecraft .text .Text ;
43
34
import net .modfest .scatteredshards .networking .C2SModifyShard ;
44
35
import net .modfest .scatteredshards .util .ModMetaUtil ;
45
36
37
+ import java .util .Locale ;
46
38
import java .util .Objects ;
47
39
import java .util .Optional ;
48
40
import java .util .Set ;
41
+ import java .util .concurrent .CompletableFuture ;
49
42
50
43
public class ShardCreatorGuiDescription extends LightweightGuiDescription {
51
44
public static final Text TITLE_TEXT = Text .translatable ("gui.scattered_shards.creator.title" );
@@ -56,9 +49,9 @@ public class ShardCreatorGuiDescription extends LightweightGuiDescription {
56
49
public static final Text ICON_TEXTURE_TEXT = Text .translatable ("gui.scattered_shards.creator.icon.texture" );
57
50
public static final Text ICON_ITEM_TEXT = Text .translatable ("gui.scattered_shards.creator.icon.item" );
58
51
public static final Text ITEM_TEXT = Text .translatable ("gui.scattered_shards.creator.field.item.id" );
59
- public static final Text COMPONENT_TEXT = Text .translatable ("gui.scattered_shards.creator.field.item.component" );
60
52
public static final Text USE_MOD_ICON_TEXT = Text .translatable ("gui.scattered_shards.creator.toggle.mod_icon" );
61
53
public static final Text SAVE_TEXT = Text .translatable ("gui.scattered_shards.creator.button.save" );
54
+ private static final String PREVIOUS_VALUE = "<previous_value>" ;
62
55
63
56
private Identifier shardId ;
64
57
private Shard shard ;
@@ -115,22 +108,15 @@ public static Identifier parseTexture(String path) {
115
108
116
109
public WProtectableField itemField = new WProtectableField (ITEM_TEXT )
117
110
.setChangedListener ((it ) -> {
118
- this .item = null ;
119
- Identifier id = Identifier .tryParse (it );
120
- if (id != null ) {
121
- this .item = Registries .ITEM .containsId (id )
122
- ? Registries .ITEM .get (id )
123
- : null ;
111
+ if (it .isBlank () || Objects .equals (it , PREVIOUS_VALUE )) {
112
+ return ;
124
113
}
125
- updateItemIcon ();
126
- });
127
114
128
- public WProtectableField componentField = new WProtectableField (COMPONENT_TEXT )
129
- .setChangedListener ((it ) -> {
130
115
try {
131
- updateComponents (new StringReader (it ));
116
+ updateItem (new StringReader (it ));
132
117
} catch (Exception ignored ) {
133
118
}
119
+
134
120
updateItemIcon ();
135
121
});
136
122
@@ -142,71 +128,13 @@ public static Identifier parseTexture(String path) {
142
128
private Identifier iconPath = null ;
143
129
144
130
145
- private <T > void updateComponents (StringReader reader ) throws CommandSyntaxException {
146
- ComponentChanges .Builder changesBuilder = ComponentChanges .builder ();
147
- Set <ComponentType <?>> known = new ReferenceArraySet <>();
148
-
149
- // Begin of component list
150
- reader .expect ('[' );
151
- reader .skipWhitespace ();
152
-
153
- // Body of component list
154
- while (reader .canRead () && reader .peek () != ']' ) {
155
- boolean negation = false ;
156
-
157
- if (reader .peek () == '!' ) {
158
- // Negate incoming block
159
- reader .skip ();
160
- negation = true ;
161
- }
162
-
163
- // Component Type
164
- @ SuppressWarnings ("unchecked" ) // We could avoid this with a separate method for getting the values but eh
165
- ComponentType <T > componentType = (ComponentType <T >) ItemStringReader .Reader .readComponentType (reader );
166
- reader .skipWhitespace ();
167
- if (!known .add (componentType ))
168
- throw new SimpleCommandExceptionType (Text .literal ("Same component cannot appear twice" )).create ();
169
-
170
- if (negation )
171
- changesBuilder .remove (componentType );
172
- else {
173
- reader .expect ('=' );
174
- reader .skipWhitespace ();
175
-
176
- // Component Value
177
-
178
- int index = reader .getCursor ();
179
-
180
- NbtElement nbtElement = new StringNbtReader (reader ).parseElement ();
181
- DataResult <T > dataResult = componentType .getCodecOrThrow ().parse (NbtOps .INSTANCE , nbtElement );
182
-
183
- changesBuilder .add (componentType , dataResult .getOrThrow (error -> {
184
- reader .setCursor (index );
185
- return new SimpleCommandExceptionType (Text .literal ("Component is malformed" )).create ();
186
- }));
187
-
188
- reader .skipWhitespace ();
189
- }
190
-
191
- // List separation
192
-
193
- if (!reader .canRead () || reader .peek () != ',' )
194
- break ;
195
-
196
- reader .skip ();
197
- reader .skipWhitespace ();
198
- if (!reader .canRead ())
199
- throw new SimpleCommandExceptionType (Text .literal ("Expected component" )).create ();
200
- }
201
-
202
- // End of components list
203
- reader .expect (']' );
204
-
205
- ComponentChanges componentChanges = changesBuilder .build ();
131
+ private void updateItem (StringReader reader ) throws CommandSyntaxException {
132
+ var itemReader = new ItemStringReader (MinecraftClient .getInstance ().world .getRegistryManager ());
133
+ var result = itemReader .consume (reader );
206
134
135
+ this .item = result .item ().value ();
207
136
ComponentMap .Builder mapBuilder = ComponentMap .builder ();
208
- mapBuilder .addAll (componentChanges .toAddedRemovedPair ().added ());
209
-
137
+ mapBuilder .addAll (result .components ().toAddedRemovedPair ().added ());
210
138
this .itemComponents = mapBuilder .build ();
211
139
}
212
140
@@ -257,13 +185,20 @@ public ShardCreatorGuiDescription(Identifier shardId, Shard shard, String modId)
257
185
}
258
186
}
259
187
});
260
- shard .icon ().ifLeft (a -> ComponentChanges . CODEC . encodeStart ( JsonOps . INSTANCE , a . getComponentChanges ()). ifSuccess ( componentJson -> {
188
+ shard .icon ().ifLeft (itemStack -> {
261
189
this .iconToggle .setRight ();
262
- this .itemField .setText (Registries .ITEM .getId (a .getItem ()).toString ());
263
- String nbt = componentJson .toString ();
264
- if ("{}" .equals (nbt )) nbt = "" ;
265
- this .componentField .setText (nbt );
266
- }));
190
+
191
+ if (itemStack .getComponentChanges ().isEmpty ()) {
192
+ this .itemField .setText (Registries .ITEM .getId (itemStack .getItem ()).toString ());
193
+ } else {
194
+ // TODO
195
+ this .itemField .setText (PREVIOUS_VALUE );
196
+ }
197
+
198
+ this .item = itemStack .getItem ();
199
+ this .itemComponents = itemStack .getComponents ();
200
+ updateItemIcon ();
201
+ });
267
202
268
203
shardPanel .setShard (shard );
269
204
}
@@ -298,7 +233,6 @@ public ShardCreatorGuiDescription(Identifier shardId) {
298
233
textureIconPanel .add (textureToggle );
299
234
300
235
itemIconPanel .add (itemField );
301
- itemIconPanel .add (componentField );
302
236
303
237
editorPanel .add (saveButton );
304
238
0 commit comments