Skip to content

Commit

Permalink
feat: Make Message allow no argument
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyaPeyaPeyang committed Sep 5, 2023
1 parent 3a40d23 commit 9cac815
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.kunlab.scenamatica.action.actions.AbstractActionArgument;
import org.kunlab.scenamatica.action.utils.PlayerUtils;
import org.kunlab.scenamatica.action.utils.TextUtils;
import org.kunlab.scenamatica.commons.utils.MapUtils;
import org.kunlab.scenamatica.enums.ScenarioType;
import org.kunlab.scenamatica.events.actor.ActorMessageReceiveEvent;
import org.kunlab.scenamatica.interfaces.action.types.Executable;
import org.kunlab.scenamatica.interfaces.action.types.Watchable;
Expand Down Expand Up @@ -51,15 +51,17 @@ public void execute(@NotNull ScenarioEngine engine, @Nullable MessageAction.Argu
@Override
public boolean isFired(@NotNull MessageAction.Argument argument, @NotNull ScenarioEngine engine, @NotNull Event event)
{
Player recipient = PlayerUtils.getPlayerOrThrow(argument.getRecipient());
String content = argument.getMessage();
Player recipient = null;
if (argument.getRecipient() != null)
recipient = PlayerUtils.getPlayerOrThrow(argument.getRecipient());

assert event instanceof ActorMessageReceiveEvent;
ActorMessageReceiveEvent e = (ActorMessageReceiveEvent) event;

TextComponent message = e.getMessage();
return TextUtils.isSameContent(message, content)
&& e.getPlayer().getUniqueId().equals(recipient.getUniqueId());
return (content == null || TextUtils.isSameContent(message, content))
&& (recipient == null || e.getPlayer().getUniqueId().equals(recipient.getUniqueId()));
}

@Override
Expand All @@ -73,9 +75,6 @@ public List<Class<? extends Event>> getAttachingEvents()
@Override
public Argument deserializeArgument(@NotNull Map<String, Object> map, @NotNull BeanSerializer serializer)
{
MapUtils.checkType(map, Argument.KEY_MESSAGE, String.class);
MapUtils.checkType(map, Argument.KEY_RECIPIENT, String.class);

return new Argument(
(String) map.get(Argument.KEY_MESSAGE),
(String) map.get(Argument.KEY_RECIPIENT)
Expand All @@ -89,11 +88,15 @@ public static class Argument extends AbstractActionArgument
public static final String KEY_MESSAGE = "message";
public static final String KEY_RECIPIENT = "recipient";

@NotNull
String message;
@NotNull
String recipient;

public Argument(String message, String recipient)
{
this.message = message;
this.recipient = recipient;
}

@Override
public boolean isSame(TriggerArgument argument)
{
Expand All @@ -104,7 +107,15 @@ public boolean isSame(TriggerArgument argument)
return Objects.equals(this.message, a.message) && Objects.equals(this.recipient, a.recipient);
}

// TODO: Create validation for argument
@Override
public void validate(@NotNull ScenarioEngine engine, @NotNull ScenarioType type)
{
if (type == ScenarioType.ACTION_EXECUTE)
{
throwIfNotPresent(KEY_MESSAGE, this.message);
throwIfNotPresent(KEY_RECIPIENT, this.recipient);
}
}

@Override
public String getArgumentString()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# noinspection YAMLSchemaValidation
scenamatica: ${project.version}

name: actions_scenamatica_message_2
description: Testing message action without message works or not
on:
- type: on_load
- type: manual_dispatch

context:
actors:
- name: Actor001

scenario:
- type: execute
action: message
with:
message: "Hello, World!"
recipient: Actor001
- type: expect
action: message
with:
recipient: Actor001
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# noinspection YAMLSchemaValidation
scenamatica: ${project.version}

name: actions_scenamatica_message_3
description: Testing message action without recipient works or not
on:
- type: on_load
- type: manual_dispatch

context:
actors:
- name: Actor001

scenario:
- type: execute
action: message
with:
message: "Hello, World!"
recipient: Actor001
- type: expect
action: message
with:
message: "Hello, World!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# noinspection YAMLSchemaValidation
scenamatica: ${project.version}

name: actions_scenamatica_message_4
description: Testing message action without argument works or not
on:
- type: on_load
- type: manual_dispatch

context:
actors:
- name: Actor001

scenario:
- type: execute
action: message
with:
message: "Hello, World!"
recipient: Actor001
- type: expect
action: message
11 changes: 2 additions & 9 deletions scenamatica-file.json
Original file line number Diff line number Diff line change
Expand Up @@ -674,10 +674,7 @@
"with": {
"$ref": "#/definitions/action/definitions/scenamatica/message"
}
},
"required": [
"with"
]
}
}
},
{
Expand Down Expand Up @@ -1783,11 +1780,7 @@
"type": "string",
"description": "メッセージを受け取るプレイヤーの名前または UUID です。"
}
},
"required": [
"message",
"recipient"
]
}
},
"milestone": {
"properties": {
Expand Down

0 comments on commit 9cac815

Please sign in to comment.