-
-
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.
feat(actions): Player Level change action
- Loading branch information
1 parent
978df2f
commit a068aa2
Showing
7 changed files
with
256 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
132 changes: 132 additions & 0 deletions
132
...e/src/main/java/org/kunlab/scenamatica/action/actions/player/PlayerLevelChangeAction.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,132 @@ | ||
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.PlayerLevelChangeEvent; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.kunlab.scenamatica.commons.utils.MapUtils; | ||
import org.kunlab.scenamatica.enums.ScenarioType; | ||
import org.kunlab.scenamatica.interfaces.action.types.Executable; | ||
import org.kunlab.scenamatica.interfaces.action.types.Requireable; | ||
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; | ||
import java.util.Objects; | ||
|
||
public class PlayerLevelChangeAction extends AbstractPlayerAction<PlayerLevelChangeAction.Argument> | ||
implements Watchable<PlayerLevelChangeAction.Argument>, Executable<PlayerLevelChangeAction.Argument>, | ||
Requireable<PlayerLevelChangeAction.Argument> | ||
{ | ||
public static final String KEY_ACTION_NAME = "player_level_change"; | ||
|
||
@Override | ||
public String getName() | ||
{ | ||
return KEY_ACTION_NAME; | ||
} | ||
|
||
@Override | ||
public void execute(@NotNull ScenarioEngine engine, @Nullable Argument argument) | ||
{ | ||
argument = this.requireArgsNonNull(argument); | ||
|
||
Player player = argument.getTarget(); | ||
player.setLevel(argument.getNewLevel()); | ||
} | ||
|
||
@Override | ||
public boolean isFired(@NotNull Argument argument, @NotNull ScenarioEngine engine, @NotNull Event event) | ||
{ | ||
if (!super.checkMatchedPlayerEvent(argument, engine, event)) | ||
return false; | ||
|
||
PlayerLevelChangeEvent e = (PlayerLevelChangeEvent) event; | ||
|
||
return (argument.getOldLevel() == null || argument.getOldLevel() == e.getOldLevel()) | ||
&& (argument.getNewLevel() == null || argument.getNewLevel() == e.getNewLevel()); | ||
} | ||
|
||
@Override | ||
public List<Class<? extends Event>> getAttachingEvents() | ||
{ | ||
return Collections.singletonList( | ||
PlayerLevelChangeEvent.class | ||
); | ||
} | ||
|
||
@Override | ||
public Argument deserializeArgument(@NotNull Map<String, Object> map, @NotNull BeanSerializer serializer) | ||
{ | ||
return new Argument( | ||
super.deserializeTarget(map), | ||
MapUtils.getAsNumberOrNull(map, Argument.KEY_OLD_LEVEL, Number::intValue), | ||
MapUtils.getAsNumberOrNull(map, Argument.KEY_NEW_LEVEL, Number::intValue) | ||
); | ||
} | ||
|
||
@Override | ||
public boolean isConditionFulfilled(@Nullable Argument argument, @NotNull ScenarioEngine engine) | ||
{ | ||
argument = this.requireArgsNonNull(argument); | ||
|
||
Player player = argument.getTarget(); | ||
return player.getLevel() == argument.getNewLevel(); | ||
} | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = true) | ||
public static class Argument extends AbstractPlayerActionArgument | ||
{ | ||
public static final String KEY_OLD_LEVEL = "oldLevel"; | ||
public static final String KEY_NEW_LEVEL = "level"; | ||
|
||
Integer oldLevel; | ||
Integer newLevel; | ||
|
||
public Argument(String target, Integer oldLevel, Integer newLevel) | ||
{ | ||
super(target); | ||
this.oldLevel = oldLevel; | ||
this.newLevel = newLevel; | ||
} | ||
|
||
@Override | ||
public void validate(@NotNull ScenarioEngine engine, @NotNull ScenarioType type) | ||
{ | ||
super.validate(engine, type); | ||
if (type == ScenarioType.ACTION_EXECUTE || type == ScenarioType.CONDITION_REQUIRE) | ||
throwIfPresent(KEY_OLD_LEVEL, this.oldLevel); | ||
} | ||
|
||
@Override | ||
public boolean isSame(TriggerArgument argument) | ||
{ | ||
if (!(argument instanceof Argument)) | ||
return false; | ||
|
||
Argument arg = (Argument) argument; | ||
|
||
return super.isSame(arg) | ||
&& Objects.equals(this.oldLevel, arg.oldLevel) | ||
&& Objects.equals(this.newLevel, arg.newLevel); | ||
} | ||
|
||
@Override | ||
public String getArgumentString() | ||
{ | ||
return appendArgumentString( | ||
super.getArgumentString(), | ||
KEY_OLD_LEVEL, this.oldLevel, | ||
KEY_NEW_LEVEL, this.newLevel | ||
); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
ScenamaticaPlugin/src/main/resources/scenarios/actions/player/Level-Change-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,25 @@ | ||
# noinspection YAMLSchemaValidation | ||
scenamatica: ${project.version} | ||
|
||
name: actions_player_level_change_2 | ||
description: Testing PlayerChangeLevelAction without level works or not | ||
on: | ||
- type: on_load | ||
- type: manual_dispatch | ||
|
||
context: | ||
actors: | ||
- name: Actor001 | ||
|
||
scenario: | ||
- type: execute | ||
action: player_level_change | ||
with: | ||
target: Actor001 | ||
level: 10 | ||
- type: expect | ||
action: player_level_change | ||
with: | ||
target: Actor001 | ||
level: 10 | ||
oldLevel: 0 |
24 changes: 24 additions & 0 deletions
24
ScenamaticaPlugin/src/main/resources/scenarios/actions/player/Level-Change-3.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_level_change_3 | ||
description: Testing PlayerChangeLevelAction without old level works or not | ||
on: | ||
- type: on_load | ||
- type: manual_dispatch | ||
|
||
context: | ||
actors: | ||
- name: Actor001 | ||
|
||
scenario: | ||
- type: execute | ||
action: player_level_change | ||
with: | ||
target: Actor001 | ||
level: 10 | ||
- type: expect | ||
action: player_level_change | ||
with: | ||
target: Actor001 | ||
level: 10 |
23 changes: 23 additions & 0 deletions
23
ScenamaticaPlugin/src/main/resources/scenarios/actions/player/Level-Change-4.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,23 @@ | ||
# noinspection YAMLSchemaValidation | ||
scenamatica: ${project.version} | ||
|
||
name: actions_player_level_change_4 | ||
description: Testing PlayerChangeLevelAction without target works or not | ||
on: | ||
- type: on_load | ||
- type: manual_dispatch | ||
|
||
context: | ||
actors: | ||
- name: Actor001 | ||
|
||
scenario: | ||
- type: execute | ||
action: player_level_change | ||
with: | ||
target: Actor001 | ||
level: 10 | ||
- type: expect | ||
action: player_level_change | ||
with: | ||
target: Actor001 |
21 changes: 21 additions & 0 deletions
21
ScenamaticaPlugin/src/main/resources/scenarios/actions/player/Level-Change-5.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,21 @@ | ||
# noinspection YAMLSchemaValidation | ||
scenamatica: ${project.version} | ||
|
||
name: actions_player_level_change_5 | ||
description: Testing PlayerChangeLevelAction without argument works or not | ||
on: | ||
- type: on_load | ||
- type: manual_dispatch | ||
|
||
context: | ||
actors: | ||
- name: Actor001 | ||
|
||
scenario: | ||
- type: execute | ||
action: player_level_change | ||
with: | ||
target: Actor001 | ||
level: 10 | ||
- type: expect | ||
action: player_level_change |
30 changes: 30 additions & 0 deletions
30
ScenamaticaPlugin/src/main/resources/scenarios/actions/player/Level-Change.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,30 @@ | ||
# noinspection YAMLSchemaValidation | ||
scenamatica: ${project.version} | ||
|
||
name: actions_player_level_change | ||
description: Testing PlayerChangeLevelAction works or not | ||
on: | ||
- type: on_load | ||
- type: manual_dispatch | ||
|
||
context: | ||
actors: | ||
- name: Actor001 | ||
|
||
scenario: | ||
- type: execute | ||
action: player_advancement | ||
with: | ||
target: Actor001 | ||
advancement: minecraft:nether/distract_piglin | ||
- type: expect | ||
action: player_advancement | ||
with: | ||
target: Actor001 | ||
advancement: minecraft:nether/distract_piglin | ||
timeout: 20 | ||
- type: require | ||
action: player_advancement | ||
with: | ||
target: Actor001 | ||
advancement: minecraft:nether/distract_piglin |