Skip to content

Commit

Permalink
feat: Player Chat Action
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyaPeyaPeyang committed Sep 1, 2023
1 parent 8c49ce1 commit 588047a
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static List<? extends AbstractPlayerAction<?>> getActions()
actions.add(new PlayerGameModeAction());
actions.add(new PlayerAdvancementAction());
actions.add(new PlayerAnimationAction());
actions.add(new PlayerChatAction());
actions.add(new PlayerDeathAction());
actions.add(new PlayerDropItemAction());
actions.add(new PlayerFlightAction());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.jetbrains.annotations.NotNull;
import org.kunlab.scenamatica.action.actions.AbstractActionArgument;
import org.kunlab.scenamatica.action.utils.PlayerUtils;
import org.kunlab.scenamatica.enums.ScenarioType;
import org.kunlab.scenamatica.interfaces.action.ActionArgument;
import org.kunlab.scenamatica.interfaces.scenario.ScenarioEngine;
import org.kunlab.scenamatica.interfaces.scenariofile.trigger.TriggerArgument;

import java.util.Objects;
Expand Down Expand Up @@ -46,6 +48,13 @@ public Player getTarget()
return PlayerUtils.getPlayerOrThrow(this.target);
}

@Override
public void validate(@NotNull ScenarioEngine engine, @NotNull ScenarioType type)
{
if (type == ScenarioType.ACTION_EXECUTE)
throwIfNotPresent(KEY_TARGET_PLAYER, this.target);
}

@Override
public String getArgumentString()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package org.kunlab.scenamatica.action.actions.player;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerChatEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kunlab.scenamatica.enums.ScenarioType;
import org.kunlab.scenamatica.interfaces.action.types.Executable;
import org.kunlab.scenamatica.interfaces.action.types.Watchable;
import org.kunlab.scenamatica.interfaces.scenario.ScenarioEngine;
import org.kunlab.scenamatica.interfaces.scenariofile.BeanSerializer;
import org.kunlab.scenamatica.interfaces.scenariofile.trigger.TriggerArgument;

import java.util.Collections;
import java.util.List;
import java.util.Map;

public class PlayerChatAction extends AbstractPlayerAction<PlayerChatAction.Argument>
implements Executable<PlayerChatAction.Argument>, Watchable<PlayerChatAction.Argument>
{
public static final String KEY_ACTION_NAME = "player_chat";

@Override
public String getName()
{
return KEY_ACTION_NAME;
}

@Override
public void execute(@NotNull ScenarioEngine engine, @Nullable Argument argument)
{
argument = this.requireArgsNonNull(argument);

Player p = argument.getTarget();
p.chat(argument.message);
}

@Override
public boolean isFired(@NotNull Argument argument, @NotNull ScenarioEngine engine, @NotNull Event event)
{
if (!super.checkMatchedPlayerEvent(argument, engine, event))
return false;

assert event instanceof PlayerChatEvent;
PlayerChatEvent playerChatEvent = (PlayerChatEvent) event;

return playerChatEvent.getMessage().matches(argument.message)
&& playerChatEvent.getFormat().matches(argument.format);
}

@Override
public List<Class<? extends Event>> getAttachingEvents()
{
//noinspection deprecation
return Collections.singletonList(
PlayerChatEvent.class
);
}

@Override
public Argument deserializeArgument(@NotNull Map<String, Object> map, @NotNull BeanSerializer serializer)
{
return new Argument(
super.deserializeTarget(map),
(String) map.get(Argument.KEY_MESSAGE),
(String) map.get(Argument.KEY_FORMAT)
);
}

@Value
@EqualsAndHashCode(callSuper = true)
public static class Argument extends AbstractPlayerActionArgument
{
public static final String KEY_MESSAGE = "message";
public static final String KEY_FORMAT = "format";

String message;
String format;

public Argument(@NotNull String target, String message, String format)
{
super(target);
this.message = message;
this.format = format;
}

@Override
public boolean isSame(TriggerArgument argument)
{
if (!(argument instanceof Argument))
return false;

Argument arg = (Argument) argument;

return super.isSame(arg)
&& this.message.equals(arg.message)
&& this.format.equals(arg.format);
}

@Override
public void validate(@NotNull ScenarioEngine engine, @NotNull ScenarioType type)
{
super.validate(engine, type);

if (type == ScenarioType.ACTION_EXECUTE)
{
throwIfNotPresent(KEY_MESSAGE, this.message);
throwIfPresent(KEY_FORMAT, this.format);
}
}

@Override
public String getArgumentString()
{
return appendArgumentString(
super.getArgumentString(),
KEY_MESSAGE, this.message,
KEY_FORMAT, this.format
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ public static boolean isSameContent(net.kyori.adventure.text.Component adventure

return component.content().equals(content);
}

public static boolean isContentMatches(net.kyori.adventure.text.Component component, String content)
{
net.kyori.adventure.text.TextComponent textComponent;
if (component instanceof net.kyori.adventure.text.TextComponent)
textComponent = (net.kyori.adventure.text.TextComponent) component;
else
textComponent = net.kyori.adventure.text.TextComponent.ofChildren(component);

return textComponent.content().matches(content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# noinspection YAMLSchemaValidation
scenamatica: ${project.version}

name: actions_player_chat_2
description: Testing PlayerChatAction detects the message with regex or not
on:
- type: on_load
- type: manual_dispatch

context:
actors:
- name: Actor001

scenario:
- type: execute
action: player_chat
with:
target: Actor001
message: "1"
- type: execute
action: player_chat
with:
target: Actor001
message: "^\\d$"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# noinspection YAMLSchemaValidation
scenamatica: ${project.version}

name: actions_player_chat
description: Testing PlayerChatAction works or not
on:
- type: on_load
- type: manual_dispatch

context:
actors:
- name: Actor001

scenario:
- type: execute
action: player_chat
with:
target: Actor001
message: "Hello World!"
- type: execute
action: player_chat
with:
target: Actor001
message: "Hello World!"
35 changes: 35 additions & 0 deletions scenamatica-file.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"inventory_open",
"player_advancement",
"player_animation",
"player_chat",
"player_death",
"player_drop_item",
"player_flight",
Expand Down Expand Up @@ -414,6 +415,26 @@
]
}
},
{
"if": {
"properties": {
"action": {
"const": "player_chat",
"description": "プレイヤーのチャットを検知します。"
}
}
},
"then": {
"properties": {
"with": {
"$ref": "#/definitions/action/definitions/player/chat"
}
},
"required": [
"with"
]
}
},
{
"if": {
"properties": {
Expand Down Expand Up @@ -1477,6 +1498,20 @@
"type"
]
},
"chat": {
"$ref": "#/definitions/action/definitions/player/$base",
"properties": {
"message": {
"type": "string",
"description": "送信するメッセージまたは判定用の正規表現です。"
},
"format": {
"type": "string",
"description": "メッセージのフォーマットです。",
"example": "<%1$s> %2$s"
}
}
},
"death": {
"$ref": "#/definitions/action/definitions/player/$base",
"properties": {
Expand Down

0 comments on commit 588047a

Please sign in to comment.