Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input API #7190

Open
wants to merge 16 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import org.bukkit.util.CachedServerIcon;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.bukkit.InputKey;

import java.io.StreamCorruptedException;
import java.util.ArrayList;
Expand Down Expand Up @@ -1559,6 +1560,14 @@ public String toVariableNameString(EnchantmentOffer eo) {
.name("Experience Cooldown Change Reason")
.description("Represents a change reason of an <a href='events.html#experience cooldown change event'>experience cooldown change event</a>.")
.since("INSERT VERSION"));

if (Skript.classExists("org.bukkit.Input")) {
Classes.registerClass(new EnumClassInfo<>(InputKey.class, "inputkey", "input keys")
.user("input ?keys?")
.name("Input Key")
.description("Represents a movement input key that is pressed by a player")
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
.since("INSERT VERSION"));
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
}
}

}
60 changes: 18 additions & 42 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@
*/
package ch.njol.skript.classes.data;

import java.time.Duration;
import java.time.temporal.TemporalAmount;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.bukkitutil.InventoryUtils;
import ch.njol.skript.command.CommandEvent;
Expand All @@ -53,14 +49,7 @@
import io.papermc.paper.event.player.PlayerInventorySlotChangeEvent;
import io.papermc.paper.event.player.PlayerStonecutterRecipeSelectEvent;
import io.papermc.paper.event.player.PlayerTradeEvent;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.FireworkEffect;
import org.bukkit.GameMode;
import org.bukkit.Keyed;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.*;
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
Expand All @@ -79,7 +68,6 @@
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.Vehicle;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockCanBuildEvent;
import org.bukkit.event.block.BlockDamageEvent;
Expand Down Expand Up @@ -138,34 +126,10 @@
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerBedLeaveEvent;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerEditBookEvent;
import org.bukkit.event.player.PlayerEggThrowEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemBreakEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerItemDamageEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerItemMendEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPickupArrowEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerExpCooldownChangeEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.player.PlayerExpCooldownChangeEvent.ChangeReason;
import org.bukkit.event.player.PlayerQuitEvent.QuitReason;
import org.bukkit.event.player.PlayerRiptideEvent;
import org.bukkit.event.player.PlayerShearEntityEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.event.player.PlayerToggleFlightEvent;
import org.bukkit.event.server.ServerCommandEvent;
import org.bukkit.event.vehicle.VehicleDamageEvent;
import org.bukkit.event.vehicle.VehicleDestroyEvent;
Expand All @@ -187,14 +151,11 @@
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.Recipe;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.bukkit.InputKey;

/**
* @author Peter Güttinger
*/
@SuppressWarnings("deprecation")
public final class BukkitEventValues {

Expand Down Expand Up @@ -2018,5 +1979,20 @@ public Timespan get(PlayerExpCooldownChangeEvent event) {
}
}, EventValues.TIME_PAST);

// PlayerInputEvent
if (Skript.classExists("org.bukkit.Input")) {
EventValues.registerEventValue(PlayerInputEvent.class, InputKey[].class, new Getter<>() {
@Override
public InputKey[] get(PlayerInputEvent event) {
return InputKey.fromInput(event.getInput()).toArray(new InputKey[0]);
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(PlayerInputEvent.class, InputKey[].class, new Getter<>() {
@Override
public InputKey[] get(PlayerInputEvent event) {
return InputKey.fromInput(event.getPlayer().getCurrentInput()).toArray(new InputKey[0]);
}
}, EventValues.TIME_PAST);
}
}
}
88 changes: 88 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondIsPressingKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package ch.njol.skript.conditions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.Input;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerInputEvent;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.bukkit.InputKey;

@Name("Is Pressing Key")
@Description("Checks if a player is pressing a certain input key.")
@Examples({
"on player input:",
"\tif player is pressing forward movement key:",
"\t\tsend \"You are moving forward!\""
})
@Since("INSERT VERSION")
@Keywords({"press", "input"})
@RequiredPlugins("Minecraft 1.21.3+")
public class CondIsPressingKey extends Condition {

static {
if (Skript.classExists("org.bukkit.Input")) {
Skript.registerCondition(CondIsPressingKey.class,
"%players% (is|are) pressing %inputkeys%",
"%players% (isn't|is not|aren't|are not) pressing %inputkeys%",
"%players% (was|were) pressing %inputkeys%",
"%players% (wasn't|was not|weren't|were not) pressing %inputkeys%"
);
}
}

private Expression<Player> players;
private Expression<InputKey> inputKeys;
private boolean past;

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
//noinspection unchecked
players = (Expression<Player>) expressions[0];
//noinspection unchecked
inputKeys = (Expression<InputKey>) expressions[1];
past = matchedPattern > 1;
if (past && !getParser().isCurrentEvent(PlayerInputEvent.class))
Skript.warning("Checking for the past state outside of a 'player input' event is useless.");
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
setNegated(matchedPattern == 1 || matchedPattern == 3);
return true;
}

@Override
public boolean check(Event event) {
Player eventPlayer = !past && event instanceof PlayerInputEvent inputEvent ? inputEvent.getPlayer() : null;
InputKey[] inputKeys = this.inputKeys.getAll(event);
boolean and = this.inputKeys.getAnd();
return players.check(event, player -> {
Input input;
if (player.equals(eventPlayer)) {
input = ((PlayerInputEvent) event).getInput();
} else {
input = player.getCurrentInput();
}
return CollectionUtils.check(inputKeys, inputKey -> inputKey.check(input), and);
}, isNegated());
}

@Override
public String toString(@Nullable Event event, boolean debug) {
StringBuilder builder = new StringBuilder();
builder.append(players.toString(event, debug));
if (past) {
builder.append(players.isSingle() ? " was " : " were ");
} else {
builder.append(players.isSingle() ? " is " : " are ");
}
builder.append(isNegated() ? "not " : "");
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
builder.append("pressing ");
builder.append(inputKeys.toString(event, debug));
return builder.toString();
}

}
54 changes: 54 additions & 0 deletions src/main/java/ch/njol/skript/events/EvtPlayerInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ch.njol.skript.events;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerInputEvent;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.bukkit.InputKey;

import java.util.Set;

public class EvtPlayerInput extends SkriptEvent {

static {
if (Skript.classExists("org.bukkit.Input")) {
Skript.registerEvent("Player Input", EvtPlayerInput.class, PlayerInputEvent.class,
"player input",
"[player] press[ing] %inputkeys%")
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
.description("Called when a player sends an updated input to the server.")
.examples("on player input:",
"\tsend \"You are pressing: %event-inputs%\" to player")
.since("INSERT VERSION")
.requiredPlugins("Minecraft 1.21.3+");
}
}

private Literal<InputKey> keysToCheck;

@Override
public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.ParseResult parseResult) {
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
if (matchedPattern == 1)
//noinspection unchecked
keysToCheck = (Literal<InputKey>) args[0];
return true;
}

@Override
public boolean check(Event event) {
PlayerInputEvent inputEvent = (PlayerInputEvent) event;
if (keysToCheck != null) {
Set<InputKey> givenKeys = InputKey.fromInput(inputEvent.getInput());
keysToCheck.check(event, givenKeys::contains);
}
return true;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return keysToCheck == null ? "player input" : "player pressing " + keysToCheck.toString(event, debug);
}

}
71 changes: 71 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprCurrentInputKeys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.*;
import ch.njol.skript.expressions.base.PropertyExpression;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.registrations.EventValues;
import ch.njol.util.Kleenean;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerInputEvent;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.bukkit.InputKey;

import java.util.ArrayList;
import java.util.List;

@Name("Player Input Keys")
@Description("Get the current input keys of a player.")
@Examples("broadcast \"%player% is pressing %current input keys of player%\"")
@Since("INSERT VERSION")
@RequiredPlugins("Minecraft 1.21.3+")
public class ExprCurrentInputKeys extends PropertyExpression<Player, InputKey> {

static {
if (Skript.classExists("org.bukkit.Input"))
register(ExprCurrentInputKeys.class, InputKey.class, "[current] (inputs|input keys)", "players");
}

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
setExpr((Expression<? extends Player>) expressions[0]);
return true;
}

@Override
protected InputKey[] get(Event event, Player[] source) {
Player eventPlayer = getTime() == EventValues.TIME_NOW && event instanceof PlayerInputEvent inputEvent ? inputEvent.getPlayer() : null;
List<InputKey> inputKeys = new ArrayList<>();
for (Player player : source) {
if (player.equals(eventPlayer)) {
inputKeys.addAll(InputKey.fromInput(((PlayerInputEvent) event).getInput()));
} else {
inputKeys.addAll(InputKey.fromInput(player.getCurrentInput()));
}
}
return inputKeys.toArray(new InputKey[0]);
}

@Override
public boolean isSingle() {
return false;
}

@Override
public Class<? extends InputKey> getReturnType() {
return InputKey.class;
}

@Override
public boolean setTime(int time) {
return time != EventValues.TIME_FUTURE && setTime(time, PlayerInputEvent.class);
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "current input keys of " + getExpr().toString(event, debug);
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
}

}
10 changes: 1 addition & 9 deletions src/main/java/ch/njol/skript/lang/ExpressionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,7 @@ public boolean isSingle() {

@Override
public boolean check(Event event, Checker<? super T> checker, boolean negated) {
for (Expression<? extends T> expr : expressions) {
boolean result = expr.check(event, checker) ^ negated;
// exit early if we find a FALSE and we're ANDing, or a TRUE and we're ORing
if (and && !result)
return false;
if (!and && result)
return true;
}
return and;
return CollectionUtils.check(expressions, expr -> expr.check(event, checker) ^ negated, and);
}

@Override
Expand Down
Loading
Loading