From 11c33f8d0bedf8862b1dec63d6234e3cb5d496aa Mon Sep 17 00:00:00 2001 From: PseudoKnight Date: Thu, 26 Dec 2024 14:46:27 -0800 Subject: [PATCH] Add get_player_input() --- .../com/laytonsmith/abstraction/MCPlayer.java | 2 + .../abstraction/MCPlayerInput.java | 11 ++++ .../bukkit/BukkitMCPlayerInput.java | 48 ++++++++++++++++ .../bukkit/entities/BukkitMCPlayer.java | 7 +++ .../core/functions/PlayerManagement.java | 55 +++++++++++++++++++ .../com/laytonsmith/testing/StaticTest.java | 2 + 6 files changed, 125 insertions(+) create mode 100644 src/main/java/com/laytonsmith/abstraction/MCPlayerInput.java create mode 100644 src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCPlayerInput.java diff --git a/src/main/java/com/laytonsmith/abstraction/MCPlayer.java b/src/main/java/com/laytonsmith/abstraction/MCPlayer.java index ebe8d0601..f8321fc5f 100644 --- a/src/main/java/com/laytonsmith/abstraction/MCPlayer.java +++ b/src/main/java/com/laytonsmith/abstraction/MCPlayer.java @@ -204,4 +204,6 @@ public interface MCPlayer extends MCCommandSender, MCHumanEntity, MCOfflinePlaye void sendEquipmentChange(MCLivingEntity entity, MCEquipmentSlot slot, MCItemStack item); int getPing(); + + MCPlayerInput getCurrentInput(); } diff --git a/src/main/java/com/laytonsmith/abstraction/MCPlayerInput.java b/src/main/java/com/laytonsmith/abstraction/MCPlayerInput.java new file mode 100644 index 000000000..32b5a9f4c --- /dev/null +++ b/src/main/java/com/laytonsmith/abstraction/MCPlayerInput.java @@ -0,0 +1,11 @@ +package com.laytonsmith.abstraction; + +public interface MCPlayerInput { + boolean forward(); + boolean backward(); + boolean left(); + boolean right(); + boolean jump(); + boolean sneak(); + boolean sprint(); +} diff --git a/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCPlayerInput.java b/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCPlayerInput.java new file mode 100644 index 000000000..a76819c0f --- /dev/null +++ b/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCPlayerInput.java @@ -0,0 +1,48 @@ +package com.laytonsmith.abstraction.bukkit; + +import com.laytonsmith.abstraction.MCPlayerInput; +import org.bukkit.Input; + +public class BukkitMCPlayerInput implements MCPlayerInput { + + private final Input input; + + public BukkitMCPlayerInput(Input input) { + this.input = input; + } + + @Override + public boolean forward() { + return this.input.isForward(); + } + + @Override + public boolean backward() { + return this.input.isBackward(); + } + + @Override + public boolean left() { + return this.input.isLeft(); + } + + @Override + public boolean right() { + return this.input.isRight(); + } + + @Override + public boolean jump() { + return this.input.isJump(); + } + + @Override + public boolean sneak() { + return this.input.isSneak(); + } + + @Override + public boolean sprint() { + return this.input.isSprint(); + } +} diff --git a/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java b/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java index a45201562..027d8ad5a 100644 --- a/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java +++ b/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java @@ -9,6 +9,7 @@ import com.laytonsmith.abstraction.MCNote; import com.laytonsmith.abstraction.MCOfflinePlayer; import com.laytonsmith.abstraction.MCPlayer; +import com.laytonsmith.abstraction.MCPlayerInput; import com.laytonsmith.abstraction.MCPlayerInventory; import com.laytonsmith.abstraction.MCScoreboard; import com.laytonsmith.abstraction.MCWorldBorder; @@ -19,6 +20,7 @@ import com.laytonsmith.abstraction.bukkit.BukkitConvertor; import com.laytonsmith.abstraction.bukkit.BukkitMCItemStack; import com.laytonsmith.abstraction.bukkit.BukkitMCLocation; +import com.laytonsmith.abstraction.bukkit.BukkitMCPlayerInput; import com.laytonsmith.abstraction.bukkit.BukkitMCPlayerInventory; import com.laytonsmith.abstraction.bukkit.BukkitMCScoreboard; import com.laytonsmith.abstraction.bukkit.BukkitMCServer; @@ -872,4 +874,9 @@ public void sendEquipmentChange(MCLivingEntity entity, MCEquipmentSlot slot, MCI public int getPing() { return p.getPing(); } + + @Override + public MCPlayerInput getCurrentInput() { + return new BukkitMCPlayerInput(p.getCurrentInput()); + } } diff --git a/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java b/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java index ed78e48ab..5497cff13 100644 --- a/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java +++ b/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java @@ -14,6 +14,7 @@ import com.laytonsmith.abstraction.MCNamespacedKey; import com.laytonsmith.abstraction.MCOfflinePlayer; import com.laytonsmith.abstraction.MCPlayer; +import com.laytonsmith.abstraction.MCPlayerInput; import com.laytonsmith.abstraction.MCServer; import com.laytonsmith.abstraction.MCWorld; import com.laytonsmith.abstraction.MCWorldBorder; @@ -7252,4 +7253,58 @@ public Boolean runAsync() { return false; } } + + @api + public static class get_player_input extends AbstractFunction { + + public String getName() { + return "get_player_input"; + } + + public Integer[] numArgs() { + return new Integer[]{0, 1}; + } + + public String docs() { + return "array {[player]} Returns a player's current input. (MC 1.21.3+)" + + " This can be used to detect which movement keys the player is pressing." + + " Array contains the following keys: forward, backward, left, right, jump, sneak, and sprint."; + } + + public Construct exec(Target t, Environment env, Mixed... args) throws ConfigRuntimeException { + MCPlayer p; + if(args.length == 1) { + p = Static.GetPlayer(args[0].val(), t); + } else { + p = env.getEnv(CommandHelperEnvironment.class).GetPlayer(); + Static.AssertPlayerNonNull(p, t); + } + CArray ret = CArray.GetAssociativeArray(t); + MCPlayerInput input = p.getCurrentInput(); + ret.set("forward", CBoolean.get(input.forward()), t); + ret.set("backward", CBoolean.get(input.backward()), t); + ret.set("left", CBoolean.get(input.left()), t); + ret.set("right", CBoolean.get(input.right()), t); + ret.set("jump", CBoolean.get(input.jump()), t); + ret.set("sneak", CBoolean.get(input.sneak()), t); + ret.set("sprint", CBoolean.get(input.sprint()), t); + return ret; + } + + public Class[] thrown() { + return new Class[]{CREPlayerOfflineException.class, CRELengthException.class}; + } + + public Version since() { + return MSVersion.V3_3_5; + } + + public boolean isRestricted() { + return true; + } + + public Boolean runAsync() { + return false; + } + } } diff --git a/src/test/java/com/laytonsmith/testing/StaticTest.java b/src/test/java/com/laytonsmith/testing/StaticTest.java index b29626cd4..e789a3b25 100644 --- a/src/test/java/com/laytonsmith/testing/StaticTest.java +++ b/src/test/java/com/laytonsmith/testing/StaticTest.java @@ -24,6 +24,7 @@ import com.laytonsmith.abstraction.MCNote; import com.laytonsmith.abstraction.MCPattern; import com.laytonsmith.abstraction.MCPlayer; +import com.laytonsmith.abstraction.MCPlayerInput; import com.laytonsmith.abstraction.MCPlugin; import com.laytonsmith.abstraction.MCPluginMeta; import com.laytonsmith.abstraction.MCPotionData; @@ -466,6 +467,7 @@ public static MCPlayer GetOnlinePlayer(String name, String worldName, MCServer s when(p.isOnline()).thenReturn(true); when(p.getName()).thenReturn(name); when(p.getServer()).thenReturn(s); + when(p.getCurrentInput()).thenReturn(mock(MCPlayerInput.class)); when(p.isOp()).thenReturn(true); if(s != null && s.getOnlinePlayers() != null) { Collection online = s.getOnlinePlayers();