Skip to content

Commit

Permalink
Prefill fields when editing
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEpicBlock committed Jul 4, 2024
1 parent 01d4a47 commit a587034
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public class Shard {
).apply(instance, Shard::new));

public static final PacketCodec<RegistryByteBuf, Shard> PACKET_CODEC = PacketCodecs.codec(CODEC).cast();

public static final Either<ItemStack, Identifier> MISSING_ICON = Either.right(ScatteredShards.id("textures/gui/shards/missing_icon.png"));

public static final Identifier MISSING_ICON_ID = ScatteredShards.id("textures/gui/shards/missing_icon.png");
public static final Either<ItemStack, Identifier> MISSING_ICON = Either.right(MISSING_ICON_ID);
public static final Identifier MISSING_SHARD_SOURCE = ScatteredShards.id("missing");
public static final Identifier LOST_AND_FOUND_SHARD_SOURCE = ScatteredShards.id("lost_and_found");
public static final Shard MISSING_SHARD = new Shard(ShardType.MISSING_ID, Text.of("Missing"), Text.of(""), Text.of(""), Text.of("None"), MISSING_SHARD_SOURCE, MISSING_ICON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.component.ComponentChanges;
import net.minecraft.component.ComponentMap;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
Expand All @@ -32,6 +33,8 @@
import net.modfest.scatteredshards.client.screen.widget.WShardPanel;
import net.modfest.scatteredshards.networking.C2SModifyShard;

import java.util.Objects;

public class ShardCreatorGuiDescription extends LightweightGuiDescription {
public static final String BASE_KEY = "gui.scattered_shards.creator.";
public static final Text TITLE_TEXT = Text.translatable(BASE_KEY + "title");
Expand Down Expand Up @@ -161,7 +164,7 @@ private void updateTextureIcon() {
public ShardCreatorGuiDescription(Identifier shardId, Shard shard, String modId) {
this(shardId);
this.shard = shard;
shardPanel.setShard(shard);

this.modIcon = FabricLoader.getInstance().getModContainer(modId)
.flatMap(it -> it.getMetadata().getIconPath(16))
.filter(it -> it != null && it.startsWith("assets/"))
Expand All @@ -170,12 +173,41 @@ public ShardCreatorGuiDescription(Identifier shardId, Shard shard, String modId)
int firstSlash = it.indexOf("/");
String namespace = it.substring(0,firstSlash);
String path = it.substring(firstSlash+1);

return Identifier.of(namespace, path);
})
.orElse(Shard.MISSING_ICON.right().get()); //TODO: Deal with non-resource icons here.
Shard.getSourceForModId(modId).ifPresent(shard::setSource);
shard.setSourceId(Identifier.of(modId, "shard_pack"));

// Initialize field values
this.nameField.setText(shard.name().getLiteralString());
this.loreField.setText(shard.lore().getLiteralString());
this.hintField.setText(shard.hint().getLiteralString());
shard.icon().ifRight(a -> {
this.iconToggle.setLeft();
if (Objects.equals(a, modIcon)) {
this.textureToggle.setToggle(true);
} else {
this.textureToggle.setToggle(false);
if (Objects.equals(a, Shard.MISSING_ICON_ID)) {
this.textureField.setText("");
} else {
this.textureField.setText(a.toString());
}
}
});
shard.icon().ifLeft(a -> {
ComponentChanges.CODEC.encodeStart(JsonOps.INSTANCE, a.getComponentChanges()).ifSuccess(componentJson -> {
this.iconToggle.setRight();
this.itemField.setText(Registries.ITEM.getId(a.getItem()).toString());
String nbt = componentJson.toString();
if ("{}".equals(nbt)) nbt = "";
this.nbtField.setText(nbt);
});
});

shardPanel.setShard(shard);
}

public ShardCreatorGuiDescription(Identifier shardId) {
Expand Down

0 comments on commit a587034

Please sign in to comment.