Skip to content

Commit f8d8e99

Browse files
authored
(Draft) API and some GUI (#3)
* shard panel beginning * shard type loading and registry * more shard panel things * merge elegantly * remove old comments * replace enchanting phrases accessor with our own field
1 parent 2615b9d commit f8d8e99

File tree

19 files changed

+334
-112
lines changed

19 files changed

+334
-112
lines changed

gradlew

100755100644
File mode changed.

gradlew.bat

100644100755
File mode changed.

src/main/java/net/modfest/scatteredshards/ScatteredShards.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package net.modfest.scatteredshards;
22

33
import net.minecraft.util.Identifier;
4+
import net.modfest.scatteredshards.core.api.shard.ShardType;
45
import net.modfest.scatteredshards.load.ShardSetLoader;
6+
import net.modfest.scatteredshards.load.ShardTypeLoader;
57
import net.modfest.scatteredshards.networking.ScatteredShardsNetworking;
68
import net.modfest.scatteredshards.core.ScatteredShardsContent;
79
import org.quiltmc.loader.api.ModContainer;
@@ -21,6 +23,8 @@ public static Identifier id(String path) {
2123

2224
@Override
2325
public void onInitialize(ModContainer mod) {
26+
ShardType.register();
27+
ShardTypeLoader.register();
2428
ShardSetLoader.register();
2529
ScatteredShardsNetworking.register();
2630
ScatteredShardsContent.register();

src/main/java/net/modfest/scatteredshards/api/ScatteredShardsAPI.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.modfest.scatteredshards.ScatteredShards;
99
import net.modfest.scatteredshards.api.impl.ScatteredShardsAPIImpl;
1010
import net.modfest.scatteredshards.core.api.shard.Shard;
11+
import net.modfest.scatteredshards.core.api.shard.ShardType;
1112
import net.modfest.scatteredshards.networking.ScatteredShardsNetworking;
1213

1314
public class ScatteredShardsAPI {
@@ -19,14 +20,22 @@ public static Multimap<Identifier, Shard> getShardSets() {
1920
public static BiMap<Identifier, Shard> getShardData() {
2021
return ScatteredShardsAPIImpl.shardData;
2122
}
22-
23+
24+
public static BiMap<Identifier, ShardType> getShardTypes() {
25+
return ScatteredShardsAPIImpl.shardTypes;
26+
}
27+
28+
public static void registerShardType(Identifier id, ShardType shardType) {
29+
ScatteredShardsAPIImpl.REGISTERED_SHARD_TYPES.put(id, shardType);
30+
}
31+
2332
public static void triggerShardCollection(ServerPlayerEntity player, Identifier shardId) {
2433
Shard shard = getShardData().get(shardId);
2534
if (shard == null) {
2635
ScatteredShards.LOGGER.warn("Tried to trigger a shard collection for shard '" + shardId.toString() + "' which does not exist.", new IllegalArgumentException()); //Gets us a stack trace for the culprit
2736
return;
2837
}
29-
38+
3039
//TODO: Log this in the player's shard inventory serverside
3140
ShardEvents.COLLECT.invoker().handle(player, shardId, shard);
3241
ScatteredShardsNetworking.s2cCollectShard(player, shardId); // This will update the clientside shard binder as well

src/main/java/net/modfest/scatteredshards/api/impl/ScatteredShardsAPIImpl.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package net.modfest.scatteredshards.api.impl;
22

3-
import net.minecraft.util.Identifier;
4-
import net.modfest.scatteredshards.core.api.shard.Shard;
5-
import net.modfest.scatteredshards.load.ShardSetLoader;
6-
73
import com.google.common.collect.BiMap;
84
import com.google.common.collect.HashBiMap;
95
import com.google.common.collect.Multimap;
106
import com.google.common.collect.MultimapBuilder;
7+
import net.minecraft.util.Identifier;
8+
import net.modfest.scatteredshards.core.api.shard.Shard;
9+
import net.modfest.scatteredshards.core.api.shard.ShardType;
10+
import net.modfest.scatteredshards.load.ShardSetLoader;
11+
import net.modfest.scatteredshards.load.ShardTypeLoader;
1112

1213
public class ScatteredShardsAPIImpl {
1314

14-
private static final Multimap<Identifier, Shard> REGISTERED_SHARD_SETS = MultimapBuilder.hashKeys().arrayListValues(3).build();
15-
private static final BiMap<Identifier, Shard> REGISTERED_SHARDS = HashBiMap.create();
15+
public static final Multimap<Identifier, Shard> REGISTERED_SHARD_SETS = MultimapBuilder.hashKeys().arrayListValues(3).build();
16+
public static final BiMap<Identifier, Shard> REGISTERED_SHARDS = HashBiMap.create();
1617
public static Multimap<Identifier, Shard> shardSets = null;
1718
public static BiMap<Identifier, Shard> shardData = null;
1819

20+
public static final BiMap<Identifier, ShardType> REGISTERED_SHARD_TYPES = HashBiMap.create();
21+
public static BiMap<Identifier, ShardType> shardTypes;
22+
1923
static {
20-
update();
24+
updateShards();
2125
}
2226

2327
private static Multimap<Identifier, Shard> createShardSets() {
@@ -34,7 +38,18 @@ private static BiMap<Identifier, Shard> createShardData() {
3438
return map;
3539
}
3640

37-
public static void update() {
41+
private static BiMap<Identifier, ShardType> createShardTypes() {
42+
BiMap<Identifier, ShardType> map = HashBiMap.create();
43+
map.putAll(ShardTypeLoader.MAP);
44+
map.putAll(REGISTERED_SHARD_TYPES);
45+
return map;
46+
}
47+
48+
public static void updateShardTypes() {
49+
shardTypes = createShardTypes();
50+
}
51+
52+
public static void updateShards() {
3853
shardSets = createShardSets();
3954
shardData = createShardData();
4055
}

src/main/java/net/modfest/scatteredshards/client/command/ShardCommand.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
import com.mojang.brigadier.Command;
44
import com.mojang.brigadier.context.CommandContext;
55
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6+
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
67
import com.mojang.brigadier.suggestion.Suggestions;
78
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
9+
import com.mojang.datafixers.util.Either;
810
import net.minecraft.command.argument.IdentifierArgumentType;
11+
import net.minecraft.item.Items;
12+
import net.minecraft.text.Text;
913
import net.minecraft.util.Identifier;
1014
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
1115
import net.modfest.scatteredshards.client.screen.ShardCreatorGuiDescription;
16+
import net.modfest.scatteredshards.core.api.shard.Shard;
17+
import net.modfest.scatteredshards.core.api.shard.ShardType;
1218
import org.quiltmc.qsl.command.api.client.ClientCommandRegistrationCallback;
1319
import org.quiltmc.qsl.command.api.client.QuiltClientCommandSource;
1420

@@ -19,10 +25,30 @@
1925

2026
public class ShardCommand {
2127

28+
public static final DynamicCommandExceptionType INVALID_ID = new DynamicCommandExceptionType(
29+
id -> Text.translatable("error.scattered_shards.invalid_set_id", id)
30+
);
31+
2232
public static int view(CommandContext<QuiltClientCommandSource> context) throws CommandSyntaxException {
2333
Identifier id = context.getArgument("set_id", Identifier.class);
34+
var shards = ScatteredShardsAPI.getShardSets().get(id);
35+
if (shards.isEmpty()) {
36+
throw INVALID_ID.create(id);
37+
}
38+
return Command.SINGLE_SUCCESS;
39+
}
40+
41+
public static int creator(CommandContext<QuiltClientCommandSource> context) throws CommandSyntaxException {
2442
var client = context.getSource().getClient();
25-
client.send(() -> client.setScreen(new ShardCreatorGuiDescription.Screen()));
43+
var shard = new Shard(
44+
ShardType.SECRET,
45+
Text.literal("My Custom Card"),
46+
Text.literal("What is this text for?"),
47+
Text.literal("What is this secret?"),
48+
//Either.right(ScatteredShards.id("icon.png"))
49+
Either.left(Items.DIAMOND_SWORD.getDefaultStack())
50+
);
51+
client.send(() -> client.setScreen(new ShardCreatorGuiDescription.Screen(shard)));
2652
return Command.SINGLE_SUCCESS;
2753
}
2854

@@ -39,6 +65,8 @@ public static void register() {
3965
.then(literal("view")
4066
.then(argument("set_id", IdentifierArgumentType.identifier())
4167
.suggests(ShardCommand::suggestShardSets)
42-
.executes(ShardCommand::view)))));
68+
.executes(ShardCommand::view)))
69+
.then(literal("creator")
70+
.executes(ShardCommand::creator))));
4371
}
4472
}

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

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,30 @@
22

33
import io.github.cottonmc.cotton.gui.client.CottonClientScreen;
44
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
5-
import io.github.cottonmc.cotton.gui.widget.WButton;
65
import io.github.cottonmc.cotton.gui.widget.WGridPanel;
7-
import io.github.cottonmc.cotton.gui.widget.WLabel;
8-
import io.github.cottonmc.cotton.gui.widget.WSprite;
96
import io.github.cottonmc.cotton.gui.widget.data.Insets;
10-
import net.minecraft.text.Text;
11-
import net.minecraft.util.Identifier;
7+
import net.modfest.scatteredshards.core.api.shard.Shard;
128

139
public class ShardCreatorGuiDescription extends LightweightGuiDescription {
1410

1511
public static class Screen extends CottonClientScreen {
1612

17-
public Screen() {
18-
super(new ShardCreatorGuiDescription());
19-
}
20-
21-
@Override
22-
public void tick() {
23-
super.tick();
24-
//System.out.println("bruh!");
13+
public Screen(Shard shard) {
14+
super(new ShardCreatorGuiDescription(shard));
2515
}
2616

27-
@Override
28-
public void removed() {
29-
super.removed();
30-
//throw new IllegalStateException("lol");
31-
System.out.println("removed");
17+
public Screen() {
18+
this(Shard.MISSING_SHARD);
3219
}
3320
}
3421

35-
public ShardCreatorGuiDescription() {
36-
WGridPanel root = new WGridPanel();
22+
public ShardCreatorGuiDescription(Shard shard) {
23+
WGridPanel root = new WGridPanel(1);
3724
setRootPanel(root);
3825
root.setSize(256, 240);
3926
root.setInsets(Insets.ROOT_PANEL);
4027

41-
WSprite icon = new WSprite(new Identifier("minecraft:textures/item/redstone.png"));
42-
root.add(icon, 0, 2, 1, 1);
43-
44-
WButton button = new WButton(Text.translatable("gui.examplemod.examplebutton"));
45-
root.add(button, 0, 3, 4, 1);
46-
47-
WLabel label = new WLabel(Text.literal("Test"), 0xFFFFFF);
48-
root.add(label, 0, 4, 2, 1);
49-
28+
root.add(new WShardPanel(shard, true), 128, 12);
5029
root.validate(this);
51-
System.out.println("joe");
5230
}
5331
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package net.modfest.scatteredshards.client.screen;
2+
3+
import com.mojang.datafixers.util.Either;
4+
import io.github.cottonmc.cotton.gui.widget.*;
5+
import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment;
6+
import net.minecraft.client.gui.GuiGraphics;
7+
import net.minecraft.item.ItemStack;
8+
import net.minecraft.text.Style;
9+
import net.minecraft.text.Text;
10+
import net.minecraft.util.Identifier;
11+
import net.modfest.scatteredshards.core.api.shard.Shard;
12+
13+
import java.util.function.Supplier;
14+
15+
public class WShardPanel extends WPlainPanel {
16+
17+
public static final Style HINT_STYLE = Style.EMPTY.withFont(new Identifier("minecraft:alt"));
18+
19+
private static class WStackIcon extends WItem {
20+
21+
public WStackIcon(ItemStack stack) {
22+
super(stack);
23+
}
24+
25+
@Override
26+
public void paint(GuiGraphics context, int x, int y, int mouseX, int mouseY) {
27+
context.getMatrices().push();
28+
context.getMatrices().translate(-x, -y, 0.0f);
29+
context.getMatrices().scale(2.0f, 2.0f, 1.0f);
30+
super.paint(context, x, y, mouseX, mouseY);
31+
context.getMatrices().pop();
32+
}
33+
}
34+
35+
private static class WDynamicTextLabel extends WLabel {
36+
37+
private final Supplier<Text> dynamicText;
38+
39+
public WDynamicTextLabel(Supplier<Text> text, int color) {
40+
super(null, color);
41+
this.dynamicText = text;
42+
}
43+
44+
@Override
45+
public void paint(GuiGraphics context, int x, int y, int mouseX, int mouseY) {
46+
text = dynamicText.get();
47+
super.paint(context, x, y, mouseX, mouseY);
48+
}
49+
}
50+
51+
private final boolean dynamic;
52+
53+
private static WWidget createIcon(Either<ItemStack, Identifier> icon) {
54+
if (icon.left().isPresent()) {
55+
return new WStackIcon(icon.left().get());
56+
} else if (icon.right().isPresent()) {
57+
return new WSprite(icon.right().get());
58+
} else {
59+
throw new UnsupportedOperationException();
60+
}
61+
}
62+
63+
private WLabel createLabel(Supplier<Text> supplier, int color) {
64+
var label = dynamic
65+
? new WDynamicTextLabel(supplier, color)
66+
: new WLabel(supplier.get(), color);
67+
return label.setHorizontalAlignment(HorizontalAlignment.CENTER);
68+
}
69+
70+
private WLabel createHintLabel(Supplier<Text> hint, int color) {
71+
return createLabel(() -> {
72+
return hint.get().copy().fillStyle(HINT_STYLE);
73+
}, color);
74+
}
75+
76+
public WShardPanel(Shard shard, boolean dynamic) {
77+
this.dynamic = dynamic;
78+
79+
WSprite backing = new WSprite(shard.shardType().getBackingTexture());
80+
add(backing, 0, 20, 48, 64);
81+
82+
WWidget icon = createIcon(shard.icon());
83+
add(icon, 8, 28, 16, 16);
84+
85+
WLabel name = createLabel(shard::name, 0xFFFFFF);
86+
name.setSize(20, -1);
87+
add(name, 14, 0);
88+
89+
WLabel typeDescription = createLabel(() -> shard.shardType().getDescription(), shard.shardType().textColor());
90+
add(typeDescription, 14, 100);
91+
add(createHintLabel(shard::hint, 0xFFFFFF), 14, 120);
92+
}
93+
}

0 commit comments

Comments
 (0)