-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c49ce1
commit 588047a
Showing
7 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...onEngine/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerChatAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
ScenamaticaPlugin/src/main/resources/scenarios/actions/player/Chat-2.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$" |
24 changes: 24 additions & 0 deletions
24
ScenamaticaPlugin/src/main/resources/scenarios/actions/player/Chat.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters