Skip to content

Commit 1adc216

Browse files
committed
Make item components work
1 parent a890808 commit 1adc216

File tree

2 files changed

+25
-92
lines changed

2 files changed

+25
-92
lines changed

src/main/java/net/modfest/scatteredshards/client/screen/ShardCreatorGuiDescription.java

Lines changed: 25 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import com.mojang.brigadier.StringReader;
44
import com.mojang.brigadier.exceptions.CommandSyntaxException;
5-
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
65
import com.mojang.datafixers.util.Either;
7-
import com.mojang.serialization.DataResult;
8-
import com.mojang.serialization.JsonOps;
96
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
107
import io.github.cottonmc.cotton.gui.client.CottonClientScreen;
118
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
@@ -16,18 +13,12 @@
1613
import io.github.cottonmc.cotton.gui.widget.data.Axis;
1714
import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment;
1815
import io.github.cottonmc.cotton.gui.widget.data.Insets;
19-
import it.unimi.dsi.fastutil.objects.ReferenceArraySet;
2016
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
2117
import net.minecraft.client.MinecraftClient;
2218
import net.minecraft.command.argument.ItemStringReader;
23-
import net.minecraft.component.ComponentChanges;
2419
import net.minecraft.component.ComponentMap;
25-
import net.minecraft.component.ComponentType;
2620
import net.minecraft.item.Item;
2721
import net.minecraft.item.ItemStack;
28-
import net.minecraft.nbt.NbtElement;
29-
import net.minecraft.nbt.NbtOps;
30-
import net.minecraft.nbt.StringNbtReader;
3122
import net.minecraft.registry.Registries;
3223
import net.minecraft.resource.Resource;
3324
import net.minecraft.text.Text;
@@ -43,9 +34,11 @@
4334
import net.modfest.scatteredshards.networking.C2SModifyShard;
4435
import net.modfest.scatteredshards.util.ModMetaUtil;
4536

37+
import java.util.Locale;
4638
import java.util.Objects;
4739
import java.util.Optional;
4840
import java.util.Set;
41+
import java.util.concurrent.CompletableFuture;
4942

5043
public class ShardCreatorGuiDescription extends LightweightGuiDescription {
5144
public static final Text TITLE_TEXT = Text.translatable("gui.scattered_shards.creator.title");
@@ -56,9 +49,9 @@ public class ShardCreatorGuiDescription extends LightweightGuiDescription {
5649
public static final Text ICON_TEXTURE_TEXT = Text.translatable("gui.scattered_shards.creator.icon.texture");
5750
public static final Text ICON_ITEM_TEXT = Text.translatable("gui.scattered_shards.creator.icon.item");
5851
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");
6052
public static final Text USE_MOD_ICON_TEXT = Text.translatable("gui.scattered_shards.creator.toggle.mod_icon");
6153
public static final Text SAVE_TEXT = Text.translatable("gui.scattered_shards.creator.button.save");
54+
private static final String PREVIOUS_VALUE = "<previous_value>";
6255

6356
private Identifier shardId;
6457
private Shard shard;
@@ -115,22 +108,15 @@ public static Identifier parseTexture(String path) {
115108

116109
public WProtectableField itemField = new WProtectableField(ITEM_TEXT)
117110
.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;
124113
}
125-
updateItemIcon();
126-
});
127114

128-
public WProtectableField componentField = new WProtectableField(COMPONENT_TEXT)
129-
.setChangedListener((it) -> {
130115
try {
131-
updateComponents(new StringReader(it));
116+
updateItem(new StringReader(it));
132117
} catch (Exception ignored) {
133118
}
119+
134120
updateItemIcon();
135121
});
136122

@@ -142,71 +128,13 @@ public static Identifier parseTexture(String path) {
142128
private Identifier iconPath = null;
143129

144130

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);
206134

135+
this.item = result.item().value();
207136
ComponentMap.Builder mapBuilder = ComponentMap.builder();
208-
mapBuilder.addAll(componentChanges.toAddedRemovedPair().added());
209-
137+
mapBuilder.addAll(result.components().toAddedRemovedPair().added());
210138
this.itemComponents = mapBuilder.build();
211139
}
212140

@@ -257,13 +185,20 @@ public ShardCreatorGuiDescription(Identifier shardId, Shard shard, String modId)
257185
}
258186
}
259187
});
260-
shard.icon().ifLeft(a -> ComponentChanges.CODEC.encodeStart(JsonOps.INSTANCE, a.getComponentChanges()).ifSuccess(componentJson -> {
188+
shard.icon().ifLeft(itemStack -> {
261189
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+
});
267202

268203
shardPanel.setShard(shard);
269204
}
@@ -298,7 +233,6 @@ public ShardCreatorGuiDescription(Identifier shardId) {
298233
textureIconPanel.add(textureToggle);
299234

300235
itemIconPanel.add(itemField);
301-
itemIconPanel.add(componentField);
302236

303237
editorPanel.add(saveButton);
304238

src/main/resources/assets/scattered_shards/lang/en_us.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
"gui.scattered_shards.creator.icon.texture": "Texture Icon",
4747
"gui.scattered_shards.creator.icon.item": "Item Icon",
4848
"gui.scattered_shards.creator.field.item.id": "Item ID...",
49-
"gui.scattered_shards.creator.field.item.component": "Item Components...",
5049
"gui.scattered_shards.creator.field.texture": "Texture path...",
5150
"gui.scattered_shards.creator.toggle.mod_icon": "Use mod icon",
5251
"gui.scattered_shards.creator.button.save": "Save",

0 commit comments

Comments
 (0)