Skip to content

Commit

Permalink
feat(actions): Player Level change action
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyaPeyaPeyang committed Oct 14, 2023
1 parent 978df2f commit a068aa2
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static List<? extends AbstractPlayerAction<?>> getActions()
actions.add(new PlayerJoinAction());
actions.add(new PlayerKickAction());
actions.add(new PlayerLaunchProjectileAction());
actions.add(new PlayerLevelChangeAction());
actions.add(new PlayerMoveAction<>());
actions.add(new PlayerQuitAction());
actions.add(new PlayerRespawnAction());
Expand Down
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
);
}
}
}
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
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
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
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
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

0 comments on commit a068aa2

Please sign in to comment.