Skip to content

Commit 8e80c32

Browse files
author
Kaleidox
committed
use subcomponent style structure
1 parent 0e8d6d0 commit 8e80c32

6 files changed

Lines changed: 30 additions & 15 deletions

File tree

src/main/java/org/comroid/interaction/InteractionCore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ public void register(Object target) {
5757
}
5858

5959
private void verifyComponentExists(Class<?> type) {
60-
child(type).assertion("No component of type %s was found".formatted(type.getCanonicalName()));
60+
component(type).assertion("No component of type %s was found".formatted(type.getCanonicalName()));
6161
}
6262

6363
public void handle(InteractionContext context, Throwable error) {
6464
log.log(Level.SEVERE, "Encountered an error during interaction handling", error);
6565

66-
var handlers = (context == null ? children(ErrorHandler.class) : context.children(ErrorHandler.class)).iterator();
66+
var handlers = (context == null ? components(ErrorHandler.class) : context.components(ErrorHandler.class)).iterator();
6767
if (!handlers.hasNext()) return;
6868

6969
do {

src/main/java/org/comroid/interaction/adapter/jda/DiscordCommandRegistrator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void initialize() {
4848
for (var base : tree.getNodes()) {
4949
CommandData data;
5050
var ctx = base.getDefinitionValues(KEY_CONTEXT).findAny().orElse(CONTEXT_COMMAND);
51-
var cap = adp.child(NameCapitalizer.class).assertion();
51+
var cap = adp.component(NameCapitalizer.class).assertion();
5252

5353
if (base instanceof GroupNode group) {
5454
if (!ctx.equalsIgnoreCase(CONTEXT_COMMAND)) {

src/main/java/org/comroid/interaction/adapter/jda/DiscordResponseChain.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,20 @@ public void deferResponse(InteractionContext context) {
3131
var privacy = privacy(context);
3232
if (privacy == Interaction.PrivacyLevel.PRIVATE) return;
3333

34-
context.child(IReplyCallback.class).assertion().deferReply().setEphemeral(privacy == Interaction.PrivacyLevel.EPHEMERAL).map(context::addChild).queue();
34+
context.component(IReplyCallback.class)
35+
.assertion()
36+
.deferReply()
37+
.setEphemeral(privacy == Interaction.PrivacyLevel.EPHEMERAL)
38+
.map(context::addChild)
39+
.queue();
3540
}
3641

3742
@Override
3843
public void sendResponse(InteractionContext context, MessageCreateData response) {
3944
var privacy = privacy(context);
4045

4146
if (privacy == Interaction.PrivacyLevel.PRIVATE) {
42-
var result = context.child(User.class);
47+
var result = context.component(User.class);
4348
if (result.isNull()) {
4449
log.warning("Dropping response because there is no message target compatible to the set privacy level %s: %s".formatted(privacy, response));
4550
return;
@@ -51,13 +56,13 @@ public void sendResponse(InteractionContext context, MessageCreateData response)
5156
}
5257

5358
if (async(context)) {
54-
var deferredReply = context.child(InteractionHook.class);
59+
var deferredReply = context.component(InteractionHook.class);
5560
if (deferredReply.isNonNull()) {
5661
deferredReply.assertion().editOriginal(JdaUtil.convertToEditData(response)).queue();
5762
return;
5863
}
5964
} else {
60-
var directReply = context.child(IReplyCallback.class);
65+
var directReply = context.component(IReplyCallback.class);
6166
if (directReply.isNonNull()) {
6267
directReply.assertion().reply(response).setEphemeral(privacy == Interaction.PrivacyLevel.EPHEMERAL).queue();
6368
return;
@@ -97,10 +102,10 @@ public MessageCreateData convertResponse(Response response) {
97102
}
98103

99104
static Interaction.PrivacyLevel privacy(InteractionContext context) {
100-
return context.child(Interaction.class).map(Interaction::privacy).orElse(Interaction.PrivacyLevel.EPHEMERAL);
105+
return context.component(Interaction.class).map(Interaction::privacy).orElse(Interaction.PrivacyLevel.EPHEMERAL);
101106
}
102107

103108
static boolean async(InteractionContext context) {
104-
return context.child(Interaction.class).filter(Interaction::async).isNonNull();
109+
return context.component(Interaction.class).filter(Interaction::async).isNonNull();
105110
}
106111
}

src/main/java/org/comroid/interaction/adapter/jda/JdaAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public JdaAdapter(@NonNull InteractionCore parent, JDA jda) {
5555
addChildren(DiscordNameCapitalizer.INSTANCE, new DiscordCommandRegistrator(this), DiscordResponseChain.INSTANCE);
5656
}
5757

58+
@Override
59+
public boolean isSubComponent() {
60+
return true;
61+
}
62+
5863
@Override
5964
public Stream<Object> streamOwnChildren() {
6065
return Stream.of(jda);

src/main/java/org/comroid/interaction/model/InteractionContext.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public static InteractionContext.Builder basic(InteractionCore core, String... f
6969
@Singular Map<ParameterNode, Object> parameters;
7070
MultiValueMap<String, Object> definitions;
7171

72+
@Override
73+
public boolean isSubComponent() {
74+
return true;
75+
}
76+
7277
public Object getValue(String key) {
7378
return values.getOrDefault(key, null);
7479
}
@@ -86,17 +91,19 @@ public void invoke() {
8691
return null;
8792
});
8893
} else try {
89-
handleResponse(node.invoke(this));
94+
var response = node.invoke(this);
95+
96+
handleResponse(response);
9097
} catch (Throwable t) {
9198
core.handle(this, t);
9299
}
93100
}
94101

95102
@SuppressWarnings("unchecked")
96103
private void handleResponse(final Object response) {
97-
children(ResponseConverter.class).map(converter -> converter.convertResponse(response)).filter(Objects::nonNull).forEach(formatted -> {
104+
components(ResponseConverter.class).map(converter -> converter.convertResponse(response)).filter(Objects::nonNull).forEach(formatted -> {
98105
var fType = formatted.getClass();
99-
children(ResponseHandler.class).filter(handler -> handler.getResponseType().isAssignableFrom(fType))
106+
components(ResponseHandler.class).filter(handler -> handler.getResponseType().isAssignableFrom(fType))
100107
.forEach(handler -> handler.sendResponse(this, formatted));
101108
});
102109
}

src/main/java/org/comroid/interaction/node/MethodNode.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import lombok.Getter;
55
import lombok.SneakyThrows;
66
import lombok.experimental.FieldDefaults;
7-
import org.comroid.api.Polyfill;
87
import org.comroid.api.java.Activator;
98
import org.comroid.interaction.annotation.Interaction;
109
import org.comroid.interaction.model.InteractionContext;
@@ -54,10 +53,9 @@ public Object invoke(InteractionContext context) {
5453
Object value = context.getParameter(param);
5554

5655
if (parser != null) value = Activator.get(parser).createInstance().parse(String.valueOf(value));
57-
else if (pType.isEnum()) value = Enum.valueOf(Polyfill.uncheckedCast(pType), String.valueOf(value));
5856

5957
args[i] = value;
60-
} else args[i] = context.child(pType).get();
58+
} else args[i] = context.component(pType).get();
6159
}
6260

6361
var target = source instanceof InstanceRegistry instance ? instance.getInstance() : null;

0 commit comments

Comments
 (0)