Skip to content

Commit

Permalink
Refactor PLAYER_STATE flag and add more options
Browse files Browse the repository at this point in the history
  • Loading branch information
imDaniX committed Feb 11, 2024
1 parent 95e267d commit 5db69f8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,70 +27,78 @@
import fun.reactions.model.environment.Environment;
import fun.reactions.util.naming.Aliased;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;

import java.util.Locale;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Aliased.Names("STATE")
public class PlayerStateFlag implements Flag, Activity.Personal {
private static final Vector ZERO = new Vector();

@Override
public boolean proceed(@NotNull Environment env, @NotNull Player player, @NotNull String paramsStr) {
Posture pt = Posture.getByName(paramsStr);
if (pt == null) return false;
switch (pt) {
case SNEAK:
return player.isSneaking();
case FLY:
return player.isFlying();
case SPRINT:
return player.isSprinting();
case VEHICLE:
return player.isInsideVehicle();
case SLEEP:
return player.isSleeping();
case STAND:
if (player.isSleeping()) return false;
if (player.isSneaking()) return false;
if (player.isSprinting()) return false;
if (player.isFlying()) return false;
if (player.isGliding()) return false;
return !player.isInsideVehicle();
case OP:
return player.isOp();
case VEHICLE_TYPED:
if (!player.isInsideVehicle()) return false;
return player.getVehicle().getType().name().equalsIgnoreCase(paramsStr.substring(8));
case SPECTATOR_TARGET:
return player.getSpectatorTarget() != null;
case GLIDE:
return player.isGliding();
}
return false;
State state = State.getByName(paramsStr);
return state != null && state.check.test(player, paramsStr);
}

@Override
public @NotNull String getName() {
return "PLAYER_STATE";
}

private enum Posture {
SNEAK,
SPRINT,
STAND,
VEHICLE,
VEHICLE_TYPED,
SLEEP,
FLY,
OP,
SPECTATOR_TARGET,
GLIDE;
private enum State {
SNEAK(Player::isSneaking, true),
SPRINT(Player::isSprinting, true),
VEHICLE(Player::isInsideVehicle, true),
VEHICLE_TYPED((player, paramsStr) -> {
if (!player.isInsideVehicle()) return false;
return player.getVehicle().getType().name().equalsIgnoreCase(paramsStr.substring(8));
}, true),
SLEEP(Player::isSleeping, true),
FLY(Player::isFlying, true),
OP(Player::isOp, false),
SPECTATOR_TARGET(player -> player.getSpectatorTarget() != null, true),
GLIDE(Player::isGliding, true),
GLOW(Player::isGlowing, false),
DEAD(Player::isDead, true),
ALIVE(player -> !player.isDead(), true),
SWIM(Player::isSwimming, true),
CLIMB(Player::isClimbing, true),
STAND((player, paramsStr) -> {
if (!player.getVelocity().equals(ZERO)) return false;
for (State state : State.values()) {
if (state.changesPosture && state.check.test(player, paramsStr)) return false;
}
return true;
}, false),
SIMPLE((player, paramsStr) -> {
for (State state : State.values()) {
if (state.changesPosture && state.check.test(player, paramsStr)) return false;
}
return true;
}, false);

private final BiPredicate<Player, String> check;
private final boolean changesPosture;

private static final Map<String, Posture> BY_NAME = Stream.of(values()).collect(Collectors.toMap(Enum::name, d -> d));
private static final Map<String, State> BY_NAME = Stream.of(values()).collect(Collectors.toMap(Enum::name, d -> d));

State(Predicate<Player> check, boolean changesPosture) {
this((player, paramsStr) -> check.test(player), changesPosture);
}

State(BiPredicate<Player, String> check, boolean changesPosture) {
this.check = check;
this.changesPosture = changesPosture;
}

public static Posture getByName(@NotNull String name) {
public static State getByName(@NotNull String name) {
name = name.toUpperCase(Locale.ROOT);
if (name.startsWith("VEHICLE_")) {
return VEHICLE_TYPED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

@Deprecated
public class PvpFlag implements Flag, Activity.Personal { // TODO: Requires rework
@Override
public boolean proceed(@NotNull Environment env, @NotNull Player player, @NotNull String paramsStr) {
Expand Down
2 changes: 1 addition & 1 deletion reactions/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: '${project.parent.description}'
api-version: '1.19'
main: fun.reactions.ReActionsPlugin
version: '${project.version}${build-suffix}'
website: https://github.com/imDaniX/ReActions
website: https://github.com/GlowingInk/ReActions
authors:
- fromgate
- MaxDikiy
Expand Down

0 comments on commit 5db69f8

Please sign in to comment.