Skip to content

Commit

Permalink
Add per-card list order
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Dec 26, 2024
1 parent 6f4e637 commit 0d54212
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
21 changes: 17 additions & 4 deletions src/main/java/net/modfest/scatteredshards/api/shard/Shard.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.Stream;

public class Shard {
Expand All @@ -31,31 +32,34 @@ public class Shard {
TextCodecs.CODEC.fieldOf("lore").forGetter(Shard::lore),
TextCodecs.CODEC.fieldOf("hint").forGetter(Shard::hint),
Identifier.CODEC.fieldOf("source_id").forGetter(Shard::sourceId),
ICON_CODEC.fieldOf("icon").forGetter(Shard::icon)
ICON_CODEC.fieldOf("icon").forGetter(Shard::icon),
Codec.intRange(0, Integer.MAX_VALUE).optionalFieldOf("list_order").forGetter(Shard::listOrder)
).apply(instance, Shard::new));

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

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 Shard MISSING_SHARD = new Shard(ShardType.MISSING_ID, Text.of("Missing"), Text.of(""), Text.of(""), MISSING_SHARD_SOURCE, MISSING_ICON);
public static final Shard MISSING_SHARD = new Shard(ShardType.MISSING_ID, Text.of("Missing"), Text.of(""), Text.of(""), MISSING_SHARD_SOURCE, MISSING_ICON, Optional.empty());

protected Identifier shardTypeId;
protected Text name;
protected Text lore;
protected Text hint;
protected Integer listOrder;
protected Identifier sourceId;
protected Either<ItemStack, Identifier> icon;

public Shard(Identifier shardTypeId, Text name, Text lore, Text hint, Identifier sourceId, Either<ItemStack, Identifier> icon) {
public Shard(Identifier shardTypeId, Text name, Text lore, Text hint, Identifier sourceId, Either<ItemStack, Identifier> icon, Optional<Integer> listOrder) {
Stream.of(name, lore, hint, icon).forEach(Objects::requireNonNull);
this.shardTypeId = shardTypeId;
this.name = name;
this.lore = lore;
this.hint = hint;
this.sourceId = sourceId;
this.icon = icon;
this.listOrder = listOrder.orElse(null);
}

public Identifier shardTypeId() {
Expand All @@ -74,6 +78,10 @@ public Text hint() {
return hint;
}

public Optional<Integer> listOrder() {
return listOrder == null ? Optional.empty() : Optional.of(listOrder);
}

public Identifier sourceId() {
return sourceId;
}
Expand Down Expand Up @@ -102,6 +110,11 @@ public Shard setHint(Text value) {
return this;
}

public Shard setListOrder(Integer value) {
this.listOrder = value;
return this;
}

public Shard setIcon(Either<ItemStack, Identifier> icon) {
this.icon = icon;
return this;
Expand Down Expand Up @@ -136,7 +149,7 @@ public JsonObject toJson() {

public Shard copy() {
Either<ItemStack, Identifier> icon = icon().mapBoth(stack -> stack, id -> id);
return new Shard(shardTypeId, name.copy(), lore.copy(), hint.copy(), sourceId, icon);
return new Shard(shardTypeId, name.copy(), lore.copy(), hint.copy(), sourceId, icon, listOrder());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class ShardCreatorGuiDescription extends LightweightGuiDescription {
public static final Text NAME_TEXT = Text.translatable("gui.scattered_shards.creator.field.name");
public static final Text LORE_TEXT = Text.translatable("gui.scattered_shards.creator.field.lore");
public static final Text HINT_TEXT = Text.translatable("gui.scattered_shards.creator.field.hint");
public static final Text ORDER_TEXT = Text.translatable("gui.scattered_shards.creator.field.order");
public static final Text TEXTURE_TEXT = Text.translatable("gui.scattered_shards.creator.field.texture");
public static final Text ICON_TEXTURE_TEXT = Text.translatable("gui.scattered_shards.creator.icon.texture");
public static final Text ICON_ITEM_TEXT = Text.translatable("gui.scattered_shards.creator.icon.item");
Expand Down Expand Up @@ -84,6 +85,19 @@ public class ShardCreatorGuiDescription extends LightweightGuiDescription {
.setTextChangedListener(it -> shard.setHint(it))
.setMaxLength(70);

public WProtectableField listOrderField = new WProtectableField(ORDER_TEXT)
.setTextChangedListener(it -> {
if (it.getString().isEmpty()) {
shard.setListOrder(null);
} else {
try {
shard.setListOrder(Integer.parseInt(it.getString()));
} catch (NumberFormatException ignored) {
shard.setListOrder(null);
}
}
}).setMaxLength(8);

public WAlternativeToggle iconToggle = new WAlternativeToggle(ICON_TEXTURE_TEXT, ICON_ITEM_TEXT);
public WCardPanel cardPanel = new WCardPanel();
public WLayoutBox textureIconPanel = new WLayoutBox(Axis.VERTICAL);
Expand Down Expand Up @@ -244,6 +258,7 @@ public ShardCreatorGuiDescription(Identifier shardId, Shard shard, String modId)
this.nameField.setText(shard.name().getLiteralString());
this.loreField.setText(shard.lore().getLiteralString());
this.hintField.setText(shard.hint().getLiteralString());
this.listOrderField.setText(shard.listOrder().map(i -> Integer.toString(i)).orElse(""));
shard.icon().ifRight(a -> {
this.iconToggle.setLeft();
if (Objects.equals(a, modIcon)) {
Expand Down Expand Up @@ -279,7 +294,7 @@ public ShardCreatorGuiDescription(Identifier shardId) {
editorPanel.setSpacing(3);
editorPanel.setHorizontalAlignment(HorizontalAlignment.LEFT);

editorPanel.add(titleLabel);
// editorPanel.add(titleLabel);
editorPanel.add(nameField);
editorPanel.add(loreField);
editorPanel.add(hintField);
Expand All @@ -300,6 +315,8 @@ public ShardCreatorGuiDescription(Identifier shardId) {
itemIconPanel.add(itemField);
itemIconPanel.add(componentField);

editorPanel.add(listOrderField);

editorPanel.add(saveButton);

iconToggle.onLeft(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,19 @@ public void setShardSet(Identifier setId, ShardLibrary library, ShardCollection
List<Identifier> shardSet = new ArrayList<>(library.shardSets().get(setId));
shardSet.sort((a, b) -> {
int aPriority = library.shards().get(a)
.map(Shard::shardTypeId)
.flatMap(library.shardTypes()::get)
.map(ShardType::listOrder)
.map(Shard::listOrder)
.orElseGet(() -> library.shards().get(a)
.map(Shard::shardTypeId)
.flatMap(library.shardTypes()::get)
.map(ShardType::listOrder))
.orElse(Integer.MAX_VALUE);

int bPriority = library.shards().get(b)
.map(Shard::shardTypeId)
.flatMap(library.shardTypes()::get)
.map(ShardType::listOrder)
.map(Shard::listOrder)
.orElseGet(() -> library.shards().get(b)
.map(Shard::shardTypeId)
.flatMap(library.shardTypes()::get)
.map(ShardType::listOrder))
.orElse(Integer.MAX_VALUE);

return Integer.compare(aPriority, bPriority);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/scattered_shards/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"gui.scattered_shards.creator.field.name": "Name...",
"gui.scattered_shards.creator.field.lore": "Lore...",
"gui.scattered_shards.creator.field.hint": "Hint...",
"gui.scattered_shards.creator.field.order": "Order (Ascending, Empty for Default)",
"gui.scattered_shards.creator.icon.texture": "Texture Icon",
"gui.scattered_shards.creator.icon.item": "Item Icon",
"gui.scattered_shards.creator.field.item.id": "Item ID...",
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/scattered_shards/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@
"gui.scattered_shards.creator.field.item.component": "物品组件...",
"gui.scattered_shards.tablet.tooltip.global_collection": "%s 的人有这张卡",
"gui.scattered_shards.tablet.label.progress.sets": "已参观 %d/%d 个模组",
"gui.scattered_shards.tablet.label.progress.cards": "%d/%d 张卡片"
"gui.scattered_shards.tablet.label.progress.cards": "%d/%d 张卡片",
"gui.scattered_shards.creator.field.order": "序号 (递增排序, 留空取卡面默认)"
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/scattered_shards/lang/zh_tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@
"gui.scattered_shards.creator.field.item.component": "物品組件...",
"gui.scattered_shards.tablet.tooltip.global_collection": "%s 的人有這張卡",
"gui.scattered_shards.tablet.label.progress.sets": "已參觀 %d/%d 個模組",
"gui.scattered_shards.tablet.label.progress.cards": "%d/%d 張卡片"
"gui.scattered_shards.tablet.label.progress.cards": "%d/%d 張卡片",
"gui.scattered_shards.creator.field.order": "序號 (遞增排序, 留空取卡面缺省)"
}

0 comments on commit 0d54212

Please sign in to comment.